Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | /* |
michael@0 | 4 | * Test searching for the selected text using the context menu |
michael@0 | 5 | */ |
michael@0 | 6 | |
michael@0 | 7 | function test() { |
michael@0 | 8 | waitForExplicitFinish(); |
michael@0 | 9 | |
michael@0 | 10 | const ss = Services.search; |
michael@0 | 11 | const ENGINE_NAME = "Foo"; |
michael@0 | 12 | var contextMenu; |
michael@0 | 13 | |
michael@0 | 14 | function observer(aSub, aTopic, aData) { |
michael@0 | 15 | switch (aData) { |
michael@0 | 16 | case "engine-added": |
michael@0 | 17 | var engine = ss.getEngineByName(ENGINE_NAME); |
michael@0 | 18 | ok(engine, "Engine was added."); |
michael@0 | 19 | ss.currentEngine = engine; |
michael@0 | 20 | break; |
michael@0 | 21 | case "engine-current": |
michael@0 | 22 | is(ss.currentEngine.name, ENGINE_NAME, "currentEngine set"); |
michael@0 | 23 | startTest(); |
michael@0 | 24 | break; |
michael@0 | 25 | case "engine-removed": |
michael@0 | 26 | Services.obs.removeObserver(observer, "browser-search-engine-modified"); |
michael@0 | 27 | finish(); |
michael@0 | 28 | break; |
michael@0 | 29 | } |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | Services.obs.addObserver(observer, "browser-search-engine-modified", false); |
michael@0 | 33 | ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine_mozsearch.xml", |
michael@0 | 34 | Ci.nsISearchEngine.DATA_XML, "data:image/x-icon,%00", |
michael@0 | 35 | false); |
michael@0 | 36 | |
michael@0 | 37 | function startTest() { |
michael@0 | 38 | contextMenu = document.getElementById("contentAreaContextMenu"); |
michael@0 | 39 | ok(contextMenu, "Got context menu XUL"); |
michael@0 | 40 | |
michael@0 | 41 | doOnloadOnce(testContextMenu); |
michael@0 | 42 | let tab = gBrowser.selectedTab = gBrowser.addTab("data:text/plain;charset=utf8,test%20search"); |
michael@0 | 43 | registerCleanupFunction(function () { |
michael@0 | 44 | gBrowser.removeTab(tab); |
michael@0 | 45 | }); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | function testContextMenu() { |
michael@0 | 49 | function rightClickOnDocument() { |
michael@0 | 50 | info("rightClickOnDocument: " + content.window.location); |
michael@0 | 51 | waitForBrowserContextMenu(checkContextMenu); |
michael@0 | 52 | var clickTarget = content.document.body; |
michael@0 | 53 | var eventDetails = { type: "contextmenu", button: 2 }; |
michael@0 | 54 | EventUtils.synthesizeMouseAtCenter(clickTarget, eventDetails, content); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | // check the search menu item and then perform a search |
michael@0 | 58 | function checkContextMenu() { |
michael@0 | 59 | info("checkContextMenu"); |
michael@0 | 60 | var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0]; |
michael@0 | 61 | ok(searchItem, "Got search context menu item"); |
michael@0 | 62 | is(searchItem.label, 'Search ' + ENGINE_NAME + ' for "test search"', "Check context menu label"); |
michael@0 | 63 | is(searchItem.disabled, false, "Check that search context menu item is enabled"); |
michael@0 | 64 | doOnloadOnce(checkSearchURL); |
michael@0 | 65 | searchItem.click(); |
michael@0 | 66 | contextMenu.hidePopup(); |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function checkSearchURL(event) { |
michael@0 | 70 | is(event.originalTarget.URL, |
michael@0 | 71 | "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&client=app&channel=contextsearch", |
michael@0 | 72 | "Checking context menu search URL"); |
michael@0 | 73 | // Remove the tab opened by the search |
michael@0 | 74 | gBrowser.removeCurrentTab(); |
michael@0 | 75 | ss.removeEngine(ss.currentEngine); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | var selectionListener = { |
michael@0 | 79 | notifySelectionChanged: function(doc, sel, reason) { |
michael@0 | 80 | if (reason != Ci.nsISelectionListener.SELECTALL_REASON || sel.toString() != "test search") |
michael@0 | 81 | return; |
michael@0 | 82 | info("notifySelectionChanged: Text selected"); |
michael@0 | 83 | content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate). |
michael@0 | 84 | removeSelectionListener(selectionListener); |
michael@0 | 85 | SimpleTest.executeSoon(rightClickOnDocument); |
michael@0 | 86 | } |
michael@0 | 87 | }; |
michael@0 | 88 | |
michael@0 | 89 | // Delay the select all to avoid intermittent selection failures. |
michael@0 | 90 | setTimeout(function delaySelectAll() { |
michael@0 | 91 | info("delaySelectAll: " + content.window.location.toString()); |
michael@0 | 92 | // add a listener to know when the selection takes effect |
michael@0 | 93 | content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate). |
michael@0 | 94 | addSelectionListener(selectionListener); |
michael@0 | 95 | // select the text on the page |
michael@0 | 96 | goDoCommand('cmd_selectAll'); |
michael@0 | 97 | }, 500); |
michael@0 | 98 | } |
michael@0 | 99 | } |