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, |
82 notifierType, | |
70 permissionLevel, | 83 permissionLevel, |
71 function (notifierExists) { | 84 function (wasChanged) { |
72 if (chrome.runtime.lastError || !notifierExists) { | 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 }; | |
81 | |
82 function notifyOnShowSettings(senderId) { | |
83 return new Promise(function (resolve, reject) { | |
84 notificationProvider.notifyOnShowSettings(senderId, | |
85 function (notifierExists) { | |
86 if (chrome.runtime.lastError || !notifierExists) { | |
87 reject(new Error("Unable to send onShowSettings message")); | |
88 return; | |
89 } | |
90 resolve(notifierExists); | |
91 return; | |
92 }); | |
93 }); | |
94 }; | 93 }; |
95 | 94 |
96 function failTest(testName) { | 95 function failTest(testName) { |
97 chrome.test.fail(testName); | 96 chrome.test.fail(testName); |
98 }; | 97 }; |
99 | 98 |
100 function testFunctions() { | 99 function testNotifyOnClicked(){ |
101 var myId = chrome.runtime.id; | 100 // Create a notification, so there will be one existing notification. |
102 var id1 = "id1"; | |
103 var returnId = myId + "-" + id1; | |
104 var content = { | |
105 type: "basic", | |
106 iconUrl: "icon.png", | |
107 title: "Title", | |
108 message: "This is the message." | |
109 }; | |
110 | |
111 // Create a notification, so there will be one existing notification | |
112 createNotification(id1, content) | 101 createNotification(id1, content) |
113 .catch(function() { failTest("notifications.create"); }) | 102 .catch(function() { failTest("notifications.create"); }) |
114 // Notify the sender that a notificaion was clicked. | |
115 .then(function() { return notifyOnClicked(myId, returnId); }) | |
116 .catch(function() { failTest("NotifyOnClicked1"); }) | |
117 // Try to notify that an non-existent notification was clicked. | 103 // Try to notify that an non-existent notification was clicked. |
118 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) | 104 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) |
119 // Fail if it returns true. | 105 // Fail if it returns true. |
120 .then(function() { failTest("NotifyOnClicked2"); }) | 106 .then(function() { failTest("NotifyOnClicked"); }) |
121 // Notify the sender that a notificaion button was clicked. | 107 // Notify the sender that a notificaion was clicked. |
122 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) | 108 .catch(function() { return notifyOnClicked(myId, returnId); }) |
123 .catch(function() { failTest("NotifyOnButtonClicked"); }) | 109 .catch(function() { failTest("NotifyOnClicked"); }); |
110 | |
111 chrome.notifications.onClicked.addListener(function(id) { | |
dewittj
2014/08/14 20:30:30
Please add listeners before you cause the events t
liyanhou
2014/08/15 18:57:59
Done.
| |
112 chrome.test.succeed(); | |
113 }); | |
114 } | |
115 | |
116 function testNotifyOnButtonClicked() { | |
117 // Create a notification, so there will be one existing notification. | |
118 createNotification(id1, content) | |
119 .catch(function() { failTest("notifications.create"); }) | |
124 // Try to notify that a non-existent notification button was clicked. | 120 // Try to notify that a non-existent notification button was clicked. |
125 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) | 121 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) |
126 .then(function() { failTest("NotifyOnButtonClicked"); }) | 122 .then(function() { failTest("NotifyOnButtonClicked"); }) |
123 // Notify the sender that a notificaion button was clicked. | |
124 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) | |
125 .catch(function() { failTest("NotifyOnButtonClicked"); }); | |
126 | |
127 chrome.notifications.onButtonClicked.addListener(function(id, buttonIndex) { | |
128 chrome.test.succeed(); | |
129 }); | |
130 } | |
131 | |
132 function testNotifyOnClosed() { | |
133 // Create a notification, so there will be one existing notification. | |
134 createNotification(id1, content) | |
135 .catch(function() { failTest("notifications.create"); }) | |
127 // Try to notify that an non-existent notification was cleared. | 136 // Try to notify that an non-existent notification was cleared. |
128 .catch(function () { return notifyOnCleared(myId, "doesNotExist"); }) | 137 .then(function () { return notifyOnCleared(myId, "doesNotExist"); }) |
129 .then(function() { failTest("NotifyOnCleared"); }) | 138 .then(function() { failTest("NotifyOnCleared"); }) |
130 // Notify that the original notification was cleared. | 139 // Notify that the original notification was cleared. |
131 .catch(function() { return notifyOnCleared(myId, returnId); }) | 140 .catch(function() { return notifyOnCleared(myId, returnId); }) |
132 .catch(function() { failTest("NotifyOnCleared"); }) | 141 .catch(function() { failTest("NotifyOnCleared"); }) |
133 .then(function () { return notifyOnPermissionLevelChanged(myId, | 142 |
134 "granted"); }) | 143 chrome.notifications.onClosed.addListener(function(id, byUser) { |
144 chrome.test.succeed(); | |
145 }); | |
146 } | |
147 | |
148 function testNotifyOnPermissionLevelChanged() { | |
149 // Create a notification, so there will be one existing notification. | |
150 createNotification(id1, content) | |
151 .catch(function() { failTest("notifications.create"); }) | |
152 // Try to notify a web type notifier its permissional level is changed. | |
153 .then(function() { return notifyOnPermissionLevelChanged("SomeURL", | |
154 "web", | |
155 "granted"); }) | |
156 .then(function() { failTest("NotifyOnPermissionLevelChanged"); }) | |
157 // Notify that the permission level of current notifier is changed. | |
158 .catch(function () { return notifyOnPermissionLevelChanged(myId, | |
159 "application", | |
160 "denied"); }) | |
135 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }) | 161 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }) |
136 .then(function () { return notifyOnShowSettings(myId); }) | |
137 .catch(function() { failTest("NotifyOnShowSettings"); }) | |
138 .then(function() { chrome.test.succeed(); }); | |
139 }; | |
140 | 162 |
141 chrome.test.runTests([ testFunctions ]); | 163 chrome.notifications.onPermissionLevelChanged.addListener(function(level) { |
164 chrome.test.succeed(); | |
165 }); | |
166 } | |
167 | |
168 chrome.test.runTests([ testNotifyOnClicked, | |
169 testNotifyOnButtonClicked, | |
170 testNotifyOnClosed, | |
171 testNotifyOnPermissionLevelChanged ]); | |
OLD | NEW |