browser_hometab_restore_defaults.js (5034B)
1 add_task(async function testRestoreDefaultsBtn_visible() { 2 await SpecialPowers.pushPrefEnv({ 3 set: [ 4 // Hide Pocket prefs so we don't trigger network requests when we reset all preferences 5 [ 6 "browser.newtabpage.activity-stream.discoverystream.endpointSpocsClear", 7 "", 8 ], 9 // Set a user pref to false to force the Restore Defaults button to be visible 10 ["browser.newtabpage.activity-stream.feeds.topsites", false], 11 ], 12 }); 13 14 let tab = await BrowserTestUtils.openNewForegroundTab( 15 gBrowser, 16 "about:preferences#home", 17 false 18 ); 19 let browser = tab.linkedBrowser; 20 21 await BrowserTestUtils.waitForCondition( 22 () => 23 SpecialPowers.spawn( 24 browser, 25 [], 26 () => 27 content.document.getElementById("restoreDefaultHomePageBtn") !== null 28 ), 29 "Wait for the button to be added to the page" 30 ); 31 32 await BrowserTestUtils.waitForCondition( 33 () => 34 SpecialPowers.spawn( 35 browser, 36 [], 37 () => 38 content.document.querySelector( 39 "[data-subcategory='topsites'] checkbox" 40 ) !== null 41 ), 42 "Wait for the preference checkbox to load" 43 ); 44 45 await BrowserTestUtils.waitForCondition( 46 () => 47 SpecialPowers.spawn( 48 browser, 49 [], 50 () => 51 content.document.getElementById("restoreDefaultHomePageBtn") 52 .hidden === false 53 ), 54 "Should show the Restore Defaults btn because pref is changed" 55 ); 56 57 await SpecialPowers.spawn(browser, [], () => 58 content.document.getElementById("restoreDefaultHomePageBtn").click() 59 ); 60 61 await BrowserTestUtils.waitForCondition( 62 () => 63 SpecialPowers.spawn( 64 browser, 65 [], 66 () => 67 content.document.querySelector( 68 "[data-subcategory='topsites'] checkbox" 69 ).checked 70 ), 71 "Should have checked preference" 72 ); 73 74 await BrowserTestUtils.waitForCondition( 75 () => 76 SpecialPowers.spawn( 77 browser, 78 [], 79 () => 80 content.document.getElementById("restoreDefaultHomePageBtn").style 81 .visibility === "hidden" 82 ), 83 "Should not show the Restore Defaults btn if prefs were reset" 84 ); 85 86 const topsitesPref = await SpecialPowers.Services.prefs.getBoolPref( 87 "browser.newtabpage.activity-stream.feeds.topsites" 88 ); 89 Assert.ok(topsitesPref, "Topsites pref should have the default value"); 90 91 await SpecialPowers.popPrefEnv(); 92 BrowserTestUtils.removeTab(tab); 93 }); 94 95 add_task(async function testRestoreDefaultsBtn_hidden() { 96 await SpecialPowers.pushPrefEnv({ 97 set: [ 98 [ 99 "browser.newtabpage.activity-stream.discoverystream.endpointSpocsClear", 100 "", 101 ], 102 ], 103 }); 104 105 let tab = await BrowserTestUtils.openNewForegroundTab( 106 gBrowser, 107 "about:preferences#home", 108 false 109 ); 110 let browser = tab.linkedBrowser; 111 112 await BrowserTestUtils.waitForCondition( 113 () => 114 SpecialPowers.spawn( 115 browser, 116 [], 117 () => 118 content.document.getElementById("restoreDefaultHomePageBtn") !== null 119 ), 120 "Wait for the button to be added to the page" 121 ); 122 123 await BrowserTestUtils.waitForCondition( 124 () => 125 SpecialPowers.spawn( 126 browser, 127 [], 128 () => 129 content.document.querySelector( 130 "[data-subcategory='topsites'] checkbox" 131 ) !== null 132 ), 133 "Wait for the preference checkbox to load" 134 ); 135 136 const btnDefault = await SpecialPowers.spawn( 137 browser, 138 [], 139 () => 140 content.document.getElementById("restoreDefaultHomePageBtn").style 141 .visibility 142 ); 143 Assert.equal( 144 btnDefault, 145 "hidden", 146 "When no prefs are changed button should not show up" 147 ); 148 149 await BrowserTestUtils.waitForCondition( 150 () => 151 SpecialPowers.spawn( 152 browser, 153 [], 154 () => 155 content.document.querySelector( 156 "[data-subcategory='topsites'] checkbox" 157 ).checked 158 ), 159 "Should have checked preference" 160 ); 161 162 // Uncheck a pref 163 await SpecialPowers.spawn(browser, [], () => 164 content.document 165 .querySelector("[data-subcategory='topsites'] checkbox") 166 .click() 167 ); 168 169 await BrowserTestUtils.waitForCondition( 170 () => 171 SpecialPowers.spawn( 172 browser, 173 [], 174 () => 175 !content.document.querySelector( 176 "[data-subcategory='topsites'] checkbox" 177 ).checked 178 ), 179 "Should have unchecked preference" 180 ); 181 182 await BrowserTestUtils.waitForCondition( 183 () => 184 SpecialPowers.spawn( 185 browser, 186 [], 187 () => 188 content.document.getElementById("restoreDefaultHomePageBtn").style 189 .visibility === "" 190 ), 191 "Should show the Restore Defaults btn if prefs were changed" 192 ); 193 194 // Reset the pref 195 await SpecialPowers.Services.prefs.clearUserPref( 196 "browser.newtabpage.activity-stream.feeds.topsites" 197 ); 198 199 await SpecialPowers.popPrefEnv(); 200 BrowserTestUtils.removeTab(tab); 201 });