diff --git a/browser/components/urlbar/UrlbarProviderPreloadedSites.jsm b/browser/components/urlbar/UrlbarProviderPreloadedSites.jsm index 231fdef9745b8da3a26d23a19d26f33200bff895..b3229c9a807d2c22e2d5a70d46fb1615f389f4b7 100644 --- a/browser/components/urlbar/UrlbarProviderPreloadedSites.jsm +++ b/browser/components/urlbar/UrlbarProviderPreloadedSites.jsm @@ -157,7 +157,7 @@ class ProviderPreloadedSites extends UrlbarProvider { // will be guaranteed to return a result very quickly using this approach. // Bug 1651101 is filed to improve this behaviour. let result = await this._getAutofillResult(queryContext); - if (!result || instance != this.queryInstance) { + if (instance != this.queryInstance) { return false; } this._autofillData = { result, instance }; @@ -176,20 +176,12 @@ class ProviderPreloadedSites extends UrlbarProvider { // fetched. We don't expect this to be true since we also check the instance // in isActive and clear _autofillData in cancelQuery, but we sanity check it. if ( - !this._autofillData || - this._autofillData.instance != this.queryInstance + this._autofillData.result && + this._autofillData.instance == this.queryInstance ) { - this.logger.error("startQuery invoked with an invalid _autofillData"); - return; - } - - this._autofillData.result.heuristic = true; - addCallback(this, this._autofillData.result); - this._autofillData = null; - - if (!this._searchString) { - // The user hasn't typed anything, or they've only typed a scheme. - return; + this._autofillData.result.heuristic = true; + addCallback(this, this._autofillData.result); + this._autofillData = null; } // Now, add non-autofill preloaded sites. diff --git a/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js b/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js index f1852cf20cbed7df7fb6143b406a33afa7ce18f6..1e85b72ec5e32ea8d290ad49bf1bd168df3229a7 100644 --- a/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js +++ b/browser/components/urlbar/tests/unit/test_UrlbarPrefs.js @@ -57,6 +57,7 @@ add_task(function makeResultBuckets_true() { { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_UNIFIED_COMPLETE }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_PRELOADED }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, ], @@ -115,6 +116,10 @@ add_task(function makeResultBuckets_true() { flex: 2, group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, }, + { + flex: 1, + group: UrlbarUtils.RESULT_GROUP.PRELOADED, + }, ], }, { @@ -148,6 +153,7 @@ add_task(function makeResultBuckets_false() { { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_BOOKMARK_KEYWORD }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_UNIFIED_COMPLETE }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_AUTOFILL }, + { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_PRELOADED }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_TOKEN_ALIAS_ENGINE }, { group: UrlbarUtils.RESULT_GROUP.HEURISTIC_FALLBACK }, ], @@ -184,6 +190,10 @@ add_task(function makeResultBuckets_false() { flex: 2, group: UrlbarUtils.RESULT_GROUP.ABOUT_PAGES, }, + { + flex: 1, + group: UrlbarUtils.RESULT_GROUP.PRELOADED, + }, ], }, { diff --git a/browser/components/urlbar/tests/unit/test_providerPreloaded.js b/browser/components/urlbar/tests/unit/test_providerPreloaded.js new file mode 100644 index 0000000000000000000000000000000000000000..e652d9bb96c66efd5409024b5a4329bbddbfd1b7 --- /dev/null +++ b/browser/components/urlbar/tests/unit/test_providerPreloaded.js @@ -0,0 +1,578 @@ +/** + * Test for bug 1211726 - preload list of top web sites for better + * autocompletion on empty profiles. + */ + +testEngine_setup(); + +const PREF_FEATURE_ENABLED = "browser.urlbar.usepreloadedtopurls.enabled"; +const PREF_FEATURE_EXPIRE_DAYS = + "browser.urlbar.usepreloadedtopurls.expire_days"; + +XPCOMUtils.defineLazyModuleGetters(this, { + UrlbarProviderPreloadedSites: + "resource:///modules/UrlbarProviderPreloadedSites.jsm", +}); + +Cu.importGlobalProperties(["fetch"]); + +let yahoooURI = "https://yahooo.com/"; +let gooogleURI = "https://gooogle.com/"; + +UrlbarProviderPreloadedSites.populatePreloadedSiteStorage([ + [yahoooURI, "Yahooo"], + [gooogleURI, "Gooogle"], +]); + +async function assert_feature_works(condition) { + info("List Results do appear " + condition); + let context = createContext("ooo", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + heuristic: true, + engineName: SUGGESTIONS_ENGINE_NAME, + providerName: "HeuristicFallback", + }), + makeVisitResult(context, { + uri: yahoooURI, + title: "Yahooo", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${yahoooURI}`, + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: gooogleURI, + title: "Gooogle", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${gooogleURI}`, + providerName: "PreloadedSites", + }), + ], + }); + + info("Autofill does appear " + condition); + context = createContext("gooo", { isPrivate: false }); + await check_results({ + context, + autofilled: "gooogle.com/", + completed: gooogleURI, + matches: [ + makeVisitResult(context, { + uri: gooogleURI, + title: gooogleURI.slice(0, -1), // Trim trailing slash. + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: `page-icon:${gooogleURI}`, + providerName: "PreloadedSites", + heuristic: true, + }), + ], + }); +} + +async function assert_feature_does_not_appear(condition) { + info("List Results don't appear " + condition); + let context = createContext("ooo", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + heuristic: true, + engineName: SUGGESTIONS_ENGINE_NAME, + providerName: "HeuristicFallback", + }), + ], + }); + + info("Autofill doesn't appear " + condition); + context = createContext("gooo", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + heuristic: true, + engineName: SUGGESTIONS_ENGINE_NAME, + providerName: "HeuristicFallback", + }), + ], + }); +} + +add_task(async function test_it_works() { + // Not expired but OFF + Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); + Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, false); + await assert_feature_does_not_appear("when OFF by prefs"); + + // Now turn it ON + Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); + await assert_feature_works("when ON by prefs"); + + // And expire + Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 0); + await assert_feature_does_not_appear("when expired"); + + await cleanupPlaces(); +}); + +add_task(async function test_sorting_against_bookmark() { + let boookmarkURI = "https://boookmark.com/"; + await PlacesTestUtils.addBookmarkWithDetails({ + uri: boookmarkURI, + title: "Boookmark", + }); + + Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); + Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); + + info("Preloaded Top Sites are placed lower than Bookmarks"); + let context = createContext("ooo", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + engineName: SUGGESTIONS_ENGINE_NAME, + heuristic: true, + }), + makeBookmarkResult(context, { + uri: boookmarkURI, + title: "Boookmark", + }), + makeVisitResult(context, { + uri: yahoooURI, + title: "Yahooo", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${yahoooURI}`, + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: gooogleURI, + title: "Gooogle", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${gooogleURI}`, + providerName: "PreloadedSites", + }), + ], + }); + + await cleanupPlaces(); +}); + +add_task(async function test_sorting_against_history() { + let histoooryURI = "https://histooory.com/"; + await PlacesTestUtils.addVisits({ uri: histoooryURI, title: "Histooory" }); + + Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); + Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); + + info("Preloaded Top Sites are placed lower than History entries"); + let context = createContext("ooo", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + engineName: SUGGESTIONS_ENGINE_NAME, + heuristic: true, + }), + makeVisitResult(context, { + uri: histoooryURI, + title: "Histooory", + }), + makeVisitResult(context, { + uri: yahoooURI, + title: "Yahooo", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${yahoooURI}`, + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: gooogleURI, + title: "Gooogle", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: `page-icon:${gooogleURI}`, + providerName: "PreloadedSites", + }), + ], + }); + + await cleanupPlaces(); +}); + +add_task(async function test_scheme_and_www() { + // Order is important to check sorting + let sites = [ + ["https://www.ooops-https-www.com/", "Ooops"], + ["https://ooops-https.com/", "Ooops"], + ["HTTP://ooops-HTTP.com/", "Ooops"], + ["HTTP://www.ooops-HTTP-www.com/", "Ooops"], + ["https://foo.com/", "Title with www"], + ["https://www.bar.com/", "Tile"], + ]; + + let titlesMap = new Map(sites); + + UrlbarProviderPreloadedSites.populatePreloadedSiteStorage(sites); + + // No matches when just typing the protocol. + let context = createContext("https://", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeSearchResult(context, { + engineName: SUGGESTIONS_ENGINE_NAME, + heuristic: true, + }), + ], + }); + + context = createContext("www.", { isPrivate: false }); + await check_results({ + context, + autofilled: "www.ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + makeVisitResult(context, { + uri: "http://www.ooops-http-www.com/", + title: titlesMap.get("HTTP://www.ooops-HTTP-www.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:http://www.ooops-http-www.com/", + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: "https://www.bar.com/", + title: titlesMap.get("https://www.bar.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:https://www.bar.com/", + providerName: "PreloadedSites", + }), + ], + }); + + context = createContext("http://www.", { isPrivate: false }); + await check_results({ + context, + autofilled: "http://www.ooops-http-www.com/", + completed: "http://www.ooops-http-www.com/", + matches: [ + makeVisitResult(context, { + uri: "http://www.ooops-http-www.com/", + title: "www.ooops-http-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:http://www.ooops-http-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + ], + }); + + context = createContext("ftp://ooops", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeVisitResult(context, { + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + uri: "ftp://ooops/", + title: "ftp://ooops/", + providerName: "HeuristicFallback", + heuristic: true, + }), + ], + }); + + context = createContext("ww", { isPrivate: false }); + await check_results({ + context, + autofilled: "www.ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + makeVisitResult(context, { + uri: "http://www.ooops-http-www.com/", + title: titlesMap.get("HTTP://www.ooops-HTTP-www.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:http://www.ooops-http-www.com/", + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: "https://foo.com/", + title: titlesMap.get("https://foo.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:https://foo.com/", + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: "https://www.bar.com/", + title: titlesMap.get("https://www.bar.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:https://www.bar.com/", + providerName: "PreloadedSites", + }), + ], + }); + + context = createContext("ooops", { isPrivate: false }); + await check_results({ + context, + autofilled: "ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + makeVisitResult(context, { + uri: "https://ooops-https.com/", + title: titlesMap.get("https://ooops-https.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:https://ooops-https.com/", + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: "http://ooops-http.com/", + title: titlesMap.get("HTTP://ooops-HTTP.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:http://ooops-http.com/", + providerName: "PreloadedSites", + }), + makeVisitResult(context, { + uri: "http://www.ooops-http-www.com/", + title: titlesMap.get("HTTP://www.ooops-HTTP-www.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:http://www.ooops-http-www.com/", + providerName: "PreloadedSites", + }), + ], + }); + + context = createContext("www.ooops", { isPrivate: false }); + await check_results({ + context, + autofilled: "www.ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + makeVisitResult(context, { + uri: "http://www.ooops-http-www.com/", + title: titlesMap.get("HTTP://www.ooops-HTTP-www.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:http://www.ooops-http-www.com/", + providerName: "PreloadedSites", + }), + ], + }); + + context = createContext("ooops-https-www", { isPrivate: false }); + await check_results({ + context, + autofilled: "ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + ], + }); + + context = createContext("www.ooops-https.", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeVisitResult(context, { + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + uri: "http://www.ooops-https./", + title: "http://www.ooops-https./", + providerName: "HeuristicFallback", + heuristic: true, + }), + makeSearchResult(context, { + engineName: SUGGESTIONS_ENGINE_NAME, + providerName: "HeuristicFallback", + }), + ], + }); + + context = createContext("https://ooops", { isPrivate: false }); + await check_results({ + context, + autofilled: "https://ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + makeVisitResult(context, { + uri: "https://ooops-https.com/", + title: titlesMap.get("https://ooops-https.com/"), + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + tags: null, + iconUri: "page-icon:https://ooops-https.com/", + providerName: "PreloadedSites", + }), + ], + }); + + context = createContext("https://www.ooops", { isPrivate: false }); + await check_results({ + context, + autofilled: "https://www.ooops-https-www.com/", + completed: "https://www.ooops-https-www.com/", + matches: [ + makeVisitResult(context, { + uri: "https://www.ooops-https-www.com/", + title: "https://www.ooops-https-www.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: "page-icon:https://www.ooops-https-www.com/", + providerName: "PreloadedSites", + heuristic: true, + }), + ], + }); + + context = createContext("http://www.ooops-http.", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeVisitResult(context, { + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + uri: "http://www.ooops-http./", + title: "http://www.ooops-http./", + providerName: "HeuristicFallback", + heuristic: true, + }), + ], + }); + + context = createContext("http://ooops-https", { isPrivate: false }); + await check_results({ + context, + matches: [ + makeVisitResult(context, { + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + uri: "http://ooops-https/", + title: "http://ooops-https/", + providerName: "HeuristicFallback", + heuristic: true, + }), + ], + }); + + await cleanupPlaces(); +}); + +add_task(async function test_data_file() { + let response = await fetch( + "chrome://browser/content/urlbar/preloaded-top-urls.json" + ); + + info("Source file is supplied and fetched OK"); + Assert.ok(response.ok); + + info("The JSON is parsed"); + let sites = await response.json(); + + // Add test site so this test doesn't depend on the contents of the data file. + sites.push(["https://www.example.com/", "Example"]); + + info("Storage is populated"); + UrlbarProviderPreloadedSites.populatePreloadedSiteStorage(sites); + + let lastSite = sites.pop(); + let uri = Services.io.newURI(lastSite[0]); + + info("Storage is populated from JSON correctly"); + let context = createContext(uri.host, { isPrivate: false }); + await check_results({ + context, + autofilled: uri.host + "/", + completed: uri.spec, + matches: [ + makeVisitResult(context, { + uri: uri.spec, + title: uri.spec.slice(0, -1), // Trim trailing slash. + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: `page-icon:${uri.spec}`, + providerName: "PreloadedSites", + heuristic: true, + }), + ], + }); + + await cleanupPlaces(); +}); + +add_task(async function test_partial_scheme() { + // "tt" should not result in a match of "ttps://whatever.com/". + let testUrl = "http://www.ttt.com/"; + UrlbarProviderPreloadedSites.populatePreloadedSiteStorage([ + [testUrl, "Test"], + ]); + let context = createContext("tt", { isPrivate: false }); + await check_results({ + context, + autofilled: "ttt.com/", + completed: testUrl, + matches: [ + makeVisitResult(context, { + uri: testUrl, + title: "www.ttt.com", + source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL, + iconUri: `page-icon:${testUrl}`, + heuristic: true, + providerName: "PreloadedSites", + }), + ], + }); + await cleanupPlaces(); +}); diff --git a/browser/components/urlbar/tests/unit/xpcshell.ini b/browser/components/urlbar/tests/unit/xpcshell.ini index d4b0319e21e9fb8672f7c497474a2bd6cb5af90b..54989e1d457d0680f18cfb6a8adf3cf7e6b32505 100644 --- a/browser/components/urlbar/tests/unit/xpcshell.ini +++ b/browser/components/urlbar/tests/unit/xpcshell.ini @@ -29,6 +29,7 @@ skip-if = os == 'linux' # bug 1474616 [test_providerKeywords.js] [test_providerOmnibox.js] [test_providerOpenTabs.js] +[test_providerPreloaded.js] [test_providersManager.js] [test_providersManager_filtering.js] [test_providersManager_maxResults.js] diff --git a/toolkit/components/places/tests/unifiedcomplete/test_preloaded_sites.js b/toolkit/components/places/tests/unifiedcomplete/test_preloaded_sites.js deleted file mode 100644 index 41c668c7b5b6e7e97a6b5eed871982db380c0b07..0000000000000000000000000000000000000000 --- a/toolkit/components/places/tests/unifiedcomplete/test_preloaded_sites.js +++ /dev/null @@ -1,322 +0,0 @@ -/** - * Test for bug 1211726 - preload list of top web sites for better - * autocompletion on empty profiles. - */ - -const PREF_FEATURE_ENABLED = "browser.urlbar.usepreloadedtopurls.enabled"; -const PREF_FEATURE_EXPIRE_DAYS = - "browser.urlbar.usepreloadedtopurls.expire_days"; - -const autocompleteObject = Cc[ - "@mozilla.org/autocomplete/search;1?name=unifiedcomplete" -].getService(Ci.mozIPlacesAutoComplete); - -Cu.importGlobalProperties(["fetch"]); - -// With or without trailing slash - no matter. URI.spec does have it always. -// Then, check_autocomplete() doesn't cut it off (uses stripPrefix()). -let yahoooURI = NetUtil.newURI("https://yahooo.com/"); -let gooogleURI = NetUtil.newURI("https://gooogle.com/"); - -autocompleteObject.populatePreloadedSiteStorage([ - [yahoooURI.spec, "Yahooo"], - [gooogleURI.spec, "Gooogle"], -]); - -async function assert_feature_works(condition) { - info("List Results do appear " + condition); - await check_autocomplete({ - search: "ooo", - matches: [ - { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] }, - { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] }, - ], - }); - - info("Autofill does appear " + condition); - await check_autocomplete({ - search: "gooo", - autofilled: "gooogle.com/", // Will fail without trailing slash - completed: "https://gooogle.com/", - }); -} - -async function assert_feature_does_not_appear(condition) { - info("List Results don't appear " + condition); - await check_autocomplete({ - search: "ooo", - matches: [], - }); - - info("Autofill doesn't appear " + condition); - // "search" is what you type, - // "autofilled" is what you get in response in the url bar, - // "completed" is what you get there when you hit enter. - // So if they are all equal - it's the proof there was no autofill - // (knowing we have a suitable preloaded top site). - await check_autocomplete({ - search: "gooo", - autofilled: "gooo", - completed: "gooo", - }); -} - -add_task(async function test_it_works() { - // Not expired but OFF - Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); - Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, false); - await assert_feature_does_not_appear("when OFF by prefs"); - - // Now turn it ON - Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); - await assert_feature_works("when ON by prefs"); - - // And expire - Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 0); - await assert_feature_does_not_appear("when expired"); - - await cleanup(); -}); - -add_task(async function test_sorting_against_bookmark() { - let boookmarkURI = NetUtil.newURI("https://boookmark.com"); - await PlacesTestUtils.addBookmarkWithDetails({ - uri: boookmarkURI, - title: "Boookmark", - }); - - Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); - Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); - - info("Preloaded Top Sites are placed lower than Bookmarks"); - await check_autocomplete({ - checkSorting: true, - search: "ooo", - matches: [ - { uri: boookmarkURI, title: "Boookmark", style: ["bookmark"] }, - { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] }, - { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] }, - ], - }); - - await cleanup(); -}); - -add_task(async function test_sorting_against_history() { - let histoooryURI = NetUtil.newURI("https://histooory.com"); - await PlacesTestUtils.addVisits({ uri: histoooryURI, title: "Histooory" }); - - Services.prefs.setBoolPref(PREF_FEATURE_ENABLED, true); - Services.prefs.setIntPref(PREF_FEATURE_EXPIRE_DAYS, 14); - - info("Preloaded Top Sites are placed lower than History entries"); - await check_autocomplete({ - checkSorting: true, - search: "ooo", - matches: [ - { uri: histoooryURI, title: "Histooory" }, - { uri: yahoooURI, title: "Yahooo", style: ["preloaded-top-site"] }, - { uri: gooogleURI, title: "Gooogle", style: ["preloaded-top-site"] }, - ], - }); - - await cleanup(); -}); - -add_task(async function test_scheme_and_www() { - // Order is important to check sorting - let sites = [ - ["https://www.ooops-https-www.com/", "Ooops"], - ["https://ooops-https.com/", "Ooops"], - ["HTTP://ooops-HTTP.com/", "Ooops"], - ["HTTP://www.ooops-HTTP-www.com/", "Ooops"], - ["https://foo.com/", "Title with www"], - ["https://www.bar.com/", "Tile"], - ]; - - let titlesMap = new Map(sites); - - autocompleteObject.populatePreloadedSiteStorage(sites); - - let tests = [ - // User typed, - // Inline autofill (`autofilled`), - // Substitute after enter is pressed (`completed`), - // [List matches, with sorting] - // not tested if omitted - // !!! first one is always an autofill entry !!! - - [ - // Protocol by itself doesn't match anything - "https://", - "https://", - "https://", - [], - ], - - [ - "www.", - "www.ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [ - ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"], - "HTTP://www.ooops-HTTP-www.com/", - "https://www.bar.com/", - ], - ], - - [ - "http://www.", - "http://www.ooops-http-www.com/", - "http://www.ooops-http-www.com/", - [["http://www.ooops-http-www.com/", "www.ooops-http-www.com"]], - ], - - ["ftp://ooops", "ftp://ooops", "ftp://ooops", []], - - [ - "ww", - "www.ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [ - ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"], - "HTTP://www.ooops-HTTP-www.com/", - ["https://foo.com/", "Title with www", ["preloaded-top-site"]], - "https://www.bar.com/", - ], - ], - - [ - "ooops", - "ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [ - ["ooops-https-www.com/", "https://www.ooops-https-www.com"], - "https://ooops-https.com/", - "HTTP://ooops-HTTP.com/", - "HTTP://www.ooops-HTTP-www.com/", - ], - ], - - [ - "www.ooops", - "www.ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [ - ["www.ooops-https-www.com/", "https://www.ooops-https-www.com"], - "HTTP://www.ooops-HTTP-www.com/", - ], - ], - - [ - "ooops-https-www", - "ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [["ooops-https-www.com/", "https://www.ooops-https-www.com"]], - ], - - ["www.ooops-https.", "www.ooops-https.", "www.ooops-https.", []], - - [ - "https://ooops", - "https://ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [ - ["https://ooops-https-www.com/", "https://www.ooops-https-www.com"], - "https://ooops-https.com/", - ], - ], - - [ - "https://www.ooops", - "https://www.ooops-https-www.com/", - "https://www.ooops-https-www.com/", - [["https://www.ooops-https-www.com/", "https://www.ooops-https-www.com"]], - ], - - [ - "http://www.ooops-http.", - "http://www.ooops-http.", - "http://www.ooops-http.", - [], - ], - - ["http://ooops-https", "http://ooops-https", "http://ooops-https", []], - ]; - - function toMatch(entry, index) { - if (Array.isArray(entry)) { - return { - value: entry[0], - comment: entry[1], - style: entry[2] || ["autofill", "heuristic", "preloaded-top-site"], - }; - } - return { - uri: NetUtil.newURI(entry), - title: titlesMap.get(entry), - style: ["preloaded-top-site"], - }; - } - - for (let test of tests) { - let matches = test[3] ? test[3].map(toMatch) : null; - info("User types: " + test[0]); - await check_autocomplete({ - checkSorting: true, - search: test[0], - autofilled: test[1].toLowerCase(), - completed: test[2].toLowerCase(), - matches, - }); - } - - await cleanup(); -}); - -add_task(async function test_data_file() { - let response = await fetch( - "chrome://browser/content/urlbar/preloaded-top-urls.json" - ); - - info("Source file is supplied and fetched OK"); - Assert.ok(response.ok); - - info("The JSON is parsed"); - let sites = await response.json(); - - info("Storage is populated"); - autocompleteObject.populatePreloadedSiteStorage(sites); - - let lastSite = sites.pop(); - let uri = NetUtil.newURI(lastSite[0]); - - info("Storage is populated from JSON correctly"); - await check_autocomplete({ - search: uri.host, - autofilled: uri.host + "/", - completed: uri.spec, - }); - - await cleanup(); -}); - -add_task(async function test_partial_scheme() { - // "tt" should not result in a match of "ttps://whatever.com/". - autocompleteObject.populatePreloadedSiteStorage([ - ["http://www.ttt.com/", "Test"], - ]); - await check_autocomplete({ - search: "tt", - autofilled: "ttt.com/", - completed: "http://www.ttt.com/", - matches: [ - { - value: "ttt.com/", - comment: "www.ttt.com", - style: ["autofill", "heuristic", "preloaded-top-site"], - }, - ], - }); - await cleanup(); -}); diff --git a/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini b/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini index 85b9a4bc903793820ce9c751ba3e2566172e9f58..52e62be2d0b728587fbc4aa9108dada30d02d49f 100644 --- a/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini +++ b/toolkit/components/places/tests/unifiedcomplete/xpcshell.ini @@ -20,7 +20,6 @@ support-files = [test_history_autocomplete_tags.js] [test_ignore_protocol.js] [test_multi_word_search.js] -[test_preloaded_sites.js] [test_search_engine_restyle.js] [test_special_search.js] [test_swap_protocol.js]