Skip navigation
Currently Being Moderated

I am writting a test  using a sequencer to test for multiple button click

Jan 4, 2011 6:19 AM

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();
        }
    }
}

 
Replies
  • Currently Being Moderated
    Jan 4, 2011 8:24 AM   in reply to nikos101

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 4, 2011 9:53 AM   in reply to nikos101

    You don't seem to be starting the timer

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 4, 2011 9:53 AM   in reply to nikos101

    And yes, you could make it a parameterized test and run it a bunch of times, but let's get it working just the once first.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points