tor-browser

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

simulate-click-handler.sub.https.window.js (3737B)


      1 // META: script=/notifications/resources/helpers.js
      2 
      3 const origin = "https://{{hosts[][]}}:{{ports[https][0]}}";
      4 const data = {
      5  options: {
      6    action: "default",
      7    close: true,
      8    notificationCloseEvent: false,
      9    url: "https://tests.peter.sh/notification-generator/#"
     10  }
     11 };
     12 const storageEntry = {
     13  id: "foo",
     14  title: "bar",
     15  dir: "rtl",
     16  body: "baz",
     17  tag: "basil",
     18  icon: "https://example.com/",
     19  requireInteraction: false,
     20  silent: true,
     21 
     22  // corresponding to `data` above
     23  dataSerialized: "AgAAAAAA8f8AAAAACAD//wcAAIAEAP//b3B0aW9ucwAAAAAACAD//wYAAIAEAP//YWN0aW9uAAAHAACABAD//2RlZmF1bHQABQAAgAQA//9jbG9zZQAAAAEAAAACAP//FgAAgAQA//9ub3RpZmljYXRpb25DbG9zZUV2ZW50AAAAAAAAAgD//wMAAIAEAP//dXJsAAAAAAAvAACABAD//2h0dHBzOi8vdGVzdHMucGV0ZXIuc2gvbm90aWZpY2F0aW9uLWdlbmVyYXRvci8jAAAAAAATAP//AAAAABMA//8=",
     24  actions: [{ name: "basilisk", title: "obelisk" }],
     25  serviceWorkerRegistrationScope: `${origin}/_mozilla/notifications/`
     26 };
     27 
     28 promise_setup(async () => {
     29  await prepareActiveServiceWorker("simulate-click-handler-sw.js");
     30 })
     31 
     32 /**
     33 * @param {object} options
     34 * @param {boolean} options.autoClosed
     35 */
     36 async function simulateClickingExistingNotification(t, { autoClosed }) {
     37  await SpecialPowers.spawnChrome([origin, storageEntry, autoClosed], async (origin, storageEntry, autoClosed) => {
     38    // Simulate an existing notification
     39    const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
     40    svc.put(origin, storageEntry, storageEntry.serviceWorkerRegistrationScope);
     41 
     42    const uri = Services.io.newURI(origin);
     43    const principal = Services.scriptSecurityManager.createContentPrincipal(uri, {});
     44 
     45    // Now simulate a click
     46    const handler = Cc["@mozilla.org/notification-handler;1"].getService(Ci.nsINotificationHandler);
     47    handler.respondOnClick(principal, storageEntry.id, "basilisk", autoClosed);
     48  });
     49 
     50  t.add_cleanup(async () => {
     51    await SpecialPowers.spawnChrome([origin, storageEntry.id], async (origin, id) => {
     52      const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
     53      return svc.delete(origin, id);
     54    });
     55  })
     56 }
     57 
     58 async function getFromDB() {
     59  return await SpecialPowers.spawnChrome([origin, storageEntry.id], async (origin, id) => {
     60    const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
     61    return svc.getById(origin, id);
     62  });
     63 }
     64 
     65 promise_test(async (t) => {
     66  const promise = new Promise(r => {
     67    navigator.serviceWorker.addEventListener("message", r, { once: true })
     68  });
     69 
     70  await simulateClickingExistingNotification(t, { autoClosed: false });
     71 
     72  /** @type {NotificationEvent} */
     73  const { data: { notification, action } } = await promise;
     74 
     75  assert_equals(notification.title, storageEntry.title);
     76  assert_equals(notification.dir, storageEntry.dir);
     77  assert_equals(notification.body, storageEntry.body);
     78  assert_equals(notification.tag, storageEntry.tag);
     79  assert_equals(notification.icon, storageEntry.icon);
     80  assert_object_equals(notification.actions, [{ action: "basilisk", title: "obelisk" }]);
     81  assert_object_equals(notification.data, data);
     82  assert_equals(action, "basilisk");
     83 
     84  const entry = await getFromDB();
     85  assert_true(!!entry, "The entry should still be there");
     86 }, "Fire notificationclick via NotificationHandler autoClosed: false");
     87 
     88 promise_test(async (t) => {
     89  const promise = new Promise(r => {
     90    navigator.serviceWorker.addEventListener("message", r, { once: true })
     91  });
     92 
     93  await simulateClickingExistingNotification(t, { autoClosed: true });
     94 
     95  await promise;
     96 
     97  const entry = await getFromDB();
     98  assert_true(!entry, "The entry should not be there");
     99 }, "Fire notificationclick via NotificationHandler with autoClosed: true");