browser_top_sites_private.js (5952B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(this, { 7 AboutNewTab: "resource:///modules/AboutNewTab.sys.mjs", 8 NewTabUtils: "resource://gre/modules/NewTabUtils.sys.mjs", 9 TopSites: "resource:///modules/topsites/TopSites.sys.mjs", 10 }); 11 12 const EN_US_TOPSITES = 13 "https://www.youtube.com/,https://www.facebook.com/,https://www.amazon.com/,https://www.reddit.com/,https://www.wikipedia.org/,https://twitter.com/"; 14 15 async function addTestVisits() { 16 // Add some visits to a URL. 17 for (let i = 0; i < 5; i++) { 18 await PlacesTestUtils.addVisits("http://example.com/"); 19 } 20 21 // Wait for example.com to be listed first. 22 await updateTopSites(sites => { 23 return sites && sites[0] && sites[0].url == "http://example.com/"; 24 }); 25 26 await PlacesUtils.bookmarks.insert({ 27 parentGuid: PlacesUtils.bookmarks.unfiledGuid, 28 url: "https://www.youtube.com/", 29 title: "YouTube", 30 }); 31 32 // Adding a bookmark will refresh the TopSites feed. 33 if (Services.prefs.getBoolPref("browser.topsites.component.enabled")) { 34 await TestUtils.topicObserved("topsites-refreshed"); 35 } 36 } 37 38 add_setup(async function () { 39 await SpecialPowers.pushPrefEnv({ 40 set: [ 41 ["browser.urlbar.suggest.topsites", true], 42 ["browser.urlbar.suggest.quickactions", false], 43 ["browser.newtabpage.activity-stream.default.sites", EN_US_TOPSITES], 44 ], 45 }); 46 47 await updateTopSites( 48 sites => sites && sites.length == EN_US_TOPSITES.split(",").length 49 ); 50 51 let tab = await BrowserTestUtils.openNewForegroundTab( 52 gBrowser, 53 "http://example.com/" 54 ); 55 56 registerCleanupFunction(() => { 57 BrowserTestUtils.removeTab(tab); 58 }); 59 }); 60 61 add_task(async function topSitesPrivateWindow() { 62 // Top Sites should also be shown in private windows. 63 let privateWin = await BrowserTestUtils.openNewBrowserWindow({ 64 private: true, 65 }); 66 await addTestVisits(); 67 let sites; 68 if (Services.prefs.getBoolPref("browser.topsites.component.enabled")) { 69 sites = await TopSites.getSites(); 70 } else { 71 sites = AboutNewTab.getTopSites(); 72 } 73 Assert.equal( 74 sites.length, 75 7, 76 "The test suite browser should have 7 Top Sites." 77 ); 78 let urlbar = privateWin.gURLBar; 79 await UrlbarTestUtils.promisePopupOpen(privateWin, () => { 80 if (urlbar.getAttribute("pageproxystate") == "invalid") { 81 urlbar.handleRevert(); 82 } 83 EventUtils.synthesizeMouseAtCenter(urlbar.inputField, {}, privateWin); 84 }); 85 Assert.ok(urlbar.view.isOpen, "UrlbarView should be open."); 86 await UrlbarTestUtils.promiseSearchComplete(privateWin); 87 88 Assert.equal( 89 UrlbarTestUtils.getResultCount(privateWin), 90 7, 91 "The number of results should be the same as the number of Top Sites (7)." 92 ); 93 94 // Top sites should also be shown in a private window if the search string 95 // gets cleared. 96 await UrlbarTestUtils.promiseAutocompleteResultPopup({ 97 window: privateWin, 98 value: "example", 99 }); 100 urlbar.select(); 101 EventUtils.synthesizeKey("KEY_Backspace", {}, privateWin); 102 Assert.ok(urlbar.view.isOpen, "UrlbarView should be open."); 103 await UrlbarTestUtils.promiseSearchComplete(privateWin); 104 Assert.equal( 105 UrlbarTestUtils.getResultCount(privateWin), 106 7, 107 "The number of results should be the same as the number of Top Sites (7)." 108 ); 109 110 await BrowserTestUtils.closeWindow(privateWin); 111 112 await PlacesUtils.bookmarks.eraseEverything(); 113 await PlacesUtils.history.clear(); 114 await UrlbarTestUtils.promisePopupClose(window, () => { 115 gURLBar.blur(); 116 }); 117 }); 118 119 add_task(async function topSitesTabSwitch() { 120 // Add some visits 121 for (let i = 0; i < 5; i++) { 122 await PlacesTestUtils.addVisits(["http://example.com/"]); 123 } 124 125 // Switch to the originating tab, to check for switch to the current tab. 126 gBrowser.selectedTab = gBrowser.tabs[0]; 127 128 // Wait for the expected number of Top sites. 129 await updateTopSites(sites => sites?.length == 7); 130 let sites; 131 if (Services.prefs.getBoolPref("browser.topsites.component.enabled")) { 132 sites = await TopSites.getSites(); 133 } else { 134 sites = AboutNewTab.getTopSites(); 135 } 136 Assert.equal( 137 sites.length, 138 7, 139 "The test suite browser should have 7 Top Sites." 140 ); 141 142 async function checkResults(win, expectedResultType) { 143 let resultCount = UrlbarTestUtils.getResultCount(win); 144 let result; 145 for (let i = 0; i < resultCount; ++i) { 146 result = await UrlbarTestUtils.getDetailsOfResultAt(win, i); 147 if (result.url == "http://example.com/") { 148 break; 149 } 150 } 151 Assert.equal( 152 result.type, 153 expectedResultType, 154 `Should provide a result of type ${expectedResultType}.` 155 ); 156 } 157 158 info("Test in a non-private window"); 159 await UrlbarTestUtils.promisePopupOpen(window, () => { 160 EventUtils.synthesizeMouseAtCenter(gURLBar.inputField, {}); 161 }); 162 Assert.ok(gURLBar.view.isOpen, "UrlbarView should be open."); 163 await UrlbarTestUtils.promiseSearchComplete(window); 164 await checkResults(window, UrlbarUtils.RESULT_TYPE.TAB_SWITCH); 165 await UrlbarTestUtils.promisePopupClose(window); 166 167 info("Test in a private window, switch to tab should not be offered"); 168 // Top Sites should also be shown in private windows. 169 let privateWin = await BrowserTestUtils.openNewBrowserWindow({ 170 private: true, 171 }); 172 173 let urlbar = privateWin.gURLBar; 174 await UrlbarTestUtils.promisePopupOpen(privateWin, () => { 175 if (urlbar.getAttribute("pageproxystate") == "invalid") { 176 urlbar.handleRevert(); 177 } 178 EventUtils.synthesizeMouseAtCenter(urlbar.inputField, {}, privateWin); 179 }); 180 181 Assert.ok(urlbar.view.isOpen, "UrlbarView should be open."); 182 await UrlbarTestUtils.promiseSearchComplete(privateWin); 183 await checkResults(privateWin, UrlbarUtils.RESULT_TYPE.URL); 184 await UrlbarTestUtils.promisePopupClose(privateWin); 185 await BrowserTestUtils.closeWindow(privateWin); 186 187 await PlacesUtils.history.clear(); 188 });