Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 const notificationProvider = chrome.notificationProvider; | 5 const notificationProvider = chrome.notificationProvider; |
| 6 | 6 |
| 7 function createNotification(notificationId, options) { | |
| 8 return new Promise(function (resolve, reject) { | |
| 9 chrome.notifications.create(notificationId, options, function (id) { | |
| 10 if (chrome.runtime.lastError) { | |
| 11 reject(new Error("Unable to create notification")); | |
| 12 return; | |
| 13 } | |
| 14 resolve(id); | |
| 15 return; | |
| 16 }); | |
| 17 }); | |
| 18 }; | |
| 19 | |
| 7 function notifyOnCleared(senderId, notificationId) { | 20 function notifyOnCleared(senderId, notificationId) { |
| 8 return new Promise(function (resolve, reject) { | 21 return new Promise(function (resolve, reject) { |
| 9 notificationProvider.notifyOnCleared(senderId, | 22 notificationProvider.notifyOnCleared(senderId, |
| 10 notificationId, | 23 notificationId, |
| 11 function (wasCleared) { | 24 function (wasCleared) { |
| 12 if (chrome.runtime.lastError || !wasCleared) { | 25 if (chrome.runtime.lastError || !wasCleared) { |
| 13 reject(new Error("Unable to send onClear message")); | 26 reject(new Error("Unable to send onClear message")); |
| 14 return; | 27 return; |
| 15 } | 28 } |
| 16 resolve(wasCleared); | 29 resolve(wasCleared); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 77 resolve(notifierExists); | 90 resolve(notifierExists); |
| 78 return; | 91 return; |
| 79 }); | 92 }); |
| 80 }); | 93 }); |
| 81 }; | 94 }; |
| 82 | 95 |
| 83 function failTest(testName) { | 96 function failTest(testName) { |
| 84 chrome.test.fail(testName); | 97 chrome.test.fail(testName); |
| 85 }; | 98 }; |
| 86 | 99 |
| 100 function listenForShowSettings() {} | |
| 101 | |
| 87 function testFunctions() { | 102 function testFunctions() { |
| 88 var testName = "testFunctions"; | 103 var myId = chrome.runtime.id; |
| 89 var notifierId; | 104 var id1 = "id1"; |
| 90 var notId; | 105 var content = { |
| 91 notificationProvider.onCreated.addListener(function(senderId, | 106 type: "basic", |
| 92 notificationId, | 107 iconUrl: "icon.png", |
| 93 content) { | 108 title: "Title", |
| 94 notifierId = senderId; | 109 message: "This is the message." |
| 95 notId = notificationId; | 110 }; |
| 96 | 111 |
| 97 notifyOnClicked(notifierId, notId) | 112 // Add listener to onShowSettings to show that this extension has advanced |
| 98 .catch(function() { failTest("NotifyOnClickedFailed"); }) | 113 // settings. |
| 99 .then(function() { return notifyOnButtonClicked(notifierId, notId, 0); }) | 114 chrome.notifications.onShowSettings.addListener(listenForShowSettings); |
| 100 .catch(function() { failTest("NotifyOnButtonClickedFailed"); }) | 115 |
| 101 .then(function () { return notifyOnCleared(notifierId, notId); }) | 116 createNotification(id1, content) |
|
Pete Williamson
2014/08/11 17:53:50
You probably don't need all these catch statements
liyanhou
2014/08/11 18:24:06
I was doing that for more detailed error messages
Pete Williamson
2014/08/11 18:31:49
OK, in that case these are fine. I just wanted to
| |
| 102 .catch(function() { failTest("NotifyOnClearedFailed"); }) | 117 .catch(function() { failTest("notifications.create"); }) |
| 103 .then(function () { return notifyOnPermissionLevelChanged(notifierId, | 118 .then(function() { return notifyOnClicked(myId, id1); }) |
| 104 "granted"); }) | 119 .catch(function() { failTest("NotifyOnClickedFailed"); }) |
| 105 .catch(function() { failTest("NotifyOnPermissionLevelChangedFailed"); }) | 120 .then(function() { return notifyOnButtonClicked(myId, id1, 0); }) |
| 106 .then(function () { return notifyOnShowSettings(notifierId); }) | 121 .catch(function() { failTest("NotifyOnButtonClickedFailed"); }) |
| 107 .catch(function() { failTest("NotifyOnShowSettingsFailed"); }) | 122 .then(function () { return notifyOnCleared(myId, id1); }) |
| 108 .then(function() { chrome.test.succeed(testName); }); | 123 .catch(function() { failTest("NotifyOnClearedFailed"); }) |
| 109 }); | 124 // Notify the sender that the permission level is changed by the user |
| 125 .then(function () { return notifyOnPermissionLevelChanged(myId, | |
| 126 "granted"); }) | |
| 127 .catch(function() { failTest("NotifyOnPermissionLevelChangedFailed"); }) | |
| 128 .then(function () { return notifyOnShowSettings(myId); }) | |
| 129 .catch(function() { failTest("NotifyOnShowSettingsFailed1"); }) | |
| 130 // Try to notify a non existent sender that a user checked its settings | |
| 131 .then(function () { return notifyOnShowSettings("DoesNotExist"); }) | |
| 132 // Fail if it returns true | |
| 133 .then(function() { failTest("NotifyOnShowSettingsFailed2"); }) | |
| 134 .catch(function() { chrome.test.succeed(); }) | |
| 110 }; | 135 }; |
| 111 | 136 |
| 112 chrome.test.runTests([ testFunctions ]); | 137 chrome.test.runTests([ testFunctions ]); |
| OLD | NEW |