OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 const notifications = chrome.notifications; |
| 6 |
| 7 const red_dot = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA" + |
| 8 "AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO" + |
| 9 "9TXL0Y4OHwAAAABJRU5ErkJggg=="; |
| 10 |
| 11 var basicNotificationOptions = { |
| 12 type: "basic", |
| 13 title: "Basic title", |
| 14 message: "Basic message", |
| 15 iconUrl: red_dot |
| 16 }; |
| 17 |
| 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")); |
| 23 return; |
| 24 } |
| 25 resolve(id); |
| 26 return; |
| 27 }); |
| 28 }); |
| 29 }; |
| 30 |
| 31 function update(id, options) { |
| 32 return new Promise(function (resolve, reject) { |
| 33 notifications.update(id, options, function (ok) { |
| 34 if (chrome.runtime.lastError || !ok) { |
| 35 reject(new Error("Unable to update notification")); |
| 36 return; |
| 37 } |
| 38 resolve(ok); |
| 39 return; |
| 40 }); |
| 41 }); |
| 42 } |
| 43 |
| 44 function clear(id) { |
| 45 return new Promise(function (resolve, reject) { |
| 46 notifications.clear(id, function (ok) { |
| 47 if (chrome.runtime.lastError || !ok) { |
| 48 reject(new Error("Unable to clear notification")); |
| 49 return; |
| 50 } |
| 51 resolve(ok); |
| 52 return; |
| 53 }); |
| 54 }); |
| 55 } |
| 56 |
| 57 function getAll() { |
| 58 return new Promise(function (resolve, reject) { |
| 59 notifications.getAll(function (ids) { |
| 60 if (chrome.runtime.lastError) { |
| 61 reject(new Error(chrome.runtime.lastError.message)); |
| 62 return; |
| 63 } |
| 64 |
| 65 if (ids === undefined) { |
| 66 resolve([]); |
| 67 return |
| 68 } |
| 69 |
| 70 var id_list = Object.keys(ids); |
| 71 resolve(id_list); |
| 72 }); |
| 73 }); |
| 74 } |
| 75 |
| 76 function clearAll() { |
| 77 return getAll().then(function (ids) { |
| 78 var idPromises = ids.map(function (id) { return clear(id); }); |
| 79 return Promise.all(idPromises); |
| 80 }); |
| 81 } |
| 82 |
| 83 function succeedTest(testName) { |
| 84 return function () { |
| 85 return clearAll().then( |
| 86 function () { chrome.test.succeed(testName); }, |
| 87 function (error) { |
| 88 console.log("Unknown error in clearAll: " + |
| 89 JSON.stringify(arguments)); |
| 90 }); |
| 91 }; |
| 92 } |
| 93 |
| 94 function failTest(testName) { |
| 95 return function () { |
| 96 return clearAll().then( |
| 97 function () { chrome.test.fail(testName); }, |
| 98 function (error) { |
| 99 console.log("Unknown error in clearAll: " + |
| 100 JSON.stringify(error.message)); |
| 101 }); |
| 102 }; |
| 103 } |
| 104 |
| 105 function testIdUsage() { |
| 106 var testName = "testIdUsage"; |
| 107 console.log("Starting testIdUsage."); |
| 108 var succeed = succeedTest(testName); |
| 109 var fail = failTest(testName); |
| 110 |
| 111 var createNotification = function (idString) { |
| 112 var options = { |
| 113 type: "basic", |
| 114 iconUrl: red_dot, |
| 115 title: "Attention!", |
| 116 message: "Check out Cirque du Soleil" |
| 117 }; |
| 118 |
| 119 return create(idString, options); |
| 120 }; |
| 121 |
| 122 var updateNotification = function (idString) { |
| 123 var options = { title: "!", message: "!" }; |
| 124 return update(idString, options); |
| 125 }; |
| 126 |
| 127 // Should successfully create the notification |
| 128 createNotification("foo") |
| 129 // And update it. |
| 130 .then(updateNotification) |
| 131 .catch(fail) |
| 132 // Next try to update a non-existent notification. |
| 133 .then(function () { return updateNotification("foo2"); }) |
| 134 // And fail if it returns true. |
| 135 .then(fail) |
| 136 // Next try to clear a non-existent notification. |
| 137 .catch(function () { return clear("foo2"); }) |
| 138 .then(fail) |
| 139 // And finally clear the original notification. |
| 140 .catch(function () { return clear("foo"); }) |
| 141 .catch(fail) |
| 142 .then(succeed); |
| 143 }; |
| 144 |
| 145 function testBaseFormat() { |
| 146 var testName = "testBaseFormat"; |
| 147 console.log("Starting " + testName); |
| 148 var succeed = succeedTest(testName); |
| 149 var fail = failTest(testName); |
| 150 |
| 151 var createNotificationWithout = function(toDelete) { |
| 152 var options = { |
| 153 type: "basic", |
| 154 iconUrl: red_dot, |
| 155 title: "Attention!", |
| 156 message: "Check out Cirque du Soleil", |
| 157 contextMessage: "Foobar.", |
| 158 priority: 1, |
| 159 eventTime: 123457896.12389, |
| 160 expandedMessage: "This is a longer expanded message.", |
| 161 isClickable: true |
| 162 }; |
| 163 |
| 164 for (var i = 0; i < toDelete.length; i++) { |
| 165 delete options[toDelete[i]]; |
| 166 } |
| 167 |
| 168 return create("", options); |
| 169 }; |
| 170 |
| 171 // Construct some exclusion lists. The |createNotificationWithout| function |
| 172 // starts with a complex notification and then deletes items in this list. |
| 173 var basicNotification= [ |
| 174 "buttons", |
| 175 "items", |
| 176 "progress", |
| 177 "imageUrl" |
| 178 ]; |
| 179 var bareNotification = basicNotification.concat([ |
| 180 "priority", |
| 181 "eventTime", |
| 182 "expandedMessage", |
| 183 ]); |
| 184 var basicNoType = basicNotification.concat(["type"]); |
| 185 var basicNoIcon = basicNotification.concat(["iconUrl"]); |
| 186 var basicNoTitle = basicNotification.concat(["title"]); |
| 187 var basicNoMessage = basicNotification.concat(["message"]); |
| 188 |
| 189 // Try creating a basic notification with just some of the fields. |
| 190 createNotificationWithout(basicNotification) |
| 191 // Try creating a basic notification with all possible fields. |
| 192 .then(function () { return createNotificationWithout([]); }) |
| 193 // Try creating a basic notification with the minimum in fields. |
| 194 .then(function () { return createNotificationWithout(bareNotification); }) |
| 195 // After this line we are checking to make sure that there is an error |
| 196 // when notifications are created without the proper fields. |
| 197 .catch(fail) |
| 198 // Error if no type. |
| 199 .then(function () { return createNotificationWithout(basicNoType) }) |
| 200 // Error if no icon. |
| 201 .catch(function () { return createNotificationWithout(basicNoIcon) }) |
| 202 // Error if no title. |
| 203 .catch(function () { return createNotificationWithout(basicNoTitle) }) |
| 204 // Error if no message. |
| 205 .catch(function () { return createNotificationWithout(basicNoMessage) }) |
| 206 .then(fail, succeed); |
| 207 }; |
| 208 |
| 209 function testListItem() { |
| 210 var testName = "testListItem"; |
| 211 console.log("Starting " + testName); |
| 212 var succeed = succeedTest(testName); |
| 213 var fail = failTest(testName); |
| 214 |
| 215 var item = { title: "Item title.", message: "Item message." }; |
| 216 var options = { |
| 217 type: "list", |
| 218 iconUrl: red_dot, |
| 219 title: "Attention!", |
| 220 message: "Check out Cirque du Soleil", |
| 221 contextMessage: "Foobar.", |
| 222 priority: 1, |
| 223 eventTime: 123457896.12389, |
| 224 items: [item, item, item, item, item], |
| 225 isClickable: true |
| 226 }; |
| 227 create("id", options).then(succeed, fail); |
| 228 }; |
| 229 |
| 230 function arrayEquals(a, b) { |
| 231 if (a === b) return true; |
| 232 if (a == null || b == null) return false; |
| 233 if (a.length !== b.length) return false; |
| 234 |
| 235 for (var i = 0; i < a.length; i++) { |
| 236 if (a[i] !== b[i]) return false; |
| 237 } |
| 238 return true; |
| 239 }; |
| 240 |
| 241 function testGetAll() { |
| 242 var testName = "testGetAll"; |
| 243 console.log("Starting " + testName); |
| 244 var succeed = succeedTest(testName); |
| 245 var fail = failTest(testName); |
| 246 var in_ids = ["a", "b", "c", "d"]; |
| 247 |
| 248 // First do a get all, make sure the list is empty. |
| 249 getAll() |
| 250 .then(function (ids) { |
| 251 chrome.test.assertEq(0, ids.length); |
| 252 }) |
| 253 // Then create a bunch of notifications. |
| 254 .then(function () { |
| 255 var newNotifications = in_ids.map(function (id) { |
| 256 return create(id, basicNotificationOptions); |
| 257 }); |
| 258 return Promise.all(newNotifications); |
| 259 }) |
| 260 // Try getAll again. |
| 261 .then(function () { return getAll(); }) |
| 262 // Check that the right set of notifications is in the center. |
| 263 .then(function (ids) { |
| 264 chrome.test.assertEq(4, ids.length); |
| 265 chrome.test.assertTrue(arrayEquals(ids, in_ids)); |
| 266 succeed(); |
| 267 }, fail); |
| 268 } |
| 269 |
| 270 function testProgress() { |
| 271 var testName = "testProgress"; |
| 272 console.log("Starting " + testName); |
| 273 var succeed = succeedTest(testName); |
| 274 var fail = failTest(testName); |
| 275 var progressOptions = { |
| 276 type: "progress", |
| 277 title: "Basic title", |
| 278 message: "Basic message", |
| 279 iconUrl: red_dot, |
| 280 progress: 30 |
| 281 }; |
| 282 |
| 283 // First, create a basic progress notification. |
| 284 create("progress", progressOptions) |
| 285 // and update it to have a different progress level. |
| 286 .then(function () { return update("progress", { progress: 60 }); }) |
| 287 // If either of the above failed, the test fails. |
| 288 .catch(fail) |
| 289 // Now the following parts should all cause an error: |
| 290 // First update the progress to a low value, out-of-range |
| 291 .then(function () { return update("progress", { progress: -10 }); }) |
| 292 // First update the progress to a high value, out-of-range |
| 293 .then(fail, function () { return update("progress", { progress: 101 }); }) |
| 294 .then(function () { return clear("progress"); }) |
| 295 // Finally try to create a notification that has a progress value but not |
| 296 // progress type. |
| 297 .then(fail, function () { |
| 298 progressOptions.type = "basic"; |
| 299 return create("progress", progressOptions); |
| 300 }).then(fail, succeed); |
| 301 } |
| 302 |
| 303 chrome.test.runTests([ |
| 304 testIdUsage, testBaseFormat, testListItem, testGetAll, testProgress |
| 305 ]); |
OLD | NEW |