Bug 1377852 - Do not display the 2nd storage pressure notification when there is one being displayed draft
authorFischer.json <fischer.json@gmail.com>
Thu, 17 Aug 2017 15:10:52 +0800
changeset 648091 d1cffec2518e62b8cf07f319058c0e0d2785b607
parent 647200 4e93516e92e58d166ad37b8544c3230024afb587
child 726701 9df4e92c3fa332ac4356f04f44beb69851c4b275
push id74620
push userbmo:fliu@mozilla.com
push dateThu, 17 Aug 2017 07:33:00 +0000
bugs1377852
milestone57.0a1
Bug 1377852 - Do not display the 2nd storage pressure notification when there is one being displayed 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
@@ -482,16 +482,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;
@@ -503,17 +510,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);
       }
     });
@@ -552,17 +558,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();
+});