|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 /* |
|
5 * Test Google search plugin URLs |
|
6 */ |
|
7 |
|
8 "use strict"; |
|
9 |
|
10 const BROWSER_SEARCH_PREF = "browser.search."; |
|
11 |
|
12 const MOZ_PARAM_LOCALE = /\{moz:locale\}/g; |
|
13 const MOZ_PARAM_DIST_ID = /\{moz:distributionID\}/g; |
|
14 const MOZ_PARAM_OFFICIAL = /\{moz:official\}/g; |
|
15 |
|
16 let runtime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime); |
|
17 // Custom search parameters |
|
18 const MOZ_OFFICIAL = runtime.isOfficialBranding ? "official" : "unofficial"; |
|
19 |
|
20 var google_client; |
|
21 switch (runtime.defaultUpdateChannel) { |
|
22 case "beta": |
|
23 google_client = "firefox-beta"; |
|
24 break; |
|
25 case "aurora": |
|
26 google_client = "firefox-aurora"; |
|
27 break; |
|
28 case "nightly": |
|
29 google_client = "firefox-nightly"; |
|
30 break; |
|
31 default: |
|
32 google_client = "firefox-a"; |
|
33 break; |
|
34 } |
|
35 |
|
36 const GOOGLE_CLIENT = google_client; |
|
37 const MOZ_DISTRIBUTION_ID = runtime.distributionID; |
|
38 |
|
39 function test() { |
|
40 let engine = Services.search.getEngineByName("Google"); |
|
41 ok(engine, "Google is installed"); |
|
42 |
|
43 is(Services.search.defaultEngine, engine, "Check that Google is the default search engine"); |
|
44 |
|
45 let distributionID; |
|
46 try { |
|
47 distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID"); |
|
48 } catch (ex) { |
|
49 distributionID = MOZ_DISTRIBUTION_ID; |
|
50 } |
|
51 |
|
52 let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t&rls={moz:distributionID}:{moz:locale}:{moz:official}&client=" + GOOGLE_CLIENT; |
|
53 base = base.replace(MOZ_PARAM_LOCALE, getLocale()); |
|
54 base = base.replace(MOZ_PARAM_DIST_ID, distributionID); |
|
55 base = base.replace(MOZ_PARAM_OFFICIAL, MOZ_OFFICIAL); |
|
56 |
|
57 let url; |
|
58 |
|
59 // Test search URLs (including purposes). |
|
60 url = engine.getSubmission("foo").uri.spec; |
|
61 is(url, base, "Check search URL for 'foo'"); |
|
62 |
|
63 waitForExplicitFinish(); |
|
64 |
|
65 var gCurrTest; |
|
66 var gTests = [ |
|
67 { |
|
68 name: "context menu search", |
|
69 searchURL: base + "&channel=rcs", |
|
70 run: function () { |
|
71 // Simulate a contextmenu search |
|
72 // FIXME: This is a bit "low-level"... |
|
73 BrowserSearch.loadSearch("foo", false, "contextmenu"); |
|
74 } |
|
75 }, |
|
76 { |
|
77 name: "keyword search", |
|
78 searchURL: base + "&channel=fflb", |
|
79 run: function () { |
|
80 gURLBar.value = "? foo"; |
|
81 gURLBar.focus(); |
|
82 EventUtils.synthesizeKey("VK_RETURN", {}); |
|
83 } |
|
84 }, |
|
85 { |
|
86 name: "search bar search", |
|
87 searchURL: base + "&channel=sb", |
|
88 run: function () { |
|
89 let sb = BrowserSearch.searchBar; |
|
90 sb.focus(); |
|
91 sb.value = "foo"; |
|
92 registerCleanupFunction(function () { |
|
93 sb.value = ""; |
|
94 }); |
|
95 EventUtils.synthesizeKey("VK_RETURN", {}); |
|
96 } |
|
97 }, |
|
98 { |
|
99 name: "new tab search", |
|
100 searchURL: base + "&channel=nts", |
|
101 run: function () { |
|
102 function doSearch(doc) { |
|
103 // Re-add the listener, and perform a search |
|
104 gBrowser.addProgressListener(listener); |
|
105 doc.getElementById("newtab-search-text").value = "foo"; |
|
106 doc.getElementById("newtab-search-submit").click(); |
|
107 } |
|
108 |
|
109 // load about:newtab, but remove the listener first so it doesn't |
|
110 // get in the way |
|
111 gBrowser.removeProgressListener(listener); |
|
112 gBrowser.loadURI("about:newtab"); |
|
113 info("Waiting for about:newtab load"); |
|
114 tab.linkedBrowser.addEventListener("load", function load(event) { |
|
115 if (event.originalTarget != tab.linkedBrowser.contentDocument || |
|
116 event.target.location.href == "about:blank") { |
|
117 info("skipping spurious load event"); |
|
118 return; |
|
119 } |
|
120 tab.linkedBrowser.removeEventListener("load", load, true); |
|
121 |
|
122 // Observe page setup |
|
123 let win = gBrowser.contentWindow; |
|
124 if (win.gSearch.currentEngineName == |
|
125 Services.search.currentEngine.name) { |
|
126 doSearch(win.document); |
|
127 } |
|
128 else { |
|
129 info("Waiting for newtab search init"); |
|
130 win.addEventListener("ContentSearchService", function done(event) { |
|
131 info("Got newtab search event " + event.detail.type); |
|
132 if (event.detail.type == "State") { |
|
133 win.removeEventListener("ContentSearchService", done); |
|
134 // Let gSearch respond to the event before continuing. |
|
135 executeSoon(() => doSearch(win.document)); |
|
136 } |
|
137 }); |
|
138 } |
|
139 }, true); |
|
140 } |
|
141 }, |
|
142 { |
|
143 name: "home page search", |
|
144 searchURL: base + "&channel=np&source=hp", |
|
145 run: function () { |
|
146 // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB) |
|
147 ignoreAllUncaughtExceptions(true); |
|
148 |
|
149 // load about:home, but remove the listener first so it doesn't |
|
150 // get in the way |
|
151 gBrowser.removeProgressListener(listener); |
|
152 gBrowser.loadURI("about:home"); |
|
153 info("Waiting for about:home load"); |
|
154 tab.linkedBrowser.addEventListener("load", function load(event) { |
|
155 if (event.originalTarget != tab.linkedBrowser.contentDocument || |
|
156 event.target.location.href == "about:blank") { |
|
157 info("skipping spurious load event"); |
|
158 return; |
|
159 } |
|
160 tab.linkedBrowser.removeEventListener("load", load, true); |
|
161 |
|
162 // Observe page setup |
|
163 let doc = gBrowser.contentDocument; |
|
164 let mutationObserver = new MutationObserver(function (mutations) { |
|
165 for (let mutation of mutations) { |
|
166 if (mutation.attributeName == "searchEngineName") { |
|
167 // Re-add the listener, and perform a search |
|
168 gBrowser.addProgressListener(listener); |
|
169 doc.getElementById("searchText").value = "foo"; |
|
170 doc.getElementById("searchSubmit").click(); |
|
171 } |
|
172 } |
|
173 }); |
|
174 mutationObserver.observe(doc.documentElement, { attributes: true }); |
|
175 }, true); |
|
176 } |
|
177 } |
|
178 ]; |
|
179 |
|
180 function nextTest() { |
|
181 // Make sure we listen again for uncaught exceptions in the next test or cleanup. |
|
182 ignoreAllUncaughtExceptions(false); |
|
183 |
|
184 if (gTests.length) { |
|
185 gCurrTest = gTests.shift(); |
|
186 info("Running : " + gCurrTest.name); |
|
187 executeSoon(gCurrTest.run); |
|
188 } else { |
|
189 finish(); |
|
190 } |
|
191 } |
|
192 |
|
193 let tab = gBrowser.selectedTab = gBrowser.addTab(); |
|
194 |
|
195 let listener = { |
|
196 onStateChange: function onStateChange(webProgress, req, flags, status) { |
|
197 info("onStateChange"); |
|
198 // Only care about top-level document starts |
|
199 let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | |
|
200 Ci.nsIWebProgressListener.STATE_START; |
|
201 if (!(flags & docStart) || !webProgress.isTopLevel) |
|
202 return; |
|
203 |
|
204 info("received document start"); |
|
205 |
|
206 ok(req instanceof Ci.nsIChannel, "req is a channel"); |
|
207 is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); |
|
208 info("Actual URI: " + req.URI.spec); |
|
209 |
|
210 req.cancel(Components.results.NS_ERROR_FAILURE); |
|
211 |
|
212 executeSoon(nextTest); |
|
213 } |
|
214 } |
|
215 |
|
216 registerCleanupFunction(function () { |
|
217 gBrowser.removeProgressListener(listener); |
|
218 gBrowser.removeTab(tab); |
|
219 }); |
|
220 |
|
221 tab.linkedBrowser.addEventListener("load", function load() { |
|
222 tab.linkedBrowser.removeEventListener("load", load, true); |
|
223 gBrowser.addProgressListener(listener); |
|
224 nextTest(); |
|
225 }, true); |
|
226 } |