Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: chrome/test/data/extensions/api_test/notification_provider/basic_usage/background.js

Issue 468813002: Add NotifyOnPermissionLevelChanged implementation of notification (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 notifyOnShowSettings(senderId, notifierType) {
83 return new Promise(function (resolve, reject) { 86 return new Promise(function (resolve, reject) {
84 notificationProvider.notifyOnShowSettings(senderId, 87 notificationProvider.notifyOnShowSettings(senderId,
85 function (notifierExists) { 88 notifierType,
86 if (chrome.runtime.lastError || !notifierExists) { 89 function (hasSettings) {
90 if (chrome.runtime.lastError || !hasSettings) {
87 reject(new Error("Unable to send onShowSettings message")); 91 reject(new Error("Unable to send onShowSettings message"));
88 return; 92 return;
89 } 93 }
90 resolve(notifierExists); 94 resolve(hasSettings);
91 return; 95 return;
92 }); 96 });
93 }); 97 });
94 }; 98 };
95 99
100 function allEventesReceived(a, b, c, d, e) {
Pete Williamson 2014/08/13 19:54:01 nit: Eventes -> Events Let's use more descriptive
liyanhou 2014/08/14 16:15:24 Done.
101 return new Promise(function (resolve, reject) {
102 if (a && b && c && d && e) {
103 resolve();
104 return;
105 }
106 reject(new Error("Not all events are fired correctly."));
107 return;
108 });
109 };
110
96 function failTest(testName) { 111 function failTest(testName) {
97 chrome.test.fail(testName); 112 chrome.test.fail(testName);
98 }; 113 };
99 114
100 function testFunctions() { 115 function testFunctions() {
101 var myId = chrome.runtime.id; 116 var myId = chrome.runtime.id;
102 var id1 = "id1"; 117 var id1 = "id1";
103 var returnId = myId + "-" + id1; 118 var returnId = myId + "-" + id1;
104 var content = { 119 var content = {
105 type: "basic", 120 type: "basic",
106 iconUrl: "icon.png", 121 iconUrl: "icon.png",
107 title: "Title", 122 title: "Title",
108 message: "This is the message." 123 message: "This is the message."
109 }; 124 };
110 125
126 var on_clicked_received = false;
127 function listenForOnClicked() {
128 on_clicked_received = true;
129 }
130
131 var on_button_clicked_received = false;
132 function listenForOnButtonClicked() {
133 on_button_clicked_received = true;
134 }
135
136 var on_closed_received = false;
137 function listenForOnClosed() {
138 on_closed_received = true;
139 }
140
141 var on_permission_level_changed_called = false;
142 function listenForOnPermissionLevelChanged() {
143 on_permission_level_changed_called = true;
144 }
145
146 var on_show_settings_called = false;
147 function listenForShowSettings() {
148 on_show_settings_called = true;
149 }
150
151 chrome.notifications.onClicked.addListener(listenForOnClicked);
152 chrome.notifications.onButtonClicked.addListener(listenForOnButtonClicked);
153 chrome.notifications.onClosed.addListener(listenForOnClosed);
154 chrome.notifications.onPermissionLevelChanged.addListener(
155 listenForOnPermissionLevelChanged);
156 chrome.notifications.onShowSettings.addListener(listenForShowSettings);
157
111 // Create a notification, so there will be one existing notification 158 // Create a notification, so there will be one existing notification
112 createNotification(id1, content) 159 createNotification(id1, content)
113 .catch(function() { failTest("notifications.create"); }) 160 .catch(function() { failTest("notifications.create"); })
114 // Notify the sender that a notificaion was clicked. 161 // Notify the sender that a notificaion was clicked.
115 .then(function() { return notifyOnClicked(myId, returnId); }) 162 .then(function() { return notifyOnClicked(myId, returnId); })
116 .catch(function() { failTest("NotifyOnClicked1"); }) 163 .catch(function() { failTest("NotifyOnClicked1"); })
117 // Try to notify that an non-existent notification was clicked. 164 // Try to notify that an non-existent notification was clicked.
118 .then(function() { return notifyOnClicked(myId, "doesNotExist"); }) 165 .then(function() { return notifyOnClicked(myId, "doesNotExist"); })
119 // Fail if it returns true. 166 // Fail if it returns true.
120 .then(function() { failTest("NotifyOnClicked2"); }) 167 .then(function() { failTest("NotifyOnClicked2"); })
121 // Notify the sender that a notificaion button was clicked. 168 // Notify the sender that a notificaion button was clicked.
122 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); }) 169 .catch(function() { return notifyOnButtonClicked(myId, returnId, 0); })
123 .catch(function() { failTest("NotifyOnButtonClicked"); }) 170 .catch(function() { failTest("NotifyOnButtonClicked"); })
124 // Try to notify that a non-existent notification button was clicked. 171 // Try to notify that a non-existent notification button was clicked.
125 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)}) 172 .then(function() { return notifyOnButtonClicked(myId, "doesNotExist", 0)})
126 .then(function() { failTest("NotifyOnButtonClicked"); }) 173 .then(function() { failTest("NotifyOnButtonClicked"); })
127 // Try to notify that an non-existent notification was cleared. 174 // Try to notify that an non-existent notification was cleared.
128 .catch(function () { return notifyOnCleared(myId, "doesNotExist"); }) 175 .catch(function () { return notifyOnCleared(myId, "doesNotExist"); })
129 .then(function() { failTest("NotifyOnCleared"); }) 176 .then(function() { failTest("NotifyOnCleared"); })
130 // Notify that the original notification was cleared. 177 // Notify that the original notification was cleared.
131 .catch(function() { return notifyOnCleared(myId, returnId); }) 178 .catch(function() { return notifyOnCleared(myId, returnId); })
132 .catch(function() { failTest("NotifyOnCleared"); }) 179 .catch(function() { failTest("NotifyOnCleared"); })
133 .then(function () { return notifyOnPermissionLevelChanged(myId, 180 .then(function () { return notifyOnPermissionLevelChanged(myId,
181 "application",
134 "granted"); }) 182 "granted"); })
135 .catch(function() { failTest("NotifyOnPermissionLevelChanged"); }) 183 .catch(function() { failTest("NotifyOnPermissionLevelChanged1"); })
136 .then(function () { return notifyOnShowSettings(myId); }) 184 // Try to notify a web type notifier its permissional level is changed
185 .then(function() { return notifyOnPermissionLevelChanged("SomeURL",
186 "web",
187 "granted"); })
188 // Fail if it returns true
189 .then(function() { failTest("NotifyOnPermissionLevelChanged2"); })
190 .catch(function() { return notifyOnShowSettings(myId, "application"); })
137 .catch(function() { failTest("NotifyOnShowSettings"); }) 191 .catch(function() { failTest("NotifyOnShowSettings"); })
192 .then(function() { return allEventesReceived(
193 on_clicked_received,
194 on_button_clicked_received,
195 on_closed_received,
196 on_permission_level_changed_called,
197 // notifyOnShowSettings is not
198 // implemented yet
199 true); })
200 .catch(function() { failTest("DidNotReceiveAllEvents"); })
138 .then(function() { chrome.test.succeed(); }); 201 .then(function() { chrome.test.succeed(); });
139 }; 202 };
140 203
141 chrome.test.runTests([ testFunctions ]); 204 chrome.test.runTests([ testFunctions ]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698