Bug 1352711 - Add a notifications.onShown event and use it to fix an intermittent in test_ext_notifications.html, r?mixedpuppy
MozReview-Commit-ID: GC9poUR4jnu
--- a/toolkit/components/extensions/ext-notifications.js
+++ b/toolkit/components/extensions/ext-notifications.js
@@ -57,21 +57,26 @@ Notification.prototype = {
notifications.delete(this.id);
};
// Don't try to emit events if the extension has been unloaded
if (!notifications) {
return;
}
- if (topic === "alertclickcallback") {
- emitAndDelete("clicked");
- }
- if (topic === "alertfinished") {
- emitAndDelete("closed");
+ switch (topic) {
+ case "alertclickcallback":
+ emitAndDelete("clicked");
+ break;
+ case "alertfinished":
+ emitAndDelete("closed");
+ break;
+ case "alertshow":
+ notifications.emit("shown", data);
+ break;
}
},
};
this.notifications = class extends ExtensionAPI {
constructor(extension) {
super(extension);
@@ -151,14 +156,25 @@ this.notifications = class extends Exten
};
notificationsMap.get(extension).on("clicked", listener);
return () => {
notificationsMap.get(extension).off("clicked", listener);
};
}).api(),
+ onShown: new EventManager(context, "notifications.onShown", fire => {
+ let listener = (event, notificationId) => {
+ fire.async(notificationId, true);
+ };
+
+ notificationsMap.get(extension).on("shown", listener);
+ return () => {
+ notificationsMap.get(extension).off("shown", listener);
+ };
+ }).api(),
+
// Intend to implement this later: https://bugzilla.mozilla.org/show_bug.cgi?id=1190681
onButtonClicked: ignoreEvent(context, "notifications.onButtonClicked"),
},
};
}
};
--- a/toolkit/components/extensions/schemas/notifications.json
+++ b/toolkit/components/extensions/schemas/notifications.json
@@ -406,12 +406,24 @@
},
{
"name": "onShowSettings",
"unsupported": true,
"type": "function",
"description": "Fired when the user clicked on a link for the app's notification settings.",
"parameters": [
]
+ },
+ {
+ "name": "onShown",
+ "type": "function",
+ "description": "Fired when the notification is shown.",
+ "parameters": [
+ {
+ "type": "string",
+ "name": "notificationId",
+ "description": "The notificationId of the shown notification."
+ }
+ ]
}
]
}
]
--- a/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
@@ -82,46 +82,46 @@ add_task(async function test_notificatio
is(x, "5", "got correct id from onClosed listener");
x = await extension.awaitMessage("running");
is(x, "5", "got correct id from notifications.create");
await extension.awaitFinish();
await extension.unload();
});
add_task(async function test_notification_clear() {
- async function background() {
+ function background() {
let opts = {
type: "basic",
title: "Testing Notification",
message: "Carry on",
};
- browser.notifications.onClosed.addListener(id => {
- browser.test.sendMessage("closed", id);
+ let createdId = "99";
+
+ browser.notifications.onShown.addListener(async id => {
+ browser.test.assertEq(createdId, id, "onShown received the expected id.");
+ let wasCleared = await browser.notifications.clear(id);
+ browser.test.assertTrue(wasCleared, "notifications.clear returned true.");
});
- let id = await browser.notifications.create("99", opts);
+ browser.notifications.onClosed.addListener(id => {
+ browser.test.assertEq(createdId, id, "onClosed received the expected id.");
+ browser.test.notifyPass("background test passed");
+ });
- let wasCleared = await browser.notifications.clear(id);
- browser.test.sendMessage("cleared", wasCleared);
-
- browser.test.notifyPass("background test passed");
+ browser.notifications.create(createdId, opts);
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["notifications"],
},
background,
});
await extension.startup();
- let x = await extension.awaitMessage("closed");
- is(x, "99", "got correct id from onClosed listener");
- x = await extension.awaitMessage("cleared");
- is(x, true, "got correct boolean from notifications.clear");
await extension.awaitFinish();
await extension.unload();
});
add_task(async function test_notifications_empty_getAll() {
async function background() {
let notifications = await browser.notifications.getAll();