I'm using flex sdk 4.1 and have a situation where a button click is sometimes called twice. I am writting a test for this using a sequencer and it will run for at least one button click, what I am not sure is how to test for multiple click events(which shouldn't happen, but occours randomly in my project)
package tests.view
{
import flash.events.MouseEvent;
import flexunit.framework.Assert;
import mx.events.FlexEvent;
import org.flexunit.async.Async;
import org.fluint.sequence.SequenceEventDispatcher;
import org.fluint.sequence.SequenceRunner;
import org.fluint.sequence.SequenceWaiter;
import org.fluint.uiImpersonation.UIImpersonator;
import views.AddComments;
public class TestAddComments
{
private var view:AddComments;
[Before(async,ui)]
public function setUp() : void
{
view = new AddComments;
Async.proceedOnEvent( this, view, FlexEvent.CREATION_COMPLETE, 600 );
UIImpersonator.addChild( view );
}
[Test]
public function testButtonClick():void
{
view.addCommentBtn
var sequence:SequenceRunner = new SequenceRunner( this );
sequence.addStep( new SequenceEventDispatcher( view.addCommentBtn, new MouseEvent( 'click', true, false ) ) );
sequence.addStep( new SequenceWaiter( view.addCommentBtn, MouseEvent.CLICK, 100 ) );
sequence.run();
}
}
}
The only way you could test for that type of circumstance... one where something may happen... is to do so with a timeout. In other words, you cannot let the test pass simply because the first event occurred.
Two ways to handle this... with sequences:
Instead, you write a test that expects 2 clicks. In the assertion handler (which would only be reached if both clicks occur) you would fail the test:
fail('i shouldnt be here');
In the Waiter for the 2nd click, you would specify a timeout handler. That timeout handler will get called in the 2nd click does not occur in the specified time... (finding that time is the hard part). The default timeout handler fails the test, however, in your case, your timeout handler would just happily do any assertions needed and move on. A timeout does not necessarily mean failure... in your case, it is correct
Second choice:
You could create a timer. Create an event handler for the button click which increments a counter. Start the timer, do your test. However, have your test pending on the timer's completion rather than something like the click.. In that case, you would then be able to just check the count:
private var clickCount:int = 0;
[Test]
public function testButtonClick():void {
var timer:Timer = new Timer( 100, 1 );
Async.handleEvent( this, timer, TimerEvent.COMPLETE, checkClickCount );
view.addCommentBtn.addEventListener( 'click', function( e:Event ):void { clickCount++ } );
view.addCommentBtn.dispatchEvent( new MouseEvent( 'click;, true, false ) );
}
private function checkClickCount( e:Event ):void {
assertEquals( 1, clickCount );
}
However, I can't imagine how the test you are describing could ever yield more than one click event... I am guessing the problem is elsewhere, but this will help you verify
Mike
btw is there any way I could run that test say , 100 times?
I'm also getting timout errors for this:
[Test(async,timeout="5000")]
public function testButtonClick():void {
var timer:Timer = new Timer( 100,1 );
Async.handleEvent( this, timer, TimerEvent.TIMER_COMPLETE, checkClickCount,200 );
view.addCommentBtn.addEventListener( 'click', function( e:Event ):void { clickCount++ } );
view.addCommentBtn.dispatchEvent( new MouseEvent( MouseEvent.CLICK, true, false ) );
}
thanks again my friends for any light you can shed on this:)
opps this was the problem:
private function checkClickCount( e:Event, passThroughData:Object ):void {
Assert.assertEquals( 1, clickCount );
}
I was thinking of having some sort of loop in this code to repeat the test 100 times, I'm not really sure how to do that param thing you mentioned.
[Test(async,timeout="5000")]
public function testButtonClick():void {
var timer:Timer = new Timer( 100,1 );
Async.handleEvent( this, timer, TimerEvent.TIMER_COMPLETE, checkClickCount,150 );
view.addCommentBtn.addEventListener( 'click', function( e:Event ):void { clickCount++ } );
view.addCommentBtn.dispatchEvent( new MouseEvent( MouseEvent.CLICK, true, false ) );
timer.start();
}
North America
Europe, Middle East and Africa
Asia Pacific