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