|
| 1 | +package { |
| 2 | +import flash.display.Sprite; |
| 3 | +import flash.display.DisplayObject; |
| 4 | +import flash.text.TextField; |
| 5 | +import flash.text.TextFormat; |
| 6 | +import flash.events.Event; |
| 7 | +import flash.utils.setTimeout; |
| 8 | + |
| 9 | +public class Test extends Sprite { |
| 10 | + private var desc: String = null; |
| 11 | + private var cb: Function = null; |
| 12 | + private var currentFrame: int = 0; |
| 13 | + |
| 14 | + public function Test() { |
| 15 | + stage.scaleMode = "noScale"; |
| 16 | + |
| 17 | + testRender(); |
| 18 | + testEvents(); |
| 19 | + |
| 20 | + trace("Tests finished"); |
| 21 | + } |
| 22 | + |
| 23 | + private function testRender() { |
| 24 | + testBoundsUpdate("none", function(text:TextField, cb:Function) { |
| 25 | + cb(); |
| 26 | + }); |
| 27 | + |
| 28 | + // NOTE: setTimeout with 0 time produces nondeterministic |
| 29 | + // results, make sure its > than frame time |
| 30 | + |
| 31 | + testBoundsUpdate("set timeout", function(text:TextField, cb:Function) { |
| 32 | + setTimeout(cb, 100); |
| 33 | + }); |
| 34 | + testBoundsUpdate("add child + set timeout", function(text:TextField, cb:Function) { |
| 35 | + addChild(text); |
| 36 | + setTimeout(cb, 100); |
| 37 | + }); |
| 38 | + |
| 39 | + var that = this; |
| 40 | + setTimeout(function():void { |
| 41 | + that.cb(); |
| 42 | + }, 100); |
| 43 | + testBoundsUpdate("add child + set timeout before construction", function(text:TextField, cb:Function) { |
| 44 | + addChild(text); |
| 45 | + that.cb = cb; |
| 46 | + }); |
| 47 | + |
| 48 | + testBoundsUpdate("enter frame", function(text:TextField, cb:Function) { |
| 49 | + runEventOnce(cb, "enterFrame"); |
| 50 | + }); |
| 51 | + testBoundsUpdate("frame constructed", function(text:TextField, cb:Function) { |
| 52 | + runEventOnce(cb, "frameConstructed"); |
| 53 | + }); |
| 54 | + testBoundsUpdate("exit frame", function(text:TextField, cb:Function) { |
| 55 | + runEventOnce(cb, "exitFrame"); |
| 56 | + }); |
| 57 | + |
| 58 | + testBoundsUpdate("add child", function(text:TextField, cb:Function) { |
| 59 | + addChild(text); |
| 60 | + cb(); |
| 61 | + }); |
| 62 | + testBoundsUpdate("add child + enter frame", function(text:TextField, cb:Function) { |
| 63 | + addChild(text); |
| 64 | + runEventOnce(cb, "enterFrame"); |
| 65 | + }); |
| 66 | + testBoundsUpdate("add child + frame constructed", function(text:TextField, cb:Function) { |
| 67 | + addChild(text); |
| 68 | + runEventOnce(cb, "frameConstructed"); |
| 69 | + }); |
| 70 | + testBoundsUpdate("add child + exit frame", function(text:TextField, cb:Function) { |
| 71 | + addChild(text); |
| 72 | + runEventOnce(cb, "exitFrame"); |
| 73 | + }); |
| 74 | + |
| 75 | + testBoundsUpdate("invisible + add child + enter frame", function(text:TextField, cb:Function) { |
| 76 | + text.visible = "false"; |
| 77 | + addChild(text); |
| 78 | + cb(); |
| 79 | + }); |
| 80 | + testBoundsUpdate("add child + invisible + enter frame", function(text:TextField, cb:Function) { |
| 81 | + addChild(text); |
| 82 | + text.visible = "false"; |
| 83 | + cb(); |
| 84 | + }); |
| 85 | + testBoundsUpdate("add child + remove child + enter frame", function(text:TextField, cb:Function) { |
| 86 | + addChild(text); |
| 87 | + removeChild(text); |
| 88 | + runEventOnce(cb); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + private function testEvents() { |
| 93 | + var test = this; |
| 94 | + var events = ["enterFrame", "frameConstructed", "exitFrame"]; |
| 95 | + |
| 96 | + for each (var fromEventName in events) { |
| 97 | + runEventOnce(function(): void { |
| 98 | + var fromFrame = test.currentFrame; |
| 99 | + for each (var toEventName in events) { |
| 100 | + testBoundsUpdate("event " + fromEventName + " -> " + toEventName, function(text:TextField, cb:Function) { |
| 101 | + addChild(text); |
| 102 | + runEventOnce(function():void { |
| 103 | + var toFrame = test.currentFrame; |
| 104 | + trace("// " + "event " + fromEventName + " -> " + toEventName); |
| 105 | + trace("// " + fromFrame + " -> " + toFrame); |
| 106 | + cb(); |
| 107 | + }, toEventName); |
| 108 | + }); |
| 109 | + } |
| 110 | + }, fromEventName); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private function runEventOnce(fun: Function, eventName: String = Event.ENTER_FRAME, target: DisplayObject = null):void { |
| 115 | + if (target == null) { |
| 116 | + target = stage; |
| 117 | + } |
| 118 | + function handler(event:Event):void { |
| 119 | + target.removeEventListener(eventName, handler); |
| 120 | + fun(); |
| 121 | + } |
| 122 | + |
| 123 | + target.addEventListener(eventName, handler); |
| 124 | + } |
| 125 | + |
| 126 | + private function testBoundsUpdate(desc: String, fun: Function, before: Function = null):void { |
| 127 | + this.desc = desc; |
| 128 | + var text = new TextField(); |
| 129 | + text.width = 100; |
| 130 | + text.height = 20; |
| 131 | + if (before != null) { |
| 132 | + before(text); |
| 133 | + } |
| 134 | + |
| 135 | + text.autoSize = "center"; |
| 136 | + fun(text, function():void { |
| 137 | + text.wordWrap = true; |
| 138 | + trace("Testing: " + desc); |
| 139 | + trace(" " + text.x + "," + text.y + "," + text.width + "," + text.height); |
| 140 | + }); |
| 141 | + } |
| 142 | +} |
| 143 | +} |
0 commit comments