summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_notifications_incognito.js
blob: fda60c3a82ca1d31a914651983e8c63a6c605a6f (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";

const createdAlerts = [];

const mockAlertsService = {
  showPersistentNotification(persistentData, alert, alertListener) {
    this.showAlert(alert, alertListener);
  },

  showAlert(alert, listener) {
    createdAlerts.push(alert);
    listener.observe(null, "alertfinished", alert.cookie);
  },

  showAlertNotification(
    imageUrl,
    title,
    text,
    textClickable,
    cookie,
    alertListener,
    name,
    dir,
    lang,
    data,
    principal,
    privateBrowsing
  ) {
    this.showAlert({ cookie, title, text, privateBrowsing }, alertListener);
  },

  closeAlert(name) {
    // This mock immediately close the alert on show, so this is empty.
  },

  QueryInterface: ChromeUtils.generateQI(["nsIAlertsService"]),

  createInstance(iid) {
    return this.QueryInterface(iid);
  },
};

const registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
registrar.registerFactory(
  Components.ID("{173a036a-d678-4415-9cff-0baff6bfe554}"),
  "alerts service",
  ALERTS_SERVICE_CONTRACT_ID,
  mockAlertsService
);

add_task(async function test_notification_privateBrowsing_flag() {
  let extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      permissions: ["notifications"],
    },
    files: {
      "page.html": `<meta charset="utf-8"><script src="page.js"></script>`,
      async "page.js"() {
        let closedPromise = new Promise(resolve => {
          browser.notifications.onClosed.addListener(resolve);
        });
        let createdId = await browser.notifications.create("notifid", {
          type: "basic",
          title: "titl",
          message: "msg",
        });
        let closedId = await closedPromise;
        browser.test.assertEq(createdId, closedId, "ID of closed notification");
        browser.test.assertEq(
          "{}",
          JSON.stringify(await browser.notifications.getAll()),
          "no notifications left"
        );
        browser.test.sendMessage("notification_closed");
      },
    },
  });
  await extension.startup();

  async function checkPrivateBrowsingFlag(privateBrowsing) {
    let contentPage = await ExtensionTestUtils.loadContentPage(
      `moz-extension://${extension.uuid}/page.html`,
      { extension, remote: extension.extension.remote, privateBrowsing }
    );
    await extension.awaitMessage("notification_closed");
    await contentPage.close();

    Assert.equal(createdAlerts.length, 1, "expected one alert");
    let notification = createdAlerts.shift();
    Assert.equal(notification.cookie, "notifid", "notification id");
    Assert.equal(notification.title, "titl", "notification title");
    Assert.equal(notification.text, "msg", "notification text");
    Assert.equal(notification.privateBrowsing, privateBrowsing, "pbm flag");
  }

  await checkPrivateBrowsingFlag(false);
  await checkPrivateBrowsingFlag(true);

  await extension.unload();
});