| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // Disassociating one provider_host shouldn't remove all process refs | 145 // Disassociating one provider_host shouldn't remove all process refs |
| 146 // from the version yet. | 146 // from the version yet. |
| 147 provider_host1_->SetWaitingVersion(NULL); | 147 provider_host1_->SetWaitingVersion(NULL); |
| 148 ASSERT_TRUE(version_->HasProcessToRun()); | 148 ASSERT_TRUE(version_->HasProcessToRun()); |
| 149 | 149 |
| 150 // Disassociating the other provider_host will remove all process refs. | 150 // Disassociating the other provider_host will remove all process refs. |
| 151 provider_host2_->SetWaitingVersion(NULL); | 151 provider_host2_->SetWaitingVersion(NULL); |
| 152 ASSERT_FALSE(version_->HasProcessToRun()); | 152 ASSERT_FALSE(version_->HasProcessToRun()); |
| 153 } | 153 } |
| 154 | 154 |
| 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 | 155 } // namespace content |
| OLD | NEW |