UIImpersonator.addChild() doesn't dispatch the right events

390 Views Asked by At

I am running FlexUnit tests through Ant. The test test1 fails with the message "Timeout Occurred before expected event" but test2 passes. The only difference between the two tests is that one uses UIImpersonator.addChild() whereas the other uses FlexGlobals.topLevelApplication.addElement().

test1 fails even if I listen for "addedToStage" event. Listening for "added" event, however, makes test1 pass.

[Test(async, ui, description="Fails")]
public function test1():void
{
    var c:UIComponent = new UIComponent;
    Async.proceedOnEvent(this, c, FlexEvent.CREATION_COMPLETE);
    UIImpersonator.addChild(c);
}

[Test(async, ui, description="Passes")]
public function test2():void
{
    var c:UIComponent = new UIComponent;
    Async.proceedOnEvent(this, c, FlexEvent.CREATION_COMPLETE);
    FlexGlobals.topLevelApplication.addElement(c);
}
3

There are 3 best solutions below

4
csomakk On

assuming Flex4/spark. addChild adds a MovieClip, not a Flex UIElement, it doesn't even know FlexEvent type as it is a flash.core object. It will only throw addedToStage or added events (Event.added), but in unit test, it is not added to stage, because UIImpersonator is not part of the stage

2
csomakk On

when adding a child, it will not initiate the flex component lifecycle, because displayobject is flash core element, not flex.

0
ajkerr On

I ran into this same issue today using the new Apache FlexUnit 4.2.0 release.

When trying to run the sampleCIProject included in the binary distribution, none of the tests that used Async would succeed. The exception was exactly as described above.

After looking at the source code for a while, I noticed that the core FlexUnit libraries have two flavours: flexunit-4.2.0-20140410-as3_4.12.0.swc and flexunit-4.2.0-20140410-flex_4.12.0.swc.

The first of these is intended for pure AS3 projects, while the latter is intended for projects that use Flex. The sampleCIProject included both of these libraries in the library path, and I'm assuming that it was using the UIImpersonator class from the pure AS3 library rather than the Flex-supporting one. I removed flexunit-4.2.0-20140410-as3_4.12.0.swc from the project, and lo and behold, the Async tests started working again.

Probably a bit late for you, but I hope this helps someone else.