Bug 1507615 - part2 : add synthesizeTouch() for BrowserTestUtils and test. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D12334
This commit is contained in:
alwu committed 2018-11-20 01:19:18 +00:00
1 parent 917ce6d903
commit 1333592b39
5 files changed
+184 -2

No files matched your search

@@ -1155,6 +1155,63 @@ var BrowserTestUtils = {
});
},
/**
* Versions of EventUtils.jsm synthesizeTouch functions that synthesize a
* touch event in a child process and return promises that resolve when the
* event has fired and completed. Instead of a window, a browser is required
* to be passed to this function.
*
* @param target
* One of the following:
* - a selector string that identifies the element to target. The syntax is as
* for querySelector.
* - An array of selector strings. Each selector after the first
* selects for an element in the iframe specified by the previous
* selector.
* - a CPOW element (for easier test-conversion).
* - a function to be run in the content process that returns the element to
* target
* - null, in which case the offset is from the content document's edge.
* @param {integer} offsetX
* x offset from target's left bounding edge
* @param {integer} offsetY
* y offset from target's top bounding edge
* @param {Object} event object
* Additional arguments, similar to the EventUtils.jsm version
* @param {Browser} browser
* Browser element, must not be null
*
* @returns {Promise}
* @resolves True if the touch event was cancelled.
*/
synthesizeTouch(target, offsetX, offsetY, event, browser) {
return new Promise((resolve, reject) => {
let mm = browser.messageManager;
mm.addMessageListener("Test:SynthesizeTouchDone", function touchMsg(message) {
mm.removeMessageListener("Test:SynthesizeTouchDone", touchMsg);
if (message.data.hasOwnProperty("defaultPrevented")) {
resolve(message.data.defaultPrevented);
} else {
reject(new Error(message.data.error));
}
});
let cpowObject = null;
let targetFn = null;
if (typeof target == "function") {
targetFn = target.toString();
target = null;
} else if (typeof target != "string" && !Array.isArray(target)) {
cpowObject = target;
target = null;
}
mm.sendAsyncMessage("Test:SynthesizeTouch",
{target, targetFn, x: offsetX, y: offsetY, event},
{object: cpowObject});
});
},
/**
* Wait for a message to be fired from a particular message manager
*
@@ -91,6 +91,50 @@ addMessageListener("Test:SynthesizeMouse", (message) => {
sendAsyncMessage("Test:SynthesizeMouseDone", { defaultPrevented: result });
});
addMessageListener("Test:SynthesizeTouch", (message) => {
let data = message.data;
let target = data.target;
if (typeof target == "string") {
target = content.document.querySelector(target);
}
else if (Array.isArray(target)) {
let elem = {contentDocument: content.document};
for (let sel of target) {
elem = elem.contentDocument.querySelector(sel);
}
target = elem;
}
else if (typeof data.targetFn == "string") {
let runnablestr = `
(() => {
return (${data.targetFn});
})();`
target = eval(runnablestr)();
}
else {
target = message.objects.object;
}
if (target) {
if (target.ownerDocument !== content.document) {
// Account for nodes found in iframes.
let cur = target;
do {
cur = cur.ownerGlobal.frameElement;
} while (cur && cur.ownerDocument !== content.document);
// node must be in this document tree.
if (!cur) {
sendAsyncMessage("Test:SynthesizeTouchDone",
{ error: "target must be in the main document tree"});
return;
}
}
}
let result = EventUtils.synthesizeTouch(target, data.x, data.y, data.event, content)
sendAsyncMessage("Test:SynthesizeTouchDone", { defaultPrevented: result });
});
addMessageListener("Test:SendChar", message => {
let result = EventUtils.sendChar(message.data.char, content);
sendAsyncMessage("Test:SendCharDone", { result, seq: message.data.seq });
@@ -408,7 +408,7 @@ function synthesizeMouse(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
function synthesizeTouch(aTarget, aOffsetX, aOffsetY, aEvent, aWindow)
{
var rect = aTarget.getBoundingClientRect();
synthesizeTouchAtPoint(rect.left + aOffsetX, rect.top + aOffsetY,
return synthesizeTouchAtPoint(rect.left + aOffsetX, rect.top + aOffsetY,
aEvent, aWindow);
}
@@ -480,6 +480,7 @@ function synthesizeMouseAtPoint(left, top, aEvent, aWindow = window)
function synthesizeTouchAtPoint(left, top, aEvent, aWindow = window)
{
var utils = _getDOMWindowUtils(aWindow);
let defaultPrevented = false;
if (utils) {
var id = aEvent.id || utils.DEFAULT_TOUCH_POINTER_ID;
@@ -490,13 +491,14 @@ function synthesizeTouchAtPoint(left, top, aEvent, aWindow = window)
var modifiers = _parseModifiers(aEvent, aWindow);
if (("type" in aEvent) && aEvent.type) {
utils.sendTouchEvent(aEvent.type, [id], [left], [top], [rx], [ry], [angle], [force], 1, modifiers);
defaultPrevented = utils.sendTouchEvent(aEvent.type, [id], [left], [top], [rx], [ry], [angle], [force], 1, modifiers);
}
else {
utils.sendTouchEvent("touchstart", [id], [left], [top], [rx], [ry], [angle], [force], 1, modifiers);
utils.sendTouchEvent("touchend", [id], [left], [top], [rx], [ry], [angle], [force], 1, modifiers);
}
}
return defaultPrevented;
}
// Call synthesizeMouse with coordinates at the center of aTarget.
@@ -51,6 +51,7 @@ support-files =
support-files =
gizmo.mp4
file_video.html
[browser_autoplay_policy_touchScroll.js]
[browser_autoplay_policy_web_audio.js]
support-files =
file_empty.html
@@ -0,0 +1,78 @@
/**
* This test is used to ensure that touch in point can activate document and
* allow autoplay, but touch scroll can't activate document.
*/
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";
const PAGE = "https://example.com/browser/toolkit/content/tests/browser/file_nonAutoplayAudio.html";
function checkMediaPlayingState(isPlaying) {
let audio = content.document.getElementById("testAudio");
if (!audio) {
ok(false, "can't get the audio element!");
}
is(!audio.paused, isPlaying, "media playing state is correct.");
}
async function callMediaPlay(shouldStartPlaying) {
let audio = content.document.getElementById("testAudio");
if (!audio) {
ok(false, "can't get the audio element!");
}
info(`call media.play().`);
let playPromise = new Promise((resolve, reject) => {
audio.play().then(() => {
audio.isPlayStarted = true;
resolve();
});
setTimeout(() => {
if (audio.isPlayStarted) {
return;
}
reject();
}, 3000);
});
let isStartPlaying = await playPromise.then(() => true, () => false);
is(isStartPlaying, shouldStartPlaying,
"media is " + (isStartPlaying ? "" : "not ") + "playing.");
}
async function synthesizeTouchScroll(target, browser) {
const offset = 100;
await BrowserTestUtils.synthesizeTouch(target, 0, 0,
{ type: "touchstart" }, browser);
await BrowserTestUtils.synthesizeTouch(target, offset / 2, offset / 2,
{ type: "touchmove" }, browser);
await BrowserTestUtils.synthesizeTouch(target, offset, offset,
{ type: "touchend" }, browser);
}
add_task(async function setup_test_preference() {
return SpecialPowers.pushPrefEnv({"set": [
["media.autoplay.default", SpecialPowers.Ci.nsIAutoplay.PROMPT],
["media.autoplay.enabled.user-gestures-needed", true],
["media.autoplay.ask-permission", true],
]});
});
add_task(async function testTouchScroll() {
await BrowserTestUtils.withNewTab({
gBrowser,
url: PAGE,
}, async (browser) => {
info(`- media should not start playing -`);
await ContentTask.spawn(browser, false, checkMediaPlayingState);
info(`- simulate touch scroll which should not activate document -`);
await synthesizeTouchScroll("#testAudio", browser);
await ContentTask.spawn(browser, false, callMediaPlay);
info(`- simulate touch at a point which should activate document -`);
await BrowserTestUtils.synthesizeTouch("#testAudio", 0, 0, {}, browser);
await ContentTask.spawn(browser, true, callMediaPlay);
});
});