gecko-dev/toolkit/components/extensions/test/mochitest/test_ext_notifications.html
Rob Wu 73811a0f67 Bug 1298716 - Unify use of ExtensionTestUtils.loadExtension r=billm
MozReview-Commit-ID: DT6rx6KYODl

--HG--
extra : rebase_source : b37924fe255993006fc9a7dfa115d0527d6d0e10
2016-08-28 17:22:03 -07:00

226 lines
6.4 KiB
HTML

<!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() {
function background() {
let opts = {
type: "basic",
title: "Testing Notification",
message: "Carry on",
};
browser.notifications.create(opts).then(id => {
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() {
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");
});
browser.notifications.create("5", opts).then(id => {
return browser.notifications.create("5", opts);
}).then(id => {
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() {
function background() {
let opts = {
type: "basic",
title: "Testing Notification",
message: "Carry on",
};
browser.notifications.onClosed.addListener(id => {
browser.test.sendMessage("closed", id);
});
browser.notifications.create("99", opts).then(id => {
return browser.notifications.clear(id);
}).then(wasCleared => {
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() {
function background() {
browser.notifications.getAll().then(notifications => {
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() {
function background() {
let opts = {
type: "basic",
iconUrl: "a.png",
title: "Testing Notification",
message: "Carry on",
};
browser.notifications.create("p1", opts).then(() => {
return browser.notifications.create("p2", opts);
}).then(() => {
return browser.notifications.getAll();
}).then(notifications => {
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>