OLD | NEW |
| (Empty) |
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. | |
4 | |
5 const notificationProvider = chrome.notificationProvider; | |
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 | |
17 function createNotification(notificationId, options) { | |
18 return new Promise(function (resolve, reject) { | |
19 chrome.notifications.create(notificationId, options, function (id) { | |
20 if (chrome.runtime.lastError) { | |
21 reject(new Error("Unable to create notification")); | |
22 return; | |
23 } | |
24 resolve(id); | |
25 return; | |
26 }); | |
27 }); | |
28 }; | |
29 | |
30 function notifyOnCleared(senderId, notificationId) { | |
31 return new Promise(function (resolve, reject) { | |
32 notificationProvider.notifyOnCleared(senderId, | |
33 notificationId, | |
34 function (wasCleared) { | |
35 if (chrome.runtime.lastError || !wasCleared) { | |
36 reject(new Error("Unable to send onClear message")); | |
37 return; | |
38 } | |
39 resolve(wasCleared); | |
40 return; | |
41 }); | |
42 }); | |
43 }; | |
44 | |
45 function notifyOnClicked(senderId, notificationId) { | |
46 return new Promise(function (resolve, reject) { | |
47 notificationProvider.notifyOnClicked(senderId, | |
48 notificationId, | |
49 function (matchExists) { | |
50 if (chrome.runtime.lastError || !matchExists) { | |
51 reject(new Error("Unable to send onClick message")); | |
52 return; | |
53 } | |
54 resolve(matchExists); | |
55 return; | |
56 }); | |
57 }); | |
58 }; | |
59 | |
60 function notifyOnButtonClicked(senderId, notificationId, buttonIndex) { | |
61 return new Promise(function (resolve, reject) { | |
62 notificationProvider.notifyOnButtonClicked(senderId, | |
63 notificationId, | |
64 buttonIndex, | |
65 function (matchExists) { | |
66 if (chrome.runtime.lastError || !matchExists) { | |
67 reject(new Error("Unable to send onButtonClick message")); | |
68 return; | |
69 } | |
70 resolve(matchExists); | |
71 return; | |
72 }); | |
73 }); | |
74 }; | |
75 | |
76 function notifyOnPermissionLevelChanged(senderId, | |
77 notifierType, | |
78 permissionLevel) { | |
79 return new Promise(function (resolve, reject) { | |
80 notificationProvider.notifyOnPermissionLevelChanged( | |
81 senderId, | |
82 notifierType, | |
83 permissionLevel, | |
84 function (wasChanged) { | |
85 if (chrome.runtime.lastError || !wasChanged) { | |
86 reject(new Error("Unable to send onPermissionLevelChanged message")); | |
87 return; | |
88 } | |
89 resolve(wasChanged); | |
90 return; | |
91 }); | |
92 }); | |
93 }; | |
94 | |
95 function notifyOnShowSettings(senderId, notifierType) { | |
96 return new Promise(function (resolve, reject) { | |
97 notificationProvider.notifyOnShowSettings(senderId, | |
98 notifierType, | |
99 function (hasSettings) { | |
100 if (chrome.runtime.lastError || !hasSettings) { | |
101 reject(new Error("Unable to send onShowSettings message")); | |
102 return; | |
103 } | |
104 resolve(hasSettings); | |
105 return; | |
106 }); | |
107 }); | |
108 }; | |
109 | |
110 function failTest(testName) { | |
111 chrome.test.fail(testName); | |
112 }; | |
113 | |
114 function testNotifyOnClicked(){ | |
115 chrome.notifications.onClicked.addListener(function(id) { | |
116 chrome.test.succeed(); | |
117 }); | |
118 | |
119 // Create a notification, so there will be one existing notification. | |
120 createNotification(id1, content) | |
121 .catch(function() { failTest("notifications.create"); }) | |
122 // Try to notify that an non-existent notification was clicked. | |
123 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) | |
124 // Fail if it returns true. | |
125 .then(function() { failTest("NotifyOnClicked"); }) | |
126 // Notify the sender that a notificaion was clicked. | |
127 .catch(function() { return notifyOnClicked(myId, returnId); }) | |
128 .catch(function() { failTest("NotifyOnClicked"); }); | |
129 } | |
130 | |
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 testNotifyOnPermissionLevelChanged() { | |
164 chrome.notifications.onPermissionLevelChanged.addListener(function(level) { | |
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 web type notifier its permissional level is changed. | |
172 .then(function() { return notifyOnPermissionLevelChanged("SomeURL", | |
173 "web", | |
174 "granted"); }) | |
175 .then(function() { failTest("NotifyOnPermissionLevelChanged"); }) | |
176 // Notify that the permission level of current notifier is changed. | |
177 .catch(function () { return notifyOnPermissionLevelChanged(myId, | |
178 "application", | |
179 "granted"); }) | |
180 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }); | |
181 } | |
182 | |
183 function testNotifyOnShowSettings() { | |
184 chrome.notifications.onShowSettings.addListener(function() { | |
185 chrome.test.succeed(); | |
186 }); | |
187 | |
188 // Create a notification, so there will be one existing notification. | |
189 createNotification(id1, content) | |
190 .catch(function() { failTest("notifications.create"); }) | |
191 // Try to notify a non existent sender that a user checked its settings. | |
192 .then(function () { return notifyOnShowSettings("DoesNotExist", | |
193 "application"); }) | |
194 // Fail if it returns true. | |
195 .then(function() { failTest("NotifyOnShowSettings"); }) | |
196 // Notify current notifier that a user checked its settings. | |
197 .catch(function () { return notifyOnShowSettings(myId, "application"); }) | |
198 .catch(function() { failTest("NotifyOnShowSettings"); }); | |
199 } | |
200 | |
201 chrome.test.runTests([ testNotifyOnClicked, | |
202 testNotifyOnButtonClicked, | |
203 testNotifyOnClosed, | |
204 testNotifyOnPermissionLevelChanged, | |
205 testNotifyOnShowSettings ]); | |
OLD | NEW |