| 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/embedded_worker_test_helper.h" |
| 7 #include "content/browser/service_worker/service_worker_context_core.h" | 8 #include "content/browser/service_worker/service_worker_context_core.h" |
| 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 8 #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_provider_host_registry.h
" |
| 9 #include "content/browser/service_worker/service_worker_register_job.h" | 12 #include "content/browser/service_worker/service_worker_register_job.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 11 #include "content/browser/service_worker/service_worker_version.h" | 14 #include "content/browser/service_worker/service_worker_version.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 17 |
| 15 namespace content { | 18 namespace content { |
| 16 | 19 |
| 17 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 20 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
| 18 | 21 |
| 19 class ServiceWorkerProviderHostTest : public testing::Test { | 22 class ServiceWorkerProviderHostTest : public testing::Test { |
| 20 protected: | 23 protected: |
| 21 ServiceWorkerProviderHostTest() | 24 ServiceWorkerProviderHostTest() |
| 22 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 25 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 23 virtual ~ServiceWorkerProviderHostTest() {} | 26 virtual ~ServiceWorkerProviderHostTest() {} |
| 24 | 27 |
| 25 virtual void SetUp() OVERRIDE { | 28 virtual void SetUp() OVERRIDE { |
| 26 context_.reset( | 29 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 27 new ServiceWorkerContextCore(base::FilePath(), | |
| 28 base::MessageLoopProxy::current(), | |
| 29 base::MessageLoopProxy::current(), | |
| 30 NULL, | |
| 31 NULL, | |
| 32 NULL)); | |
| 33 | 30 |
| 34 scope_ = GURL("http://www.example.com/*"); | 31 scope_ = GURL("http://www.example.com/*"); |
| 35 script_url_ = GURL("http://www.example.com/service_worker.js"); | 32 script_url_ = GURL("http://www.example.com/service_worker.js"); |
| 36 registration_ = new ServiceWorkerRegistration( | 33 registration_ = new ServiceWorkerRegistration( |
| 37 scope_, script_url_, 1L, context_->AsWeakPtr()); | 34 scope_, script_url_, 1L, context()->AsWeakPtr()); |
| 38 version_ = new ServiceWorkerVersion( | 35 version_ = new ServiceWorkerVersion( |
| 39 registration_, | 36 registration_, 1L, context()->AsWeakPtr()); |
| 40 1L, context_->AsWeakPtr()); | |
| 41 | 37 |
| 42 // Prepare provider hosts (for the same process). | 38 // Prepare provider hosts (for the same process). |
| 43 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 39 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
| 44 kRenderProcessId, 1 /* provider_id */, | 40 kRenderProcessId, 1 /* provider_id */, |
| 45 context_->AsWeakPtr(), NULL)); | 41 context()->AsWeakPtr(), NULL)); |
| 46 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 42 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
| 47 kRenderProcessId, 2 /* provider_id */, | 43 kRenderProcessId, 2 /* provider_id */, |
| 48 context_->AsWeakPtr(), NULL)); | 44 context()->AsWeakPtr(), NULL)); |
| 49 scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost( | 45 scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost( |
| 50 kRenderProcessId, 3 /* provider_id */, | 46 kRenderProcessId, 3 /* provider_id */, |
| 51 context_->AsWeakPtr(), NULL)); | 47 context()->AsWeakPtr(), NULL)); |
| 52 provider_host1_ = host1->AsWeakPtr(); | 48 provider_host1_ = host1->AsWeakPtr(); |
| 53 provider_host2_ = host2->AsWeakPtr(); | 49 provider_host2_ = host2->AsWeakPtr(); |
| 54 provider_host3_ = host3->AsWeakPtr(); | 50 provider_host3_ = host3->AsWeakPtr(); |
| 55 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 51 provider_registry()->AddProviderHost(make_scoped_ptr(host1.release())); |
| 56 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 52 provider_registry()->AddProviderHost(make_scoped_ptr(host2.release())); |
| 57 context_->AddProviderHost(make_scoped_ptr(host3.release())); | 53 provider_registry()->AddProviderHost(make_scoped_ptr(host3.release())); |
| 58 } | 54 } |
| 59 | 55 |
| 60 virtual void TearDown() OVERRIDE { | 56 virtual void TearDown() OVERRIDE { |
| 61 version_ = 0; | 57 version_ = 0; |
| 62 registration_ = 0; | 58 registration_ = 0; |
| 63 context_.reset(); | 59 helper_.reset(); |
| 60 } |
| 61 |
| 62 ServiceWorkerContextCore* context() { |
| 63 return helper_->context(); |
| 64 } |
| 65 |
| 66 ServiceWorkerProviderHostRegistry* provider_registry() { |
| 67 return helper_->context_wrapper()->provider_registry(); |
| 64 } | 68 } |
| 65 | 69 |
| 66 content::TestBrowserThreadBundle thread_bundle_; | 70 content::TestBrowserThreadBundle thread_bundle_; |
| 67 scoped_ptr<ServiceWorkerContextCore> context_; | 71 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 72 |
| 68 scoped_refptr<ServiceWorkerRegistration> registration_; | 73 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 69 scoped_refptr<ServiceWorkerVersion> version_; | 74 scoped_refptr<ServiceWorkerVersion> version_; |
| 70 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 75 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 71 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 76 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 72 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | 77 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; |
| 73 GURL scope_; | 78 GURL scope_; |
| 74 GURL script_url_; | 79 GURL script_url_; |
| 75 | 80 |
| 76 private: | 81 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 82 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 } | 157 } |
| 153 | 158 |
| 154 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { | 159 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { |
| 155 protected: | 160 protected: |
| 156 ServiceWorkerProviderHostWaitingVersionTest() | 161 ServiceWorkerProviderHostWaitingVersionTest() |
| 157 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 162 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 158 next_provider_id_(1L) {} | 163 next_provider_id_(1L) {} |
| 159 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} | 164 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} |
| 160 | 165 |
| 161 virtual void SetUp() OVERRIDE { | 166 virtual void SetUp() OVERRIDE { |
| 162 context_.reset( | 167 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 163 new ServiceWorkerContextCore(base::FilePath(), | |
| 164 base::MessageLoopProxy::current(), | |
| 165 base::MessageLoopProxy::current(), | |
| 166 NULL, | |
| 167 NULL, | |
| 168 NULL)); | |
| 169 | 168 |
| 170 // Prepare provider hosts (for the same process). | 169 // Prepare provider hosts (for the same process). |
| 171 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); | 170 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); |
| 172 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); | 171 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); |
| 173 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); | 172 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); |
| 174 } | 173 } |
| 175 | 174 |
| 176 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( | 175 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( |
| 177 const GURL& document_url) { | 176 const GURL& document_url) { |
| 178 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | 177 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 179 kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL)); | 178 kRenderProcessId, next_provider_id_++, context()->AsWeakPtr(), NULL)); |
| 180 host->SetDocumentUrl(document_url); | 179 host->SetDocumentUrl(document_url); |
| 181 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | 180 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 182 context_->AddProviderHost(host.Pass()); | 181 provider_registry()->AddProviderHost(host.Pass()); |
| 183 return provider_host; | 182 return provider_host; |
| 184 } | 183 } |
| 185 | 184 |
| 186 virtual void TearDown() OVERRIDE { | 185 virtual void TearDown() OVERRIDE { |
| 187 context_.reset(); | 186 helper_.reset(); |
| 187 } |
| 188 |
| 189 ServiceWorkerContextCore* context() { |
| 190 return helper_->context(); |
| 191 } |
| 192 |
| 193 ServiceWorkerProviderHostRegistry* provider_registry() { |
| 194 return helper_->context_wrapper()->provider_registry(); |
| 188 } | 195 } |
| 189 | 196 |
| 190 content::TestBrowserThreadBundle thread_bundle_; | 197 content::TestBrowserThreadBundle thread_bundle_; |
| 191 scoped_ptr<ServiceWorkerContextCore> context_; | 198 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 192 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 199 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 193 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 200 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 194 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | 201 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; |
| 195 | 202 |
| 196 private: | 203 private: |
| 197 int64 next_provider_id_; | 204 int64 next_provider_id_; |
| 198 | 205 |
| 199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); | 206 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); |
| 200 }; | 207 }; |
| 201 | 208 |
| 202 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | 209 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, |
| 203 AssociateWaitingVersionToDocuments) { | 210 AssociateWaitingVersionToDocuments) { |
| 204 const GURL scope("http://www.example.com/*"); | 211 const GURL scope("http://www.example.com/*"); |
| 205 const GURL script_url("http://www.example.com/service_worker.js"); | 212 const GURL script_url("http://www.example.com/service_worker.js"); |
| 206 | 213 |
| 207 scoped_refptr<ServiceWorkerRegistration> registration( | 214 scoped_refptr<ServiceWorkerRegistration> registration( |
| 208 new ServiceWorkerRegistration( | 215 new ServiceWorkerRegistration( |
| 209 scope, script_url, 1L, context_->AsWeakPtr())); | 216 scope, script_url, 1L, context()->AsWeakPtr())); |
| 210 scoped_refptr<ServiceWorkerVersion> version( | 217 scoped_refptr<ServiceWorkerVersion> version( |
| 211 new ServiceWorkerVersion(registration, 1L, context_->AsWeakPtr())); | 218 new ServiceWorkerVersion(registration, 1L, context()->AsWeakPtr())); |
| 212 | 219 |
| 213 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( | 220 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( |
| 214 context_->AsWeakPtr(), version.get()); | 221 context()->AsWeakPtr(), version.get()); |
| 215 EXPECT_EQ(version.get(), provider_host1_->waiting_version()); | 222 EXPECT_EQ(version.get(), provider_host1_->waiting_version()); |
| 216 EXPECT_EQ(version.get(), provider_host2_->waiting_version()); | 223 EXPECT_EQ(version.get(), provider_host2_->waiting_version()); |
| 217 EXPECT_EQ(NULL, provider_host3_->waiting_version()); | 224 EXPECT_EQ(NULL, provider_host3_->waiting_version()); |
| 218 } | 225 } |
| 219 | 226 |
| 220 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, | 227 TEST_F(ServiceWorkerProviderHostWaitingVersionTest, |
| 221 DisassociateWaitingVersionFromDocuments) { | 228 DisassociateWaitingVersionFromDocuments) { |
| 222 const GURL scope1("http://www.example.com/*"); | 229 const GURL scope1("http://www.example.com/*"); |
| 223 const GURL script_url1("http://www.example.com/service_worker.js"); | 230 const GURL script_url1("http://www.example.com/service_worker.js"); |
| 224 scoped_refptr<ServiceWorkerRegistration> registration1( | 231 scoped_refptr<ServiceWorkerRegistration> registration1( |
| 225 new ServiceWorkerRegistration( | 232 new ServiceWorkerRegistration( |
| 226 scope1, script_url1, 1L, context_->AsWeakPtr())); | 233 scope1, script_url1, 1L, context()->AsWeakPtr())); |
| 227 scoped_refptr<ServiceWorkerVersion> version1( | 234 scoped_refptr<ServiceWorkerVersion> version1( |
| 228 new ServiceWorkerVersion(registration1, 1L, context_->AsWeakPtr())); | 235 new ServiceWorkerVersion(registration1, 1L, context()->AsWeakPtr())); |
| 229 | 236 |
| 230 const GURL scope2("http://www.example.ca/*"); | 237 const GURL scope2("http://www.example.ca/*"); |
| 231 const GURL script_url2("http://www.example.ca/service_worker.js"); | 238 const GURL script_url2("http://www.example.ca/service_worker.js"); |
| 232 scoped_refptr<ServiceWorkerRegistration> registration2( | 239 scoped_refptr<ServiceWorkerRegistration> registration2( |
| 233 new ServiceWorkerRegistration( | 240 new ServiceWorkerRegistration( |
| 234 scope2, script_url2, 2L, context_->AsWeakPtr())); | 241 scope2, script_url2, 2L, context()->AsWeakPtr())); |
| 235 scoped_refptr<ServiceWorkerVersion> version2( | 242 scoped_refptr<ServiceWorkerVersion> version2( |
| 236 new ServiceWorkerVersion(registration2, 2L, context_->AsWeakPtr())); | 243 new ServiceWorkerVersion(registration2, 2L, context()->AsWeakPtr())); |
| 237 | 244 |
| 238 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( | 245 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( |
| 239 context_->AsWeakPtr(), version1.get()); | 246 context()->AsWeakPtr(), version1.get()); |
| 240 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( | 247 ServiceWorkerRegisterJob::AssociateWaitingVersionToDocuments( |
| 241 context_->AsWeakPtr(), version2.get()); | 248 context()->AsWeakPtr(), version2.get()); |
| 242 | 249 |
| 243 // Host1 and host2 are associated with version1 as a waiting version, whereas | 250 // Host1 and host2 are associated with version1 as a waiting version, whereas |
| 244 // host3 is associated with version2. | 251 // host3 is associated with version2. |
| 245 EXPECT_EQ(version1.get(), provider_host1_->waiting_version()); | 252 EXPECT_EQ(version1.get(), provider_host1_->waiting_version()); |
| 246 EXPECT_EQ(version1.get(), provider_host2_->waiting_version()); | 253 EXPECT_EQ(version1.get(), provider_host2_->waiting_version()); |
| 247 EXPECT_EQ(version2.get(), provider_host3_->waiting_version()); | 254 EXPECT_EQ(version2.get(), provider_host3_->waiting_version()); |
| 248 | 255 |
| 249 // Disassociate version1 from host1 and host2. | 256 // Disassociate version1 from host1 and host2. |
| 250 ServiceWorkerRegisterJob::DisassociateWaitingVersionFromDocuments( | 257 ServiceWorkerRegisterJob::DisassociateWaitingVersionFromDocuments( |
| 251 context_->AsWeakPtr(), version1->version_id()); | 258 context()->AsWeakPtr(), version1->version_id()); |
| 252 EXPECT_EQ(NULL, provider_host1_->waiting_version()); | 259 EXPECT_EQ(NULL, provider_host1_->waiting_version()); |
| 253 EXPECT_EQ(NULL, provider_host2_->waiting_version()); | 260 EXPECT_EQ(NULL, provider_host2_->waiting_version()); |
| 254 EXPECT_EQ(version2.get(), provider_host3_->waiting_version()); | 261 EXPECT_EQ(version2.get(), provider_host3_->waiting_version()); |
| 255 } | 262 } |
| 256 | 263 |
| 257 } // namespace content | 264 } // namespace content |
| OLD | NEW |