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 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // Disassociating one provider_host shouldn't remove all process refs | 144 // Disassociating one provider_host shouldn't remove all process refs |
145 // from the version yet. | 145 // from the version yet. |
146 provider_host1_->SetWaitingVersion(NULL); | 146 provider_host1_->SetWaitingVersion(NULL); |
147 ASSERT_TRUE(version_->HasProcessToRun()); | 147 ASSERT_TRUE(version_->HasProcessToRun()); |
148 | 148 |
149 // Disassociating the other provider_host will remove all process refs. | 149 // Disassociating the other provider_host will remove all process refs. |
150 provider_host2_->SetWaitingVersion(NULL); | 150 provider_host2_->SetWaitingVersion(NULL); |
151 ASSERT_FALSE(version_->HasProcessToRun()); | 151 ASSERT_FALSE(version_->HasProcessToRun()); |
152 } | 152 } |
153 | 153 |
154 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { | |
155 protected: | |
156 ServiceWorkerProviderHostWaitingVersionTest() | |
157 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | |
158 next_provider_id_(1L) {} | |
159 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} | |
160 | |
161 virtual void SetUp() OVERRIDE { | |
162 context_.reset( | |
163 new ServiceWorkerContextCore(base::FilePath(), | |
164 base::MessageLoopProxy::current(), | |
165 base::MessageLoopProxy::current(), | |
166 NULL, | |
167 NULL, | |
168 NULL)); | |
169 | |
170 // Prepare provider hosts (for the same process). | |
171 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); | |
172 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); | |
173 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); | |
174 } | |
175 | |
176 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( | |
177 const GURL& document_url) { | |
178 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | |
179 kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL)); | |
180 host->SetDocumentUrl(document_url); | |
181 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | |
182 context_->AddProviderHost(host.Pass()); | |
183 return provider_host; | |
184 } | |
185 | |
186 virtual void TearDown() OVERRIDE { | |
187 context_.reset(); | |
188 } | |
189 | |
190 content::TestBrowserThreadBundle thread_bundle_; | |
191 scoped_ptr<ServiceWorkerContextCore> context_; | |
192 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | |
193 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | |
194 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | |
195 | |
196 private: | |
197 int64 next_provider_id_; | |
198 | |
199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); | |
200 }; | |
201 | |
202 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | |
203 AssociateInstallingVersionToDocuments) { | |
204 const GURL scope1("http://www.example.com/*"); | |
205 const GURL script_url1("http://www.example.com/service_worker1.js"); | |
206 scoped_refptr<ServiceWorkerRegistration> registration1( | |
207 new ServiceWorkerRegistration( | |
208 scope1, script_url1, 1L, context_->AsWeakPtr())); | |
209 scoped_refptr<ServiceWorkerVersion> version1( | |
210 new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr())); | |
211 | |
212 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
213 context_->AsWeakPtr(), version1); | |
214 EXPECT_EQ(version1.get(), provider_host1_->installing_version()); | |
215 EXPECT_EQ(version1.get(), provider_host2_->installing_version()); | |
216 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
217 | |
218 // Version2 is associated with the same registration as version1, so the | |
219 // waiting version of host1 and host2 should be replaced. | |
220 scoped_refptr<ServiceWorkerVersion> version2( | |
221 new ServiceWorkerVersion(registration1, 2L, context_->AsWeakPtr())); | |
222 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
223 context_->AsWeakPtr(), version2); | |
224 EXPECT_EQ(version2.get(), provider_host1_->installing_version()); | |
225 EXPECT_EQ(version2.get(), provider_host2_->installing_version()); | |
226 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
227 | |
228 const GURL scope3(provider_host1_->document_url()); | |
229 const GURL script_url3("http://www.example.com/service_worker3.js"); | |
230 scoped_refptr<ServiceWorkerRegistration> registration3( | |
231 new ServiceWorkerRegistration( | |
232 scope3, script_url3, 3L, context_->AsWeakPtr())); | |
233 scoped_refptr<ServiceWorkerVersion> version3( | |
234 new ServiceWorkerVersion(registration3, 3L, context_->AsWeakPtr())); | |
235 | |
236 // Although version3 can match longer than version2 for host1, it should be | |
237 // ignored because version3 is associated with a different registration from | |
238 // version2. | |
239 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
240 context_->AsWeakPtr(), version3); | |
241 EXPECT_EQ(version2.get(), provider_host1_->installing_version()); | |
242 EXPECT_EQ(version2.get(), provider_host2_->installing_version()); | |
243 EXPECT_EQ(NULL, provider_host3_->installing_version()); | |
244 } | |
245 | |
246 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | |
247 DisassociateVersionFromDocuments) { | |
248 const GURL scope1("http://www.example.com/*"); | |
249 const GURL script_url1("http://www.example.com/service_worker.js"); | |
250 scoped_refptr<ServiceWorkerRegistration> registration1( | |
251 new ServiceWorkerRegistration( | |
252 scope1, script_url1, 1L, context_->AsWeakPtr())); | |
253 scoped_refptr<ServiceWorkerVersion> version1( | |
254 new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr())); | |
255 | |
256 const GURL scope2("http://www.example.ca/*"); | |
257 const GURL script_url2("http://www.example.ca/service_worker.js"); | |
258 scoped_refptr<ServiceWorkerRegistration> registration2( | |
259 new ServiceWorkerRegistration( | |
260 scope2, script_url2, 2L, context_->AsWeakPtr())); | |
261 scoped_refptr<ServiceWorkerVersion> version2( | |
262 new ServiceWorkerVersion(registration2, 2L, context_->AsWeakPtr())); | |
263 | |
264 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
265 context_->AsWeakPtr(), version1); | |
266 ServiceWorkerRegisterJob::AssociateInstallingVersionToDocuments( | |
267 context_->AsWeakPtr(), version2); | |
268 | |
269 // Host1 and host2 are associated with version1 as a waiting version, whereas | |
270 // host3 is associated with version2. | |
271 EXPECT_EQ(version1.get(), provider_host1_->installing_version()); | |
272 EXPECT_EQ(version1.get(), provider_host2_->installing_version()); | |
273 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); | |
274 | |
275 // Disassociate version1 from host1 and host2. | |
276 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | |
277 context_->AsWeakPtr(), version1); | |
278 EXPECT_EQ(NULL, provider_host1_->installing_version()); | |
279 EXPECT_EQ(NULL, provider_host2_->installing_version()); | |
280 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); | |
281 } | |
282 | |
283 } // namespace content | 154 } // namespace content |
OLD | NEW |