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

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: 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
« no previous file with comments | « no previous file | chrome/browser/services/gcm/push_messaging_service_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 InProcessBrowserTest::SetUp(); 129 InProcessBrowserTest::SetUp();
130 } 130 }
131 131
132 // InProcessBrowserTest: 132 // InProcessBrowserTest:
133 void SetUpOnMainThread() override { 133 void SetUpOnMainThread() override {
134 gcm_service_ = static_cast<FakeGCMProfileService*>( 134 gcm_service_ = static_cast<FakeGCMProfileService*>(
135 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse( 135 GCMProfileServiceFactory::GetInstance()->SetTestingFactoryAndUse(
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(const std::string& path) {
145 ui_test_utils::NavigateToURL( 145 ui_test_utils::NavigateToURL(browser(), https_server_->GetURL(path));
146 browser(), https_server_->GetURL(GetTestURL())); 146 }
147
148 void LoadTestPage() {
149 LoadTestPage(GetTestURL());
147 } 150 }
148 151
149 bool RunScript(const std::string& script, std::string* result) { 152 bool RunScript(const std::string& script, std::string* result) {
150 return content::ExecuteScriptAndExtractString( 153 return content::ExecuteScriptAndExtractString(
151 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), 154 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
152 script, 155 script,
153 result); 156 result);
154 } 157 }
155 158
159 void TryToRegisterSuccessfully(
160 const std::string& expected_push_registration_id);
161
156 net::SpawnedTestServer* https_server() const { return https_server_.get(); } 162 net::SpawnedTestServer* https_server() const { return https_server_.get(); }
157 163
158 FakeGCMProfileService* gcm_service() const { return gcm_service_; } 164 FakeGCMProfileService* gcm_service() const { return gcm_service_; }
159 165
160 PushMessagingServiceImpl* push_service() { 166 PushMessagingServiceImpl* push_service() {
161 return static_cast<PushMessagingServiceImpl*>( 167 return static_cast<PushMessagingServiceImpl*>(
162 gcm_service_->push_messaging_service()); 168 gcm_service_->push_messaging_service());
163 } 169 }
164 170
165 protected: 171 protected:
(...skipping 18 matching lines...) Expand all
184 RegisterFailsNotVisibleMessages) { 190 RegisterFailsNotVisibleMessages) {
185 std::string script_result; 191 std::string script_result;
186 192
187 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 193 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
188 ASSERT_EQ("ok - service worker registered", script_result); 194 ASSERT_EQ("ok - service worker registered", script_result);
189 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 195 ASSERT_TRUE(RunScript("registerPush()", &script_result));
190 EXPECT_EQ("AbortError - Registration failed - permission denied", 196 EXPECT_EQ("AbortError - Registration failed - permission denied",
191 script_result); 197 script_result);
192 } 198 }
193 199
200 void PushMessagingBrowserTest::TryToRegisterSuccessfully(
201 const std::string& expected_push_registration_id) {
202 std::string script_result;
203
204 EXPECT_TRUE(RunScript("registerServiceWorker()", &script_result));
205 EXPECT_EQ("ok - service worker registered", script_result);
206
207 InfoBarResponder accepting_responder(browser(), true);
208 EXPECT_TRUE(RunScript("requestNotificationPermission()", &script_result));
209 EXPECT_EQ("permission status - granted", script_result);
210
211 EXPECT_TRUE(RunScript("registerPush()", &script_result));
212 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - "
213 + expected_push_registration_id, script_result);
214 }
215
194 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 216 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
195 RegisterSuccessNotificationsGranted) { 217 RegisterSuccessNotificationsGranted) {
196 std::string script_result; 218 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
197 219
198 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 220 PushMessagingApplicationId app_id(https_server()->GetURL(""),
199 ASSERT_EQ("ok - service worker registered", script_result); 221 0LL /* service_worker_registration_id */);
200
201 InfoBarResponder accepting_responder(browser(), true);
202 ASSERT_TRUE(RunScript("requestNotificationPermission()", &script_result));
203 ASSERT_EQ("permission status - granted", script_result);
204
205 ASSERT_TRUE(RunScript("registerPush()", &script_result));
206 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result);
207
208 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
209 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 222 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
210 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 223 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
211 } 224 }
212 225
213 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 226 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
214 RegisterSuccessNotificationsPrompt) { 227 RegisterSuccessNotificationsPrompt) {
215 std::string script_result; 228 std::string script_result;
216 229
217 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 230 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
218 ASSERT_EQ("ok - service worker registered", script_result); 231 ASSERT_EQ("ok - service worker registered", script_result);
219 232
220 InfoBarResponder accepting_responder(browser(), true); 233 InfoBarResponder accepting_responder(browser(), true);
221 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 234 ASSERT_TRUE(RunScript("registerPush()", &script_result));
222 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result); 235 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result);
223 236
224 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L); 237 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0LL);
225 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 238 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
226 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 239 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
227 } 240 }
228 241
229 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, 242 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest,
230 RegisterFailureNotificationsBlocked) { 243 RegisterFailureNotificationsBlocked) {
231 std::string script_result; 244 std::string script_result;
232 245
233 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 246 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
234 ASSERT_EQ("ok - service worker registered", script_result); 247 ASSERT_EQ("ok - service worker registered", script_result);
(...skipping 18 matching lines...) Expand all
253 ASSERT_EQ("permission status - granted", script_result); 266 ASSERT_EQ("permission status - granted", script_result);
254 267
255 ASSERT_TRUE(RunScript("removeManifest()", &script_result)); 268 ASSERT_TRUE(RunScript("removeManifest()", &script_result));
256 ASSERT_EQ("manifest removed", script_result); 269 ASSERT_EQ("manifest removed", script_result);
257 270
258 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 271 ASSERT_TRUE(RunScript("registerPush()", &script_result));
259 EXPECT_EQ("AbortError - Registration failed - no sender id provided", 272 EXPECT_EQ("AbortError - Registration failed - no sender id provided",
260 script_result); 273 script_result);
261 } 274 }
262 275
276 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, RegisterPersisted) {
277 std::string script_result;
278
279 // An app ID for each Service Worker registration ID we'll use.
280 PushMessagingApplicationId app_id_sw0(https_server()->GetURL(""), 0LL);
281 PushMessagingApplicationId app_id_sw1(https_server()->GetURL(""), 1LL);
282 PushMessagingApplicationId app_id_sw2(https_server()->GetURL(""), 2LL);
283
284 // First, test that Service Worker registration IDs are assigned in order of
285 // registering the Service Workers, and the (fake) push registration ids are
286 // assigned in order of push registration (even when these orders are
287 // different).
288
289 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
290 EXPECT_EQ(app_id_sw0.ToString(), gcm_service()->last_registered_app_id());
291
292 LoadTestPage("files/push_messaging/subscope1/test.html");
293 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
294 ASSERT_EQ("ok - service worker registered", script_result);
295
296 LoadTestPage("files/push_messaging/subscope2/test.html");
297 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result));
298 ASSERT_EQ("ok - service worker registered", script_result);
299
300 // Note that we need to reload the page after registering, otherwise
301 // navigator.serviceWorker.ready is going to be resolved with the parent
302 // Service Worker which still controls the page.
303 LoadTestPage("files/push_messaging/subscope2/test.html");
304 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */);
305 EXPECT_EQ(app_id_sw2.ToString(), gcm_service()->last_registered_app_id());
306
307 LoadTestPage("files/push_messaging/subscope1/test.html");
308 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */);
309 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
310
311 // Now test that the Service Worker registration IDs and push registration IDs
312 // generated above were persisted to SW storage, by checking that they are
313 // unchanged despite requesting them in a different order.
314 // TODO(johnme): Ideally we would restart the browser at this point to check
315 // they were persisted to disk, but that's not currently possible since the
316 // test server uses random port numbers for each test (even PRE_Foo and Foo),
317 // so we wouldn't be able to load the test pages with the same origin.
318
319 LoadTestPage("files/push_messaging/subscope1/test.html");
320 TryToRegisterSuccessfully("1-2" /* expected_push_registration_id */);
321 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
322
323 LoadTestPage("files/push_messaging/subscope2/test.html");
324 TryToRegisterSuccessfully("1-1" /* expected_push_registration_id */);
325 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
326
327 LoadTestPage();
328 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
329 EXPECT_EQ(app_id_sw1.ToString(), gcm_service()->last_registered_app_id());
330 }
331
263 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) { 332 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventSuccess) {
264 std::string script_result; 333 std::string script_result;
265 334
266 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 335 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
267 ASSERT_EQ("ok - service worker registered", script_result);
268 336
269 InfoBarResponder accepting_responder(browser(), true); 337 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0LL);
270 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
271 ASSERT_EQ("permission status - granted", script_result);
272
273 ASSERT_TRUE(RunScript("registerPush()", &script_result));
274 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result);
275
276 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
277 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 338 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
278 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 339 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
279 340
280 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 341 ASSERT_TRUE(RunScript("isControlled()", &script_result));
281 ASSERT_EQ("false - is not controlled", script_result); 342 ASSERT_EQ("false - is not controlled", script_result);
282 343
283 loadTestPage(); // Reload to become controlled. 344 LoadTestPage(); // Reload to become controlled.
284 345
285 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 346 ASSERT_TRUE(RunScript("isControlled()", &script_result));
286 ASSERT_EQ("true - is controlled", script_result); 347 ASSERT_EQ("true - is controlled", script_result);
287 348
288 GCMClient::IncomingMessage message; 349 GCMClient::IncomingMessage message;
289 GCMClient::MessageData messageData; 350 GCMClient::MessageData messageData;
290 messageData.insert(std::pair<std::string, std::string>("data", "testdata")); 351 messageData.insert(std::pair<std::string, std::string>("data", "testdata"));
291 message.data = messageData; 352 message.data = messageData;
292 push_service()->OnMessage(app_id.ToString(), message); 353 push_service()->OnMessage(app_id.ToString(), message);
293 ASSERT_TRUE(RunScript("pushData.get()", &script_result)); 354 ASSERT_TRUE(RunScript("pushData.get()", &script_result));
294 EXPECT_EQ("testdata", script_result); 355 EXPECT_EQ("testdata", script_result);
295 } 356 }
296 357
297 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) { 358 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoServiceWorker) {
298 std::string script_result; 359 std::string script_result;
299 360
300 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 361 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
301 ASSERT_EQ("ok - service worker registered", script_result);
302 362
303 InfoBarResponder accepting_responder(browser(), true); 363 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0LL);
304 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
305 ASSERT_EQ("permission status - granted", script_result);
306
307 ASSERT_TRUE(RunScript("registerPush()", &script_result));
308 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result);
309
310 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
311 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 364 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
312 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 365 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
313 366
314 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 367 ASSERT_TRUE(RunScript("isControlled()", &script_result));
315 ASSERT_EQ("false - is not controlled", script_result); 368 ASSERT_EQ("false - is not controlled", script_result);
316 369
317 loadTestPage(); // Reload to become controlled. 370 LoadTestPage(); // Reload to become controlled.
318 371
319 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 372 ASSERT_TRUE(RunScript("isControlled()", &script_result));
320 ASSERT_EQ("true - is controlled", script_result); 373 ASSERT_EQ("true - is controlled", script_result);
321 374
322 // Unregister service worker. Sending a message should now fail. 375 // Unregister service worker. Sending a message should now fail.
323 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result)); 376 ASSERT_TRUE(RunScript("unregisterServiceWorker()", &script_result));
324 ASSERT_EQ("service worker unregistration status: true", script_result); 377 ASSERT_EQ("service worker unregistration status: true", script_result);
325 378
326 // When the push service will receive it next message, given that there is no 379 // When the push service will receive it next message, given that there is no
327 // SW available, it should unregister |app_id|. 380 // SW available, it should unregister |app_id|.
(...skipping 11 matching lines...) Expand all
339 EXPECT_EQ(app_id.ToString(), callback.app_id()); 392 EXPECT_EQ(app_id.ToString(), callback.app_id());
340 393
341 // No push data should have been received. 394 // No push data should have been received.
342 ASSERT_TRUE(RunScript("pushData.getImmediately()", &script_result)); 395 ASSERT_TRUE(RunScript("pushData.getImmediately()", &script_result));
343 EXPECT_EQ("null", script_result); 396 EXPECT_EQ("null", script_result);
344 } 397 }
345 398
346 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoPermission) { 399 IN_PROC_BROWSER_TEST_F(PushMessagingBrowserTest, PushEventNoPermission) {
347 std::string script_result; 400 std::string script_result;
348 401
349 ASSERT_TRUE(RunScript("registerServiceWorker()", &script_result)); 402 TryToRegisterSuccessfully("1-0" /* expected_push_registration_id */);
350 ASSERT_EQ("ok - service worker registered", script_result);
351 403
352 InfoBarResponder accepting_responder(browser(), true); 404 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0LL);
353 ASSERT_TRUE(RunScript("requestNotificationPermission();", &script_result));
354 ASSERT_EQ("permission status - granted", script_result);
355
356 ASSERT_TRUE(RunScript("registerPush()", &script_result));
357 EXPECT_EQ(std::string(kPushMessagingEndpoint) + " - 1-0", script_result);
358
359 PushMessagingApplicationId app_id(https_server()->GetURL(""), 0L);
360 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id()); 405 EXPECT_EQ(app_id.ToString(), gcm_service()->last_registered_app_id());
361 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]); 406 EXPECT_EQ("1234567890", gcm_service()->last_registered_sender_ids()[0]);
362 407
363 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 408 ASSERT_TRUE(RunScript("isControlled()", &script_result));
364 ASSERT_EQ("false - is not controlled", script_result); 409 ASSERT_EQ("false - is not controlled", script_result);
365 410
366 loadTestPage(); // Reload to become controlled. 411 LoadTestPage(); // Reload to become controlled.
367 412
368 ASSERT_TRUE(RunScript("isControlled()", &script_result)); 413 ASSERT_TRUE(RunScript("isControlled()", &script_result));
369 ASSERT_EQ("true - is controlled", script_result); 414 ASSERT_EQ("true - is controlled", script_result);
370 415
371 // Revoke Push permission. 416 // Revoke Push permission.
372 browser()->profile()->GetHostContentSettingsMap()-> 417 browser()->profile()->GetHostContentSettingsMap()->
373 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING); 418 ClearSettingsForOneType(CONTENT_SETTINGS_TYPE_PUSH_MESSAGING);
374 419
375 // When the push service will receive its next message, given that there is no 420 // When the push service will receive its next message, given that there is no
376 // SW available, it should unregister |app_id|. 421 // SW available, it should unregister |app_id|.
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 476
432 ASSERT_TRUE(RunScript("registerPush()", &script_result)); 477 ASSERT_TRUE(RunScript("registerPush()", &script_result));
433 EXPECT_EQ("AbortError - Registration failed - permission denied", 478 EXPECT_EQ("AbortError - Registration failed - permission denied",
434 script_result); 479 script_result);
435 480
436 ASSERT_TRUE(RunScript("hasPermission()", &script_result)); 481 ASSERT_TRUE(RunScript("hasPermission()", &script_result));
437 EXPECT_EQ("permission status - denied", script_result); 482 EXPECT_EQ("permission status - denied", script_result);
438 } 483 }
439 484
440 } // namespace gcm 485 } // namespace gcm
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/services/gcm/push_messaging_service_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698