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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
7 #include "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
8 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
10 #include "content/browser/service_worker/service_worker_provider_host.h" | 10 #include "content/browser/service_worker/service_worker_provider_host.h" |
11 #include "content/browser/service_worker/service_worker_register_job.h" | 11 #include "content/browser/service_worker/service_worker_register_job.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 12 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/browser/service_worker/service_worker_version.h" | 13 #include "content/browser/service_worker/service_worker_version.h" |
14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace content { | 17 namespace content { |
18 | 18 |
19 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 19 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
20 | 20 |
21 class ServiceWorkerProviderHostTest : public testing::Test { | 21 class ServiceWorkerProviderHostTest : public testing::Test { |
22 protected: | 22 protected: |
23 ServiceWorkerProviderHostTest() | 23 ServiceWorkerProviderHostTest() |
24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
25 ~ServiceWorkerProviderHostTest() override {} | 25 ~ServiceWorkerProviderHostTest() override {} |
26 | 26 |
27 void SetUp() override { | 27 void SetUp() override { |
28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); | 28 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
29 context_ = helper_->context(); | 29 context_ = helper_->context(); |
30 pattern_ = GURL("http://www.example.com/"); | |
31 script_url_ = GURL("http://www.example.com/service_worker.js"); | 30 script_url_ = GURL("http://www.example.com/service_worker.js"); |
32 registration_ = new ServiceWorkerRegistration( | 31 registration1_ = new ServiceWorkerRegistration( |
33 pattern_, 1L, context_->AsWeakPtr()); | 32 GURL("http://www.example.com/"), 1L, context_->AsWeakPtr()); |
34 version_ = new ServiceWorkerVersion( | 33 registration2_ = new ServiceWorkerRegistration( |
35 registration_.get(), script_url_, 1L, context_->AsWeakPtr()); | 34 GURL("http://www.example.com/example"), 2L, context_->AsWeakPtr()); |
36 | 35 |
37 // Prepare provider hosts (for the same process). | 36 // Prepare provider hosts (for the same process). |
38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 37 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
39 kRenderProcessId, MSG_ROUTING_NONE, 1 /* provider_id */, | 38 kRenderProcessId, MSG_ROUTING_NONE, 1 /* provider_id */, |
40 context_->AsWeakPtr(), NULL)); | 39 context_->AsWeakPtr(), NULL)); |
| 40 host1->SetDocumentUrl(GURL("http://www.example.com/example1.html")); |
41 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 41 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
42 kRenderProcessId, MSG_ROUTING_NONE, 2 /* provider_id */, | 42 kRenderProcessId, MSG_ROUTING_NONE, 2 /* provider_id */, |
43 context_->AsWeakPtr(), NULL)); | 43 context_->AsWeakPtr(), NULL)); |
| 44 host2->SetDocumentUrl(GURL("http://www.example.com/example2.html")); |
44 provider_host1_ = host1->AsWeakPtr(); | 45 provider_host1_ = host1->AsWeakPtr(); |
45 provider_host2_ = host2->AsWeakPtr(); | 46 provider_host2_ = host2->AsWeakPtr(); |
46 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 47 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
47 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 48 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
48 } | 49 } |
49 | 50 |
50 void TearDown() override { | 51 void TearDown() override { |
51 version_ = 0; | 52 registration1_ = 0; |
52 registration_ = 0; | 53 registration2_ = 0; |
53 helper_.reset(); | 54 helper_.reset(); |
54 } | 55 } |
55 | 56 |
56 bool HasProcessToRun() const { | 57 bool PatternHasProcessToRun(const GURL& pattern) const { |
57 return context_->process_manager()->PatternHasProcessToRun(pattern_); | 58 return context_->process_manager()->PatternHasProcessToRun(pattern); |
58 } | 59 } |
59 | 60 |
60 content::TestBrowserThreadBundle thread_bundle_; | 61 content::TestBrowserThreadBundle thread_bundle_; |
61 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 62 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
62 ServiceWorkerContextCore* context_; | 63 ServiceWorkerContextCore* context_; |
63 scoped_refptr<ServiceWorkerRegistration> registration_; | 64 scoped_refptr<ServiceWorkerRegistration> registration1_; |
64 scoped_refptr<ServiceWorkerVersion> version_; | 65 scoped_refptr<ServiceWorkerRegistration> registration2_; |
65 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 66 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
66 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 67 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
67 GURL pattern_; | |
68 GURL script_url_; | 68 GURL script_url_; |
69 | 69 |
70 private: | 70 private: |
71 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 71 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
72 }; | 72 }; |
73 | 73 |
74 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 74 TEST_F(ServiceWorkerProviderHostTest, PotentialRegistration_ProcessStatus) { |
75 provider_host1_->AssociateRegistration(registration_.get()); | 75 provider_host1_->AddMatchingRegistration(registration1_.get()); |
76 ASSERT_TRUE(HasProcessToRun()); | 76 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
77 | 77 |
78 // Associating version_ to a provider_host's active version will internally | 78 // Add a same registration twice has no effect. |
79 // add the provider_host's process ref to the version. | 79 provider_host1_->AddMatchingRegistration(registration1_.get()); |
80 registration_->SetActiveVersion(version_.get()); | 80 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
81 ASSERT_TRUE(HasProcessToRun()); | |
82 | 81 |
83 // Re-associating the same version and provider_host should just work too. | 82 // Different matching registrations could be added. |
84 registration_->SetActiveVersion(version_.get()); | 83 provider_host1_->AddMatchingRegistration(registration2_.get()); |
85 ASSERT_TRUE(HasProcessToRun()); | 84 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
86 | 85 |
87 // Resetting the provider_host's active version should remove process refs | 86 // Remove matching registration will decrease the process refs for its |
88 // from the version. | 87 // pattern. |
89 provider_host1_->DisassociateRegistration(); | 88 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
90 ASSERT_FALSE(HasProcessToRun()); | 89 ASSERT_FALSE(PatternHasProcessToRun(registration1_->pattern())); |
| 90 |
| 91 // Multiple provider hosts could add the same matching registration. |
| 92 // The process refs will become 0 after all provider hosts removed them. |
| 93 provider_host2_->AddMatchingRegistration(registration2_.get()); |
| 94 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
| 95 ASSERT_TRUE(PatternHasProcessToRun(registration2_->pattern())); |
| 96 provider_host2_->RemoveMatchingRegistration(registration2_.get()); |
| 97 ASSERT_FALSE(PatternHasProcessToRun(registration2_->pattern())); |
91 } | 98 } |
92 | 99 |
93 TEST_F(ServiceWorkerProviderHostTest, | 100 TEST_F(ServiceWorkerProviderHostTest, AssociatedRegistration_ProcessStatus) { |
94 SetActiveVersion_MultipleHostsForSameProcess) { | 101 // Associate the registration will also increases the process refs for |
95 provider_host1_->AssociateRegistration(registration_.get()); | 102 // the registration's pattern. |
96 provider_host2_->AssociateRegistration(registration_.get()); | 103 provider_host1_->AssociateRegistration(registration1_.get()); |
97 ASSERT_TRUE(HasProcessToRun()); | 104 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
98 | 105 |
99 // Associating version_ to two providers as active version. | 106 // Disassociate the registration shouldn't effect the process refs for |
100 registration_->SetActiveVersion(version_.get()); | 107 // the registration's pattern. |
101 ASSERT_TRUE(HasProcessToRun()); | |
102 | |
103 // Disassociating one provider_host shouldn't remove all process refs | |
104 // from the version yet. | |
105 provider_host1_->DisassociateRegistration(); | 108 provider_host1_->DisassociateRegistration(); |
106 ASSERT_TRUE(HasProcessToRun()); | 109 ASSERT_TRUE(PatternHasProcessToRun(registration1_->pattern())); |
107 | |
108 // Disassociating the other provider_host will remove all process refs. | |
109 provider_host2_->DisassociateRegistration(); | |
110 ASSERT_FALSE(HasProcessToRun()); | |
111 } | 110 } |
112 | 111 |
113 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 112 TEST_F(ServiceWorkerProviderHostTest, MatchRegistration) { |
114 provider_host1_->AssociateRegistration(registration_.get()); | 113 provider_host1_->AddMatchingRegistration(registration1_.get()); |
115 ASSERT_TRUE(HasProcessToRun()); | 114 provider_host1_->AddMatchingRegistration(registration2_.get()); |
116 | 115 |
117 // Associating version_ to a provider_host's waiting version will internally | 116 // Match registration should return the longest matching one. |
118 // add the provider_host's process ref to the version. | 117 ASSERT_EQ(provider_host1_->MatchRegistration(), registration2_); |
119 registration_->SetWaitingVersion(version_.get()); | 118 provider_host1_->RemoveMatchingRegistration(registration2_.get()); |
120 ASSERT_TRUE(HasProcessToRun()); | 119 ASSERT_EQ(provider_host1_->MatchRegistration(), registration1_); |
121 | 120 |
122 // Re-associating the same version and provider_host should just work too. | 121 // Should return nullptr after remove all matching registrations. |
123 registration_->SetWaitingVersion(version_.get()); | 122 provider_host1_->RemoveMatchingRegistration(registration1_.get()); |
124 ASSERT_TRUE(HasProcessToRun()); | 123 ASSERT_EQ(provider_host1_->MatchRegistration(), nullptr); |
125 | |
126 // Resetting the provider_host's waiting version should remove process refs | |
127 // from the version. | |
128 provider_host1_->DisassociateRegistration(); | |
129 ASSERT_FALSE(HasProcessToRun()); | |
130 } | |
131 | |
132 TEST_F(ServiceWorkerProviderHostTest, | |
133 SetWaitingVersion_MultipleHostsForSameProcess) { | |
134 provider_host1_->AssociateRegistration(registration_.get()); | |
135 provider_host2_->AssociateRegistration(registration_.get()); | |
136 ASSERT_TRUE(HasProcessToRun()); | |
137 | |
138 // Associating version_ to two providers as waiting version. | |
139 registration_->SetWaitingVersion(version_.get()); | |
140 ASSERT_TRUE(HasProcessToRun()); | |
141 | |
142 // Disassociating one provider_host shouldn't remove all process refs | |
143 // from the version yet. | |
144 provider_host1_->DisassociateRegistration(); | |
145 ASSERT_TRUE(HasProcessToRun()); | |
146 | |
147 // Disassociating the other provider_host will remove all process refs. | |
148 provider_host2_->DisassociateRegistration(); | |
149 ASSERT_FALSE(HasProcessToRun()); | |
150 } | 124 } |
151 | 125 |
152 } // namespace content | 126 } // namespace content |
OLD | NEW |