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