Using Async to wait for a specific event
I've simplified the creation and deletion calls and used a generic item. In my before method, I call Async.handleEvent to wait for PropertyChangeEvents, but this only waits for the first one. My problem lies in the fact that ItemManager is an IEventDispatcher that sends multiple property change events, but I need to wait for the specific event with the property "item". However, when I call Async inside the handler, it does not keep the Test method from running, Before assumes that it's Async responsibilities have been fulfilled and launches the next stage of tests. How can I keep Before from moving on until an event with a specific property has been received?
public class Test
{
private static const TIME_OUT:int = 3000;
[Before(async)]
public function runBeforeEveryTest() : void
{
// Create a new item
item.create(); // This is an asynchronous creation that sends many Property Change events
//Wait for the item to be created before continuing tests.
Async.handleEvent( this, ItemManager.instance, PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChangeForItem, TIME_OUT);
}
[After]
public function runAfterEveryTest() : void
{
// Delete the Item
item.delete();
}
[Test(async)]
public function itemExists() : void
{
Assert.assertTrue( item.exists() );
}
private function handlePropertyChangeForItem(evt:PropertyChangeEvent, eventObject:Object) : void
{
if (evt.property == "item")
{
// Now that the item has been created, we can start the tests.
return;
}
else
{
// Keep waiting.
Async.handleEvent( this, ItemManager.instance, PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChangeForItem, TIME_OUT);
}
}
}
Thanks in advance, it's greatly appreciated!
