browser_storagePressure_notification.js (5694B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ 3 */ 4 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 5 6 async function notifyStoragePressure(usage = 100) { 7 let notifyPromise = TestUtils.topicObserved( 8 "QuotaManager::StoragePressure", 9 () => true 10 ); 11 let usageWrapper = Cc["@mozilla.org/supports-PRUint64;1"].createInstance( 12 Ci.nsISupportsPRUint64 13 ); 14 usageWrapper.data = usage; 15 Services.obs.notifyObservers(usageWrapper, "QuotaManager::StoragePressure"); 16 return notifyPromise; 17 } 18 19 function openAboutPrefPromise(win) { 20 let promises = [ 21 BrowserTestUtils.waitForLocationChange( 22 win.gBrowser, 23 "about:preferences#privacy" 24 ), 25 TestUtils.topicObserved("privacy-pane-loaded", () => true), 26 TestUtils.topicObserved("sync-pane-loaded", () => true), 27 ]; 28 return Promise.all(promises); 29 } 30 add_setup(async function () { 31 let win = await BrowserTestUtils.openNewBrowserWindow(); 32 // Open a new tab to keep the window open. 33 await BrowserTestUtils.openNewForegroundTab( 34 win.gBrowser, 35 "https://example.com" 36 ); 37 }); 38 39 // Test only displaying notification once within the given interval 40 add_task(async function () { 41 const win = Services.wm.getMostRecentWindow("navigator:browser"); 42 const TEST_NOTIFICATION_INTERVAL_MS = 2000; 43 await SpecialPowers.pushPrefEnv({ 44 set: [ 45 [ 46 "browser.storageManager.pressureNotification.minIntervalMS", 47 TEST_NOTIFICATION_INTERVAL_MS, 48 ], 49 ], 50 }); 51 // Commenting this to see if we really need it 52 // await SpecialPowers.pushPrefEnv({set: [["privacy.reduceTimerPrecision", false]]}); 53 54 await notifyStoragePressure(); 55 await TestUtils.waitForCondition(() => 56 win.gNotificationBox.getNotificationWithValue( 57 "storage-pressure-notification" 58 ) 59 ); 60 let notification = win.gNotificationBox.getNotificationWithValue( 61 "storage-pressure-notification" 62 ); 63 is( 64 notification.localName, 65 "notification-message", 66 "Should display storage pressure notification" 67 ); 68 notification.close(); 69 70 await notifyStoragePressure(); 71 notification = win.gNotificationBox.getNotificationWithValue( 72 "storage-pressure-notification" 73 ); 74 is( 75 notification, 76 null, 77 "Should not display storage pressure notification more than once within the given interval" 78 ); 79 80 await new Promise(resolve => 81 setTimeout(resolve, TEST_NOTIFICATION_INTERVAL_MS + 1) 82 ); 83 await notifyStoragePressure(); 84 await TestUtils.waitForCondition(() => 85 win.gNotificationBox.getNotificationWithValue( 86 "storage-pressure-notification" 87 ) 88 ); 89 notification = win.gNotificationBox.getNotificationWithValue( 90 "storage-pressure-notification" 91 ); 92 is( 93 notification.localName, 94 "notification-message", 95 "Should display storage pressure notification after the given interval" 96 ); 97 notification.close(); 98 }); 99 100 // Test guiding user to the about:preferences when usage exceeds the given threshold 101 add_task(async function () { 102 const win = Services.wm.getMostRecentWindow("navigator:browser"); 103 await SpecialPowers.pushPrefEnv({ 104 set: [["browser.storageManager.pressureNotification.minIntervalMS", 0]], 105 }); 106 let tab = await BrowserTestUtils.openNewForegroundTab( 107 win.gBrowser, 108 "https://example.com" 109 ); 110 111 const BYTES_IN_GIGABYTE = 1073741824; 112 const USAGE_THRESHOLD_BYTES = 113 BYTES_IN_GIGABYTE * 114 Services.prefs.getIntPref( 115 "browser.storageManager.pressureNotification.usageThresholdGB" 116 ); 117 await notifyStoragePressure(USAGE_THRESHOLD_BYTES); 118 await TestUtils.waitForCondition(() => 119 win.gNotificationBox.getNotificationWithValue( 120 "storage-pressure-notification" 121 ) 122 ); 123 let notification = win.gNotificationBox.getNotificationWithValue( 124 "storage-pressure-notification" 125 ); 126 is( 127 notification.localName, 128 "notification-message", 129 "Should display storage pressure notification" 130 ); 131 await new Promise(r => setTimeout(r, 1000)); 132 133 let prefBtn = notification.buttonContainer.getElementsByTagName("button")[0]; 134 ok(prefBtn, "Should have an open preferences button"); 135 let aboutPrefPromise = openAboutPrefPromise(win); 136 EventUtils.synthesizeMouseAtCenter(prefBtn, {}, win); 137 await aboutPrefPromise; 138 let aboutPrefTab = win.gBrowser.selectedTab; 139 let prefDoc = win.gBrowser.selectedBrowser.contentDocument; 140 let siteDataGroup = prefDoc.getElementById("siteDataGroup"); 141 is_element_visible( 142 siteDataGroup, 143 "Should open to the siteDataGroup section in about:preferences" 144 ); 145 BrowserTestUtils.removeTab(aboutPrefTab); 146 BrowserTestUtils.removeTab(tab); 147 }); 148 149 // Test not displaying the 2nd notification if one is already being displayed 150 add_task(async function () { 151 const win = Services.wm.getMostRecentWindow("navigator:browser"); 152 const TEST_NOTIFICATION_INTERVAL_MS = 0; 153 await SpecialPowers.pushPrefEnv({ 154 set: [ 155 [ 156 "browser.storageManager.pressureNotification.minIntervalMS", 157 TEST_NOTIFICATION_INTERVAL_MS, 158 ], 159 ], 160 }); 161 162 await notifyStoragePressure(); 163 await notifyStoragePressure(); 164 let allNotifications = win.gNotificationBox.allNotifications; 165 let pressureNotificationCount = 0; 166 allNotifications.forEach(notification => { 167 if (notification.getAttribute("value") == "storage-pressure-notification") { 168 pressureNotificationCount++; 169 } 170 }); 171 is( 172 pressureNotificationCount, 173 1, 174 "Should not display the 2nd notification when there is already one" 175 ); 176 win.gNotificationBox.removeAllNotifications(); 177 }); 178 179 add_task(async function cleanup() { 180 const win = Services.wm.getMostRecentWindow("navigator:browser"); 181 await BrowserTestUtils.closeWindow(win); 182 });