1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 const notifications = chrome.notifications;
7 function arrayEquals(a, b) {
8 if (a === b) return true;
9 if (a == null || b == null) return false;
10 if (a.length !== b.length) return false;
12 for (var i = 0; i < a.length; i++) {
13 if (a[i] !== b[i]) return false;
18 function create(id, options) {
19 return new Promise(function (resolve, reject) {
20 notifications.create(id, options, function (id) {
21 if (chrome.runtime.lastError) {
22 reject(new Error("Unable to create notification"));
25 console.log("Created with id: " + id);
32 function update(id, options) {
33 return new Promise(function (resolve, reject) {
34 notifications.update(id, options, function (ok) {
35 if (chrome.runtime.lastError || !ok) {
36 reject(new Error("Unable to update notification"));
39 console.log("Updated id: ", id);
47 return new Promise(function (resolve, reject) {
48 notifications.clear(id, function (ok) {
49 if (chrome.runtime.lastError || !ok) {
50 reject(new Error("Unable to clear notification"));
60 return new Promise(function (resolve, reject) {
61 notifications.getAll(function (ids) {
62 if (chrome.runtime.lastError) {
63 reject(new Error(chrome.runtime.lastError.message));
67 if (ids === undefined) {
72 var id_list = Object.keys(ids);
79 return getAll().then(function (ids) {
80 var idPromises = ids.map(function (id) { return clear(id); });
81 return Promise.all(idPromises);
85 function succeedTest(testName) {
87 return clearAll().then(
88 function () { chrome.test.succeed(testName); },
90 console.log("Unknown error in clearAll: " +
91 JSON.stringify(arguments));
96 function failTest(testName) {
98 return clearAll().then(
99 function () { chrome.test.fail(testName); },
101 console.log("Unknown error in clearAll: " +
102 JSON.stringify(error.message));
107 function testPartialUpdate() {
108 var testName = "testPartialUpdate";
109 console.log("Starting " + testName);
110 var succeed = succeedTest(testName);
111 var fail = failTest(testName);
113 const red_dot = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" +
114 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" +
115 "9TXL0Y4OHwAAAABJRU5ErkJggg==";
117 var basicNotificationOptions = {
119 title: "Basic title",
120 message: "Basic message",
122 buttons: [{title: "Button"}]
125 // Create a notification.
126 create("testId", basicNotificationOptions)
127 // Then update a few items
129 return update("testId", {
131 message: "Too late! The show ended yesterday"
134 // Then update a few more items
136 return update("testId", {priority: 2, buttons: [{title: "NewButton"}]});
138 // The test will continue in C++, checking that all the updates "took"
139 .then(chrome.test.succeed, chrome.test.fail);
143 chrome.test.runTests([testPartialUpdate]);