Bug 1302297 - Part 3: Enhance sendMouseEvent() and sendTouchEvent(). f=stone, r=smaug
authorHo-Pang Hsu <hopang.hsu@gmail.com>
Fri, 09 Dec 2016 03:39:00 +0100
changeset 325590 9f7615dd40ba49985105ae77b39e2ec0e76726ff
parent 325589 5574e0e702d392eeaaa268f28692757063db5869
child 325591 13fd65d8a143c66d3bcf937a60b858ea91115955
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerssmaug
bugs1302297
milestone53.0a1
Bug 1302297 - Part 3: Enhance sendMouseEvent() and sendTouchEvent(). f=stone, r=smaug
dom/events/test/pointerevents/mochitest_support_external.js
--- a/dom/events/test/pointerevents/mochitest_support_external.js
+++ b/dom/events/test/pointerevents/mochitest_support_external.js
@@ -80,22 +80,55 @@ var MouseEventHelper = (function() {
 }) ();
 
 // Helper function to send MouseEvent with different parameters
 function sendMouseEvent(int_win, elemId, mouseEventType, params) {
   var elem = int_win.document.getElementById(elemId);
   if(!!elem) {
     var rect = elem.getBoundingClientRect();
     var eventObj = {type: mouseEventType};
-    if(params && "button" in params)
-      eventObj.button = params.button;
+
     if(params && "inputSource" in params)
       eventObj.inputSource = params.inputSource;
-    if(params && "buttons" in params)
-      eventObj.buttons = params.buttons;
+
+    // Check or generate a |button| value.
+    var isButtonEvent = mouseEventType === "mouseup" ||
+                        mouseEventType === "mousedown";
+
+    // Set |button| to the default value first.
+    eventObj.button = isButtonEvent ? MouseEventHelper.BUTTON_LEFT
+                                    : MouseEventHelper.BUTTON_NONE;
+
+    // |button| is passed, use and check it.
+    if (params && "button" in params) {
+      var hasButtonValue = (params.button !== MouseEventHelper.BUTTON_NONE);
+      ok(!isButtonEvent || hasButtonValue,
+         "Inappropriate |button| value caught.");
+      eventObj.button = params.button;
+    }
+
+    // Generate a |buttons| value and update buttons state
+    var buttonsMask = MouseEventHelper.computeButtonsMaskFromButton(eventObj.button);
+    switch(mouseEventType) {
+      case "mousedown":
+        MouseEventHelper.BUTTONS_STATE |= buttonsMask; // Set button flag.
+        break;
+      case "mouseup":
+        MouseEventHelper.BUTTONS_STATE &= ~buttonsMask; // Clear button flag.
+        break;
+    }
+    eventObj.buttons = MouseEventHelper.BUTTONS_STATE;
+
+    // Replace the button value for mousemove events.
+    // Since in widget level design, even when no button is pressed at all, the
+    // value of WidgetMouseEvent.button is still 0, which is the same value as
+    // the one for mouse left button.
+    if (mouseEventType === "mousemove") {
+      eventObj.button = MouseEventHelper.BUTTON_LEFT;
+    }
 
     // Default to the center of the target element but we can still send to a
     // position outside of the target element.
     var offsetX = params && "offsetX" in params ? params.offsetX : rect.width / 2;
     var offsetY = params && "offsetY" in params ? params.offsetY : rect.height / 2;
 
     console.log(elemId, eventObj);
     synthesizeMouse(elem, offsetX, offsetY, eventObj, int_win);
@@ -119,16 +152,26 @@ var TouchEventHelper = {
 
 // Helper function to send TouchEvent with different parameters
 function sendTouchEvent(int_win, elemId, touchEventType, params) {
   var elem = int_win.document.getElementById(elemId);
   if(!!elem) {
     var rect = elem.getBoundingClientRect();
     var eventObj = {type: touchEventType};
 
+    // Update touch state
+    switch(touchEventType) {
+      case "touchstart":
+        TouchEventHelper.TOUCH_STATE = true; // Set touch flag.
+        break;
+      case "touchend":
+        TouchEventHelper.TOUCH_STATE = false; // Clear touch flag.
+        break;
+    }
+
     // Default to the center of the target element but we can still send to a
     // position outside of the target element.
     var offsetX = params && "offsetX" in params ? params.offsetX : rect.width / 2;
     var offsetY = params && "offsetY" in params ? params.offsetY : rect.height / 2;
 
     console.log(elemId, eventObj);
     synthesizeTouch(elem, offsetX, offsetY, eventObj, int_win);
   } else {
@@ -147,14 +190,16 @@ function runTestInNewWindow(aFile) {
         turnOnPointerEvents(() => {
           executeTest(testWindow);
         });
         return;
       case "RESULT":
         ok(aEvent.data.result, aEvent.data.message);
         return;
       case "FIN":
+        MouseEventHelper.checkExitState();
+        TouchEventHelper.checkExitState();
         testWindow.close();
         SimpleTest.finish();
         return;
     }
   });
 }