Bug 903737 - Detect whether the user right-clicked selected text properly. r=rsilveira

This commit is contained in:
Jim Mathies 2013-08-19 04:25:58 -05:00
parent 93a56f9cab
commit 733a0db294
5 changed files with 76 additions and 3 deletions

View File

@ -293,7 +293,7 @@ var ContextMenuHandler = {
// If this is text and has a selection, we want to bring // If this is text and has a selection, we want to bring
// up the copy option on the context menu. // up the copy option on the context menu.
let selection = targetWindow.getSelection(); let selection = targetWindow.getSelection();
if (selection && selection.toString().length > 0) { if (selection && this._tapInSelection(selection, aX, aY)) {
state.string = targetWindow.getSelection().toString(); state.string = targetWindow.getSelection().toString();
state.types.push("copy"); state.types.push("copy");
state.types.push("selected-text"); state.types.push("selected-text");
@ -323,6 +323,20 @@ var ContextMenuHandler = {
sendAsyncMessage("Content:ContextMenu", state); sendAsyncMessage("Content:ContextMenu", state);
}, },
_tapInSelection: function (aSelection, aX, aY) {
if (!aSelection || !aSelection.rangeCount) {
return false;
}
for (let idx = 0; idx < aSelection.rangeCount; idx++) {
let range = aSelection.getRangeAt(idx);
let rect = range.getBoundingClientRect();
if (Util.pointWithinDOMRect(aX, aY, rect)) {
return true;
}
}
return false;
},
_getLinkURL: function ch_getLinkURL(aLink) { _getLinkURL: function ch_getLinkURL(aLink) {
let href = aLink.href; let href = aLink.href;
if (href) if (href)

View File

@ -22,6 +22,16 @@ was to get out again.
The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly
down, so suddenly that Alice had not a moment to think about stopping herself before she down, so suddenly that Alice had not a moment to think about stopping herself before she
found herself falling down a very deep well. found herself falling down a very deep well.
<div id="seldiv" style="width:300px; height:100px;">The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly
down, so suddenly that Alice had not a moment to think about stopping herself before she
found herself falling down a very deep well.
</div>
<br />
<br />
<br />
<br />
<br />
<div id="emptydiv" style="width:300px; height:100px; border: solid 1px black;"></div>
<br> <br>
<br> <br>
<br> <br>

View File

@ -284,12 +284,15 @@ gTests.push({
400, 400,
400, 400,
400, 400,
200); 350);
yield waitForCondition(function () { yield waitForCondition(function () {
return !SelectionHelperUI.isSelectionUIVisible; return !SelectionHelperUI.isSelectionUIVisible;
}, kCommonWaitMs, kCommonPollMs); }, kCommonWaitMs, kCommonPollMs);
// cancel fling from scroll above
TouchModule.cancelPending();
// active state - should be disabled after a page scroll // active state - should be disabled after a page scroll
is(SelectionHelperUI.isActive, false, "selection inactive"); is(SelectionHelperUI.isActive, false, "selection inactive");
}, },
@ -347,6 +350,32 @@ gTests.push({
tearDown: setUpAndTearDown, tearDown: setUpAndTearDown,
}); });
gTests.push({
desc: "bug 903737 - right click targeting",
setUp: setUpAndTearDown,
run: function test() {
yield hideContextUI();
let range = gWindow.document.createRange();
range.selectNode(gWindow.document.getElementById("seldiv"));
gWindow.getSelection().addRange(range);
let promise = waitForEvent(document, "popupshown");
sendContextMenuClickToElement(gWindow, gWindow.document.getElementById("seldiv"));
yield promise;
promise = waitForEvent(document, "popuphidden");
ContextMenuUI.hide();
yield promise;
let emptydiv = gWindow.document.getElementById("emptydiv");
let coords = logicalCoordsForElement(emptydiv);
InputSourceHelper.isPrecise = true;
sendContextMenuClick(coords.x, coords.y);
yield waitForCondition(function () {
return ContextUI.tabbarVisible;
});
yield hideContextUI();
},
tearDown: setUpAndTearDown,
});
function test() { function test() {
if (!isLandscapeMode()) { if (!isLandscapeMode()) {
todo(false, "browser_selection_tests need landscape mode to run."); todo(false, "browser_selection_tests need landscape mode to run.");

View File

@ -134,7 +134,7 @@ gTests.push({
is(getTrimmedSelection(gFrame).toString(), "started", "selection test"); is(getTrimmedSelection(gFrame).toString(), "started", "selection test");
let promise = waitForEvent(document, "popupshown"); let promise = waitForEvent(document, "popupshown");
sendContextMenuClick(527, 188); sendContextMenuClickToSelection(gFrame.contentDocument.defaultView);
yield promise; yield promise;
ok(promise && !(promise instanceof Error), "promise error"); ok(promise && !(promise instanceof Error), "promise error");

View File

@ -582,6 +582,26 @@ function sendContextMenuClick(aX, aY) {
1, Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH); 1, Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH);
} }
/*
* sendContextMenuClickToSelection - simulates a press-hold touch input event
* selected text in a window.
*/
function sendContextMenuClickToSelection(aWindow) {
let selection = aWindow.getSelection();
if (!selection || !selection.rangeCount) {
ok(false, "no selection to tap!");
return;
}
let range = selection.getRangeAt(0);
let rect = range.getBoundingClientRect();
let x = rect.left + (rect.width / 2);
let y = rect.top + (rect.height / 2);
let utils = aWindow.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils);
utils.sendMouseEventToWindow("contextmenu", x, y, 2, 1, 0, true,
1, Ci.nsIDOMMouseEvent.MOZ_SOURCE_TOUCH);
}
/* /*
* sendContextMenuClickToWindow - simulates a press-hold touch input event. * sendContextMenuClickToWindow - simulates a press-hold touch input event.
* *