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

Side by Side Diff: content/browser/service_worker/service_worker_context_unittest.cc

Issue 2968123002: Introduce ServiceWorkerRegistrationOptions struct for service worker options (Closed)
Patch Set: address more comments Created 3 years, 5 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "content/public/browser/service_worker_context.h" 5 #include "content/public/browser/service_worker_context.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 GURL pattern("http://www.example.com/"); 203 GURL pattern("http://www.example.com/");
204 GURL script_url("http://www.example.com/service_worker.js"); 204 GURL script_url("http://www.example.com/service_worker.js");
205 205
206 RecordableEmbeddedWorkerInstanceClient* client = nullptr; 206 RecordableEmbeddedWorkerInstanceClient* client = nullptr;
207 client = helper_->CreateAndRegisterMockInstanceClient< 207 client = helper_->CreateAndRegisterMockInstanceClient<
208 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr()); 208 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr());
209 209
210 int64_t registration_id = kInvalidServiceWorkerRegistrationId; 210 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
211 bool called = false; 211 bool called = false;
212 context()->RegisterServiceWorker( 212 context()->RegisterServiceWorker(
213 pattern, 213 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
214 script_url,
215 NULL,
216 MakeRegisteredCallback(&called, &registration_id)); 214 MakeRegisteredCallback(&called, &registration_id));
217 215
218 ASSERT_FALSE(called); 216 ASSERT_FALSE(called);
219 base::RunLoop().RunUntilIdle(); 217 base::RunLoop().RunUntilIdle();
220 EXPECT_TRUE(called); 218 EXPECT_TRUE(called);
221 219
222 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); 220 ASSERT_EQ(2UL, helper_->dispatched_events()->size());
223 ASSERT_EQ(2UL, client->events().size()); 221 ASSERT_EQ(2UL, client->events().size());
224 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, 222 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
225 client->events()[0]); 223 client->events()[0]);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 helper_.reset(new RejectInstallTestHelper); 256 helper_.reset(new RejectInstallTestHelper);
259 helper_->context_wrapper()->AddObserver(this); 257 helper_->context_wrapper()->AddObserver(this);
260 258
261 RecordableEmbeddedWorkerInstanceClient* client = nullptr; 259 RecordableEmbeddedWorkerInstanceClient* client = nullptr;
262 client = helper_->CreateAndRegisterMockInstanceClient< 260 client = helper_->CreateAndRegisterMockInstanceClient<
263 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr()); 261 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr());
264 262
265 int64_t registration_id = kInvalidServiceWorkerRegistrationId; 263 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
266 bool called = false; 264 bool called = false;
267 context()->RegisterServiceWorker( 265 context()->RegisterServiceWorker(
268 pattern, 266 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
269 script_url,
270 NULL,
271 MakeRegisteredCallback(&called, &registration_id)); 267 MakeRegisteredCallback(&called, &registration_id));
272 268
273 ASSERT_FALSE(called); 269 ASSERT_FALSE(called);
274 base::RunLoop().RunUntilIdle(); 270 base::RunLoop().RunUntilIdle();
275 EXPECT_TRUE(called); 271 EXPECT_TRUE(called);
276 272
277 ASSERT_EQ(1UL, helper_->dispatched_events()->size()); 273 ASSERT_EQ(1UL, helper_->dispatched_events()->size());
278 ASSERT_EQ(2UL, client->events().size()); 274 ASSERT_EQ(2UL, client->events().size());
279 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, 275 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
280 client->events()[0]); 276 client->events()[0]);
(...skipping 29 matching lines...) Expand all
310 helper_.reset(new RejectActivateTestHelper); 306 helper_.reset(new RejectActivateTestHelper);
311 helper_->context_wrapper()->AddObserver(this); 307 helper_->context_wrapper()->AddObserver(this);
312 308
313 RecordableEmbeddedWorkerInstanceClient* client = nullptr; 309 RecordableEmbeddedWorkerInstanceClient* client = nullptr;
314 client = helper_->CreateAndRegisterMockInstanceClient< 310 client = helper_->CreateAndRegisterMockInstanceClient<
315 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr()); 311 RecordableEmbeddedWorkerInstanceClient>(helper_->AsWeakPtr());
316 312
317 int64_t registration_id = kInvalidServiceWorkerRegistrationId; 313 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
318 bool called = false; 314 bool called = false;
319 context()->RegisterServiceWorker( 315 context()->RegisterServiceWorker(
320 pattern, script_url, NULL, 316 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
321 MakeRegisteredCallback(&called, &registration_id)); 317 MakeRegisteredCallback(&called, &registration_id));
322 318
323 ASSERT_FALSE(called); 319 ASSERT_FALSE(called);
324 base::RunLoop().RunUntilIdle(); 320 base::RunLoop().RunUntilIdle();
325 EXPECT_TRUE(called); 321 EXPECT_TRUE(called);
326 322
327 ASSERT_EQ(2UL, helper_->dispatched_events()->size()); 323 ASSERT_EQ(2UL, helper_->dispatched_events()->size());
328 ASSERT_EQ(2UL, client->events().size()); 324 ASSERT_EQ(2UL, client->events().size());
329 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker, 325 EXPECT_EQ(RecordableEmbeddedWorkerInstanceClient::Message::StartWorker,
330 client->events()[0]); 326 client->events()[0]);
(...skipping 18 matching lines...) Expand all
349 base::RunLoop().RunUntilIdle(); 345 base::RunLoop().RunUntilIdle();
350 } 346 }
351 347
352 // Make sure registrations are cleaned up when they are unregistered. 348 // Make sure registrations are cleaned up when they are unregistered.
353 TEST_F(ServiceWorkerContextTest, Unregister) { 349 TEST_F(ServiceWorkerContextTest, Unregister) {
354 GURL pattern("http://www.example.com/"); 350 GURL pattern("http://www.example.com/");
355 351
356 bool called = false; 352 bool called = false;
357 int64_t registration_id = kInvalidServiceWorkerRegistrationId; 353 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
358 context()->RegisterServiceWorker( 354 context()->RegisterServiceWorker(
359 pattern,
360 GURL("http://www.example.com/service_worker.js"), 355 GURL("http://www.example.com/service_worker.js"),
361 NULL, 356 ServiceWorkerRegistrationOptions(pattern), nullptr,
362 MakeRegisteredCallback(&called, &registration_id)); 357 MakeRegisteredCallback(&called, &registration_id));
363 358
364 ASSERT_FALSE(called); 359 ASSERT_FALSE(called);
365 base::RunLoop().RunUntilIdle(); 360 base::RunLoop().RunUntilIdle();
366 ASSERT_TRUE(called); 361 ASSERT_TRUE(called);
367 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id); 362 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
368 363
369 called = false; 364 called = false;
370 context()->UnregisterServiceWorker(pattern, 365 context()->UnregisterServiceWorker(pattern,
371 MakeUnregisteredCallback(&called)); 366 MakeUnregisteredCallback(&called));
(...skipping 26 matching lines...) Expand all
398 GURL origin1_p2("http://www.example.com/hello"); 393 GURL origin1_p2("http://www.example.com/hello");
399 GURL origin2_p1("http://www.example.com:8080/again"); 394 GURL origin2_p1("http://www.example.com:8080/again");
400 GURL origin3_p1("http://www.other.com/"); 395 GURL origin3_p1("http://www.other.com/");
401 396
402 bool called = false; 397 bool called = false;
403 int64_t registration_id1 = kInvalidServiceWorkerRegistrationId; 398 int64_t registration_id1 = kInvalidServiceWorkerRegistrationId;
404 int64_t registration_id2 = kInvalidServiceWorkerRegistrationId; 399 int64_t registration_id2 = kInvalidServiceWorkerRegistrationId;
405 int64_t registration_id3 = kInvalidServiceWorkerRegistrationId; 400 int64_t registration_id3 = kInvalidServiceWorkerRegistrationId;
406 int64_t registration_id4 = kInvalidServiceWorkerRegistrationId; 401 int64_t registration_id4 = kInvalidServiceWorkerRegistrationId;
407 context()->RegisterServiceWorker( 402 context()->RegisterServiceWorker(
408 origin1_p1,
409 GURL("http://www.example.com/service_worker.js"), 403 GURL("http://www.example.com/service_worker.js"),
410 NULL, 404 ServiceWorkerRegistrationOptions(origin1_p1), nullptr,
411 MakeRegisteredCallback(&called, &registration_id1)); 405 MakeRegisteredCallback(&called, &registration_id1));
412 context()->RegisterServiceWorker( 406 context()->RegisterServiceWorker(
413 origin1_p2,
414 GURL("http://www.example.com/service_worker2.js"), 407 GURL("http://www.example.com/service_worker2.js"),
415 NULL, 408 ServiceWorkerRegistrationOptions(origin1_p2), nullptr,
416 MakeRegisteredCallback(&called, &registration_id2)); 409 MakeRegisteredCallback(&called, &registration_id2));
417 context()->RegisterServiceWorker( 410 context()->RegisterServiceWorker(
418 origin2_p1,
419 GURL("http://www.example.com:8080/service_worker3.js"), 411 GURL("http://www.example.com:8080/service_worker3.js"),
420 NULL, 412 ServiceWorkerRegistrationOptions(origin2_p1), nullptr,
421 MakeRegisteredCallback(&called, &registration_id3)); 413 MakeRegisteredCallback(&called, &registration_id3));
422 context()->RegisterServiceWorker( 414 context()->RegisterServiceWorker(
423 origin3_p1,
424 GURL("http://www.other.com/service_worker4.js"), 415 GURL("http://www.other.com/service_worker4.js"),
425 NULL, 416 ServiceWorkerRegistrationOptions(origin3_p1), nullptr,
426 MakeRegisteredCallback(&called, &registration_id4)); 417 MakeRegisteredCallback(&called, &registration_id4));
427 418
428 ASSERT_FALSE(called); 419 ASSERT_FALSE(called);
429 base::RunLoop().RunUntilIdle(); 420 base::RunLoop().RunUntilIdle();
430 ASSERT_TRUE(called); 421 ASSERT_TRUE(called);
431 422
432 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id1); 423 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id1);
433 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id2); 424 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id2);
434 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id3); 425 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id3);
435 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id4); 426 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id4);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 EXPECT_EQ(registration_id1, notifications_[5].registration_id); 486 EXPECT_EQ(registration_id1, notifications_[5].registration_id);
496 } 487 }
497 488
498 // Make sure registering a new script shares an existing registration. 489 // Make sure registering a new script shares an existing registration.
499 TEST_F(ServiceWorkerContextTest, RegisterNewScript) { 490 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
500 GURL pattern("http://www.example.com/"); 491 GURL pattern("http://www.example.com/");
501 492
502 bool called = false; 493 bool called = false;
503 int64_t old_registration_id = kInvalidServiceWorkerRegistrationId; 494 int64_t old_registration_id = kInvalidServiceWorkerRegistrationId;
504 context()->RegisterServiceWorker( 495 context()->RegisterServiceWorker(
505 pattern,
506 GURL("http://www.example.com/service_worker.js"), 496 GURL("http://www.example.com/service_worker.js"),
507 NULL, 497 ServiceWorkerRegistrationOptions(pattern), nullptr,
508 MakeRegisteredCallback(&called, &old_registration_id)); 498 MakeRegisteredCallback(&called, &old_registration_id));
509 499
510 ASSERT_FALSE(called); 500 ASSERT_FALSE(called);
511 base::RunLoop().RunUntilIdle(); 501 base::RunLoop().RunUntilIdle();
512 ASSERT_TRUE(called); 502 ASSERT_TRUE(called);
513 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); 503 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
514 504
515 called = false; 505 called = false;
516 int64_t new_registration_id = kInvalidServiceWorkerRegistrationId; 506 int64_t new_registration_id = kInvalidServiceWorkerRegistrationId;
517 context()->RegisterServiceWorker( 507 context()->RegisterServiceWorker(
518 pattern,
519 GURL("http://www.example.com/service_worker_new.js"), 508 GURL("http://www.example.com/service_worker_new.js"),
520 NULL, 509 ServiceWorkerRegistrationOptions(pattern), nullptr,
521 MakeRegisteredCallback(&called, &new_registration_id)); 510 MakeRegisteredCallback(&called, &new_registration_id));
522 511
523 ASSERT_FALSE(called); 512 ASSERT_FALSE(called);
524 base::RunLoop().RunUntilIdle(); 513 base::RunLoop().RunUntilIdle();
525 ASSERT_TRUE(called); 514 ASSERT_TRUE(called);
526 515
527 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id); 516 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id);
528 EXPECT_EQ(old_registration_id, new_registration_id); 517 EXPECT_EQ(old_registration_id, new_registration_id);
529 518
530 ASSERT_EQ(2u, notifications_.size()); 519 ASSERT_EQ(2u, notifications_.size());
531 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); 520 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
532 EXPECT_EQ(pattern, notifications_[0].pattern); 521 EXPECT_EQ(pattern, notifications_[0].pattern);
533 EXPECT_EQ(old_registration_id, notifications_[0].registration_id); 522 EXPECT_EQ(old_registration_id, notifications_[0].registration_id);
534 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type); 523 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
535 EXPECT_EQ(pattern, notifications_[1].pattern); 524 EXPECT_EQ(pattern, notifications_[1].pattern);
536 EXPECT_EQ(new_registration_id, notifications_[1].registration_id); 525 EXPECT_EQ(new_registration_id, notifications_[1].registration_id);
537 } 526 }
538 527
539 // Make sure that when registering a duplicate pattern+script_url 528 // Make sure that when registering a duplicate pattern+script_url
540 // combination, that the same registration is used. 529 // combination, that the same registration is used.
541 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) { 530 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
542 GURL pattern("http://www.example.com/"); 531 GURL pattern("http://www.example.com/");
543 GURL script_url("http://www.example.com/service_worker.js"); 532 GURL script_url("http://www.example.com/service_worker.js");
544 533
545 bool called = false; 534 bool called = false;
546 int64_t old_registration_id = kInvalidServiceWorkerRegistrationId; 535 int64_t old_registration_id = kInvalidServiceWorkerRegistrationId;
547 context()->RegisterServiceWorker( 536 context()->RegisterServiceWorker(
548 pattern, 537 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
549 script_url,
550 NULL,
551 MakeRegisteredCallback(&called, &old_registration_id)); 538 MakeRegisteredCallback(&called, &old_registration_id));
552 539
553 ASSERT_FALSE(called); 540 ASSERT_FALSE(called);
554 base::RunLoop().RunUntilIdle(); 541 base::RunLoop().RunUntilIdle();
555 ASSERT_TRUE(called); 542 ASSERT_TRUE(called);
556 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id); 543 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
557 544
558 called = false; 545 called = false;
559 int64_t new_registration_id = kInvalidServiceWorkerRegistrationId; 546 int64_t new_registration_id = kInvalidServiceWorkerRegistrationId;
560 context()->RegisterServiceWorker( 547 context()->RegisterServiceWorker(
561 pattern, 548 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
562 script_url,
563 NULL,
564 MakeRegisteredCallback(&called, &new_registration_id)); 549 MakeRegisteredCallback(&called, &new_registration_id));
565 550
566 ASSERT_FALSE(called); 551 ASSERT_FALSE(called);
567 base::RunLoop().RunUntilIdle(); 552 base::RunLoop().RunUntilIdle();
568 ASSERT_TRUE(called); 553 ASSERT_TRUE(called);
569 EXPECT_EQ(old_registration_id, new_registration_id); 554 EXPECT_EQ(old_registration_id, new_registration_id);
570 555
571 ASSERT_EQ(2u, notifications_.size()); 556 ASSERT_EQ(2u, notifications_.size());
572 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type); 557 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
573 EXPECT_EQ(pattern, notifications_[0].pattern); 558 EXPECT_EQ(pattern, notifications_[0].pattern);
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 // Reinitialize the helper to test on-disk storage. 668 // Reinitialize the helper to test on-disk storage.
684 base::ScopedTempDir user_data_directory; 669 base::ScopedTempDir user_data_directory;
685 ASSERT_TRUE(user_data_directory.CreateUniqueTempDir()); 670 ASSERT_TRUE(user_data_directory.CreateUniqueTempDir());
686 helper_.reset(new EmbeddedWorkerTestHelper(user_data_directory.GetPath())); 671 helper_.reset(new EmbeddedWorkerTestHelper(user_data_directory.GetPath()));
687 helper_->context_wrapper()->AddObserver(this); 672 helper_->context_wrapper()->AddObserver(this);
688 } 673 }
689 674
690 int64_t registration_id = kInvalidServiceWorkerRegistrationId; 675 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
691 bool called = false; 676 bool called = false;
692 context()->RegisterServiceWorker( 677 context()->RegisterServiceWorker(
693 pattern, 678 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
694 script_url,
695 NULL,
696 MakeRegisteredCallback(&called, &registration_id)); 679 MakeRegisteredCallback(&called, &registration_id));
697 680
698 ASSERT_FALSE(called); 681 ASSERT_FALSE(called);
699 base::RunLoop().RunUntilIdle(); 682 base::RunLoop().RunUntilIdle();
700 EXPECT_TRUE(called); 683 EXPECT_TRUE(called);
701 684
702 context()->storage()->FindRegistrationForId( 685 context()->storage()->FindRegistrationForId(
703 registration_id, 686 registration_id,
704 pattern.GetOrigin(), 687 pattern.GetOrigin(),
705 base::Bind(&ExpectRegisteredWorkers, 688 base::Bind(&ExpectRegisteredWorkers,
(...skipping 22 matching lines...) Expand all
728 registration_id, 711 registration_id,
729 pattern.GetOrigin(), 712 pattern.GetOrigin(),
730 base::Bind(&ExpectRegisteredWorkers, 713 base::Bind(&ExpectRegisteredWorkers,
731 SERVICE_WORKER_ERROR_NOT_FOUND, 714 SERVICE_WORKER_ERROR_NOT_FOUND,
732 false /* expect_waiting */, 715 false /* expect_waiting */,
733 true /* expect_active */)); 716 true /* expect_active */));
734 base::RunLoop().RunUntilIdle(); 717 base::RunLoop().RunUntilIdle();
735 718
736 called = false; 719 called = false;
737 context()->RegisterServiceWorker( 720 context()->RegisterServiceWorker(
738 pattern, 721 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr,
739 script_url,
740 NULL,
741 MakeRegisteredCallback(&called, &registration_id)); 722 MakeRegisteredCallback(&called, &registration_id));
742 723
743 ASSERT_FALSE(called); 724 ASSERT_FALSE(called);
744 base::RunLoop().RunUntilIdle(); 725 base::RunLoop().RunUntilIdle();
745 EXPECT_TRUE(called); 726 EXPECT_TRUE(called);
746 727
747 context()->storage()->FindRegistrationForId( 728 context()->storage()->FindRegistrationForId(
748 registration_id, 729 registration_id,
749 pattern.GetOrigin(), 730 pattern.GetOrigin(),
750 base::Bind(&ExpectRegisteredWorkers, 731 base::Bind(&ExpectRegisteredWorkers,
(...skipping 14 matching lines...) Expand all
765 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type); 746 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type);
766 EXPECT_EQ(pattern, notifications_[2].pattern); 747 EXPECT_EQ(pattern, notifications_[2].pattern);
767 EXPECT_EQ(registration_id, notifications_[2].registration_id); 748 EXPECT_EQ(registration_id, notifications_[2].registration_id);
768 } 749 }
769 750
770 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest, 751 INSTANTIATE_TEST_CASE_P(ServiceWorkerContextRecoveryTest,
771 ServiceWorkerContextRecoveryTest, 752 ServiceWorkerContextRecoveryTest,
772 testing::Bool()); 753 testing::Bool());
773 754
774 } // namespace content 755 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698