From b18c81a34ed95d44908f48f7d5d855e3fdcee3af Mon Sep 17 00:00:00 2001 From: Jonathan Wilde Date: Tue, 25 Jun 2013 15:35:50 -0700 Subject: [PATCH] Bug 886589 - Change - Add infrastructure for tapping center of elements rather than relying on hard-coded offsets. r=jimm --- .../mochitest/browser_context_menu_tests.js | 20 +++++++------- browser/metro/base/tests/mochitest/head.js | 27 ++++++++++++++++--- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/browser/metro/base/tests/mochitest/browser_context_menu_tests.js b/browser/metro/base/tests/mochitest/browser_context_menu_tests.js index e9e783c5d0e0..c8cdfe9b8b5c 100644 --- a/browser/metro/base/tests/mochitest/browser_context_menu_tests.js +++ b/browser/metro/base/tests/mochitest/browser_context_menu_tests.js @@ -54,7 +54,7 @@ gTests.push({ // invoke selection context menu let promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, span, 85, 10); + sendContextMenuClickToElement(win, span); yield promise; // should be visible @@ -87,7 +87,7 @@ gTests.push({ let link = win.document.getElementById("text2-link"); win.getSelection().selectAllChildren(link); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, link, 40, 10); + sendContextMenuClickToElement(win, link); yield promise; // should be visible @@ -109,7 +109,7 @@ gTests.push({ link = win.document.getElementById("text2-link"); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, link, 40, 10); + sendContextMenuClickToElement(win, link); yield promise; // should be visible @@ -131,7 +131,7 @@ gTests.push({ let input = win.document.getElementById("text3-input"); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input); yield promise; // should be visible @@ -155,7 +155,7 @@ gTests.push({ input.value = "hello, I'm sorry but I must be going."; input.setSelectionRange(0, 5); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should be visible @@ -187,7 +187,7 @@ gTests.push({ input = win.document.getElementById("text3-input"); input.select(); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should be visible @@ -208,7 +208,7 @@ gTests.push({ input = win.document.getElementById("text3-input"); input.select(); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should be visible @@ -232,7 +232,7 @@ gTests.push({ input.value = "hello, I'm sorry but I must be going."; input.setSelectionRange(0, 5); promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should be visible @@ -268,7 +268,7 @@ gTests.push({ input.value = ""; promise = waitForEvent(document, "popupshown"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should be visible @@ -291,7 +291,7 @@ gTests.push({ input.value = ""; promise = waitForEvent(Elements.tray, "transitionend"); - sendContextMenuClickToElement(win, input, 20, 10); + sendContextMenuClickToElement(win, input, 20); yield promise; // should *not* be visible diff --git a/browser/metro/base/tests/mochitest/head.js b/browser/metro/base/tests/mochitest/head.js index 8ff1ffa5716d..042601bf271d 100644 --- a/browser/metro/base/tests/mochitest/head.js +++ b/browser/metro/base/tests/mochitest/head.js @@ -533,6 +533,25 @@ function synthesizeNativeMouseMUp(aElement, aOffsetX, aOffsetY) { 0x0040); // MOUSEEVENTF_MIDDLEUP } +/* + * logicalCoordsForElement - given coordinates relative to top-left of + * given element, returns logical coordinates for window. If a non-numeric + * X or Y value is given, a value for the center of the element in that + * dimension is used. + * + * @param aElement element coordinates are relative to. + * @param aX, aY relative coordinates. + */ +function logicalCoordsForElement (aElement, aX, aY) { + let coords = { x: null, y: null }; + let rect = aElement.getBoundingClientRect(); + + coords.x = isNaN(aX) ? rect.left + (rect.width / 2) : rect.left + aX; + coords.y = isNaN(aY) ? rect.top + (rect.height / 2) : rect.top + aY; + + return coords; +} + /* * sendContextMenuClick - simulates a press-hold touch input event. Event * is delivered to the main window of the application through the top-level @@ -568,8 +587,8 @@ function sendContextMenuClickToWindow(aWindow, aX, aY) { function sendContextMenuClickToElement(aWindow, aElement, aX, aY) { let utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor) .getInterface(Components.interfaces.nsIDOMWindowUtils); - let rect = aElement.getBoundingClientRect(); - utils.sendMouseEventToWindow("contextmenu", rect.left + aX, rect.top + aY, 2, 1, 0, true, + let coords = logicalCoordsForElement(aElement, aX, aY); + utils.sendMouseEventToWindow("contextmenu", coords.x, coords.y, 2, 1, 0, true, 1, Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH); } @@ -596,8 +615,8 @@ function sendTap(aWindow, aX, aY) { } function sendElementTap(aWindow, aElement, aX, aY) { - let rect = aElement.getBoundingClientRect(); - EventUtils.synthesizeMouseAtPoint(rect.left + aX, rect.top + aY, { + let coords = logicalCoordsForElement(aElement, aX, aY); + EventUtils.synthesizeMouseAtPoint(coords.x, coords.y, { clickCount: 1, inputSource: Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH }, aWindow);