browser_searchShowSuggestionsFirst.js (8097B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 const { CustomizableUITestUtils } = ChromeUtils.importESModule( 5 "resource://testing-common/CustomizableUITestUtils.sys.mjs" 6 ); 7 let gCUITestUtils = new CustomizableUITestUtils(window); 8 9 const MAIN_PREF = "browser.search.suggest.enabled"; 10 const URLBAR_PREF = "browser.urlbar.suggest.searches"; 11 const FIRST_PREF = "browser.urlbar.showSearchSuggestionsFirst"; 12 const FIRST_CHECKBOX_ID = "showSearchSuggestionsFirstCheckbox"; 13 14 add_setup(async function () { 15 // Make sure the main and urlbar suggestion prefs are enabled. 16 await SpecialPowers.pushPrefEnv({ 17 set: [ 18 [MAIN_PREF, true], 19 [URLBAR_PREF, true], 20 ], 21 }); 22 }); 23 24 // Open preferences with search suggestions shown first (the default). 25 add_task(async function openWithSearchSuggestionsShownFirst() { 26 // Initially the pref should be true so search suggestions are shown first. 27 Assert.ok( 28 Services.prefs.getBoolPref(FIRST_PREF), 29 "Pref should be true initially" 30 ); 31 32 // Open preferences. The checkbox should be checked. 33 await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true }); 34 let doc = gBrowser.selectedBrowser.contentDocument; 35 let checkbox = doc.getElementById(FIRST_CHECKBOX_ID); 36 Assert.ok(checkbox.checked, "Checkbox should be checked"); 37 Assert.ok(!checkbox.disabled, "Checkbox should be enabled"); 38 39 // Uncheck the checkbox. 40 checkbox.click(); 41 42 // The pref should now be false so that history is shown first. 43 Assert.ok( 44 !Services.prefs.getBoolPref(FIRST_PREF), 45 "Pref should now be false to show history first" 46 ); 47 48 // Make sure the checkbox state didn't change. 49 Assert.ok(!checkbox.checked, "Checkbox should remain unchecked"); 50 Assert.ok(!checkbox.disabled, "Checkbox should remain enabled"); 51 52 // Clear the pref. 53 Services.prefs.clearUserPref(FIRST_PREF); 54 await checkbox.updateComplete; 55 56 // The checkbox should have become checked again. 57 Assert.ok( 58 checkbox.checked, 59 "Checkbox should become checked after clearing pref" 60 ); 61 Assert.ok( 62 !checkbox.disabled, 63 "Checkbox should remain enabled after clearing pref" 64 ); 65 66 // Clean up. 67 gBrowser.removeCurrentTab(); 68 }); 69 70 // Open preferences with history shown first. 71 add_task(async function openWithHistoryShownFirst() { 72 // Set the pref to show history first. 73 Services.prefs.setBoolPref(FIRST_PREF, false); 74 75 // Open preferences. The checkbox should be unchecked. 76 await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true }); 77 let doc = gBrowser.selectedBrowser.contentDocument; 78 let checkbox = doc.getElementById(FIRST_CHECKBOX_ID); 79 Assert.ok(!checkbox.checked, "Checkbox should be unchecked"); 80 Assert.ok(!checkbox.disabled, "Checkbox should be enabled"); 81 82 // Check the checkbox. 83 checkbox.click(); 84 85 // Make sure the checkbox state didn't change. 86 Assert.ok(checkbox.checked, "Checkbox should remain checked"); 87 Assert.ok(!checkbox.disabled, "Checkbox should remain enabled"); 88 89 // The pref should now be true so that search suggestions are shown first. 90 Assert.ok( 91 Services.prefs.getBoolPref(FIRST_PREF), 92 "Pref should now be true to show search suggestions first" 93 ); 94 95 // Set the pref to false again. 96 Services.prefs.setBoolPref(FIRST_PREF, false); 97 await checkbox.updateComplete; 98 99 // The checkbox should have become unchecked again. 100 Assert.ok( 101 !checkbox.checked, 102 "Checkbox should become unchecked after setting pref to false" 103 ); 104 Assert.ok( 105 !checkbox.disabled, 106 "Checkbox should remain enabled after setting pref to false" 107 ); 108 109 // Clean up. 110 gBrowser.removeCurrentTab(); 111 Services.prefs.clearUserPref(FIRST_PREF); 112 }); 113 114 // Checks how the show-suggestions-first pref and checkbox reacts to updates to 115 // URLBAR_PREF and MAIN_PREF. 116 add_task(async function superprefInteraction() { 117 // Initially the pref should be true so search suggestions are shown first. 118 Assert.ok( 119 Services.prefs.getBoolPref(FIRST_PREF), 120 "Pref should be true initially" 121 ); 122 123 // Open preferences. The checkbox should be checked. 124 await openPreferencesViaOpenPreferencesAPI("search", { leaveOpen: true }); 125 let doc = gBrowser.selectedBrowser.contentDocument; 126 let checkbox = doc.getElementById(FIRST_CHECKBOX_ID); 127 Assert.ok(checkbox.checked, "Checkbox should be checked"); 128 Assert.ok(!checkbox.disabled, "Checkbox should be enabled"); 129 130 await gCUITestUtils.addSearchBar(); 131 132 // Two superior prefs control the show-suggestion-first pref: URLBAR_PREF and 133 // MAIN_PREF. Toggle each and make sure the show-suggestion-first checkbox 134 // reacts appropriately. 135 for (let superiorPref of [URLBAR_PREF, MAIN_PREF]) { 136 info(`Testing superior pref ${superiorPref}`); 137 138 // Set the superior pref to false. 139 Services.prefs.setBoolPref(superiorPref, false); 140 await checkbox.updateComplete; 141 142 // The pref should remain true. 143 Assert.ok( 144 Services.prefs.getBoolPref(FIRST_PREF), 145 "Pref should remain true" 146 ); 147 148 // The checkbox should have become unchecked and disabled. 149 Assert.ok( 150 !checkbox.checked, 151 "Checkbox should become unchecked after disabling urlbar suggestions" 152 ); 153 Assert.ok( 154 checkbox.disabled, 155 "Checkbox should become disabled after disabling urlbar suggestions" 156 ); 157 158 // Set the superior pref to true. 159 Services.prefs.setBoolPref(superiorPref, true); 160 await checkbox.updateComplete; 161 162 // The pref should remain true. 163 Assert.ok( 164 Services.prefs.getBoolPref(FIRST_PREF), 165 "Pref should remain true" 166 ); 167 168 // The checkbox should have become checked and enabled again. 169 Assert.ok( 170 checkbox.checked, 171 "Checkbox should become checked after re-enabling urlbar suggestions" 172 ); 173 Assert.ok( 174 !checkbox.disabled, 175 "Checkbox should become enabled after re-enabling urlbar suggestions" 176 ); 177 178 // Set the pref to false. 179 Services.prefs.setBoolPref(FIRST_PREF, false); 180 await checkbox.updateComplete; 181 182 // The checkbox should have become unchecked. 183 Assert.ok( 184 !checkbox.checked, 185 "Checkbox should become unchecked after setting pref to false" 186 ); 187 Assert.ok( 188 !checkbox.disabled, 189 "Checkbox should remain enabled after setting pref to false" 190 ); 191 192 // Set the superior pref to false again. 193 Services.prefs.setBoolPref(superiorPref, false); 194 await checkbox.updateComplete; 195 196 // The pref should remain false. 197 Assert.ok( 198 !Services.prefs.getBoolPref(FIRST_PREF), 199 "Pref should remain false" 200 ); 201 202 // The checkbox should remain unchecked and become disabled. 203 Assert.ok( 204 !checkbox.checked, 205 "Checkbox should remain unchecked after disabling urlbar suggestions" 206 ); 207 Assert.ok( 208 checkbox.disabled, 209 "Checkbox should become disabled after disabling urlbar suggestions" 210 ); 211 212 // Set the superior pref to true. 213 Services.prefs.setBoolPref(superiorPref, true); 214 await checkbox.updateComplete; 215 216 // The pref should remain false. 217 Assert.ok( 218 !Services.prefs.getBoolPref(FIRST_PREF), 219 "Pref should remain false" 220 ); 221 222 // The checkbox should remain unchecked and become enabled. 223 Assert.ok( 224 !checkbox.checked, 225 "Checkbox should remain unchecked after re-enabling urlbar suggestions" 226 ); 227 Assert.ok( 228 !checkbox.disabled, 229 "Checkbox should become enabled after re-enabling urlbar suggestions" 230 ); 231 232 // Finally, set the pref back to true. 233 Services.prefs.setBoolPref(FIRST_PREF, true); 234 await checkbox.updateComplete; 235 236 // The checkbox should have become checked. 237 Assert.ok( 238 checkbox.checked, 239 "Checkbox should become checked after setting pref back to true" 240 ); 241 Assert.ok( 242 !checkbox.disabled, 243 "Checkbox should remain enabled after setting pref back to true" 244 ); 245 } 246 247 // Clean up. 248 gBrowser.removeCurrentTab(); 249 250 Services.prefs.clearUserPref(FIRST_PREF); 251 Services.prefs.clearUserPref(URLBAR_PREF); 252 Services.prefs.clearUserPref(MAIN_PREF); 253 gCUITestUtils.removeSearchBar(); 254 });