browser/components/search/test/browser_bing_behavior.js

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

mercurial