Bug 1377852 - Do not display the 2nd storage pressure notification when there is one being displayed, r?jaws draft
authorFischer.json <fischer.json@gmail.com>
Thu, 17 Aug 2017 15:10:52 +0800
changeset 650262 6a7b9b4e76c44c27d3cfb93afd605c5c1c4f43d5
parent 650228 128a79130ecd6f277190d031a623f991c73c5272
child 727343 6b26dfb721bdb988754fc92bac4d83f76d8a826c
push id75318
push userbmo:fliu@mozilla.com
push dateTue, 22 Aug 2017 04:01:51 +0000
reviewersjaws
bugs1377852
milestone57.0a1
Bug 1377852 - Do not display the 2nd storage pressure notification when there is one being displayed, r?jaws MozReview-Commit-ID: 4YwdJMy6vGf
browser/base/content/browser.js
browser/base/content/test/general/browser_storagePressure_notification.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -477,16 +477,23 @@ const gStoragePressureObserver = {
   _lastNotificationTime: -1,
 
   observe(subject, topic, data) {
     if (topic != "QuotaManager::StoragePressure" ||
         !Services.prefs.getBoolPref("browser.storageManager.enabled")) {
       return;
     }
 
+    const NOTIFICATION_VALUE = "storage-pressure-notification";
+    let notificationBox = document.getElementById("high-priority-global-notificationbox");
+    if (notificationBox.getNotificationWithValue(NOTIFICATION_VALUE)) {
+      // Do not display the 2nd notification when there is already one
+      return;
+    }
+
     // Don't display notification twice within the given interval.
     // This is because
     //   - not to annoy user
     //   - give user some time to clean space.
     //     Even user sees notification and starts acting, it still takes some time.
     const MIN_NOTIFICATION_INTERVAL_MS =
       Services.prefs.getIntPref("browser.storageManager.pressureNotification.minIntervalMS");
     let duration = Date.now() - this._lastNotificationTime;
@@ -498,17 +505,16 @@ const gStoragePressureObserver = {
     const BYTES_IN_GIGABYTE = 1073741824;
     const USAGE_THRESHOLD_BYTES = BYTES_IN_GIGABYTE *
       Services.prefs.getIntPref("browser.storageManager.pressureNotification.usageThresholdGB");
     let msg = "";
     let buttons = [];
     let usage = subject.QueryInterface(Ci.nsISupportsPRUint64).data
     let prefStrBundle = document.getElementById("bundle_preferences");
     let brandShortName = document.getElementById("bundle_brand").getString("brandShortName");
-    let notificationBox = document.getElementById("high-priority-global-notificationbox");
     buttons.push({
       label: prefStrBundle.getString("spaceAlert.learnMoreButton.label"),
       accessKey: prefStrBundle.getString("spaceAlert.learnMoreButton.accesskey"),
       callback(notificationBar, button) {
         let learnMoreURL = Services.urlFormatter.formatURLPref("app.support.baseURL") + "storage-permissions";
         gBrowser.selectedTab = gBrowser.addTab(learnMoreURL);
       }
     });
@@ -547,17 +553,17 @@ const gStoragePressureObserver = {
           } else {
             win.openPreferences("panePrivacy", {origin: "storagePressure"});
           }
         }
       });
     }
 
     notificationBox.appendNotification(
-      msg, "storage-pressure-notification", null, notificationBox.PRIORITY_WARNING_HIGH, buttons, null);
+      msg, NOTIFICATION_VALUE, null, notificationBox.PRIORITY_WARNING_HIGH, buttons, null);
   }
 };
 
 /**
  * Given a starting docshell and a URI to look up, find the docshell the URI
  * is loaded in.
  * @param   aDocument
  *          A document to find instead of using just a URI - this is more specific.
--- a/browser/base/content/test/general/browser_storagePressure_notification.js
+++ b/browser/base/content/test/general/browser_storagePressure_notification.js
@@ -74,8 +74,28 @@ add_task(async function() {
 add_task(async function() {
   // Test for the old about:preferences
   await SpecialPowers.pushPrefEnv({set: [["browser.preferences.useOldOrganization", true]]});
   await testOverUsageThresholdNotification();
   // Test for the new about:preferences
   await SpecialPowers.pushPrefEnv({set: [["browser.preferences.useOldOrganization", false]]});
   await testOverUsageThresholdNotification();
 });
+
+// Test not displaying the 2nd notification if one is already being displayed
+add_task(async function() {
+  const TEST_NOTIFICATION_INTERVAL_MS = 0;
+  await SpecialPowers.pushPrefEnv({set: [["browser.storageManager.enabled", true]]});
+  await SpecialPowers.pushPrefEnv({set: [["browser.storageManager.pressureNotification.minIntervalMS", TEST_NOTIFICATION_INTERVAL_MS]]});
+
+  await notifyStoragePressure();
+  await notifyStoragePressure();
+  let notificationbox = document.getElementById("high-priority-global-notificationbox");
+  let allNotifications = notificationbox.allNotifications;
+  let pressureNotificationCount = 0;
+  allNotifications.forEach(notification => {
+    if (notification.getAttribute("value") == "storage-pressure-notification") {
+      pressureNotificationCount++;
+    }
+  });
+  is(pressureNotificationCount, 1, "Should not display the 2nd notification when there is already one");
+  notificationbox.removeAllNotifications();
+});