Created
February 10, 2011 18:18
-
-
Save destroytoday/821023 to your computer and use it in GitHub Desktop.
Proposal for the use of interfaces to allow mocking of final classes like Stage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flash.display.DisplayObject | |
{ | |
public class DisplayObject extends EventDispatcher implements IDisplayObject | |
{ | |
public function DisplayObject() | |
{ | |
} | |
public function get stage():IStage | |
{ | |
// return Stage by default | |
} | |
// ...other interface methods... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
public class InvalidatingSprite extends Sprite | |
{ | |
protected var isInvalidating:Boolean; | |
public function InvalidatingSprite() | |
{ | |
} | |
public function invalidateDisplayList() | |
{ | |
if (stage && !isInvalidating) | |
{ | |
isInvalidating = true; | |
addEventListener(Event.RENDER, renderHandler); | |
addEventListener(Event.ENTERFRAME, enterframeHandler); | |
stage.invalidate(); | |
} | |
} | |
// ...other invalidation code... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
public class InvalidatingSpriteTest | |
{ | |
public var sprite:InvalidatingSprite; | |
public function InvalidatingSpriteTest() | |
{ | |
} | |
[Before] | |
public function setUp():void | |
{ | |
sprite = new InvalidatingSprite(); | |
} | |
[After] | |
public function tearDown():void | |
{ | |
sprite = null; | |
} | |
[Test] | |
public function sprite_invalidates_stage():void | |
{ | |
var mockStage:MockStage = new MockStage(); | |
sprite.stage = mockStage; | |
sprite.invalidateDisplayList(); | |
assertThat(mockStage.hasInvalidated); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package | |
{ | |
public class MockStage implements IStage | |
{ | |
public var hasInvalidated:Boolean; | |
public function MockStage() | |
{ | |
} | |
public function invalidate():void | |
{ | |
hasInvalidated = true; | |
} | |
// ...other interface methods... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment