Bug 774809 - [BrowserAPI] Add methods to send mouse/touch events to the content. r=jlebar

This commit is contained in:
Vivien Nicolas 2012-07-20 17:41:30 +02:00
parent 51186dbf6a
commit 881efe11c9
6 changed files with 174 additions and 0 deletions

View File

@ -114,6 +114,8 @@ BrowserElementChild.prototype = {
addMsgListener("get-screenshot", this._recvGetScreenshot);
addMsgListener("set-visible", this._recvSetVisible);
addMsgListener("send-mouse-event", this._recvSendMouseEvent);
addMsgListener("send-touch-event", this._recvSendTouchEvent);
addMsgListener("get-can-go-back", this._recvCanGoBack);
addMsgListener("get-can-go-forward", this._recvCanGoForward);
addMsgListener("go-back", this._recvGoBack);
@ -435,6 +437,24 @@ BrowserElementChild.prototype = {
}
},
_recvSendMouseEvent: function(data) {
let json = data.json;
let utils = content.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
utils.sendMouseEvent(json.type, json.x, json.y, json.button,
json.clickCount, json.modifiers);
},
_recvSendTouchEvent: function(data) {
let json = data.json;
let utils = content.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
utils.sendTouchEvent(json.type, json.identifiers, json.touchesX,
json.touchesY, json.radiisX, json.radiisY,
json.rotationAngles, json.forces, json.count,
json.modifiers);
},
_recvCanGoBack: function(data) {
var webNav = docShell.QueryInterface(Ci.nsIWebNavigation);
sendAsyncMsg('got-can-go-back', {

View File

@ -13,6 +13,7 @@ Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
const BROWSER_FRAMES_ENABLED_PREF = "dom.mozBrowserFramesEnabled";
const TOUCH_EVENTS_ENABLED_PREF = "dom.w3c_touch_events.enabled";
function debug(msg) {
//dump("BrowserElementParent - " + msg + "\n");
@ -179,6 +180,10 @@ function BrowserElementParent(frameLoader) {
// Define methods on the frame element.
defineMethod('setVisible', this._setVisible);
defineMethod('sendMouseEvent', this._sendMouseEvent);
if (Services.prefs.getBoolPref(TOUCH_EVENTS_ENABLED_PREF)) {
defineMethod('sendTouchEvent', this._sendTouchEvent);
}
defineMethod('goBack', this._goBack);
defineMethod('goForward', this._goForward);
defineMethod('reload', this._reload);
@ -353,6 +358,34 @@ BrowserElementParent.prototype = {
this._sendAsyncMsg('set-visible', {visible: visible});
},
_sendMouseEvent: function(type, x, y, button, clickCount, modifiers) {
this._sendAsyncMsg("send-mouse-event", {
"type": type,
"x": x,
"y": y,
"button": button,
"clickCount": clickCount,
"modifiers": modifiers
});
},
_sendTouchEvent: function(type, identifiers, touchesX, touchesY,
radiisX, radiisY, rotationAngles, forces,
count, modifiers) {
this._sendAsyncMsg("send-touch-event", {
"type": type,
"identifiers": identifiers,
"touchesX": touchesX,
"touchesY": touchesY,
"radiisX": radiisX,
"radiisY": radiisY,
"rotationAngles": rotationAngles,
"forces": forces,
"count": count,
"modifiers": modifiers
});
},
_goBack: function() {
this._sendAsyncMsg('go-back');
},

View File

@ -86,6 +86,8 @@ MOCHITEST_FILES = \
test_browserElement_inproc_Stop.html \
browserElement_ContextmenuEvents.js \
test_browserElement_inproc_ContextmenuEvents.html \
browserElement_SendEvent.js \
test_browserElement_inproc_SendEvent.html \
$(NULL)
# Disabled due to https://bugzilla.mozilla.org/show_bug.cgi?id=774100
@ -124,6 +126,7 @@ MOCHITEST_FILES += \
test_browserElement_oop_Reload.html \
test_browserElement_oop_Stop.html \
test_browserElement_oop_ContextmenuEvents.html \
test_browserElement_oop_SendEvent.html \
$(NULL)
endif #}
endif #}

View File

@ -0,0 +1,82 @@
/* Any copyright is dedicated to the public domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Test that sendMouseEvent dispatch events.
"use strict";
SimpleTest.waitForExplicitFinish();
function runTest() {
browserElementTestHelpers.setEnabledPref(true);
browserElementTestHelpers.addToWhitelist();
var iframe = document.createElement("iframe");
iframe.mozbrowser = true;
document.body.appendChild(iframe);
iframe.addEventListener("mozbrowserloadend", function onloadend(e) {
iframe.sendMouseEvent("mousedown", 10, 10, 0, 1, 0);
});
iframe.addEventListener("mozbrowserlocationchange", function onlocchange(e) {
var a = document.createElement("a");
a.href = e.detail;
switch (a.hash) {
case "#mousedown":
ok(true, "Receive a mousedown event.");
iframe.sendMouseEvent("mousemove", 10, 10, 0, 0, 0);
break;
case "#mousemove":
ok(true, "Receive a mousemove event.");
iframe.sendMouseEvent("mouseup", 10, 10, 0, 1, 0);
break;
case "#mouseup":
ok(true, "Receive a mouseup event.");
break;
case "#click":
ok(true, "Receive a click event.");
if (SpecialPowers.getBoolPref("dom.w3c_touch_events.enabled")) {
iframe.sendTouchEvent("touchstart", [1], [10], [10], [2], [2],
[20], [0.5], 1, 0);
} else {
SimpleTest.finish();
}
break;
case "#touchstart":
ok(true, "Receive a touchstart event.");
iframe.sendTouchEvent("touchmove", [1], [10], [10], [2], [2],
[20], [0.5], 1, 0);
case "#touchmove":
ok(true, "Receive a touchmove event.");
iframe.sendTouchEvent("touchend", [1], [10], [10], [2], [2],
[20], [0.5], 1, 0);
break;
case "#touchend":
ok(true, "Receive a touchend event.");
iframe.sendTouchEvent("touchcancel", [1], [10], [10], [2], [2],
[20], [0.5], 1, 0);
SimpleTest.finish();
break;
}
});
iframe.src = "data:text/html,<html><body>" +
"<button>send[Mouse|Touch]Event</button>" +
"</body><script>" +
"function changeHash(e) {" +
" document.location.hash = e.type;" +
"};" +
"window.addEventListener('mousedown', changeHash);" +
"window.addEventListener('mousemove', changeHash);" +
"window.addEventListener('mouseup', changeHash);" +
"window.addEventListener('click', changeHash, true);" +
"window.addEventListener('touchstart', changeHash);" +
"window.addEventListener('touchmove', changeHash);" +
"window.addEventListener('touchend', changeHash);" +
"window.addEventListener('touchcancel', changeHash);" +
"</script></html>";
}
runTest();

View File

@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=774809
-->
<head>
<title>Test for Bug 774809</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=774809">Mozilla Bug 774809</a>
<script type="application/javascript;version=1.7" src="browserElement_SendEvent.js">
</script>
</body>
</html>

View File

@ -0,0 +1,18 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=774809
-->
<head>
<title>Test for Bug 774809</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="browserElementTestHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=774809">Mozilla Bug 774809</a>
<script type="application/javascript;version=1.7" src="browserElement_SendEvent.js">
</script>
</body>
</html>