OLD | NEW |
---|---|
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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "content/browser/service_worker/embedded_worker_instance.h" | 9 #include "content/browser/service_worker/embedded_worker_instance.h" |
10 #include "content/browser/service_worker/embedded_worker_registry.h" | 10 #include "content/browser/service_worker/embedded_worker_registry.h" |
(...skipping 532 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
543 public: | 543 public: |
544 typedef ServiceWorkerBlackBoxBrowserTest self; | 544 typedef ServiceWorkerBlackBoxBrowserTest self; |
545 | 545 |
546 static void ExpectResultAndRun(bool expected, | 546 static void ExpectResultAndRun(bool expected, |
547 const base::Closure& continuation, | 547 const base::Closure& continuation, |
548 bool actual) { | 548 bool actual) { |
549 EXPECT_EQ(expected, actual); | 549 EXPECT_EQ(expected, actual); |
550 continuation.Run(); | 550 continuation.Run(); |
551 } | 551 } |
552 | 552 |
553 int RenderProcessID() { | |
554 return shell()->web_contents()->GetRenderProcessHost()->GetID(); | |
555 } | |
556 | |
557 void FindRegistrationOnIO(const GURL& document_url, | 553 void FindRegistrationOnIO(const GURL& document_url, |
558 ServiceWorkerStatusCode* status, | 554 ServiceWorkerStatusCode* status, |
559 GURL* script_url, | 555 GURL* script_url, |
560 const base::Closure& continuation) { | 556 const base::Closure& continuation) { |
561 wrapper()->context()->storage()->FindRegistrationForDocument( | 557 wrapper()->context()->storage()->FindRegistrationForDocument( |
562 document_url, | 558 document_url, |
563 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO2, | 559 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO2, |
564 this, | 560 this, |
565 status, | 561 status, |
566 script_url, | 562 script_url, |
567 continuation)); | 563 continuation)); |
568 } | 564 } |
569 | 565 |
570 void FindRegistrationOnIO2( | 566 void FindRegistrationOnIO2( |
571 ServiceWorkerStatusCode* out_status, | 567 ServiceWorkerStatusCode* out_status, |
572 GURL* script_url, | 568 GURL* script_url, |
573 const base::Closure& continuation, | 569 const base::Closure& continuation, |
574 ServiceWorkerStatusCode status, | 570 ServiceWorkerStatusCode status, |
575 const scoped_refptr<ServiceWorkerRegistration>& registration) { | 571 const scoped_refptr<ServiceWorkerRegistration>& registration) { |
576 *out_status = status; | 572 *out_status = status; |
577 if (registration) { | 573 if (registration) { |
578 *script_url = registration->script_url(); | 574 *script_url = registration->script_url(); |
579 } else { | 575 } else { |
580 EXPECT_NE(SERVICE_WORKER_OK, status); | 576 EXPECT_NE(SERVICE_WORKER_OK, status); |
581 } | 577 } |
582 continuation.Run(); | 578 continuation.Run(); |
583 } | 579 } |
584 }; | 580 }; |
585 | 581 |
582 static int CountRenderProcessHosts() { | |
583 int result = 0; | |
584 for (RenderProcessHost::iterator iter(RenderProcessHost::AllHostsIterator()); | |
585 !iter.IsAtEnd(); | |
586 iter.Advance()) { | |
587 result++; | |
588 } | |
589 return result; | |
590 } | |
591 | |
586 IN_PROC_BROWSER_TEST_F(ServiceWorkerBlackBoxBrowserTest, Registration) { | 592 IN_PROC_BROWSER_TEST_F(ServiceWorkerBlackBoxBrowserTest, Registration) { |
593 // Close the only window to be sure we're not re-using its RenderProcessHost. | |
594 shell()->Close(); | |
595 EXPECT_EQ(0, CountRenderProcessHosts()); | |
596 | |
587 const std::string kWorkerUrl = "/service_worker/fetch_event.js"; | 597 const std::string kWorkerUrl = "/service_worker/fetch_event.js"; |
588 | 598 |
589 // Unregistering nothing should return true. | 599 // Unregistering nothing should return true. |
590 { | 600 { |
591 base::RunLoop run_loop; | 601 base::RunLoop run_loop; |
592 public_context()->UnregisterServiceWorker( | 602 public_context()->UnregisterServiceWorker( |
593 embedded_test_server()->GetURL("/*"), | 603 embedded_test_server()->GetURL("/*"), |
594 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, | 604 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, |
595 true, | 605 true, |
596 run_loop.QuitClosure())); | 606 run_loop.QuitClosure())); |
597 run_loop.Run(); | 607 run_loop.Run(); |
598 } | 608 } |
599 | 609 |
610 // If we use a worker URL that doesn't exist, registration fails. | |
611 { | |
612 base::RunLoop run_loop; | |
613 public_context()->RegisterServiceWorker( | |
614 embedded_test_server()->GetURL("/*"), | |
615 embedded_test_server()->GetURL("/does/not/exist"), | |
616 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, | |
617 false, | |
618 run_loop.QuitClosure())); | |
619 run_loop.Run(); | |
620 } | |
621 EXPECT_EQ(0, CountRenderProcessHosts()); | |
622 | |
600 // Register returns when the promise would be resolved. | 623 // Register returns when the promise would be resolved. |
601 { | 624 { |
602 base::RunLoop run_loop; | 625 base::RunLoop run_loop; |
603 public_context()->RegisterServiceWorker( | 626 public_context()->RegisterServiceWorker( |
604 embedded_test_server()->GetURL("/*"), | 627 embedded_test_server()->GetURL("/*"), |
605 embedded_test_server()->GetURL(kWorkerUrl), | 628 embedded_test_server()->GetURL(kWorkerUrl), |
606 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, | 629 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, |
607 true, | 630 true, |
608 run_loop.QuitClosure())); | 631 run_loop.QuitClosure())); |
609 run_loop.Run(); | 632 run_loop.Run(); |
610 } | 633 } |
634 EXPECT_EQ(1, CountRenderProcessHosts()); | |
611 | 635 |
612 // Registering again should succeed, although the algo still | 636 // Registering again should succeed, although the algo still |
613 // might not be complete. | 637 // might not be complete. |
614 { | 638 { |
615 base::RunLoop run_loop; | 639 base::RunLoop run_loop; |
616 public_context()->RegisterServiceWorker( | 640 public_context()->RegisterServiceWorker( |
617 embedded_test_server()->GetURL("/*"), | 641 embedded_test_server()->GetURL("/*"), |
618 embedded_test_server()->GetURL(kWorkerUrl), | 642 embedded_test_server()->GetURL(kWorkerUrl), |
619 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, | 643 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, |
620 true, | 644 true, |
621 run_loop.QuitClosure())); | 645 run_loop.QuitClosure())); |
622 run_loop.Run(); | 646 run_loop.Run(); |
623 } | 647 } |
624 | 648 |
625 // The registration algo might not be far enough along to have | 649 // The registration algo might not be far enough along to have |
626 // stored the registration data, so it may not be findable | 650 // stored the registration data, so it may not be findable |
627 // at this point. | 651 // at this point. |
628 | 652 |
629 // Unregistering something should return true. | 653 // Unregistering something should return true. |
630 { | 654 { |
631 base::RunLoop run_loop; | 655 base::RunLoop run_loop; |
632 public_context()->UnregisterServiceWorker( | 656 public_context()->UnregisterServiceWorker( |
633 embedded_test_server()->GetURL("/*"), | 657 embedded_test_server()->GetURL("/*"), |
634 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, | 658 base::Bind(&ServiceWorkerBlackBoxBrowserTest::ExpectResultAndRun, |
635 true, | 659 true, |
636 run_loop.QuitClosure())); | 660 run_loop.QuitClosure())); |
637 run_loop.Run(); | 661 run_loop.Run(); |
638 } | 662 } |
663 EXPECT_LE(CountRenderProcessHosts(), 1) << "Unregistering doesn't stop the " | |
dominicc (has gone to gerrit)
2014/05/20 02:01:18
Isn't it expected, actual?
Jeffrey Yasskin
2014/05/20 05:20:10
For EQ and NE, yes. For the inequalities, https://
kinuko
2014/05/20 06:30:03
Ah yes, that might be the case.
| |
664 "workers eagerly, so their RPHs " | |
665 "can still be running."; | |
639 | 666 |
640 // Should not be able to find it. | 667 // Should not be able to find it. |
641 { | 668 { |
642 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 669 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
643 GURL script_url; | 670 GURL script_url; |
644 RunOnIOThread( | 671 RunOnIOThread( |
645 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, | 672 base::Bind(&ServiceWorkerBlackBoxBrowserTest::FindRegistrationOnIO, |
646 this, | 673 this, |
647 embedded_test_server()->GetURL("/service_worker/empty.html"), | 674 embedded_test_server()->GetURL("/service_worker/empty.html"), |
648 &status, | 675 &status, |
649 &script_url)); | 676 &script_url)); |
650 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); | 677 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, status); |
651 } | 678 } |
652 } | 679 } |
653 | 680 |
654 } // namespace content | 681 } // namespace content |
OLD | NEW |