test_UrlbarProviderSearchSuggestions.js (3686B)
1 /* Any copyright is dedicated to the Public Domain. 2 https://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { UrlbarProviderSearchSuggestions } = ChromeUtils.importESModule( 7 "moz-src:///browser/components/urlbar/UrlbarProviderSearchSuggestions.sys.mjs" 8 ); 9 10 const KEYWORD_ENABLED = "keyword.enabled"; 11 const SUGGEST_ENABLED = "browser.search.suggest.enabled"; 12 const URLBAR_SUGGEST = "browser.urlbar.suggest.searches"; 13 14 add_setup(async function () { 15 await Services.search.init(); 16 }); 17 18 add_task(async function test_allowRemoteSuggestions() { 19 let suggestionsProvider = new UrlbarProviderSearchSuggestions(); 20 21 let context = createContext("bacon", { 22 isPrivate: false, 23 sapName: "urlbar", 24 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 25 }); 26 Assert.ok( 27 suggestionsProvider._allowRemoteSuggestions(context), 28 "Remote suggestions should be enabled with keyword enabled" 29 ); 30 31 info("Setting " + KEYWORD_ENABLED + "=false"); 32 Services.prefs.setBoolPref(KEYWORD_ENABLED, false); 33 34 context = createContext("bacon", { 35 isPrivate: false, 36 sapName: "urlbar", 37 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 38 }); 39 Assert.ok( 40 !suggestionsProvider._allowRemoteSuggestions(context), 41 "Remote suggestions should be disabled with keyword disabled" 42 ); 43 44 context = createContext("bacon", { 45 isPrivate: false, 46 sapName: "searchbar", 47 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 48 }); 49 Assert.ok( 50 suggestionsProvider._allowRemoteSuggestions(context), 51 "Remote suggestions should still be enabled on searchbar" 52 ); 53 54 Services.prefs.clearUserPref(KEYWORD_ENABLED); 55 }); 56 57 add_task(async function test_allowSuggestions() { 58 let suggestionsProvider = new UrlbarProviderSearchSuggestions(); 59 60 let context = createContext("bacon eggs", { 61 isPrivate: false, 62 sapName: "urlbar", 63 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 64 }); 65 Assert.ok( 66 suggestionsProvider._allowSuggestions(context), 67 "Suggestions in the urlbar should be enabled by default" 68 ); 69 70 context = createContext("bacon eggs", { 71 isPrivate: false, 72 sapName: "searchbar", 73 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 74 }); 75 Assert.ok( 76 suggestionsProvider._allowSuggestions(context), 77 "Suggestions in the searchbar should be enabled by default" 78 ); 79 80 info("Setting " + URLBAR_SUGGEST + "=false"); 81 Services.prefs.setBoolPref(URLBAR_SUGGEST, false); 82 83 context = createContext("bacon eggs", { 84 isPrivate: false, 85 sapName: "urlbar", 86 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 87 }); 88 Assert.ok( 89 !suggestionsProvider._allowSuggestions(context), 90 "Suggestions in the urlbar should be disabled" 91 ); 92 93 context = createContext("bacon eggs", { 94 isPrivate: false, 95 sapName: "searchbar", 96 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 97 }); 98 Assert.ok( 99 suggestionsProvider._allowSuggestions(context), 100 "Suggestions in the searchbar should still be enabled" 101 ); 102 103 info("Setting " + SUGGEST_ENABLED + "=false"); 104 Services.prefs.setBoolPref(SUGGEST_ENABLED, false); 105 106 context = createContext("bacon eggs", { 107 isPrivate: false, 108 sapName: "urlbar", 109 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 110 }); 111 Assert.ok( 112 !suggestionsProvider._allowSuggestions(context), 113 "Suggestions in the urlbar should be disabled" 114 ); 115 116 context = createContext("bacon eggs", { 117 isPrivate: false, 118 sapName: "searchbar", 119 sources: [UrlbarUtils.RESULT_SOURCE.SEARCH], 120 }); 121 Assert.ok( 122 !suggestionsProvider._allowSuggestions(context), 123 "Suggestions in the urlbar should be disabled" 124 ); 125 126 Services.prefs.clearUserPref(SUGGEST_ENABLED); 127 Services.prefs.clearUserPref(URLBAR_SUGGEST); 128 });