browser/components/search/test/browser_426329.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 // Instead of loading ChromeUtils.js into the test scope in browser-test.js for all tests,
michael@0 2 // we only need ChromeUtils.js for a few files which is why we are using loadSubScript.
michael@0 3 var ChromeUtils = {};
michael@0 4 this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
michael@0 5 getService(Ci.mozIJSSubScriptLoader);
michael@0 6 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js", ChromeUtils);
michael@0 7
michael@0 8 XPCOMUtils.defineLazyModuleGetter(this, "FormHistory",
michael@0 9 "resource://gre/modules/FormHistory.jsm");
michael@0 10 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 11 "resource://gre/modules/Promise.jsm");
michael@0 12
michael@0 13 function expectedURL(aSearchTerms) {
michael@0 14 const ENGINE_HTML_BASE = "http://mochi.test:8888/browser/browser/components/search/test/test.html";
michael@0 15 var textToSubURI = Cc["@mozilla.org/intl/texttosuburi;1"].
michael@0 16 getService(Ci.nsITextToSubURI);
michael@0 17 var searchArg = textToSubURI.ConvertAndEscape("utf-8", aSearchTerms);
michael@0 18 return ENGINE_HTML_BASE + "?test=" + searchArg;
michael@0 19 }
michael@0 20
michael@0 21 function simulateClick(aEvent, aTarget) {
michael@0 22 var event = document.createEvent("MouseEvent");
michael@0 23 var ctrlKeyArg = aEvent.ctrlKey || false;
michael@0 24 var altKeyArg = aEvent.altKey || false;
michael@0 25 var shiftKeyArg = aEvent.shiftKey || false;
michael@0 26 var metaKeyArg = aEvent.metaKey || false;
michael@0 27 var buttonArg = aEvent.button || 0;
michael@0 28 event.initMouseEvent("click", true, true, window,
michael@0 29 0, 0, 0, 0, 0,
michael@0 30 ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
michael@0 31 buttonArg, null);
michael@0 32 aTarget.dispatchEvent(event);
michael@0 33 }
michael@0 34
michael@0 35 // modified from toolkit/components/satchel/test/test_form_autocomplete.html
michael@0 36 function checkMenuEntries(expectedValues) {
michael@0 37 var actualValues = getMenuEntries();
michael@0 38 is(actualValues.length, expectedValues.length, "Checking length of expected menu");
michael@0 39 for (var i = 0; i < expectedValues.length; i++)
michael@0 40 is(actualValues[i], expectedValues[i], "Checking menu entry #" + i);
michael@0 41 }
michael@0 42
michael@0 43 function getMenuEntries() {
michael@0 44 var entries = [];
michael@0 45 var autocompleteMenu = searchBar.textbox.popup;
michael@0 46 // Could perhaps pull values directly from the controller, but it seems
michael@0 47 // more reliable to test the values that are actually in the tree?
michael@0 48 var column = autocompleteMenu.tree.columns[0];
michael@0 49 var numRows = autocompleteMenu.tree.view.rowCount;
michael@0 50 for (var i = 0; i < numRows; i++) {
michael@0 51 entries.push(autocompleteMenu.tree.view.getValueAt(i, column));
michael@0 52 }
michael@0 53 return entries;
michael@0 54 }
michael@0 55
michael@0 56 function* countEntries(name, value) {
michael@0 57 let deferred = Promise.defer();
michael@0 58 let count = 0;
michael@0 59 let obj = name && value ? {fieldname: name, value: value} : {};
michael@0 60 FormHistory.count(obj,
michael@0 61 { handleResult: function(result) { count = result; },
michael@0 62 handleError: function(error) { throw error; },
michael@0 63 handleCompletion: function(reason) {
michael@0 64 if (!reason) {
michael@0 65 deferred.resolve(count);
michael@0 66 }
michael@0 67 }
michael@0 68 });
michael@0 69 return deferred.promise;
michael@0 70 }
michael@0 71
michael@0 72 var searchBar;
michael@0 73 var searchButton;
michael@0 74 var searchEntries = ["test", "More Text", "Some Text"];
michael@0 75 function* promiseSetEngine() {
michael@0 76 let deferred = Promise.defer();
michael@0 77 var ss = Services.search;
michael@0 78
michael@0 79 function observer(aSub, aTopic, aData) {
michael@0 80 switch (aData) {
michael@0 81 case "engine-added":
michael@0 82 var engine = ss.getEngineByName("Bug 426329");
michael@0 83 ok(engine, "Engine was added.");
michael@0 84 ss.currentEngine = engine;
michael@0 85 break;
michael@0 86 case "engine-current":
michael@0 87 ok(ss.currentEngine.name == "Bug 426329", "currentEngine set");
michael@0 88 searchBar = BrowserSearch.searchBar;
michael@0 89 searchButton = document.getAnonymousElementByAttribute(searchBar,
michael@0 90 "anonid", "search-go-button");
michael@0 91 ok(searchButton, "got search-go-button");
michael@0 92 searchBar.value = "test";
michael@0 93
michael@0 94 Services.obs.removeObserver(observer, "browser-search-engine-modified");
michael@0 95 deferred.resolve();
michael@0 96 break;
michael@0 97 }
michael@0 98 };
michael@0 99
michael@0 100 Services.obs.addObserver(observer, "browser-search-engine-modified", false);
michael@0 101 ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/426329.xml",
michael@0 102 Ci.nsISearchEngine.DATA_XML, "data:image/x-icon,%00",
michael@0 103 false);
michael@0 104
michael@0 105 return deferred.promise;
michael@0 106 }
michael@0 107
michael@0 108 function* promiseRemoveEngine() {
michael@0 109 let deferred = Promise.defer();
michael@0 110 var ss = Services.search;
michael@0 111
michael@0 112 function observer(aSub, aTopic, aData) {
michael@0 113 if (aData == "engine-removed") {
michael@0 114 Services.obs.removeObserver(observer, "browser-search-engine-modified");
michael@0 115 deferred.resolve();
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 Services.obs.addObserver(observer, "browser-search-engine-modified", false);
michael@0 120 var engine = ss.getEngineByName("Bug 426329");
michael@0 121 ss.removeEngine(engine);
michael@0 122
michael@0 123 return deferred.promise;
michael@0 124 }
michael@0 125
michael@0 126
michael@0 127 var preSelectedBrowser;
michael@0 128 var preTabNo;
michael@0 129 function* prepareTest() {
michael@0 130 preSelectedBrowser = gBrowser.selectedBrowser;
michael@0 131 preTabNo = gBrowser.tabs.length;
michael@0 132 searchBar = BrowserSearch.searchBar;
michael@0 133
michael@0 134 let windowFocused = Promise.defer();
michael@0 135 SimpleTest.waitForFocus(windowFocused.resolve, window);
michael@0 136 yield windowFocused.promise;
michael@0 137
michael@0 138 let deferred = Promise.defer();
michael@0 139 if (document.activeElement != searchBar) {
michael@0 140 searchBar.addEventListener("focus", function onFocus() {
michael@0 141 searchBar.removeEventListener("focus", onFocus);
michael@0 142 deferred.resolve();
michael@0 143 });
michael@0 144 searchBar.focus();
michael@0 145 } else {
michael@0 146 deferred.resolve();
michael@0 147 }
michael@0 148 return deferred.promise;
michael@0 149 }
michael@0 150
michael@0 151 add_task(function testSetupEngine() {
michael@0 152 yield promiseSetEngine();
michael@0 153 });
michael@0 154
michael@0 155 add_task(function testReturn() {
michael@0 156 yield prepareTest();
michael@0 157 EventUtils.synthesizeKey("VK_RETURN", {});
michael@0 158 let event = yield promiseOnLoad();
michael@0 159
michael@0 160 is(gBrowser.tabs.length, preTabNo, "Return key did not open new tab");
michael@0 161 is(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 162 "Return key loaded results in current tab");
michael@0 163 is(event.originalTarget.URL, expectedURL(searchBar.value), "testReturn opened correct search page");
michael@0 164 });
michael@0 165
michael@0 166 add_task(function testAltReturn() {
michael@0 167 yield prepareTest();
michael@0 168 EventUtils.synthesizeKey("VK_RETURN", { altKey: true });
michael@0 169 let event = yield promiseOnLoad();
michael@0 170
michael@0 171 is(gBrowser.tabs.length, preTabNo + 1, "Alt+Return key added new tab");
michael@0 172 isnot(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 173 "Alt+Return key loaded results in new tab");
michael@0 174 is(event.originalTarget, gBrowser.contentDocument,
michael@0 175 "Alt+Return key loaded results in foreground tab");
michael@0 176 is(event.originalTarget.URL, expectedURL(searchBar.value), "testAltReturn opened correct search page");
michael@0 177 });
michael@0 178
michael@0 179 //Shift key has no effect for now, so skip it
michael@0 180 add_task(function testShiftAltReturn() {
michael@0 181 return;
michael@0 182
michael@0 183 yield prepareTest();
michael@0 184 EventUtils.synthesizeKey("VK_RETURN", { shiftKey: true, altKey: true });
michael@0 185 let event = yield promiseOnLoad();
michael@0 186
michael@0 187 is(gBrowser.tabs.length, preTabNo + 1, "Shift+Alt+Return key added new tab");
michael@0 188 isnot(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 189 "Shift+Alt+Return key loaded results in new tab");
michael@0 190 isnot(event.originalTarget, gBrowser.contentDocument,
michael@0 191 "Shift+Alt+Return key loaded results in background tab");
michael@0 192 is(event.originalTarget.URL, expectedURL(searchBar.value), "testShiftAltReturn opened correct search page");
michael@0 193 });
michael@0 194
michael@0 195 add_task(function testLeftClick() {
michael@0 196 yield prepareTest();
michael@0 197 simulateClick({ button: 0 }, searchButton);
michael@0 198 let event = yield promiseOnLoad();
michael@0 199 is(gBrowser.tabs.length, preTabNo, "LeftClick did not open new tab");
michael@0 200 is(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 201 "LeftClick loaded results in current tab");
michael@0 202 is(event.originalTarget.URL, expectedURL(searchBar.value), "testLeftClick opened correct search page");
michael@0 203 });
michael@0 204
michael@0 205 add_task(function testMiddleClick() {
michael@0 206 yield prepareTest();
michael@0 207 simulateClick({ button: 1 }, searchButton);
michael@0 208 let event = yield promiseOnLoad();
michael@0 209 is(gBrowser.tabs.length, preTabNo + 1, "MiddleClick added new tab");
michael@0 210 isnot(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 211 "MiddleClick loaded results in new tab");
michael@0 212 is(event.originalTarget, gBrowser.contentDocument,
michael@0 213 "MiddleClick loaded results in foreground tab");
michael@0 214 is(event.originalTarget.URL, expectedURL(searchBar.value), "testMiddleClick opened correct search page");
michael@0 215 });
michael@0 216
michael@0 217 add_task(function testShiftMiddleClick() {
michael@0 218 yield prepareTest();
michael@0 219 simulateClick({ button: 1, shiftKey: true }, searchButton);
michael@0 220 let event = yield promiseOnLoad();
michael@0 221 is(gBrowser.tabs.length, preTabNo + 1, "Shift+MiddleClick added new tab");
michael@0 222 isnot(event.originalTarget, preSelectedBrowser.contentDocument,
michael@0 223 "Shift+MiddleClick loaded results in new tab");
michael@0 224 isnot(event.originalTarget, gBrowser.contentDocument,
michael@0 225 "Shift+MiddleClick loaded results in background tab");
michael@0 226 is(event.originalTarget.URL, expectedURL(searchBar.value), "testShiftMiddleClick opened correct search page");
michael@0 227 });
michael@0 228
michael@0 229 add_task(function testDropText() {
michael@0 230 yield prepareTest();
michael@0 231 let promisePreventPopup = promiseEvent(searchBar, "popupshowing", true);
michael@0 232 // drop on the search button so that we don't need to worry about the
michael@0 233 // default handlers for textboxes.
michael@0 234 ChromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/plain", data: "Some Text" } ]], "copy", window);
michael@0 235 yield promisePreventPopup;
michael@0 236 let event = yield promiseOnLoad();
michael@0 237 is(event.originalTarget.URL, expectedURL(searchBar.value), "testDropText opened correct search page");
michael@0 238 is(searchBar.value, "Some Text", "drop text/plain on searchbar");
michael@0 239 });
michael@0 240
michael@0 241 add_task(function testDropInternalText() {
michael@0 242 yield prepareTest();
michael@0 243 let promisePreventPopup = promiseEvent(searchBar, "popupshowing", true);
michael@0 244 ChromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/x-moz-text-internal", data: "More Text" } ]], "copy", window);
michael@0 245 yield promisePreventPopup;
michael@0 246 let event = yield promiseOnLoad();
michael@0 247 is(event.originalTarget.URL, expectedURL(searchBar.value), "testDropInternalText opened correct search page");
michael@0 248 is(searchBar.value, "More Text", "drop text/x-moz-text-internal on searchbar");
michael@0 249
michael@0 250 // testDropLink implicitly depended on testDropInternalText, so these two tests
michael@0 251 // were merged so that if testDropInternalText failed it wouldn't cause testDropLink
michael@0 252 // to fail unexplainably.
michael@0 253 yield prepareTest();
michael@0 254 let promisePreventPopup = promiseEvent(searchBar, "popupshowing", true);
michael@0 255 ChromeUtils.synthesizeDrop(searchBar.searchButton, searchBar.searchButton, [[ {type: "text/uri-list", data: "http://www.mozilla.org" } ]], "copy", window);
michael@0 256 yield promisePreventPopup;
michael@0 257 is(searchBar.value, "More Text", "drop text/uri-list on searchbar shouldn't change anything");
michael@0 258 });
michael@0 259
michael@0 260 add_task(function testRightClick() {
michael@0 261 preTabNo = gBrowser.tabs.length;
michael@0 262 content.location.href = "about:blank";
michael@0 263 simulateClick({ button: 2 }, searchButton);
michael@0 264 let deferred = Promise.defer();
michael@0 265 setTimeout(function() {
michael@0 266 is(gBrowser.tabs.length, preTabNo, "RightClick did not open new tab");
michael@0 267 is(gBrowser.currentURI.spec, "about:blank", "RightClick did nothing");
michael@0 268 deferred.resolve();
michael@0 269 }, 5000);
michael@0 270 yield deferred.promise;
michael@0 271 });
michael@0 272
michael@0 273 add_task(function testSearchHistory() {
michael@0 274 var textbox = searchBar._textbox;
michael@0 275 for (var i = 0; i < searchEntries.length; i++) {
michael@0 276 let count = yield countEntries(textbox.getAttribute("autocompletesearchparam"), searchEntries[i]);
michael@0 277 ok(count > 0, "form history entry '" + searchEntries[i] + "' should exist");
michael@0 278 }
michael@0 279 });
michael@0 280
michael@0 281 add_task(function testAutocomplete() {
michael@0 282 var popup = searchBar.textbox.popup;
michael@0 283 let popupShownPromise = promiseEvent(popup, "popupshown");
michael@0 284 searchBar.textbox.showHistoryPopup();
michael@0 285 yield popupShownPromise;
michael@0 286 checkMenuEntries(searchEntries);
michael@0 287 });
michael@0 288
michael@0 289 add_task(function testClearHistory() {
michael@0 290 let controller = searchBar.textbox.controllers.getControllerForCommand("cmd_clearhistory")
michael@0 291 ok(controller.isCommandEnabled("cmd_clearhistory"), "Clear history command enabled");
michael@0 292 controller.doCommand("cmd_clearhistory");
michael@0 293 let count = yield countEntries();
michael@0 294 ok(count == 0, "History cleared");
michael@0 295 });
michael@0 296
michael@0 297 add_task(function asyncCleanup() {
michael@0 298 searchBar.value = "";
michael@0 299 while (gBrowser.tabs.length != 1) {
michael@0 300 gBrowser.removeTab(gBrowser.tabs[0], {animate: false});
michael@0 301 }
michael@0 302 content.location.href = "about:blank";
michael@0 303 yield promiseRemoveEngine();
michael@0 304 });
michael@0 305

mercurial