michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // This test ensures that autoFilled values are not trimmed, unless the user michael@0: // selects from the autocomplete popup. michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: const PREF_TRIMURL = "browser.urlbar.trimURLs"; michael@0: const PREF_AUTOFILL = "browser.urlbar.autoFill"; michael@0: michael@0: registerCleanupFunction(function () { michael@0: Services.prefs.clearUserPref(PREF_TRIMURL); michael@0: Services.prefs.clearUserPref(PREF_AUTOFILL); michael@0: gURLBar.handleRevert(); michael@0: }); michael@0: Services.prefs.setBoolPref(PREF_TRIMURL, true); michael@0: Services.prefs.setBoolPref(PREF_AUTOFILL, true); michael@0: michael@0: // Adding a tab would hit switch-to-tab, so it's safer to just add a visit. michael@0: let callback = { michael@0: handleError: function () {}, michael@0: handleResult: function () {}, michael@0: handleCompletion: continue_test michael@0: }; michael@0: let history = Cc["@mozilla.org/browser/history;1"] michael@0: .getService(Ci.mozIAsyncHistory); michael@0: history.updatePlaces({ uri: NetUtil.newURI("http://www.autofilltrimurl.com/") michael@0: , visits: [ { transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED michael@0: , visitDate: Date.now() * 1000 michael@0: } ] michael@0: }, callback); michael@0: } michael@0: michael@0: function continue_test() { michael@0: function test_autoFill(aTyped, aExpected, aCallback) { michael@0: gURLBar.inputField.value = aTyped.substr(0, aTyped.length - 1); michael@0: gURLBar.focus(); michael@0: gURLBar.selectionStart = aTyped.length - 1; michael@0: gURLBar.selectionEnd = aTyped.length - 1; michael@0: michael@0: EventUtils.synthesizeKey(aTyped.substr(-1), {}); michael@0: waitForSearchComplete(function () { michael@0: is(gURLBar.value, aExpected, "trim was applied correctly"); michael@0: aCallback(); michael@0: }); michael@0: } michael@0: michael@0: test_autoFill("http://", "http://", function () { michael@0: test_autoFill("http://a", "http://autofilltrimurl.com/", function () { michael@0: test_autoFill("http://www.autofilltrimurl.com", "http://www.autofilltrimurl.com/", function () { michael@0: // Now ensure selecting from the popup correctly trims. michael@0: is(gURLBar.controller.matchCount, 1, "Found the expected number of matches"); michael@0: EventUtils.synthesizeKey("VK_DOWN", {}); michael@0: is(gURLBar.value, "www.autofilltrimurl.com", "trim was applied correctly"); michael@0: gURLBar.closePopup(); michael@0: waitForClearHistory(finish); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function waitForClearHistory(aCallback) { michael@0: Services.obs.addObserver(function observeCH(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(observeCH, PlacesUtils.TOPIC_EXPIRATION_FINISHED); michael@0: aCallback(); michael@0: }, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false); michael@0: PlacesUtils.bhistory.removeAllPages(); michael@0: } michael@0: michael@0: let gOnSearchComplete = null; michael@0: function waitForSearchComplete(aCallback) { michael@0: info("Waiting for onSearchComplete"); michael@0: if (!gOnSearchComplete) { michael@0: gOnSearchComplete = gURLBar.onSearchComplete; michael@0: registerCleanupFunction(() => { michael@0: gURLBar.onSearchComplete = gOnSearchComplete; michael@0: }); michael@0: } michael@0: gURLBar.onSearchComplete = function () { michael@0: ok(gURLBar.popupOpen, "The autocomplete popup is correctly open"); michael@0: gOnSearchComplete.apply(gURLBar); michael@0: aCallback(); michael@0: } michael@0: }