1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/metro/base/tests/mochitest/browser_urlbar.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,379 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +"use strict"; 1.10 + 1.11 +var gEdit = null; 1.12 + 1.13 +/*============================================================================= 1.14 + Search engine mocking utilities 1.15 +=============================================================================*/ 1.16 + 1.17 +var gEngine = null; 1.18 + 1.19 +const kSearchEngineName = "Foo"; 1.20 +const kSearchEngineURI = chromeRoot + "res/testEngine.xml"; 1.21 + 1.22 +/* 1.23 + * addMockSearchDefault - adds a mock search engine to the top of the engine list. 1.24 + */ 1.25 +function addMockSearchDefault(aTimeoutMs) { 1.26 + let deferred = Promise.defer(); 1.27 + let timeoutMs = aTimeoutMs || kDefaultWait; 1.28 + let timerID = 0; 1.29 + 1.30 + function engineAddObserver(aSubject, aTopic, aData) { 1.31 + if (aData != "engine-added") 1.32 + return; 1.33 + 1.34 + gEngine = Services.search.getEngineByName(kSearchEngineName); 1.35 + Services.obs.removeObserver(engineAddObserver, "browser-search-engine-modified"); 1.36 + clearTimeout(timerID); 1.37 + gEngine.hidden = false; 1.38 + ok(gEngine, "mock engine was added"); 1.39 + deferred.resolve(); 1.40 + } 1.41 + 1.42 + if (gEngine) { 1.43 + deferred.resolve(); 1.44 + return deferred.promise; 1.45 + } 1.46 + 1.47 + timerID = setTimeout(function ids_canceller() { 1.48 + Services.obs.removeObserver(engineAddObserver, "browser-search-engine-modified"); 1.49 + deferred.reject(new Error("search add timeout")); 1.50 + }, timeoutMs); 1.51 + 1.52 + Services.obs.addObserver(engineAddObserver, "browser-search-engine-modified", false); 1.53 + Services.search.addEngine(kSearchEngineURI, Ci.nsISearchEngine.DATA_XML, 1.54 + "data:image/x-icon,%00", false); 1.55 + return deferred.promise; 1.56 +} 1.57 + 1.58 +/* 1.59 + * removeMockSearchDefault - removes mock "Foo" search engine. 1.60 + */ 1.61 + 1.62 +function removeMockSearchDefault(aTimeoutMs) { 1.63 + let deferred = Promise.defer(); 1.64 + let timeoutMs = aTimeoutMs || kDefaultWait; 1.65 + let timerID = 0; 1.66 + 1.67 + function engineRemoveObserver(aSubject, aTopic, aData) { 1.68 + if (aData != "engine-removed") 1.69 + return; 1.70 + 1.71 + clearTimeout(timerID); 1.72 + gEngine = null; 1.73 + Services.obs.removeObserver(engineRemoveObserver, "browser-search-engine-modified"); 1.74 + deferred.resolve(); 1.75 + } 1.76 + 1.77 + if (!gEngine) { 1.78 + deferred.resolve(); 1.79 + return deferred.promise; 1.80 + } 1.81 + 1.82 + timerID = setTimeout(function ids_canceller() { 1.83 + Services.obs.removeObserver(engineRemoveObserver, "browser-search-engine-modified"); 1.84 + deferred.reject(new Error("search remove timeout")); 1.85 + }, timeoutMs); 1.86 + 1.87 + Services.obs.addObserver(engineRemoveObserver, "browser-search-engine-modified", false); 1.88 + Services.search.removeEngine(gEngine); 1.89 + return deferred.promise; 1.90 +} 1.91 + 1.92 +/*============================================================================= 1.93 + Test cases 1.94 +=============================================================================*/ 1.95 + 1.96 +function test() { 1.97 + waitForExplicitFinish(); 1.98 + runTests(); 1.99 +} 1.100 + 1.101 +function setUp() { 1.102 + if (!gEdit) 1.103 + gEdit = document.getElementById("urlbar-edit"); 1.104 + 1.105 + yield addTab("about:blank"); 1.106 + yield showNavBar(); 1.107 +} 1.108 + 1.109 +function tearDown() { 1.110 + Browser.closeTab(Browser.selectedTab, { forceClose: true }); 1.111 +} 1.112 + 1.113 +gTests.push({ 1.114 + desc: "search engines update", 1.115 + setUp: setUp, 1.116 + tearDown: tearDown, 1.117 + run: function testSearchEngine() { 1.118 + // If the XBL hasn't initialized yet, open the popup so that it will. 1.119 + if (gEdit.popup._searches == undefined) { 1.120 + gEdit.openPopup(); 1.121 + gEdit.closePopup(); 1.122 + } 1.123 + yield waitForCondition(() => gEdit.popup._searches.itemCount); 1.124 + 1.125 + let numSearches = gEdit.popup._searches.itemCount; 1.126 + function getEngineItem() { 1.127 + return gEdit.popup._searches.querySelector("richgriditem[value="+kSearchEngineName+"]"); 1.128 + } 1.129 + 1.130 + yield addMockSearchDefault(); 1.131 + is(gEdit.popup._searches.itemCount, numSearches + 1, "added search engine count"); 1.132 + ok(getEngineItem(), "added search engine item"); 1.133 + 1.134 + yield removeMockSearchDefault(); 1.135 + is(gEdit.popup._searches.itemCount, numSearches, "normal search engine count"); 1.136 + ok(!getEngineItem(), "added search engine item"); 1.137 + } 1.138 +}); 1.139 + 1.140 +gTests.push({ 1.141 + desc: "display autocomplete while typing, handle enter", 1.142 + setUp: setUp, 1.143 + tearDown: tearDown, 1.144 + run: function testUrlbarTyping() { 1.145 + sendElementTap(window, gEdit); 1.146 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.147 + ok(!gEdit.popup.popupOpen, "focus urlbar: popup not open yet"); 1.148 + 1.149 + EventUtils.sendString("about:blank", window); 1.150 + let opened = yield waitForCondition(() => gEdit.popup.popupOpen); 1.151 + ok(opened, "type in urlbar: popup opens"); 1.152 + 1.153 + EventUtils.synthesizeKey("VK_RETURN", {}, window); 1.154 + let closed = yield waitForCondition(() => !gEdit.popup.popupOpen); 1.155 + ok(closed, "hit enter in urlbar: popup closes, page loads"); 1.156 + ok(!gEdit.isEditing, "hit enter in urlbar: not in editing mode"); 1.157 + } 1.158 +}); 1.159 + 1.160 +gTests.push({ 1.161 + desc: "Control-Enter in urlbar", 1.162 + setUp: setUp, 1.163 + tearDown: tearDown, 1.164 + run: function () { 1.165 + sendElementTap(window, gEdit); 1.166 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.167 + 1.168 + EventUtils.sendString("example", window); 1.169 + EventUtils.synthesizeKey("VK_RETURN", { accelKey: true }, window); 1.170 + is(gEdit.value, "www.example.com", "Control-enter adds www. and .com"); 1.171 + ok(!gEdit.isEditing, "hit enter in urlbar: not in editing mode"); 1.172 + } 1.173 +}); 1.174 + 1.175 +gTests.push({ 1.176 + desc: "Shift-Enter in urlbar", 1.177 + setUp: setUp, 1.178 + tearDown: tearDown, 1.179 + run: function () { 1.180 + sendElementTap(window, gEdit); 1.181 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.182 + 1.183 + EventUtils.sendString("example", window); 1.184 + EventUtils.synthesizeKey("VK_RETURN", { shiftKey: true }, window); 1.185 + is(gEdit.value, "www.example.net", "Shift-enter adds www. and .net"); 1.186 + ok(!gEdit.isEditing, "hit enter in urlbar: not in editing mode"); 1.187 + } 1.188 +}); 1.189 + 1.190 +gTests.push({ 1.191 + desc: "Control-Shift-Enter in urlbar", 1.192 + setUp: setUp, 1.193 + tearDown: tearDown, 1.194 + run: function () { 1.195 + sendElementTap(window, gEdit); 1.196 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.197 + 1.198 + EventUtils.sendString("example", window); 1.199 + EventUtils.synthesizeKey("VK_RETURN", { accelKey: true, shiftKey: true }, window); 1.200 + is(gEdit.value, "www.example.org", "Shift-enter adds www. and .org"); 1.201 + ok(!gEdit.isEditing, "hit enter in urlbar: not in editing mode"); 1.202 + } 1.203 +}); 1.204 + 1.205 +gTests.push({ 1.206 + desc: "display and select a search with keyboard", 1.207 + setUp: setUp, 1.208 + tearDown: tearDown, 1.209 + run: function testSearchKeyboard() { 1.210 + yield addMockSearchDefault(); 1.211 + 1.212 + yield waitForCondition(() => !Browser.selectedTab.isLoading()); 1.213 + 1.214 + sendElementTap(window, gEdit); 1.215 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.216 + ok(!gEdit.popup.popupOpen, "focus urlbar: popup not open yet"); 1.217 + 1.218 + let search = "mozilla"; 1.219 + EventUtils.sendString(search, window); 1.220 + yield waitForCondition(() => gEdit.popup.popupOpen); 1.221 + 1.222 + // XXX We should probably change the keyboard selection behavior entirely, 1.223 + // given that it makes little to no sense, but that's a job for a later patch. 1.224 + 1.225 + EventUtils.synthesizeKey("VK_DOWN", {}, window); 1.226 + is(gEdit.popup.selectedIndex, -1, "key select search: no result selected"); 1.227 + is(gEdit.popup._searches.selectedIndex, 0, "key select search: first search selected"); 1.228 + 1.229 + let engines = Services.search.getVisibleEngines(); 1.230 + for (let i = 0, max = engines.length - 1; i < max; i++) { 1.231 + is(gEdit.popup._searches.selectedIndex, i, "key select search: next index"); 1.232 + EventUtils.synthesizeKey("VK_DOWN", {}, window); 1.233 + } 1.234 + 1.235 + let existingValue = gEdit.value; 1.236 + EventUtils.synthesizeKey("VK_RETURN", {}, window); 1.237 + 1.238 + yield waitForCondition(() => gEdit.value != existingValue); 1.239 + 1.240 + let closed = yield waitForCondition(() => !gEdit.popup.popupOpen); 1.241 + ok(closed, "hit enter in urlbar: popup closes, page loads"); 1.242 + ok(!gEdit.isEditing, "hit enter in urlbar: not in editing mode"); 1.243 + 1.244 + let searchSubmission = gEngine.getSubmission(search, null); 1.245 + let trimmedSubmission = gEdit.trimValue(searchSubmission.uri.spec); 1.246 + is(gEdit.value, trimmedSubmission, "hit enter in urlbar: search conducted"); 1.247 + 1.248 + yield removeMockSearchDefault(); 1.249 + } 1.250 +}); 1.251 + 1.252 +gTests.push({ 1.253 + desc: "display and select a search with touch", 1.254 + setUp: setUp, 1.255 + tearDown: tearDown, 1.256 + run: function testUrlbarSearchesTouch() { 1.257 + yield addMockSearchDefault(); 1.258 + 1.259 + yield waitForCondition(() => !Browser.selectedTab.isLoading()); 1.260 + 1.261 + sendElementTap(window, gEdit); 1.262 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.263 + ok(!gEdit.popup.popupOpen, "focus urlbar: popup not open yet"); 1.264 + 1.265 + let search = "mozilla"; 1.266 + EventUtils.sendString(search, window); 1.267 + yield waitForCondition(() => gEdit.popup.popupOpen); 1.268 + 1.269 + sendElementTap(window, gEdit.popup._searches.lastChild); 1.270 + 1.271 + let closed = yield waitForCondition(() => !gEdit.popup.popupOpen); 1.272 + ok(closed, "tap search option: popup closes, page loads"); 1.273 + ok(!gEdit.isEditing, "tap search option: not in editing mode"); 1.274 + 1.275 + let searchSubmission = gEngine.getSubmission(search, null); 1.276 + let trimmedSubmission = gEdit.trimValue(searchSubmission.uri.spec); 1.277 + is(gEdit.value, trimmedSubmission, "tap search option: search conducted"); 1.278 + 1.279 + yield removeMockSearchDefault(); 1.280 + } 1.281 +}); 1.282 + 1.283 +gTests.push({ 1.284 + desc: "bug 897131 - url bar update after content tap + edge swipe", 1.285 + tearDown: tearDown, 1.286 + run: function testUrlbarTyping() { 1.287 + let tab = yield addTab("about:mozilla"); 1.288 + yield showNavBar(); 1.289 + 1.290 + sendElementTap(window, gEdit); 1.291 + ok(gEdit.isEditing, "focus urlbar: in editing mode"); 1.292 + ok(!gEdit.popup.popupOpen, "focus urlbar: popup not open yet"); 1.293 + 1.294 + EventUtils.sendString("about:blank", window); 1.295 + let opened = yield waitForCondition(() => gEdit.popup.popupOpen); 1.296 + ok(opened, "type in urlbar: popup opens"); 1.297 + 1.298 + sendElementTap(window, tab.browser); 1.299 + 1.300 + let closed = yield waitForCondition(() => !gEdit.popup.popupOpen); 1.301 + ok(closed, "autocomplete closed after tap on content"); 1.302 + ok(!ContextUI.navbarVisible, "navbar closed"); 1.303 + 1.304 + let event = document.createEvent("Events"); 1.305 + event.initEvent("MozEdgeUICompleted", true, false); 1.306 + window.dispatchEvent(event); 1.307 + 1.308 + ok(ContextUI.navbarVisible, "navbar visible"); 1.309 + is(gEdit.value, "about:mozilla", "url bar text refreshed"); 1.310 + } 1.311 +}); 1.312 + 1.313 +gTests.push({ 1.314 + desc: "Bug 916383 - Invisible autocomplete items selectable by keyboard when 'your results' not shown", 1.315 + tearDown: tearDown, 1.316 + run: function testBug916383() { 1.317 + yield addTab("about:start"); 1.318 + yield showNavBar(); 1.319 + 1.320 + sendElementTap(window, gEdit); 1.321 + 1.322 + let bookmarkItem = Browser.selectedBrowser.contentWindow.BookmarksStartView._grid.querySelector("richgriditem"); 1.323 + // Get the first bookmark item label to make sure it will show up in 'your results' 1.324 + let label = bookmarkItem.getAttribute("label"); 1.325 + 1.326 + EventUtils.sendString(label, window); 1.327 + 1.328 + let opened = yield waitForCondition(() => gEdit.popup.popupOpen); 1.329 + yield waitForCondition(() => gEdit.popup._results.itemCount > 0); 1.330 + 1.331 + ok(!gEdit.popup.hasAttribute("nomatch"), "'Popup doesnt have nomatch attribute when there are results"); 1.332 + ok(gEdit.popup._resultsContainer.getBoundingClientRect().width, "'Your results' are visible"); 1.333 + ok(gEdit.popup._results.itemCount > 0, "'Your results' are populated"); 1.334 + 1.335 + // Append a string to make sure it doesn't match anything in 'your results' 1.336 + EventUtils.sendString("zzzzzzzzzzzzzzzzzz", window); 1.337 + 1.338 + yield waitForCondition(() => gEdit.popup.hasAttribute("nomatch")); 1.339 + 1.340 + is(gEdit.popup._resultsContainer.getBoundingClientRect().width, 0, "'Your results' are hidden"); 1.341 + ok(gEdit.popup._results.itemCount === 0, "'Your results' are empty"); 1.342 + 1.343 + EventUtils.synthesizeKey("VK_DOWN", {}, window); 1.344 + is(gEdit.popup._searches.selectedIndex, 0, "key select search: first search selected"); 1.345 + 1.346 + EventUtils.synthesizeKey("VK_TAB", {}, window); 1.347 + is(gEdit.popup._searches.selectedIndex, 1, "tab key: second search selected"); 1.348 + 1.349 + EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }, window); 1.350 + is(gEdit.popup._searches.selectedIndex, 0, "shift-tab: first search selected"); 1.351 + } 1.352 +}); 1.353 + 1.354 +gTests.push({ 1.355 + desc: "Bug 891667 - Use up arrow too", 1.356 + tearDown: tearDown, 1.357 + run: function testBug891667() { 1.358 + yield addTab("about:start"); 1.359 + yield showNavBar(); 1.360 + 1.361 + sendElementTap(window, gEdit); 1.362 + 1.363 + let bookmarkItem = Browser.selectedBrowser.contentWindow.BookmarksStartView._grid.querySelector("richgriditem"); 1.364 + // Get the first bookmark item label to make sure it will show up in 'your results' 1.365 + let label = bookmarkItem.getAttribute("label"); 1.366 + 1.367 + EventUtils.sendString(label, window); 1.368 + 1.369 + yield waitForCondition(() => gEdit.popup.popupOpen); 1.370 + yield waitForCondition(() => gEdit.popup._results.itemCount > 0); 1.371 + 1.372 + ok(gEdit.popup._results.itemCount > 0, "'Your results' populated"); 1.373 + 1.374 + EventUtils.synthesizeKey("VK_UP", {}, window); 1.375 + is(gEdit.popup._results.selectedIndex, 0, "Pressing arrow up selects first item."); 1.376 + 1.377 + EventUtils.synthesizeKey("VK_BACK_SPACE", {}, window); 1.378 + yield waitForEvent(document.getElementById("urlbar-edit"), "input"); 1.379 + is(gEdit.popup._results.selectedIndex, -1, "backspace: autocomplete de-selected"); 1.380 + } 1.381 +}); 1.382 +