File: simulate-click-handler.sub.https.window.js

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (100 lines) | stat: -rw-r--r-- 3,737 bytes parent folder | download | duplicates (4)
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
// META: script=/notifications/resources/helpers.js

const origin = "https://{{hosts[][]}}:{{ports[https][0]}}";
const data = {
  options: {
    action: "default",
    close: true,
    notificationCloseEvent: false,
    url: "https://tests.peter.sh/notification-generator/#"
  }
};
const storageEntry = {
  id: "foo",
  title: "bar",
  dir: "rtl",
  body: "baz",
  tag: "basil",
  icon: "https://example.com/",
  requireInteraction: false,
  silent: true,

  // corresponding to `data` above
  dataSerialized: "AgAAAAAA8f8AAAAACAD//wcAAIAEAP//b3B0aW9ucwAAAAAACAD//wYAAIAEAP//YWN0aW9uAAAHAACABAD//2RlZmF1bHQABQAAgAQA//9jbG9zZQAAAAEAAAACAP//FgAAgAQA//9ub3RpZmljYXRpb25DbG9zZUV2ZW50AAAAAAAAAgD//wMAAIAEAP//dXJsAAAAAAAvAACABAD//2h0dHBzOi8vdGVzdHMucGV0ZXIuc2gvbm90aWZpY2F0aW9uLWdlbmVyYXRvci8jAAAAAAATAP//AAAAABMA//8=",
  actions: [{ name: "basilisk", title: "obelisk" }],
  serviceWorkerRegistrationScope: `${origin}/_mozilla/notifications/`
};

promise_setup(async () => {
  await prepareActiveServiceWorker("simulate-click-handler-sw.js");
})

/**
 * @param {object} options
 * @param {boolean} options.autoClosed
 */
async function simulateClickingExistingNotification(t, { autoClosed }) {
  await SpecialPowers.spawnChrome([origin, storageEntry, autoClosed], async (origin, storageEntry, autoClosed) => {
    // Simulate an existing notification
    const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
    svc.put(origin, storageEntry, storageEntry.serviceWorkerRegistrationScope);

    const uri = Services.io.newURI(origin);
    const principal = Services.scriptSecurityManager.createContentPrincipal(uri, {});

    // Now simulate a click
    const handler = Cc["@mozilla.org/notification-handler;1"].getService(Ci.nsINotificationHandler);
    handler.respondOnClick(principal, storageEntry.id, "basilisk", autoClosed);
  });

  t.add_cleanup(async () => {
    await SpecialPowers.spawnChrome([origin, storageEntry.id], async (origin, id) => {
      const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
      return svc.delete(origin, id);
    });
  })
}

async function getFromDB() {
  return await SpecialPowers.spawnChrome([origin, storageEntry.id], async (origin, id) => {
    const svc = Cc["@mozilla.org/notificationStorage;1"].getService(Ci.nsINotificationStorage);
    return svc.getById(origin, id);
  });
}

promise_test(async (t) => {
  const promise = new Promise(r => {
    navigator.serviceWorker.addEventListener("message", r, { once: true })
  });

  await simulateClickingExistingNotification(t, { autoClosed: false });

  /** @type {NotificationEvent} */
  const { data: { notification, action } } = await promise;

  assert_equals(notification.title, storageEntry.title);
  assert_equals(notification.dir, storageEntry.dir);
  assert_equals(notification.body, storageEntry.body);
  assert_equals(notification.tag, storageEntry.tag);
  assert_equals(notification.icon, storageEntry.icon);
  assert_object_equals(notification.actions, [{ action: "basilisk", title: "obelisk" }]);
  assert_object_equals(notification.data, data);
  assert_equals(action, "basilisk");

  const entry = await getFromDB();
  assert_true(!!entry, "The entry should still be there");
}, "Fire notificationclick via NotificationHandler autoClosed: false");

promise_test(async (t) => {
  const promise = new Promise(r => {
    navigator.serviceWorker.addEventListener("message", r, { once: true })
  });

  await simulateClickingExistingNotification(t, { autoClosed: true });

  await promise;

  const entry = await getFromDB();
  assert_true(!entry, "The entry should not be there");
}, "Fire notificationclick via NotificationHandler with autoClosed: true");