tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_notification_cleanup.html (2796B)


      1 <!DOCTYPE html>
      2 <script src="/tests/SimpleTest/SimpleTest.js"></script>
      3 <script src="/tests/dom/serviceworkers/test/utils.js"></script>
      4 <script src="MockAlertsService.js"></script>
      5 <script src="NotificationTest.js"></script>
      6 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
      7 <script>
      8  add_task(async function setup_db() {
      9    await SpecialPowers.spawnChrome([location.href], async (url) => {
     10      let { db } = ChromeUtils.importESModule(
     11        "moz-src:///dom/notification/NotificationDB.sys.mjs"
     12      );
     13 
     14      const origin = new URL(url).origin;
     15      const scope = new URL(".", url).href;
     16      await db.load();
     17      await db.taskSave({
     18        origin,
     19        notification: {
     20          id: "foo",
     21          title: "foo",
     22          actions: [],
     23          serviceWorkerRegistrationScope: scope,
     24        },
     25      });
     26      await db.taskSave({
     27        origin,
     28        notification: {
     29          id: "bar",
     30          title: "bar",
     31          actions: [],
     32          serviceWorkerRegistrationScope: scope,
     33        },
     34      });
     35      await db.taskSave({
     36        origin,
     37        notification: {
     38          id: "baz",
     39          title: "baz",
     40          actions: [],
     41          serviceWorkerRegistrationScope: scope,
     42        },
     43      });
     44    });
     45  });
     46 
     47  let registration;
     48  add_setup(async () => {
     49    // Force storage cleanup, otherwise the cleanup won't happen as other tests
     50    // may already have made notification requests
     51    await SpecialPowers.pushPrefEnv({
     52      set: [["dom.webnotifications.testing.force_storage_cleanup.enabled", true]]
     53    });
     54    /** @type {ServiceWorkerRegistration} */
     55    registration = await setupServiceWorker("notification_empty_sw.js", ".")
     56  })
     57 
     58  add_task(async function test_notification_cleanup() {
     59    // Now pretend that bar and baz is still alive in the notification backend
     60    await MockAlertsService.setHistory(["bar", "baz"]);
     61 
     62    // First use of PNotification
     63    await registration.showNotification("hello");
     64 
     65    // bar and baz should be kept, foo should be gone, hello should be added
     66    const notifications = await registration.getNotifications();
     67    is(notifications.length, 3, "Should get three notifications")
     68    isDeeply(notifications.map(n => n.title).sort(), ["bar", "baz", "hello"], "Should get three different notifications");
     69  });
     70 
     71  add_task(async function test_notification_cleanup_nosupport() {
     72    // Pretend the system doesn't support notification history retrieval
     73    await MockAlertsService.setThrowHistory(true);
     74 
     75    // First use of PNotification
     76    await registration.showNotification("doom");
     77 
     78    // everything existing should go away
     79    const notifications = await registration.getNotifications();
     80    isDeeply(notifications.map(n => n.title).sort(), ["doom"], "Should get one notification");
     81  });
     82 </script>