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.
5 const notificationProvider
= chrome
.notificationProvider
;
7 var myId
= chrome
.runtime
.id
;
9 var returnId
= myId
+ "-" + id1
;
14 message
: "This is the message."
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"));
30 function notifyOnCleared(senderId
, notificationId
) {
31 return new Promise(function (resolve
, reject
) {
32 notificationProvider
.notifyOnCleared(senderId
,
34 function (wasCleared
) {
35 if (chrome
.runtime
.lastError
|| !wasCleared
) {
36 reject(new Error("Unable to send onClear message"));
45 function notifyOnClicked(senderId
, notificationId
) {
46 return new Promise(function (resolve
, reject
) {
47 notificationProvider
.notifyOnClicked(senderId
,
49 function (matchExists
) {
50 if (chrome
.runtime
.lastError
|| !matchExists
) {
51 reject(new Error("Unable to send onClick message"));
60 function notifyOnButtonClicked(senderId
, notificationId
, buttonIndex
) {
61 return new Promise(function (resolve
, reject
) {
62 notificationProvider
.notifyOnButtonClicked(senderId
,
65 function (matchExists
) {
66 if (chrome
.runtime
.lastError
|| !matchExists
) {
67 reject(new Error("Unable to send onButtonClick message"));
76 function notifyOnPermissionLevelChanged(senderId
,
79 return new Promise(function (resolve
, reject
) {
80 notificationProvider
.notifyOnPermissionLevelChanged(
84 function (wasChanged
) {
85 if (chrome
.runtime
.lastError
|| !wasChanged
) {
86 reject(new Error("Unable to send onPermissionLevelChanged message"));
95 function notifyOnShowSettings(senderId
, notifierType
) {
96 return new Promise(function (resolve
, reject
) {
97 notificationProvider
.notifyOnShowSettings(senderId
,
99 function (hasSettings
) {
100 if (chrome
.runtime
.lastError
|| !hasSettings
) {
101 reject(new Error("Unable to send onShowSettings message"));
104 resolve(hasSettings
);
110 function failTest(testName
) {
111 chrome
.test
.fail(testName
);
114 function testNotifyOnClicked(){
115 chrome
.notifications
.onClicked
.addListener(function(id
) {
116 chrome
.test
.succeed();
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"); });
131 function testNotifyOnButtonClicked() {
132 chrome
.notifications
.onButtonClicked
.addListener(function(id
, buttonIndex
) {
133 chrome
.test
.succeed();
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"); });
147 function testNotifyOnClosed() {
148 chrome
.notifications
.onClosed
.addListener(function(id
, byUser
) {
149 chrome
.test
.succeed();
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"); });
163 function testNotifyOnPermissionLevelChanged() {
164 chrome
.notifications
.onPermissionLevelChanged
.addListener(function(level
) {
165 chrome
.test
.succeed();
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",
175 .then(function() { failTest("NotifyOnPermissionLevelChanged"); })
176 // Notify that the permission level of current notifier is changed.
177 .catch(function () { return notifyOnPermissionLevelChanged(myId
,
180 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); });
183 function testNotifyOnShowSettings() {
184 chrome
.notifications
.onShowSettings
.addListener(function() {
185 chrome
.test
.succeed();
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",
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"); });
201 chrome
.test
.runTests([ testNotifyOnClicked
,
202 testNotifyOnButtonClicked
,
204 testNotifyOnPermissionLevelChanged
,
205 testNotifyOnShowSettings
]);