1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_urlbarAutoFillTrimURLs.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,87 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +// This test ensures that autoFilled values are not trimmed, unless the user 1.9 +// selects from the autocomplete popup. 1.10 + 1.11 +function test() { 1.12 + waitForExplicitFinish(); 1.13 + 1.14 + const PREF_TRIMURL = "browser.urlbar.trimURLs"; 1.15 + const PREF_AUTOFILL = "browser.urlbar.autoFill"; 1.16 + 1.17 + registerCleanupFunction(function () { 1.18 + Services.prefs.clearUserPref(PREF_TRIMURL); 1.19 + Services.prefs.clearUserPref(PREF_AUTOFILL); 1.20 + gURLBar.handleRevert(); 1.21 + }); 1.22 + Services.prefs.setBoolPref(PREF_TRIMURL, true); 1.23 + Services.prefs.setBoolPref(PREF_AUTOFILL, true); 1.24 + 1.25 + // Adding a tab would hit switch-to-tab, so it's safer to just add a visit. 1.26 + let callback = { 1.27 + handleError: function () {}, 1.28 + handleResult: function () {}, 1.29 + handleCompletion: continue_test 1.30 + }; 1.31 + let history = Cc["@mozilla.org/browser/history;1"] 1.32 + .getService(Ci.mozIAsyncHistory); 1.33 + history.updatePlaces({ uri: NetUtil.newURI("http://www.autofilltrimurl.com/") 1.34 + , visits: [ { transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED 1.35 + , visitDate: Date.now() * 1000 1.36 + } ] 1.37 + }, callback); 1.38 +} 1.39 + 1.40 +function continue_test() { 1.41 + function test_autoFill(aTyped, aExpected, aCallback) { 1.42 + gURLBar.inputField.value = aTyped.substr(0, aTyped.length - 1); 1.43 + gURLBar.focus(); 1.44 + gURLBar.selectionStart = aTyped.length - 1; 1.45 + gURLBar.selectionEnd = aTyped.length - 1; 1.46 + 1.47 + EventUtils.synthesizeKey(aTyped.substr(-1), {}); 1.48 + waitForSearchComplete(function () { 1.49 + is(gURLBar.value, aExpected, "trim was applied correctly"); 1.50 + aCallback(); 1.51 + }); 1.52 + } 1.53 + 1.54 + test_autoFill("http://", "http://", function () { 1.55 + test_autoFill("http://a", "http://autofilltrimurl.com/", function () { 1.56 + test_autoFill("http://www.autofilltrimurl.com", "http://www.autofilltrimurl.com/", function () { 1.57 + // Now ensure selecting from the popup correctly trims. 1.58 + is(gURLBar.controller.matchCount, 1, "Found the expected number of matches"); 1.59 + EventUtils.synthesizeKey("VK_DOWN", {}); 1.60 + is(gURLBar.value, "www.autofilltrimurl.com", "trim was applied correctly"); 1.61 + gURLBar.closePopup(); 1.62 + waitForClearHistory(finish); 1.63 + }); 1.64 + }); 1.65 + }); 1.66 +} 1.67 + 1.68 +function waitForClearHistory(aCallback) { 1.69 + Services.obs.addObserver(function observeCH(aSubject, aTopic, aData) { 1.70 + Services.obs.removeObserver(observeCH, PlacesUtils.TOPIC_EXPIRATION_FINISHED); 1.71 + aCallback(); 1.72 + }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); 1.73 + PlacesUtils.bhistory.removeAllPages(); 1.74 +} 1.75 + 1.76 +let gOnSearchComplete = null; 1.77 +function waitForSearchComplete(aCallback) { 1.78 + info("Waiting for onSearchComplete"); 1.79 + if (!gOnSearchComplete) { 1.80 + gOnSearchComplete = gURLBar.onSearchComplete; 1.81 + registerCleanupFunction(() => { 1.82 + gURLBar.onSearchComplete = gOnSearchComplete; 1.83 + }); 1.84 + } 1.85 + gURLBar.onSearchComplete = function () { 1.86 + ok(gURLBar.popupOpen, "The autocomplete popup is correctly open"); 1.87 + gOnSearchComplete.apply(gURLBar); 1.88 + aCallback(); 1.89 + } 1.90 +}