browser/modules/test/browser_ContentSearch.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 const TEST_MSG = "ContentSearchTest";
michael@0 6 const CONTENT_SEARCH_MSG = "ContentSearch";
michael@0 7 const TEST_CONTENT_SCRIPT_BASENAME = "contentSearch.js";
michael@0 8
michael@0 9 function generatorTest() {
michael@0 10 // nextStep() drives the iterator returned by this function. This function's
michael@0 11 // iterator in turn drives the iterator of each test below.
michael@0 12 let currentTestIter = yield startNextTest();
michael@0 13 let arg = undefined;
michael@0 14 while (currentTestIter) {
michael@0 15 try {
michael@0 16 currentTestIter.send(arg);
michael@0 17 arg = yield null;
michael@0 18 }
michael@0 19 catch (err if err instanceof StopIteration) {
michael@0 20 currentTestIter = yield startNextTest();
michael@0 21 arg = undefined;
michael@0 22 }
michael@0 23 }
michael@0 24 }
michael@0 25
michael@0 26 function startNextTest() {
michael@0 27 if (!gTests.length) {
michael@0 28 setTimeout(() => nextStep(null), 0);
michael@0 29 return;
michael@0 30 }
michael@0 31 let nextTestGen = gTests.shift();
michael@0 32 let nextTestIter = nextTestGen();
michael@0 33 addTab(() => {
michael@0 34 info("Starting test " + nextTestGen.name);
michael@0 35 nextStep(nextTestIter);
michael@0 36 });
michael@0 37 }
michael@0 38
michael@0 39 function addTest(testGen) {
michael@0 40 gTests.push(testGen);
michael@0 41 }
michael@0 42
michael@0 43 var gTests = [];
michael@0 44 var gMsgMan;
michael@0 45
michael@0 46 addTest(function GetState() {
michael@0 47 gMsgMan.sendAsyncMessage(TEST_MSG, {
michael@0 48 type: "GetState",
michael@0 49 });
michael@0 50 let msg = yield waitForTestMsg("State");
michael@0 51 checkMsg(msg, {
michael@0 52 type: "State",
michael@0 53 data: currentStateObj(),
michael@0 54 });
michael@0 55 });
michael@0 56
michael@0 57 addTest(function SetCurrentEngine() {
michael@0 58 let newCurrentEngine = null;
michael@0 59 let oldCurrentEngine = Services.search.currentEngine;
michael@0 60 let engines = Services.search.getVisibleEngines();
michael@0 61 for (let engine of engines) {
michael@0 62 if (engine != oldCurrentEngine) {
michael@0 63 newCurrentEngine = engine;
michael@0 64 break;
michael@0 65 }
michael@0 66 }
michael@0 67 if (!newCurrentEngine) {
michael@0 68 info("Couldn't find a non-selected search engine, " +
michael@0 69 "skipping this part of the test");
michael@0 70 return;
michael@0 71 }
michael@0 72 gMsgMan.sendAsyncMessage(TEST_MSG, {
michael@0 73 type: "SetCurrentEngine",
michael@0 74 data: newCurrentEngine.name,
michael@0 75 });
michael@0 76 Services.obs.addObserver(function obs(subj, topic, data) {
michael@0 77 info("Test observed " + data);
michael@0 78 if (data == "engine-current") {
michael@0 79 ok(true, "Test observed engine-current");
michael@0 80 Services.obs.removeObserver(obs, "browser-search-engine-modified", false);
michael@0 81 nextStep();
michael@0 82 }
michael@0 83 }, "browser-search-engine-modified", false);
michael@0 84 info("Waiting for test to observe engine-current...");
michael@0 85 waitForTestMsg("CurrentEngine");
michael@0 86 let maybeMsg1 = yield null;
michael@0 87 let maybeMsg2 = yield null;
michael@0 88 let msg = maybeMsg1 || maybeMsg2;
michael@0 89 ok(!!msg,
michael@0 90 "Sanity check: One of the yields is for waitForTestMsg and should have " +
michael@0 91 "therefore produced a message object");
michael@0 92 checkMsg(msg, {
michael@0 93 type: "CurrentEngine",
michael@0 94 data: currentEngineObj(newCurrentEngine),
michael@0 95 });
michael@0 96
michael@0 97 Services.search.currentEngine = oldCurrentEngine;
michael@0 98 let msg = yield waitForTestMsg("CurrentEngine");
michael@0 99 checkMsg(msg, {
michael@0 100 type: "CurrentEngine",
michael@0 101 data: currentEngineObj(oldCurrentEngine),
michael@0 102 });
michael@0 103 });
michael@0 104
michael@0 105 addTest(function ManageEngines() {
michael@0 106 gMsgMan.sendAsyncMessage(TEST_MSG, {
michael@0 107 type: "ManageEngines",
michael@0 108 });
michael@0 109 let winWatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].
michael@0 110 getService(Ci.nsIWindowWatcher);
michael@0 111 winWatcher.registerNotification(function onOpen(subj, topic, data) {
michael@0 112 if (topic == "domwindowopened" && subj instanceof Ci.nsIDOMWindow) {
michael@0 113 subj.addEventListener("load", function onLoad() {
michael@0 114 subj.removeEventListener("load", onLoad);
michael@0 115 if (subj.document.documentURI ==
michael@0 116 "chrome://browser/content/search/engineManager.xul") {
michael@0 117 winWatcher.unregisterNotification(onOpen);
michael@0 118 ok(true, "Observed search manager window open");
michael@0 119 is(subj.opener, window,
michael@0 120 "Search engine manager opener should be this chrome window");
michael@0 121 subj.close();
michael@0 122 nextStep();
michael@0 123 }
michael@0 124 });
michael@0 125 }
michael@0 126 });
michael@0 127 info("Waiting for search engine manager window to open...");
michael@0 128 yield null;
michael@0 129 });
michael@0 130
michael@0 131 addTest(function modifyEngine() {
michael@0 132 let engine = Services.search.currentEngine;
michael@0 133 let oldAlias = engine.alias;
michael@0 134 engine.alias = "ContentSearchTest";
michael@0 135 let msg = yield waitForTestMsg("State");
michael@0 136 checkMsg(msg, {
michael@0 137 type: "State",
michael@0 138 data: currentStateObj(),
michael@0 139 });
michael@0 140 engine.alias = oldAlias;
michael@0 141 msg = yield waitForTestMsg("State");
michael@0 142 checkMsg(msg, {
michael@0 143 type: "State",
michael@0 144 data: currentStateObj(),
michael@0 145 });
michael@0 146 });
michael@0 147
michael@0 148 addTest(function search() {
michael@0 149 let engine = Services.search.currentEngine;
michael@0 150 let data = {
michael@0 151 engineName: engine.name,
michael@0 152 searchString: "ContentSearchTest",
michael@0 153 whence: "ContentSearchTest",
michael@0 154 };
michael@0 155 gMsgMan.sendAsyncMessage(TEST_MSG, {
michael@0 156 type: "Search",
michael@0 157 data: data,
michael@0 158 });
michael@0 159 let submissionURL =
michael@0 160 engine.getSubmission(data.searchString, "", data.whence).uri.spec;
michael@0 161 let listener = {
michael@0 162 onStateChange: function (webProg, req, flags, status) {
michael@0 163 let url = req.originalURI.spec;
michael@0 164 info("onStateChange " + url);
michael@0 165 let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT |
michael@0 166 Ci.nsIWebProgressListener.STATE_START;
michael@0 167 if ((flags & docStart) && webProg.isTopLevel && url == submissionURL) {
michael@0 168 gBrowser.removeProgressListener(listener);
michael@0 169 ok(true, "Search URL loaded");
michael@0 170 req.cancel(Components.results.NS_ERROR_FAILURE);
michael@0 171 nextStep();
michael@0 172 }
michael@0 173 }
michael@0 174 };
michael@0 175 gBrowser.addProgressListener(listener);
michael@0 176 info("Waiting for search URL to load: " + submissionURL);
michael@0 177 yield null;
michael@0 178 });
michael@0 179
michael@0 180 function checkMsg(actualMsg, expectedMsgData) {
michael@0 181 SimpleTest.isDeeply(actualMsg.data, expectedMsgData, "Checking message");
michael@0 182 }
michael@0 183
michael@0 184 function waitForMsg(name, type, callback) {
michael@0 185 info("Waiting for " + name + " message " + type + "...");
michael@0 186 gMsgMan.addMessageListener(name, function onMsg(msg) {
michael@0 187 info("Received " + name + " message " + msg.data.type + "\n");
michael@0 188 if (msg.data.type == type) {
michael@0 189 gMsgMan.removeMessageListener(name, onMsg);
michael@0 190 (callback || nextStep)(msg);
michael@0 191 }
michael@0 192 });
michael@0 193 }
michael@0 194
michael@0 195 function waitForTestMsg(type, callback) {
michael@0 196 waitForMsg(TEST_MSG, type, callback);
michael@0 197 }
michael@0 198
michael@0 199 function addTab(onLoad) {
michael@0 200 let tab = gBrowser.addTab();
michael@0 201 gBrowser.selectedTab = tab;
michael@0 202 tab.linkedBrowser.addEventListener("load", function load() {
michael@0 203 tab.removeEventListener("load", load, true);
michael@0 204 let url = getRootDirectory(gTestPath) + TEST_CONTENT_SCRIPT_BASENAME;
michael@0 205 gMsgMan = tab.linkedBrowser.messageManager;
michael@0 206 gMsgMan.sendAsyncMessage(CONTENT_SEARCH_MSG, {
michael@0 207 type: "AddToWhitelist",
michael@0 208 data: ["about:blank"],
michael@0 209 });
michael@0 210 waitForMsg(CONTENT_SEARCH_MSG, "AddToWhitelistAck", () => {
michael@0 211 gMsgMan.loadFrameScript(url, false);
michael@0 212 onLoad();
michael@0 213 });
michael@0 214 }, true);
michael@0 215 registerCleanupFunction(() => gBrowser.removeTab(tab));
michael@0 216 }
michael@0 217
michael@0 218 function currentStateObj() {
michael@0 219 return {
michael@0 220 engines: Services.search.getVisibleEngines().map(engine => {
michael@0 221 return {
michael@0 222 name: engine.name,
michael@0 223 iconURI: engine.getIconURLBySize(16, 16),
michael@0 224 };
michael@0 225 }),
michael@0 226 currentEngine: currentEngineObj(),
michael@0 227 };
michael@0 228 }
michael@0 229
michael@0 230 function currentEngineObj(expectedCurrentEngine) {
michael@0 231 if (expectedCurrentEngine) {
michael@0 232 is(Services.search.currentEngine.name, expectedCurrentEngine.name,
michael@0 233 "Sanity check: expected current engine");
michael@0 234 }
michael@0 235 return {
michael@0 236 name: Services.search.currentEngine.name,
michael@0 237 logoURI: Services.search.currentEngine.getIconURLBySize(65, 26),
michael@0 238 logo2xURI: Services.search.currentEngine.getIconURLBySize(130, 52),
michael@0 239 };
michael@0 240 }

mercurial