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 "content/browser/service_worker/service_worker_context_core.h" | 7 #include "content/browser/service_worker/service_worker_context_core.h" |
8 #include "content/browser/service_worker/service_worker_provider_host.h" | 8 #include "content/browser/service_worker/service_worker_provider_host.h" |
9 #include "content/browser/service_worker/service_worker_register_job.h" | 9 #include "content/browser/service_worker/service_worker_register_job.h" |
10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 registration_, | 40 registration_, |
41 1L, context_->AsWeakPtr()); | 41 1L, context_->AsWeakPtr()); |
42 | 42 |
43 // Prepare provider hosts (for the same process). | 43 // Prepare provider hosts (for the same process). |
44 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 44 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
45 kRenderProcessId, 1 /* provider_id */, | 45 kRenderProcessId, 1 /* provider_id */, |
46 context_->AsWeakPtr(), NULL)); | 46 context_->AsWeakPtr(), NULL)); |
47 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 47 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
48 kRenderProcessId, 2 /* provider_id */, | 48 kRenderProcessId, 2 /* provider_id */, |
49 context_->AsWeakPtr(), NULL)); | 49 context_->AsWeakPtr(), NULL)); |
50 scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost( | |
51 kRenderProcessId, 3 /* provider_id */, | |
52 context_->AsWeakPtr(), NULL)); | |
53 provider_host1_ = host1->AsWeakPtr(); | 50 provider_host1_ = host1->AsWeakPtr(); |
54 provider_host2_ = host2->AsWeakPtr(); | 51 provider_host2_ = host2->AsWeakPtr(); |
55 provider_host3_ = host3->AsWeakPtr(); | |
56 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 52 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
57 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 53 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
58 context_->AddProviderHost(make_scoped_ptr(host3.release())); | |
59 } | 54 } |
60 | 55 |
61 virtual void TearDown() OVERRIDE { | 56 virtual void TearDown() OVERRIDE { |
62 version_ = 0; | 57 version_ = 0; |
63 registration_ = 0; | 58 registration_ = 0; |
64 context_.reset(); | 59 context_.reset(); |
65 } | 60 } |
66 | 61 |
62 void SetActiveVersion( | |
falken
2014/08/06 16:09:35
I don't understand the motivation for this functio
nhiroki
2014/08/07 01:27:59
ServiceWorkerProviderHost::SetXXXVersion() is priv
| |
63 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | |
64 ServiceWorkerVersion* version) { | |
65 provider_host->SetActiveVersion(version); | |
66 } | |
67 | |
68 void SetWaitingVersion( | |
69 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | |
70 ServiceWorkerVersion* version) { | |
71 provider_host->SetWaitingVersion(version); | |
72 } | |
73 | |
67 content::TestBrowserThreadBundle thread_bundle_; | 74 content::TestBrowserThreadBundle thread_bundle_; |
68 scoped_ptr<ServiceWorkerContextCore> context_; | 75 scoped_ptr<ServiceWorkerContextCore> context_; |
69 scoped_refptr<ServiceWorkerRegistration> registration_; | 76 scoped_refptr<ServiceWorkerRegistration> registration_; |
70 scoped_refptr<ServiceWorkerVersion> version_; | 77 scoped_refptr<ServiceWorkerVersion> version_; |
71 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 78 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
72 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 79 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
73 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | |
74 GURL scope_; | 80 GURL scope_; |
75 GURL script_url_; | 81 GURL script_url_; |
76 | 82 |
77 private: | 83 private: |
78 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 84 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
79 }; | 85 }; |
80 | 86 |
81 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 87 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { |
82 ASSERT_FALSE(version_->HasProcessToRun()); | 88 ASSERT_FALSE(version_->HasProcessToRun()); |
83 | 89 |
84 // Associating version_ to a provider_host's active version will internally | 90 // Associating version_ to a provider_host's active version will internally |
85 // add the provider_host's process ref to the version. | 91 // add the provider_host's process ref to the version. |
86 provider_host1_->SetActiveVersion(version_); | 92 SetActiveVersion(provider_host1_, version_); |
87 ASSERT_TRUE(version_->HasProcessToRun()); | 93 ASSERT_TRUE(version_->HasProcessToRun()); |
88 | 94 |
89 // Re-associating the same version and provider_host should just work too. | 95 // Re-associating the same version and provider_host should just work too. |
90 provider_host1_->SetActiveVersion(version_); | 96 SetActiveVersion(provider_host1_, version_); |
91 ASSERT_TRUE(version_->HasProcessToRun()); | 97 ASSERT_TRUE(version_->HasProcessToRun()); |
92 | 98 |
93 // Resetting the provider_host's active version should remove process refs | 99 // Resetting the provider_host's active version should remove process refs |
94 // from the version. | 100 // from the version. |
95 provider_host1_->SetActiveVersion(NULL); | 101 SetActiveVersion(provider_host1_, NULL); |
96 ASSERT_FALSE(version_->HasProcessToRun()); | 102 ASSERT_FALSE(version_->HasProcessToRun()); |
97 } | 103 } |
98 | 104 |
99 TEST_F(ServiceWorkerProviderHostTest, | 105 TEST_F(ServiceWorkerProviderHostTest, |
100 SetActiveVersion_MultipleHostsForSameProcess) { | 106 SetActiveVersion_MultipleHostsForSameProcess) { |
101 ASSERT_FALSE(version_->HasProcessToRun()); | 107 ASSERT_FALSE(version_->HasProcessToRun()); |
102 | 108 |
103 // Associating version_ to two providers as active version. | 109 // Associating version_ to two providers as active version. |
104 provider_host1_->SetActiveVersion(version_); | 110 SetActiveVersion(provider_host1_, version_); |
105 provider_host2_->SetActiveVersion(version_); | 111 SetActiveVersion(provider_host2_, version_); |
106 ASSERT_TRUE(version_->HasProcessToRun()); | 112 ASSERT_TRUE(version_->HasProcessToRun()); |
107 | 113 |
108 // Disassociating one provider_host shouldn't remove all process refs | 114 // Disassociating one provider_host shouldn't remove all process refs |
109 // from the version yet. | 115 // from the version yet. |
110 provider_host1_->SetActiveVersion(NULL); | 116 SetActiveVersion(provider_host1_, NULL); |
111 ASSERT_TRUE(version_->HasProcessToRun()); | 117 ASSERT_TRUE(version_->HasProcessToRun()); |
112 | 118 |
113 // Disassociating the other provider_host will remove all process refs. | 119 // Disassociating the other provider_host will remove all process refs. |
114 provider_host2_->SetActiveVersion(NULL); | 120 SetActiveVersion(provider_host2_, NULL); |
115 ASSERT_FALSE(version_->HasProcessToRun()); | 121 ASSERT_FALSE(version_->HasProcessToRun()); |
116 } | 122 } |
117 | 123 |
118 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 124 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { |
119 ASSERT_FALSE(version_->HasProcessToRun()); | 125 ASSERT_FALSE(version_->HasProcessToRun()); |
120 | 126 |
121 // Associating version_ to a provider_host's waiting version will internally | 127 // Associating version_ to a provider_host's waiting version will internally |
122 // add the provider_host's process ref to the version. | 128 // add the provider_host's process ref to the version. |
123 provider_host1_->SetWaitingVersion(version_); | 129 SetWaitingVersion(provider_host1_, version_); |
124 ASSERT_TRUE(version_->HasProcessToRun()); | 130 ASSERT_TRUE(version_->HasProcessToRun()); |
125 | 131 |
126 // Re-associating the same version and provider_host should just work too. | 132 // Re-associating the same version and provider_host should just work too. |
127 provider_host1_->SetWaitingVersion(version_); | 133 SetWaitingVersion(provider_host1_, version_); |
128 ASSERT_TRUE(version_->HasProcessToRun()); | 134 ASSERT_TRUE(version_->HasProcessToRun()); |
129 | 135 |
130 // Resetting the provider_host's waiting version should remove process refs | 136 // Resetting the provider_host's waiting version should remove process refs |
131 // from the version. | 137 // from the version. |
132 provider_host1_->SetWaitingVersion(NULL); | 138 SetWaitingVersion(provider_host1_, NULL); |
133 ASSERT_FALSE(version_->HasProcessToRun()); | 139 ASSERT_FALSE(version_->HasProcessToRun()); |
134 } | 140 } |
135 | 141 |
136 TEST_F(ServiceWorkerProviderHostTest, | 142 TEST_F(ServiceWorkerProviderHostTest, |
137 SetWaitingVersion_MultipleHostsForSameProcess) { | 143 SetWaitingVersion_MultipleHostsForSameProcess) { |
138 ASSERT_FALSE(version_->HasProcessToRun()); | 144 ASSERT_FALSE(version_->HasProcessToRun()); |
139 | 145 |
140 // Associating version_ to two providers as active version. | 146 // Associating version_ to two providers as active version. |
141 provider_host1_->SetWaitingVersion(version_); | 147 SetWaitingVersion(provider_host1_, version_); |
142 provider_host2_->SetWaitingVersion(version_); | 148 SetWaitingVersion(provider_host2_, version_); |
143 ASSERT_TRUE(version_->HasProcessToRun()); | 149 ASSERT_TRUE(version_->HasProcessToRun()); |
144 | 150 |
145 // Disassociating one provider_host shouldn't remove all process refs | 151 // Disassociating one provider_host shouldn't remove all process refs |
146 // from the version yet. | 152 // from the version yet. |
147 provider_host1_->SetWaitingVersion(NULL); | 153 SetWaitingVersion(provider_host1_, NULL); |
148 ASSERT_TRUE(version_->HasProcessToRun()); | 154 ASSERT_TRUE(version_->HasProcessToRun()); |
149 | 155 |
150 // Disassociating the other provider_host will remove all process refs. | 156 // Disassociating the other provider_host will remove all process refs. |
151 provider_host2_->SetWaitingVersion(NULL); | 157 SetWaitingVersion(provider_host2_, NULL); |
152 ASSERT_FALSE(version_->HasProcessToRun()); | 158 ASSERT_FALSE(version_->HasProcessToRun()); |
153 } | 159 } |
154 | 160 |
155 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { | |
156 protected: | |
157 ServiceWorkerProviderHostWaitingVersionTest() | |
158 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
159 next_provider_id_(1L) {} | |
160 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} | |
161 | |
162 virtual void SetUp() OVERRIDE { | |
163 context_.reset( | |
164 new ServiceWorkerContextCore(base::FilePath(), | |
165 base::MessageLoopProxy::current(), | |
166 base::MessageLoopProxy::current(), | |
167 base::MessageLoopProxy::current(), | |
168 NULL, | |
169 NULL, | |
170 NULL)); | |
171 | |
172 // Prepare provider hosts (for the same process). | |
173 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); | |
174 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); | |
175 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); | |
176 } | |
177 | |
178 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( | |
179 const GURL& document_url) { | |
180 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | |
181 kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL)); | |
182 host->SetDocumentUrl(document_url); | |
183 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | |
184 context_->AddProviderHost(host.Pass()); | |
185 return provider_host; | |
186 } | |
187 | |
188 virtual void TearDown() OVERRIDE { | |
189 context_.reset(); | |
190 } | |
191 | |
192 content::TestBrowserThreadBundle thread_bundle_; | |
193 scoped_ptr<ServiceWorkerContextCore> context_; | |
194 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | |
195 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | |
196 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | |
197 | |
198 private: | |
199 int64 next_provider_id_; | |
200 | |
201 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); | |
202 }; | |
203 | |
204 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | |
205 AssociateInstallingVersionToDocuments) { | |
206 const GURL scope1("http://www.example.com/"); | |
207 const GURL script_url1("http://www.example.com/service_worker1.js"); | |
208 scoped_refptr<ServiceWorkerRegistration> registration1( | |
209 new ServiceWorkerRegistration( | |
210 scope1, script_url1, 1L, context_->AsWeakPtr())); | |
211 scoped_refptr<ServiceWorkerVersion> version1( | |
212 new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr())); | |
213 | |
214 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
215 context_->AsWeakPtr(), version1); | |
216 EXPECT_EQ(version1.get(), provider_host1_->installing_version()); | |
217 EXPECT_EQ(version1.get(), provider_host2_->installing_version()); | |
218 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
219 | |
220 // Version2 is associated with the same registration as version1, so the | |
221 // waiting version of host1 and host2 should be replaced. | |
222 scoped_refptr<ServiceWorkerVersion> version2( | |
223 new ServiceWorkerVersion(registration1, 2L, context_->AsWeakPtr())); | |
224 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
225 context_->AsWeakPtr(), version2); | |
226 EXPECT_EQ(version2.get(), provider_host1_->installing_version()); | |
227 EXPECT_EQ(version2.get(), provider_host2_->installing_version()); | |
228 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
229 | |
230 const GURL scope3(provider_host1_->document_url()); | |
231 const GURL script_url3("http://www.example.com/service_worker3.js"); | |
232 scoped_refptr<ServiceWorkerRegistration> registration3( | |
233 new ServiceWorkerRegistration( | |
234 scope3, script_url3, 3L, context_->AsWeakPtr())); | |
235 scoped_refptr<ServiceWorkerVersion> version3( | |
236 new ServiceWorkerVersion(registration3, 3L, context_->AsWeakPtr())); | |
237 | |
238 // Although version3 can match longer than version2 for host1, it should be | |
239 // ignored because version3 is associated with a different registration from | |
240 // version2. | |
241 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
242 context_->AsWeakPtr(), version3); | |
243 EXPECT_EQ(version2.get(), provider_host1_->installing_version()); | |
244 EXPECT_EQ(version2.get(), provider_host2_->installing_version()); | |
245 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
246 } | |
247 | |
248 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | |
249 DisassociateVersionFromDocuments) { | |
250 const GURL scope1("http://www.example.com/"); | |
251 const GURL script_url1("http://www.example.com/service_worker.js"); | |
252 scoped_refptr<ServiceWorkerRegistration> registration1( | |
253 new ServiceWorkerRegistration( | |
254 scope1, script_url1, 1L, context_->AsWeakPtr())); | |
255 scoped_refptr<ServiceWorkerVersion> version1( | |
256 new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr())); | |
257 | |
258 const GURL scope2("http://www.example.ca/"); | |
259 const GURL script_url2("http://www.example.ca/service_worker.js"); | |
260 scoped_refptr<ServiceWorkerRegistration> registration2( | |
261 new ServiceWorkerRegistration( | |
262 scope2, script_url2, 2L, context_->AsWeakPtr())); | |
263 scoped_refptr<ServiceWorkerVersion> version2( | |
264 new ServiceWorkerVersion(registration2, 2L, context_->AsWeakPtr())); | |
265 | |
266 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
267 context_->AsWeakPtr(), version1); | |
268 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
269 context_->AsWeakPtr(), version2); | |
270 | |
271 // Host1 and host2 are associated with version1 as a waiting version, whereas | |
272 // host3 is associated with version2. | |
273 EXPECT_EQ(version1.get(), provider_host1_->installing_version()); | |
274 EXPECT_EQ(version1.get(), provider_host2_->installing_version()); | |
275 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); | |
276 | |
277 // Disassociate version1 from host1 and host2. | |
278 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | |
279 context_->AsWeakPtr(), version1); | |
280 EXPECT_EQ(NULL, provider_host1_->installing_version()); | |
281 EXPECT_EQ(NULL, provider_host2_->installing_version()); | |
282 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); | |
283 } | |
284 | |
285 } // namespace content | 161 } // namespace content |
OLD | NEW |