summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/general/browser_storagePressure_notification.js
blob: dcafbe8bf9c7dec3e8f517b7ebfac7d05ce22143 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */
/* eslint-disable mozilla/no-arbitrary-setTimeout */

async function notifyStoragePressure(usage = 100) {
  let notifyPromise = TestUtils.topicObserved(
    "QuotaManager::StoragePressure",
    () => true
  );
  let usageWrapper = Cc["@mozilla.org/supports-PRUint64;1"].createInstance(
    Ci.nsISupportsPRUint64
  );
  usageWrapper.data = usage;
  Services.obs.notifyObservers(usageWrapper, "QuotaManager::StoragePressure");
  return notifyPromise;
}

function openAboutPrefPromise(win) {
  let promises = [
    BrowserTestUtils.waitForLocationChange(
      win.gBrowser,
      "about:preferences#privacy"
    ),
    TestUtils.topicObserved("privacy-pane-loaded", () => true),
    TestUtils.topicObserved("sync-pane-loaded", () => true),
  ];
  return Promise.all(promises);
}
add_setup(async function () {
  let win = await BrowserTestUtils.openNewBrowserWindow();
  // Open a new tab to keep the window open.
  await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "https://example.com"
  );
});

// Test only displaying notification once within the given interval
add_task(async function () {
  const win = Services.wm.getMostRecentWindow("navigator:browser");
  const TEST_NOTIFICATION_INTERVAL_MS = 2000;
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "browser.storageManager.pressureNotification.minIntervalMS",
        TEST_NOTIFICATION_INTERVAL_MS,
      ],
    ],
  });
  // Commenting this to see if we really need it
  // await SpecialPowers.pushPrefEnv({set: [["privacy.reduceTimerPrecision", false]]});

  await notifyStoragePressure();
  await TestUtils.waitForCondition(() =>
    win.gNotificationBox.getNotificationWithValue(
      "storage-pressure-notification"
    )
  );
  let notification = win.gNotificationBox.getNotificationWithValue(
    "storage-pressure-notification"
  );
  is(
    notification.localName,
    "notification-message",
    "Should display storage pressure notification"
  );
  notification.close();

  await notifyStoragePressure();
  notification = win.gNotificationBox.getNotificationWithValue(
    "storage-pressure-notification"
  );
  is(
    notification,
    null,
    "Should not display storage pressure notification more than once within the given interval"
  );

  await new Promise(resolve =>
    setTimeout(resolve, TEST_NOTIFICATION_INTERVAL_MS + 1)
  );
  await notifyStoragePressure();
  await TestUtils.waitForCondition(() =>
    win.gNotificationBox.getNotificationWithValue(
      "storage-pressure-notification"
    )
  );
  notification = win.gNotificationBox.getNotificationWithValue(
    "storage-pressure-notification"
  );
  is(
    notification.localName,
    "notification-message",
    "Should display storage pressure notification after the given interval"
  );
  notification.close();
});

// Test guiding user to the about:preferences when usage exceeds the given threshold
add_task(async function () {
  const win = Services.wm.getMostRecentWindow("navigator:browser");
  await SpecialPowers.pushPrefEnv({
    set: [["browser.storageManager.pressureNotification.minIntervalMS", 0]],
  });
  let tab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    "https://example.com"
  );

  const BYTES_IN_GIGABYTE = 1073741824;
  const USAGE_THRESHOLD_BYTES =
    BYTES_IN_GIGABYTE *
    Services.prefs.getIntPref(
      "browser.storageManager.pressureNotification.usageThresholdGB"
    );
  await notifyStoragePressure(USAGE_THRESHOLD_BYTES);
  await TestUtils.waitForCondition(() =>
    win.gNotificationBox.getNotificationWithValue(
      "storage-pressure-notification"
    )
  );
  let notification = win.gNotificationBox.getNotificationWithValue(
    "storage-pressure-notification"
  );
  is(
    notification.localName,
    "notification-message",
    "Should display storage pressure notification"
  );
  await new Promise(r => setTimeout(r, 1000));

  let prefBtn = notification.buttonContainer.getElementsByTagName("button")[0];
  ok(prefBtn, "Should have an open preferences button");
  let aboutPrefPromise = openAboutPrefPromise(win);
  EventUtils.synthesizeMouseAtCenter(prefBtn, {}, win);
  await aboutPrefPromise;
  let aboutPrefTab = win.gBrowser.selectedTab;
  let prefDoc = win.gBrowser.selectedBrowser.contentDocument;
  let siteDataGroup = prefDoc.getElementById("siteDataGroup");
  is_element_visible(
    siteDataGroup,
    "Should open to the siteDataGroup section in about:preferences"
  );
  BrowserTestUtils.removeTab(aboutPrefTab);
  BrowserTestUtils.removeTab(tab);
});

// Test not displaying the 2nd notification if one is already being displayed
add_task(async function () {
  const win = Services.wm.getMostRecentWindow("navigator:browser");
  const TEST_NOTIFICATION_INTERVAL_MS = 0;
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "browser.storageManager.pressureNotification.minIntervalMS",
        TEST_NOTIFICATION_INTERVAL_MS,
      ],
    ],
  });

  await notifyStoragePressure();
  await notifyStoragePressure();
  let allNotifications = win.gNotificationBox.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"
  );
  win.gNotificationBox.removeAllNotifications();
});

add_task(async function cleanup() {
  const win = Services.wm.getMostRecentWindow("navigator:browser");
  await BrowserTestUtils.closeWindow(win);
});