summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
blob: d1b798cf964b52cfb8f4f6c79d11f85860f3deac (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for notifications</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

// A 1x1 PNG image.
// Source: https://commons.wikimedia.org/wiki/File:1x1.png (Public Domain)
let image = atob("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
                 "ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=");
const IMAGE_ARRAYBUFFER = Uint8Array.from(image, byte => byte.charCodeAt(0)).buffer;

add_task(function* test_notification() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    let id = await browser.notifications.create(opts);

    browser.test.sendMessage("running", id);
    browser.test.notifyPass("background test passed");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  yield extension.startup();
  let x = yield extension.awaitMessage("running");
  is(x, "0", "got correct id from notifications.create");
  yield extension.awaitFinish();
  yield extension.unload();
});

add_task(function* test_notification_events() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    // Test an ignored listener.
    browser.notifications.onButtonClicked.addListener(function() {});

    // We cannot test onClicked listener without a mock
    // but we can attempt to add a listener.
    browser.notifications.onClicked.addListener(function() {});

    // Test onClosed listener.
    browser.notifications.onClosed.addListener(id => {
      browser.test.sendMessage("closed", id);
      browser.test.notifyPass("background test passed");
    });

    await browser.notifications.create("5", opts);
    let id = await browser.notifications.create("5", opts);
    browser.test.sendMessage("running", id);
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  yield extension.startup();
  let x = yield extension.awaitMessage("closed");
  is(x, "5", "got correct id from onClosed listener");
  x = yield extension.awaitMessage("running");
  is(x, "5", "got correct id from notifications.create");
  yield extension.awaitFinish();
  yield extension.unload();
});

add_task(function* test_notification_clear() {
  async function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
    };

    browser.notifications.onClosed.addListener(id => {
      browser.test.sendMessage("closed", id);
    });

    let id = await browser.notifications.create("99", opts);

    let wasCleared = await browser.notifications.clear(id);
    browser.test.sendMessage("cleared", wasCleared);

    browser.test.notifyPass("background test passed");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  yield extension.startup();
  let x = yield extension.awaitMessage("closed");
  is(x, "99", "got correct id from onClosed listener");
  x = yield extension.awaitMessage("cleared");
  is(x, true, "got correct boolean from notifications.clear");
  yield extension.awaitFinish();
  yield extension.unload();
});

add_task(function* test_notifications_empty_getAll() {
  async function background() {
    let notifications = await browser.notifications.getAll();

    browser.test.assertEq("object", typeof notifications, "getAll() returned an object");
    browser.test.assertEq(0, Object.keys(notifications).length, "the object has no properties");
    browser.test.notifyPass("getAll empty");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  yield extension.startup();
  yield extension.awaitFinish("getAll empty");
  yield extension.unload();
});

add_task(function* test_notifications_populated_getAll() {
  async function background() {
    let opts = {
      type: "basic",
      iconUrl: "a.png",
      title: "Testing Notification",
      message: "Carry on",
    };

    await browser.notifications.create("p1", opts);
    await browser.notifications.create("p2", opts);
    let notifications = await browser.notifications.getAll();

    browser.test.assertEq("object", typeof notifications, "getAll() returned an object");
    browser.test.assertEq(2, Object.keys(notifications).length, "the object has 2 properties");

    for (let notificationId of ["p1", "p2"]) {
      for (let key of Object.keys(opts)) {
        browser.test.assertEq(
          opts[key],
          notifications[notificationId][key],
          `the notification has the expected value for option: ${key}`
        );
      }
    }

    browser.test.notifyPass("getAll populated");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
    files: {
      "a.png": IMAGE_ARRAYBUFFER,
    },
  });
  yield extension.startup();
  yield extension.awaitFinish("getAll populated");
  yield extension.unload();
});

add_task(function* test_buttons_unsupported() {
  function background() {
    let opts = {
      type: "basic",
      title: "Testing Notification",
      message: "Carry on",
      buttons: [{title: "Button title"}],
    };

    let exception = {};
    try {
      browser.notifications.create(opts);
    } catch (e) {
      exception = e;
    }

    browser.test.assertTrue(
      String(exception).includes('Property "buttons" is unsupported by Firefox'),
      "notifications.create with buttons option threw an expected exception"
    );
    browser.test.notifyPass("buttons-unsupported");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["notifications"],
    },
    background,
  });
  yield extension.startup();
  yield extension.awaitFinish("buttons-unsupported");
  yield extension.unload();
});

</script>

</body>
</html>