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

Side by Side Diff: chrome/browser/services/gcm/push_messaging_browsertest.cc

Issue 778243002: Push API: Return cached registration if available. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@store2
Patch Set: Add test, refactor and rebase Created 6 years 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
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 #include <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 browser()->profile(), &FakeGCMProfileService::Build)); 136 browser()->profile(), &FakeGCMProfileService::Build));
137 gcm_service_->set_collect(true); 137 gcm_service_->set_collect(true);
138 138
139 loadTestPage(); 139 loadTestPage();
140 140
141 InProcessBrowserTest::SetUpOnMainThread(); 141 InProcessBrowserTest::SetUpOnMainThread();
142 } 142 }
143 143
144 void loadTestPage() { 144 void loadTestPage() {
145 ui_test_utils::NavigateToURL( 145 ui_test_utils::NavigateToURL(
146 browser(), https_server_->GetURL("files/push_messaging/test.html")); 146 browser(),
147 https_server_->GetURL("files/push_messaging/test.html"));
148 }
149
150 void LoadSubscope1TestPage() {
151 ui_test_utils::NavigateToURL(
152 browser(),
153 https_server_->GetURL("files/push_messaging/subscope1/test.html"));
154 }
155
156 void LoadSubscope2TestPage() {
157 ui_test_utils::NavigateToURL(
158 browser(),
159 https_server_->GetURL("files/push_messaging/subscope2/test.html"));
147 } 160 }
148 161
149 bool RunScript(const std::string& script, std::string* result) { 162 bool RunScript(const std::string& script, std::string* result) {
150 return content::ExecuteScriptAndExtractString( 163 return content::ExecuteScriptAndExtractString(
151 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 164 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
152 script, 165 script,
153 result); 166 result);
154 } 167 }
155 168
169 void TryToRegisterSuccessfully(
170 const std::string& expected_push_registration_id);
171
156 net::SpawnedTestServer* https_server() const { return https_server_.get(); } 172 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
157 173
158 FakeGCMProfileService* gcm_service() const { return gcm_service_; } 174 FakeGCMProfileService* gcm_service() const { return gcm_service_; }
159 175
160 PushMessagingServiceImpl* push_service() { 176 PushMessagingServiceImpl* push_service() {
161 return static_cast<PushMessagingServiceImpl*>( 177 return static_cast<PushMessagingServiceImpl*>(
162 gcm_service_->push_messaging_service()); 178 gcm_service_->push_messaging_service());
163 } 179 }
164 180
165 private: 181 private:
166 scoped_ptr<net::SpawnedTestServer> https_server_; 182 scoped_ptr<net::SpawnedTestServer> https_server_;
167 FakeGCMProfileService* gcm_service_; 183 FakeGCMProfileService* gcm_service_;
168 184
169 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest); 185 DISALLOW_COPY_AND_ASSIGN(PushMessagingBrowserTest);
170 }; 186 };
171 187
172 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) { 188 void PushMessagingBrowserTest::TryToRegisterSuccessfully(
189 const std::string& expected_push_registration_id) {
173 std::string script_result; 190 std::string script_result;
174 191
175 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 192 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
176 ASSERT_EQ("ok - service worker registered", script_result); 193 ASSERT_EQ("ok - service worker registered", script_result);
Michael van Ouwerkerk 2014/12/09 11:34:56 It would be nice to check that when an assertion t
johnme 2014/12/11 16:15:17 Assertion messages from helper messages are correc
177 194
178 InfoBarResponder accepting_responder(browser(), true); 195 InfoBarResponder accepting_responder(browser(), true);
179 ASSERT_TRUE(RunScript("requestNotificationPermission()", &script_result)); 196 ASSERT_TRUE(RunScript("requestNotificationPermission()", &script_result));
180 ASSERT_EQ("permission status - granted", script_result); 197 ASSERT_EQ("permission status - granted", script_result);
181 198
182 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 199 ASSERT_TRUE(RunScript("registerPush()", &script_result));
183 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result); 200 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - "
201 + expected_push_registration_id, script_result);
202 }
184 203
185 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); 204 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterSuccess) {
205 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
206
207 PushMessagingApplicationId app_id(https_server()->GetURL(""),
208 0LL /* service_worker_registration_id */);
186 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 209 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
187 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 210 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
188 } 211 }
189 212
190 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 213 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
191 RegisterFailureNoPushPermission) { 214 RegisterFailureNoPushPermission) {
192 std::string script_result; 215 std::string script_result;
193 216
194 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 217 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
195 ASSERT_EQ("ok - service worker registered", script_result); 218 ASSERT_EQ("ok - service worker registered", script_result);
(...skipping 30 matching lines...) Expand all
226 ASSERT_EQ("permission status - granted", script_result); 249 ASSERT_EQ("permission status - granted", script_result);
227 250
228 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 251 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
229 ASSERT_EQ("manifest removed", script_result); 252 ASSERT_EQ("manifest removed", script_result);
230 253
231 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 254 ASSERT_TRUE(RunScript("registerPush()", &script_result));
232 EXPECT_EQ("AbortError - Registration failed - no sender id provided", 255 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
233 script_result); 256 script_result);
234 } 257 }
235 258
259 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterPersisted) {
260 std::string script_result;
261
262 // An app ID for each Service Worker registration ID we'll use.
263 PushMessagingApplicationId app_id_sw0(https_server()->GetURL(""), 0LL);
264 PushMessagingApplicationId app_id_sw1(https_server()->GetURL(""), 1LL);
265 PushMessagingApplicationId app_id_sw2(https_server()->GetURL(""), 2LL);
266
267 // First, test that Service Worker registration IDs are assigned in order of
268 // registering the Service Workers, and the (fake) push registration ids are
269 // assigned in order of push registration (even when these orders are
270 // different).
271
272 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
273 EXPECT_EQ(app_id_sw0.ToString(), gcm_service()->last_registered_app_id());
274
275 LoadSubscope1TestPage();
276 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
277 ASSERT_EQ("ok - service worker registered", script_result);
278
279 LoadSubscope2TestPage();
280 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
281 ASSERT_EQ("ok - service worker registered", script_result);
282
283 // Note that we need to reload the page after registering, otherwise
284 // navigator.serviceWorker.ready is going to be resolved with the parent
285 // Service Worker which still controls the page.
286 LoadSubscope2TestPage();
287 TryToRegisterSuccessfully("2" /* expected_push_registration_id */);
288 EXPECT_EQ(app_id_sw2.ToString(), gcm_service()->last_registered_app_id());
289
290 LoadSubscope1TestPage();
291 TryToRegisterSuccessfully("3" /* expected_push_registration_id */);
292 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
293
294 // Now test that the Service Worker registration IDs and push registration IDs
295 // generated above were persisted to SW storage, by checking that they are
296 // unchanged despite requesting them in a different order.
297 // TODO(johnme): Ideally we would restart the browser at this point to check
298 // they were persisted to disk, but that's not currently possible since the
299 // test server uses random port numbers for each test (even PRE_Foo and Foo),
300 // so we wouldn't be able to load the test pages with the same origin.
301
302 LoadSubscope1TestPage();
303 TryToRegisterSuccessfully("3" /* expected_push_registration_id */);
304 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
305
306 LoadSubscope2TestPage();
307 TryToRegisterSuccessfully("2" /* expected_push_registration_id */);
308 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
309
310 loadTestPage();
311 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
312 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
313 }
314
236 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { 315 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
237 std::string script_result; 316 std::string script_result;
238 317
239 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 318 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
240 ASSERT_EQ("ok - service worker registered", script_result);
241
242 InfoBarResponder accepting_responder(browser(), true);
243 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
244 ASSERT_EQ("permission status - granted", script_result);
245
246 ASSERT_TRUE(RunScript("registerPush()", &script_result));
247 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result);
248 319
249 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); 320 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
250 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 321 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
251 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 322 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
252 323
253 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 324 ASSERT_TRUE(RunScript("isControlled()", &script_result));
254 ASSERT_EQ("false - is not controlled", script_result); 325 ASSERT_EQ("false - is not controlled", script_result);
255 326
256 loadTestPage(); // Reload to become controlled. 327 loadTestPage(); // Reload to become controlled.
257 328
258 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 329 ASSERT_TRUE(RunScript("isControlled()", &script_result));
259 ASSERT_EQ("true - is controlled", script_result); 330 ASSERT_EQ("true - is controlled", script_result);
260 331
261 GCMClient::IncomingMessage message; 332 GCMClient::IncomingMessage message;
262 GCMClient::MessageData messageData; 333 GCMClient::MessageData messageData;
263 messageData.insert(std::pair<std::string, std::string>("data", "testdata")); 334 messageData.insert(std::pair<std::string, std::string>("data", "testdata"));
264 message.data = messageData; 335 message.data = messageData;
265 push_service()->OnMessage(app_id.ToString(), message); 336 push_service()->OnMessage(app_id.ToString(), message);
266 ASSERT_TRUE(RunScript("pushData.get()", &script_result)); 337 ASSERT_TRUE(RunScript("pushData.get()", &script_result));
267 EXPECT_EQ("testdata", script_result); 338 EXPECT_EQ("testdata", script_result);
268 } 339 }
269 340
270 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { 341 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
271 std::string script_result; 342 std::string script_result;
272 343
273 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 344 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
274 ASSERT_EQ("ok - service worker registered", script_result);
275
276 InfoBarResponder accepting_responder(browser(), true);
277 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
278 ASSERT_EQ("permission status - granted", script_result);
279
280 ASSERT_TRUE(RunScript("registerPush()", &script_result));
281 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result);
282 345
283 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); 346 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
284 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 347 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
285 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 348 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
286 349
287 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 350 ASSERT_TRUE(RunScript("isControlled()", &script_result));
288 ASSERT_EQ("false - is not controlled", script_result); 351 ASSERT_EQ("false - is not controlled", script_result);
289 352
290 loadTestPage(); // Reload to become controlled. 353 loadTestPage(); // Reload to become controlled.
291 354
(...skipping 20 matching lines...) Expand all
312 EXPECT_EQ(app_id.ToString(), callback.app_id()); 375 EXPECT_EQ(app_id.ToString(), callback.app_id());
313 376
314 // No push data should have been received. 377 // No push data should have been received.
315 ASSERT_TRUE(RunScript("pushData.getImmediately()", &script_result)); 378 ASSERT_TRUE(RunScript("pushData.getImmediately()", &script_result));
316 EXPECT_EQ("null", script_result); 379 EXPECT_EQ("null", script_result);
317 } 380 }
318 381
319 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoPermission) { 382 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoPermission) {
320 std::string script_result; 383 std::string script_result;
321 384
322 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 385 TryToRegisterSuccessfully("1" /* expected_push_registration_id */);
323 ASSERT_EQ("ok - service worker registered", script_result);
324
325 InfoBarResponder accepting_responder(browser(), true);
326 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
327 ASSERT_EQ("permission status - granted", script_result);
328
329 ASSERT_TRUE(RunScript("registerPush()", &script_result));
330 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1", script_result);
331 386
332 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); 387 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
333 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 388 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
334 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 389 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
335 390
336 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 391 ASSERT_TRUE(RunScript("isControlled()", &script_result));
337 ASSERT_EQ("false - is not controlled", script_result); 392 ASSERT_EQ("false - is not controlled", script_result);
338 393
339 loadTestPage(); // Reload to become controlled. 394 loadTestPage(); // Reload to become controlled.
340 395
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 459
405 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 460 ASSERT_TRUE(RunScript("registerPush()", &script_result));
406 EXPECT_EQ("AbortError - Registration failed - permission denied", 461 EXPECT_EQ("AbortError - Registration failed - permission denied",
407 script_result); 462 script_result);
408 463
409 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); 464 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
410 EXPECT_EQ("permission status - denied", script_result); 465 EXPECT_EQ("permission status - denied", script_result);
411 } 466 }
412 467
413 } // namespace gcm 468 } // namespace gcm
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/push_messaging/subscope1/service_worker.js » ('j') | tools/metrics/histograms/histograms.xml » ('J')

Powered by Google App Engine
This is Rietveld 408576698