2011-03-19 00:56:46 +03:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* * http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
/*
|
|
|
|
* Test searching for the selected text using the context menu
|
|
|
|
*/
|
|
|
|
|
|
|
|
function test() {
|
|
|
|
waitForExplicitFinish();
|
|
|
|
|
|
|
|
const ss = Services.search;
|
|
|
|
const ENGINE_NAME = "Foo";
|
|
|
|
var contextMenu;
|
|
|
|
|
2015-10-30 11:54:52 +03:00
|
|
|
let envService = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
|
|
|
|
let originalValue = envService.get("XPCSHELL_TEST_PROFILE_DIR");
|
|
|
|
envService.set("XPCSHELL_TEST_PROFILE_DIR", "1");
|
|
|
|
|
|
|
|
let url = "chrome://mochitests/content/browser/browser/components/search/test/";
|
|
|
|
let resProt = Services.io.getProtocolHandler("resource")
|
|
|
|
.QueryInterface(Ci.nsIResProtocolHandler);
|
|
|
|
let originalSubstitution = resProt.getSubstitution("search-plugins");
|
|
|
|
resProt.setSubstitution("search-plugins",
|
|
|
|
Services.io.newURI(url, null, null));
|
|
|
|
|
2011-03-19 00:56:46 +03:00
|
|
|
function observer(aSub, aTopic, aData) {
|
|
|
|
switch (aData) {
|
|
|
|
case "engine-added":
|
|
|
|
var engine = ss.getEngineByName(ENGINE_NAME);
|
|
|
|
ok(engine, "Engine was added.");
|
2013-06-18 17:39:02 +04:00
|
|
|
ss.currentEngine = engine;
|
2015-10-30 11:54:52 +03:00
|
|
|
envService.set("XPCSHELL_TEST_PROFILE_DIR", originalValue);
|
|
|
|
resProt.setSubstitution("search-plugins", originalSubstitution);
|
2011-03-19 00:56:46 +03:00
|
|
|
break;
|
|
|
|
case "engine-current":
|
2013-03-12 16:38:42 +04:00
|
|
|
is(ss.currentEngine.name, ENGINE_NAME, "currentEngine set");
|
2011-03-19 00:56:46 +03:00
|
|
|
startTest();
|
|
|
|
break;
|
|
|
|
case "engine-removed":
|
|
|
|
Services.obs.removeObserver(observer, "browser-search-engine-modified");
|
|
|
|
finish();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.obs.addObserver(observer, "browser-search-engine-modified", false);
|
2015-10-30 11:54:52 +03:00
|
|
|
ss.addEngine("resource://search-plugins/testEngine_mozsearch.xml",
|
2015-09-23 13:05:37 +03:00
|
|
|
null, "data:image/x-icon,%00", false);
|
2011-03-19 00:56:46 +03:00
|
|
|
|
|
|
|
function startTest() {
|
|
|
|
contextMenu = document.getElementById("contentAreaContextMenu");
|
|
|
|
ok(contextMenu, "Got context menu XUL");
|
|
|
|
|
|
|
|
doOnloadOnce(testContextMenu);
|
2013-06-08 02:34:25 +04:00
|
|
|
let tab = gBrowser.selectedTab = gBrowser.addTab("data:text/plain;charset=utf8,test%20search");
|
|
|
|
registerCleanupFunction(function () {
|
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
});
|
2011-03-19 00:56:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function testContextMenu() {
|
2013-03-12 16:38:42 +04:00
|
|
|
function rightClickOnDocument() {
|
2013-03-14 06:23:03 +04:00
|
|
|
info("rightClickOnDocument: " + content.window.location);
|
2013-03-12 16:38:42 +04:00
|
|
|
waitForBrowserContextMenu(checkContextMenu);
|
2011-03-19 00:56:46 +03:00
|
|
|
var clickTarget = content.document.body;
|
|
|
|
var eventDetails = { type: "contextmenu", button: 2 };
|
|
|
|
EventUtils.synthesizeMouseAtCenter(clickTarget, eventDetails, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the search menu item and then perform a search
|
|
|
|
function checkContextMenu() {
|
2013-03-14 06:23:03 +04:00
|
|
|
info("checkContextMenu");
|
2011-03-19 00:56:46 +03:00
|
|
|
var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0];
|
|
|
|
ok(searchItem, "Got search context menu item");
|
|
|
|
is(searchItem.label, 'Search ' + ENGINE_NAME + ' for "test search"', "Check context menu label");
|
|
|
|
is(searchItem.disabled, false, "Check that search context menu item is enabled");
|
2013-03-14 06:23:03 +04:00
|
|
|
doOnloadOnce(checkSearchURL);
|
2011-03-19 00:56:46 +03:00
|
|
|
searchItem.click();
|
2012-10-24 08:27:58 +04:00
|
|
|
contextMenu.hidePopup();
|
2011-03-19 00:56:46 +03:00
|
|
|
}
|
|
|
|
|
2013-03-14 06:23:03 +04:00
|
|
|
function checkSearchURL(event) {
|
2011-03-19 00:56:46 +03:00
|
|
|
is(event.originalTarget.URL,
|
2015-10-01 16:05:59 +03:00
|
|
|
"http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&channel=contextsearch",
|
2011-03-19 00:56:46 +03:00
|
|
|
"Checking context menu search URL");
|
2013-06-08 02:34:25 +04:00
|
|
|
// Remove the tab opened by the search
|
|
|
|
gBrowser.removeCurrentTab();
|
|
|
|
ss.removeEngine(ss.currentEngine);
|
2011-03-19 00:56:46 +03:00
|
|
|
}
|
|
|
|
|
2013-03-14 06:23:03 +04:00
|
|
|
var selectionListener = {
|
|
|
|
notifySelectionChanged: function(doc, sel, reason) {
|
|
|
|
if (reason != Ci.nsISelectionListener.SELECTALL_REASON || sel.toString() != "test search")
|
|
|
|
return;
|
|
|
|
info("notifySelectionChanged: Text selected");
|
|
|
|
content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
|
|
|
|
removeSelectionListener(selectionListener);
|
|
|
|
SimpleTest.executeSoon(rightClickOnDocument);
|
|
|
|
}
|
|
|
|
};
|
2011-03-19 00:56:46 +03:00
|
|
|
|
2013-03-15 10:59:06 +04:00
|
|
|
// Delay the select all to avoid intermittent selection failures.
|
|
|
|
setTimeout(function delaySelectAll() {
|
|
|
|
info("delaySelectAll: " + content.window.location.toString());
|
|
|
|
// add a listener to know when the selection takes effect
|
|
|
|
content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
|
|
|
|
addSelectionListener(selectionListener);
|
|
|
|
// select the text on the page
|
|
|
|
goDoCommand('cmd_selectAll');
|
|
|
|
}, 500);
|
2011-03-19 00:56:46 +03:00
|
|
|
}
|
|
|
|
}
|