Bug 616186 - fail the event tests if dupe event is fired, r=fer, a=test
authorAlexander Surkov <surkov.alexander@gmail.com>
Wed, 15 Dec 2010 13:22:51 -0800
changeset 59273 b4213fc83849a950918dab75f19f43baf8fef938
parent 59272 0e45f2a9fccaad2cf9eaabf92575ba0fea021e9d
child 59274 469dc5e3d5b4044f0f07efc1277cc996023658ff
push id1
push usershaver@mozilla.com
push dateTue, 04 Jan 2011 17:58:04 +0000
reviewersfer, test
bugs616186
milestone2.0b9pre
Bug 616186 - fail the event tests if dupe event is fired, r=fer, a=test
accessible/tests/mochitest/events.js
accessible/tests/mochitest/events/test_mutation.html
--- a/accessible/tests/mochitest/events.js
+++ b/accessible/tests/mochitest/events.js
@@ -225,18 +225,17 @@ function eventQueue(aEventType)
         invoker.finalCheck();
 
       if (invoker.wasCaught) {
         for (var idx = 0; idx < invoker.wasCaught.length; idx++) {
           var id = this.getEventID(idx);
           var type = this.getEventType(idx);
           var unexpected = this.mEventSeq[idx].unexpected;
 
-          var typeStr = (typeof type == "string") ?
-            type : gAccRetrieval.getStringEventType(type);
+          var typeStr = this.getEventTypeAsString(idx);
 
           var msg = "test with ID = '" + id + "' failed. ";
           if (unexpected) {
             var wasCaught = invoker.wasCaught[idx];
             if (!testFailed)
               testFailed = wasCaught;
 
             ok(!wasCaught,
@@ -322,20 +321,33 @@ function eventQueue(aEventType)
       // invoker in the queue.
       this.processNextInvoker();
       return;
     }
 
     if ("debugCheck" in invoker)
       invoker.debugCheck(aEvent);
 
-    // Search through unexpected events to ensure no one of them was handled.
+    // Search through handled expected events if one of them was handled again.
     var idx = 0;
     for (; idx < this.mEventSeq.length; idx++) {
-      if (this.mEventSeq[idx].unexpected && this.compareEvents(idx, aEvent))
+      if (!this.isEventUnexpected(idx) && (invoker.wasCaught[idx] == true) &&
+          this.compareEvents(idx, aEvent)) {
+
+        var msg = "Doubled event { event type: " +
+          this.getEventTypeAsString(idx) + ", target: " +
+          prettyName(this.getEventTarget(idx)) + "} in test with ID = '" +
+          this.getEventID(idx) + "'.";
+        ok(false, msg);
+      }
+    }
+
+    // Search through unexpected events to ensure no one of them was handled.
+    for (idx = 0; idx < this.mEventSeq.length; idx++) {
+      if (this.isEventUnexpected(idx) && this.compareEvents(idx, aEvent))
         invoker.wasCaught[idx] = true;
     }
 
     // We've handled all expected events, next invoker processing is pending.
     if (this.mEventSeqIdx == this.mEventSeq.length)
       return;
 
     // Compute next expected event index.
@@ -408,25 +420,22 @@ function eventQueue(aEventType)
     // Register event listeners
     if (this.mEventSeq) {
       aInvoker.wasCaught = new Array(this.mEventSeq.length);
 
       for (var idx = 0; idx < this.mEventSeq.length; idx++) {
         var eventType = this.getEventType(idx);
 
         if (gLogger.isEnabled()) {
-          var strEventType = (typeof eventType == "string") ? eventType :
-            eventTypeToString(eventType);
-
           var msg = "registered";
           if (this.isEventUnexpected(idx))
             msg += " unexpected";
 
-          msg += ": event type: " + strEventType + ", target: " +
-            prettyName(this.getEventTarget(idx));
+          msg += ": event type: " + this.getEventTypeAsString(idx) +
+            ", target: " + prettyName(this.getEventTarget(idx));
 
           gLogger.logToConsole(msg);
           gLogger.logToDOM(msg, true);
         }
 
         if (typeof eventType == "string") {
           // DOM event
           var target = this.getEventTarget(idx);
@@ -462,16 +471,22 @@ function eventQueue(aEventType)
     }
   }
 
   this.getEventType = function eventQueue_getEventType(aIdx)
   {
     return this.mEventSeq[aIdx].type;
   }
 
+  this.getEventTypeAsString = function eventQueue_getEventTypeAsString(aIdx)
+  {
+    var type = this.mEventSeq[aIdx].type;
+    return (typeof type == "string") ? type : eventTypeToString(type);
+  }
+
   this.getEventTarget = function eventQueue_getEventTarget(aIdx)
   {
     return this.mEventSeq[aIdx].target;
   }
 
   this.getEventPhase = function eventQueue_getEventPhase(aIdx)
   {
      var eventItem = this.mEventSeq[aIdx];
@@ -562,36 +577,31 @@ function eventQueue(aEventType)
     // Dump DOM event information. Skip a11y event since it is dumped by
     // gA11yEventObserver.
     if (aOrigEvent instanceof nsIDOMEvent) {
       var info = "Event type: " + aOrigEvent.type;
       info += ". Target: " + prettyName(aOrigEvent.originalTarget);
       gLogger.logToDOM(info);
     }
 
-    var currType = this.getEventType(aExpectedEventIdx);
+    var currType = this.getEventTypeAsString(aExpectedEventIdx);
     var currTarget = this.getEventTarget(aExpectedEventIdx);
 
     var msg = "EQ: ";
     var emphText = "";
     if (aMatch) {
       emphText = "matched ";
 
-      var consoleMsg =
-        "*****\nEQ matched: " + eventTypeToString(currType) + "\n*****";
+      var consoleMsg = "*****\nEQ matched: " + currType + "\n*****";
       gLogger.logToConsole(consoleMsg);
 
     } else {
       msg += "expected";
     }
-
-    msg += " event, type: ";
-    msg += (typeof currType == "string") ?
-      currType : eventTypeToString(currType);
-    msg += ", target: " + prettyName(currTarget);
+    msg += " event, type: " + currType + ", target: " + prettyName(currTarget);
 
     gLogger.logToDOM(msg, true, emphText);
   }
 
   this.mDefEventType = aEventType;
 
   this.mInvokers = new Array();
   this.mIndex = -1;
@@ -984,18 +994,21 @@ function listenA11yEvents(aStartToListen
                         .removeObserver(gA11yEventObserver, "accessible-event");
   }
 }
 
 function addA11yEventListener(aEventType, aEventHandler)
 {
   if (!(aEventType in gA11yEventListeners))
     gA11yEventListeners[aEventType] = new Array();
-  
-  gA11yEventListeners[aEventType].push(aEventHandler);
+
+  var listenersArray = gA11yEventListeners[aEventType];
+  var index = listenersArray.indexOf(aEventHandler);
+  if (index == -1)
+    listenersArray.push(aEventHandler);
 }
 
 function removeA11yEventListener(aEventType, aEventHandler)
 {
   var listenersArray = gA11yEventListeners[aEventType];
   if (!listenersArray)
     return false;
 
--- a/accessible/tests/mochitest/events/test_mutation.html
+++ b/accessible/tests/mochitest/events/test_mutation.html
@@ -276,16 +276,17 @@
       return aNode.parentNode;
     }
 
     /**
      * Do tests.
      */
     var gQueue = null;
     //gA11yEventDumpID = "eventdump"; // debug stuff
+    //gA11yEventDumpToConsole = true;
 
     function doTests()
     {
       gQueue = new eventQueue();
 
       // Show/hide events by changing of display style of accessible DOM node
       // from 'inline' to 'none', 'none' to 'inline'.
       var id = "link1";
@@ -403,17 +404,17 @@
     <a id="link3" href="http://www.google.com">Link #3</a>
     <a id="link4" href="http://www.google.com" style="visibility:collapse">Link #4</a>
     <a id="link5" href="http://www.google.com">Link #5</a>
 
     <div id="container" role="list">
       <span id="child1"></span>
       <span id="child2" role="listitem"></span>
       <span id="child3"><span role="listitem"></span></span>
-      <span id="child4"><span role="listitem"></span><span role="listitem"></span></span>
+      <span id="child4"><span id="child4_1" role="listitem"></span><span id="child4_2" role="listitem"></span></span>
     </div>
 
     <a id="link6" href="http://www.google.com">Link #6</a>
 
     <div id="container2" class="displayNone"><a id="link7">Link #7</a></div>
     <div id="container3" class="visibilityHidden"><a id="link8">Link #8</a></div>
   </div>
 </body>