| 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 var myId = chrome.runtime.id; |
| 8 var id1 = "id1"; |
| 9 var returnId = myId + "-" + id1; |
| 10 var content = { |
| 11 type: "basic", |
| 12 iconUrl: "icon.png", |
| 13 title: "Title", |
| 14 message: "This is the message." |
| 15 }; |
| 16 |
| 7 function createNotification(notificationId, options) { | 17 function createNotification(notificationId, options) { |
| 8 return new Promise(function (resolve, reject) { | 18 return new Promise(function (resolve, reject) { |
| 9 chrome.notifications.create(notificationId, options, function (id) { | 19 chrome.notifications.create(notificationId, options, function (id) { |
| 10 if (chrome.runtime.lastError) { | 20 if (chrome.runtime.lastError) { |
| 11 reject(new Error("Unable to create notification")); | 21 reject(new Error("Unable to create notification")); |
| 12 return; | 22 return; |
| 13 } | 23 } |
| 14 resolve(id); | 24 resolve(id); |
| 15 return; | 25 return; |
| 16 }); | 26 }); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 if (chrome.runtime.lastError || !matchExists) { | 66 if (chrome.runtime.lastError || !matchExists) { |
| 57 reject(new Error("Unable to send onButtonClick message")); | 67 reject(new Error("Unable to send onButtonClick message")); |
| 58 return; | 68 return; |
| 59 } | 69 } |
| 60 resolve(matchExists); | 70 resolve(matchExists); |
| 61 return; | 71 return; |
| 62 }); | 72 }); |
| 63 }); | 73 }); |
| 64 }; | 74 }; |
| 65 | 75 |
| 66 function notifyOnPermissionLevelChanged(senderId, permissionLevel) { | 76 function notifyOnPermissionLevelChanged(senderId, |
| 77 notifierType, |
| 78 permissionLevel) { |
| 67 return new Promise(function (resolve, reject) { | 79 return new Promise(function (resolve, reject) { |
| 68 notificationProvider.notifyOnPermissionLevelChanged( | 80 notificationProvider.notifyOnPermissionLevelChanged( |
| 69 senderId, | 81 senderId, |
| 70 permissionLevel, | 82 notifierType, |
| 71 function (notifierExists) { | 83 permissionLevel, |
| 72 if (chrome.runtime.lastError || !notifierExists) { | 84 function (wasChanged) { |
| 85 if (chrome.runtime.lastError || !wasChanged) { |
| 73 reject(new Error("Unable to send onPermissionLevelChanged message")); | 86 reject(new Error("Unable to send onPermissionLevelChanged message")); |
| 74 return; | 87 return; |
| 75 } | 88 } |
| 76 resolve(notifierExists); | 89 resolve(wasChanged); |
| 77 return; | 90 return; |
| 78 }); | 91 }); |
| 79 }); | 92 }); |
| 80 }; | 93 }; |
| 81 | 94 |
| 82 function notifyOnShowSettings(senderId) { | 95 function notifyOnShowSettings(senderId, notifierType) { |
| 83 return new Promise(function (resolve, reject) { | 96 return new Promise(function (resolve, reject) { |
| 84 notificationProvider.notifyOnShowSettings(senderId, | 97 notificationProvider.notifyOnShowSettings(senderId, |
| 85 function (notifierExists) { | 98 notifierType, |
| 86 if (chrome.runtime.lastError || !notifierExists) { | 99 function (hasSettings) { |
| 100 if (chrome.runtime.lastError || !hasSettings) { |
| 87 reject(new Error("Unable to send onShowSettings message")); | 101 reject(new Error("Unable to send onShowSettings message")); |
| 88 return; | 102 return; |
| 89 } | 103 } |
| 90 resolve(notifierExists); | 104 resolve(hasSettings); |
| 91 return; | 105 return; |
| 92 }); | 106 }); |
| 93 }); | 107 }); |
| 94 }; | 108 }; |
| 95 | 109 |
| 96 function failTest(testName) { | 110 function failTest(testName) { |
| 97 chrome.test.fail(testName); | 111 chrome.test.fail(testName); |
| 98 }; | 112 }; |
| 99 | 113 |
| 100 function testFunctions() { | 114 function testNotifyOnClicked(){ |
| 101 var myId = chrome.runtime.id; | 115 chrome.notifications.onClicked.addListener(function(id) { |
| 102 var id1 = "id1"; | 116 chrome.test.succeed(); |
| 103 var returnId = myId + "-" + id1; | 117 }); |
| 104 var content = { | |
| 105 type: "basic", | |
| 106 iconUrl: "icon.png", | |
| 107 title: "Title", | |
| 108 message: "This is the message." | |
| 109 }; | |
| 110 | 118 |
| 111 // Create a notification, so there will be one existing notification | 119 // Create a notification, so there will be one existing notification. |
| 112 createNotification(id1, content) | 120 createNotification(id1, content) |
| 113 .catch(function() { failTest("notifications.create"); }) | 121 .catch(function() { failTest("notifications.create"); }) |
| 114 // Notify the sender that a notificaion was clicked. | 122 // Try to notify that an non-existent notification was clicked. |
| 115 .then(function() { return notifyOnClicked(myId, returnId); }) | 123 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) |
| 116 .catch(function() { failTest("NotifyOnClicked1"); }) | 124 // Fail if it returns true. |
| 117 // Try to notify that an non-existent notification was clicked. | 125 .then(function() { failTest("NotifyOnClicked"); }) |
| 118 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) | 126 // Notify the sender that a notificaion was clicked. |
| 119 // Fail if it returns true. | 127 .catch(function() { return notifyOnClicked(myId, returnId); }) |
| 120 .then(function() { failTest("NotifyOnClicked2"); }) | 128 .catch(function() { failTest("NotifyOnClicked"); }); |
| 121 // Notify the sender that a notificaion button was clicked. | 129 } |
| 122 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) | |
| 123 .catch(function() { failTest("NotifyOnButtonClicked"); }) | |
| 124 // Try to notify that a non-existent notification button was clicked. | |
| 125 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) | |
| 126 .then(function() { failTest("NotifyOnButtonClicked"); }) | |
| 127 // Try to notify that an non-existent notification was cleared. | |
| 128 .catch(function () { return notifyOnCleared(myId, "doesNotExist"); }) | |
| 129 .then(function() { failTest("NotifyOnCleared"); }) | |
| 130 // Notify that the original notification was cleared. | |
| 131 .catch(function() { return notifyOnCleared(myId, returnId); }) | |
| 132 .catch(function() { failTest("NotifyOnCleared"); }) | |
| 133 .then(function () { return notifyOnPermissionLevelChanged(myId, | |
| 134 "granted"); }) | |
| 135 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }) | |
| 136 .then(function () { return notifyOnShowSettings(myId); }) | |
| 137 .catch(function() { failTest("NotifyOnShowSettings"); }) | |
| 138 .then(function() { chrome.test.succeed(); }); | |
| 139 }; | |
| 140 | 130 |
| 141 chrome.test.runTests([ testFunctions ]); | 131 function testNotifyOnButtonClicked() { |
| 132 chrome.notifications.onButtonClicked.addListener(function(id, buttonIndex) { |
| 133 chrome.test.succeed(); |
| 134 }); |
| 135 |
| 136 // Create a notification, so there will be one existing notification. |
| 137 createNotification(id1, content) |
| 138 .catch(function() { failTest("notifications.create"); }) |
| 139 // Try to notify that a non-existent notification button was clicked. |
| 140 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) |
| 141 .then(function() { failTest("NotifyOnButtonClicked"); }) |
| 142 // Notify the sender that a notificaion button was clicked. |
| 143 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) |
| 144 .catch(function() { failTest("NotifyOnButtonClicked"); }); |
| 145 } |
| 146 |
| 147 function testNotifyOnClosed() { |
| 148 chrome.notifications.onClosed.addListener(function(id, byUser) { |
| 149 chrome.test.succeed(); |
| 150 }); |
| 151 |
| 152 // Create a notification, so there will be one existing notification. |
| 153 createNotification(id1, content) |
| 154 .catch(function() { failTest("notifications.create"); }) |
| 155 // Try to notify that an non-existent notification was cleared. |
| 156 .then(function () { return notifyOnCleared(myId, "doesNotExist"); }) |
| 157 .then(function() { failTest("NotifyOnCleared"); }) |
| 158 // Notify that the original notification was cleared. |
| 159 .catch(function() { return notifyOnCleared(myId, returnId); }) |
| 160 .catch(function() { failTest("NotifyOnCleared"); }); |
| 161 } |
| 162 |
| 163 function testNotifyOnShowSettings() { |
| 164 chrome.notifications.onShowSettings.addListener(function() { |
| 165 chrome.test.succeed(); |
| 166 }); |
| 167 |
| 168 // Create a notification, so there will be one existing notification. |
| 169 createNotification(id1, content) |
| 170 .catch(function() { failTest("notifications.create"); }) |
| 171 // Try to notify a non existent sender that a user checked its settings. |
| 172 .then(function () { return notifyOnShowSettings("DoesNotExist", |
| 173 "application"); }) |
| 174 // Fail if it returns true. |
| 175 .then(function() { failTest("NotifyOnShowSettings"); }) |
| 176 // Notify current notifier that a user checked its settings. |
| 177 .catch(function () { return notifyOnShowSettings(myId, "application"); }) |
| 178 .catch(function() { failTest("NotifyOnShowSettings"); }) |
| 179 } |
| 180 |
| 181 chrome.test.runTests([ testNotifyOnClicked, |
| 182 testNotifyOnButtonClicked, |
| 183 testNotifyOnClosed, |
| 184 testNotifyOnShowSettings ]); |
| OLD | NEW |