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

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

Issue 294593002: ServiceWorker: Support longest-prefix-match for registration scope (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 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/browser/service_worker/service_worker_storage.h" 5 #include "content/browser/service_worker/service_worker_storage.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "content/browser/browser_thread_impl.h" 10 #include "content/browser/browser_thread_impl.h"
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 was_called = false; 435 was_called = false;
436 436
437 storage()->GetAllRegistrations( 437 storage()->GetAllRegistrations(
438 MakeGetAllCallback(&was_called, &all_registrations)); 438 MakeGetAllCallback(&was_called, &all_registrations));
439 base::RunLoop().RunUntilIdle(); 439 base::RunLoop().RunUntilIdle();
440 ASSERT_TRUE(was_called); 440 ASSERT_TRUE(was_called);
441 EXPECT_TRUE(all_registrations.empty()); 441 EXPECT_TRUE(all_registrations.empty());
442 was_called = false; 442 was_called = false;
443 } 443 }
444 444
445 TEST_F(ServiceWorkerStorageTest, FindRegistration_ExactMatch) {
446 const GURL kDocumentUrl("http://www.example.com/scope/foobar");
447 bool was_called = false;
448 ServiceWorkerStatusCode result = SERVICE_WORKER_OK;
449 scoped_refptr<ServiceWorkerRegistration> found_registration;
450
451 // Registration for "/scope/fooba*".
jsbell 2014/05/21 19:20:03 There's a lot of repeated code here - can this be
452 const GURL kScope1("http://www.example.com/scope/fooba*");
453 const GURL kScript1("http://www.example.com/script1.js");
454 const int64 kRegistrationId1 = 1;
455 const int64 kVersionId1 = 1;
456 scoped_refptr<ServiceWorkerRegistration> live_registration1 =
457 new ServiceWorkerRegistration(
458 kScope1, kScript1, kRegistrationId1, context_ptr_);
459 scoped_refptr<ServiceWorkerVersion> live_version1 =
460 new ServiceWorkerVersion(
461 live_registration1, kVersionId1, context_ptr_);
462 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED);
463 live_registration1->set_pending_version(live_version1);
464
465 // Registration for "/scope/foobar".
466 const GURL kScope2("http://www.example.com/scope/foobar");
467 const GURL kScript2("http://www.example.com/script2.js");
468 const int64 kRegistrationId2 = 2;
469 const int64 kVersionId2 = 2;
470 scoped_refptr<ServiceWorkerRegistration> live_registration2 =
471 new ServiceWorkerRegistration(
472 kScope2, kScript2, kRegistrationId2, context_ptr_);
473 scoped_refptr<ServiceWorkerVersion> live_version2 =
474 new ServiceWorkerVersion(
475 live_registration2, kVersionId2, context_ptr_);
476 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED);
477 live_registration2->set_pending_version(live_version2);
478
479 // Registration for "/scope/foobar*".
480 const GURL kScope3("http://www.example.com/scope/foobar*");
481 const GURL kScript3("http://www.example.com/script3.js");
482 const int64 kRegistrationId3 = 3;
483 const int64 kVersionId3 = 3;
484 scoped_refptr<ServiceWorkerRegistration> live_registration3 =
485 new ServiceWorkerRegistration(
486 kScope3, kScript3, kRegistrationId3, context_ptr_);
487 scoped_refptr<ServiceWorkerVersion> live_version3 =
488 new ServiceWorkerVersion(
489 live_registration3, kVersionId3, context_ptr_);
490 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED);
491 live_registration3->set_pending_version(live_version3);
492
493 // Notify storage of they being installed.
494 storage()->NotifyInstallingRegistration(live_registration1);
495 storage()->NotifyInstallingRegistration(live_registration2);
496 storage()->NotifyInstallingRegistration(live_registration3);
497
498 // Find a registration among installing ones.
499 storage()->FindRegistrationForDocument(
500 kDocumentUrl,
501 MakeFindCallback(&was_called, &result, &found_registration));
502 base::RunLoop().RunUntilIdle();
503 ASSERT_TRUE(was_called);
504 EXPECT_EQ(SERVICE_WORKER_OK, result);
505 EXPECT_EQ(live_registration2, found_registration);
jsbell 2014/05/21 19:20:03 See note about exact match vs. longest match. Per
506 was_called = false;
507 found_registration = NULL;
508
509 // Store registrations.
510 storage()->StoreRegistration(live_registration1, live_version1,
511 MakeStatusCallback(&was_called, &result));
512 EXPECT_FALSE(was_called);
513 base::RunLoop().RunUntilIdle();
514 ASSERT_TRUE(was_called);
515 EXPECT_EQ(SERVICE_WORKER_OK, result);
516 was_called = false;
517 storage()->StoreRegistration(live_registration2, live_version2,
518 MakeStatusCallback(&was_called, &result));
519 EXPECT_FALSE(was_called);
520 base::RunLoop().RunUntilIdle();
521 ASSERT_TRUE(was_called);
522 EXPECT_EQ(SERVICE_WORKER_OK, result);
523 was_called = false;
524 storage()->StoreRegistration(live_registration3, live_version3,
525 MakeStatusCallback(&was_called, &result));
526 EXPECT_FALSE(was_called);
527 base::RunLoop().RunUntilIdle();
528 ASSERT_TRUE(was_called);
529 EXPECT_EQ(SERVICE_WORKER_OK, result);
530 was_called = false;
531
532 // Notify storage of installations no longer happening.
533 storage()->NotifyDoneInstallingRegistration(live_registration1);
534 storage()->NotifyDoneInstallingRegistration(live_registration2);
535 storage()->NotifyDoneInstallingRegistration(live_registration3);
536
537 // Find a registration among installed ones.
538 storage()->FindRegistrationForDocument(
539 kDocumentUrl,
540 MakeFindCallback(&was_called, &result, &found_registration));
541 base::RunLoop().RunUntilIdle();
542 ASSERT_TRUE(was_called);
543 EXPECT_EQ(SERVICE_WORKER_OK, result);
544 EXPECT_EQ(live_registration2, found_registration);
545 was_called = false;
546 found_registration = NULL;
547 }
548
549 TEST_F(ServiceWorkerStorageTest, FindRegistration_LongestWildcardMatch) {
550 const GURL kDocumentUrl("http://www.example.com/scope/foobar");
551 bool was_called = false;
552 ServiceWorkerStatusCode result = SERVICE_WORKER_OK;
553 scoped_refptr<ServiceWorkerRegistration> found_registration;
554
555 // Registration for "/scope/foo*".
556 const GURL kScope1("http://www.example.com/scope/foo*");
557 const GURL kScript1("http://www.example.com/script1.js");
558 const int64 kRegistrationId1 = 1;
559 const int64 kVersionId1 = 1;
560 scoped_refptr<ServiceWorkerRegistration> live_registration1 =
561 new ServiceWorkerRegistration(
562 kScope1, kScript1, kRegistrationId1, context_ptr_);
563 scoped_refptr<ServiceWorkerVersion> live_version1 =
564 new ServiceWorkerVersion(
565 live_registration1, kVersionId1, context_ptr_);
566 live_version1->SetStatus(ServiceWorkerVersion::INSTALLED);
567 live_registration1->set_pending_version(live_version1);
568
569 // Registration for "/scope/fooba*".
570 const GURL kScope2("http://www.example.com/scope/fooba*");
571 const GURL kScript2("http://www.example.com/script2.js");
572 const int64 kRegistrationId2 = 2;
573 const int64 kVersionId2 = 2;
574 scoped_refptr<ServiceWorkerRegistration> live_registration2 =
575 new ServiceWorkerRegistration(
576 kScope2, kScript2, kRegistrationId2, context_ptr_);
577 scoped_refptr<ServiceWorkerVersion> live_version2 =
578 new ServiceWorkerVersion(
579 live_registration2, kVersionId2, context_ptr_);
580 live_version2->SetStatus(ServiceWorkerVersion::INSTALLED);
581 live_registration2->set_pending_version(live_version2);
582
583 // Registration for "/scope/foob*".
584 const GURL kScope3("http://www.example.com/scope/foob*");
585 const GURL kScript3("http://www.example.com/script3.js");
586 const int64 kRegistrationId3 = 3;
587 const int64 kVersionId3 = 3;
588 scoped_refptr<ServiceWorkerRegistration> live_registration3 =
589 new ServiceWorkerRegistration(
590 kScope3, kScript3, kRegistrationId3, context_ptr_);
591 scoped_refptr<ServiceWorkerVersion> live_version3 =
592 new ServiceWorkerVersion(
593 live_registration3, kVersionId3, context_ptr_);
594 live_version3->SetStatus(ServiceWorkerVersion::INSTALLED);
595 live_registration3->set_pending_version(live_version3);
596
597 // Notify storage of they being installed.
598 storage()->NotifyInstallingRegistration(live_registration1);
599 storage()->NotifyInstallingRegistration(live_registration2);
600 storage()->NotifyInstallingRegistration(live_registration3);
601
602 // Find a registration among installing ones.
603 storage()->FindRegistrationForDocument(
604 kDocumentUrl,
605 MakeFindCallback(&was_called, &result, &found_registration));
606 base::RunLoop().RunUntilIdle();
607 ASSERT_TRUE(was_called);
608 EXPECT_EQ(SERVICE_WORKER_OK, result);
609 EXPECT_EQ(live_registration2, found_registration);
610 was_called = false;
611 found_registration = NULL;
612
613 // Store registrations.
614 storage()->StoreRegistration(live_registration1, live_version1,
615 MakeStatusCallback(&was_called, &result));
616 EXPECT_FALSE(was_called);
617 base::RunLoop().RunUntilIdle();
618 ASSERT_TRUE(was_called);
619 EXPECT_EQ(SERVICE_WORKER_OK, result);
620 was_called = false;
621 storage()->StoreRegistration(live_registration2, live_version2,
622 MakeStatusCallback(&was_called, &result));
623 EXPECT_FALSE(was_called);
624 base::RunLoop().RunUntilIdle();
625 ASSERT_TRUE(was_called);
626 EXPECT_EQ(SERVICE_WORKER_OK, result);
627 was_called = false;
628 storage()->StoreRegistration(live_registration3, live_version3,
629 MakeStatusCallback(&was_called, &result));
630 EXPECT_FALSE(was_called);
631 base::RunLoop().RunUntilIdle();
632 ASSERT_TRUE(was_called);
633 EXPECT_EQ(SERVICE_WORKER_OK, result);
634 was_called = false;
635
636 // Notify storage of installations no longer happening.
637 storage()->NotifyDoneInstallingRegistration(live_registration1);
638 storage()->NotifyDoneInstallingRegistration(live_registration2);
639 storage()->NotifyDoneInstallingRegistration(live_registration3);
640
641 // Find a registration among installed ones.
642 storage()->FindRegistrationForDocument(
643 kDocumentUrl,
644 MakeFindCallback(&was_called, &result, &found_registration));
645 base::RunLoop().RunUntilIdle();
646 ASSERT_TRUE(was_called);
647 EXPECT_EQ(SERVICE_WORKER_OK, result);
648 EXPECT_EQ(live_registration2, found_registration);
649 was_called = false;
650 found_registration = NULL;
651 }
652
445 } // namespace content 653 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698