| 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" |
| 8 #include "content/browser/service_worker/service_worker_provider_host.h" | 9 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 9 #include "content/browser/service_worker/service_worker_register_job.h" | 10 #include "content/browser/service_worker/service_worker_register_job.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 11 #include "content/browser/service_worker/service_worker_registration.h" |
| 11 #include "content/browser/service_worker/service_worker_version.h" | 12 #include "content/browser/service_worker/service_worker_version.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 static const int kRenderProcessId = 33; // Dummy process ID for testing. | 18 static const int kRenderProcessId = 33; // Dummy process ID for testing. |
| 18 | 19 |
| 19 class ServiceWorkerProviderHostTest : public testing::Test { | 20 class ServiceWorkerProviderHostTest : public testing::Test { |
| 20 protected: | 21 protected: |
| 21 ServiceWorkerProviderHostTest() | 22 ServiceWorkerProviderHostTest() |
| 22 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 23 virtual ~ServiceWorkerProviderHostTest() {} | 24 virtual ~ServiceWorkerProviderHostTest() {} |
| 24 | 25 |
| 25 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
| 26 context_.reset( | 27 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 27 new ServiceWorkerContextCore(base::FilePath(), | 28 context_ = helper_->context(); |
| 28 base::MessageLoopProxy::current(), | |
| 29 base::MessageLoopProxy::current(), | |
| 30 NULL, | |
| 31 NULL, | |
| 32 NULL)); | |
| 33 | |
| 34 scope_ = GURL("http://www.example.com/*"); | 29 scope_ = GURL("http://www.example.com/*"); |
| 35 script_url_ = GURL("http://www.example.com/service_worker.js"); | 30 script_url_ = GURL("http://www.example.com/service_worker.js"); |
| 36 registration_ = new ServiceWorkerRegistration( | 31 registration_ = new ServiceWorkerRegistration( |
| 37 scope_, script_url_, 1L, context_->AsWeakPtr()); | 32 scope_, script_url_, 1L, context_->AsWeakPtr()); |
| 38 version_ = new ServiceWorkerVersion( | 33 version_ = new ServiceWorkerVersion( |
| 39 registration_, | 34 registration_, |
| 40 1L, context_->AsWeakPtr()); | 35 1L, context_->AsWeakPtr()); |
| 41 | 36 |
| 42 // Prepare provider hosts (for the same process). | 37 // Prepare provider hosts (for the same process). |
| 43 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 38 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
| 44 kRenderProcessId, 1 /* provider_id */, | 39 kRenderProcessId, 1 /* provider_id */, |
| 45 context_->AsWeakPtr(), NULL)); | 40 context_->AsWeakPtr(), NULL)); |
| 46 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 41 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
| 47 kRenderProcessId, 2 /* provider_id */, | 42 kRenderProcessId, 2 /* provider_id */, |
| 48 context_->AsWeakPtr(), NULL)); | 43 context_->AsWeakPtr(), NULL)); |
| 49 scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost( | 44 scoped_ptr<ServiceWorkerProviderHost> host3(new ServiceWorkerProviderHost( |
| 50 kRenderProcessId, 3 /* provider_id */, | 45 kRenderProcessId, 3 /* provider_id */, |
| 51 context_->AsWeakPtr(), NULL)); | 46 context_->AsWeakPtr(), NULL)); |
| 52 provider_host1_ = host1->AsWeakPtr(); | 47 provider_host1_ = host1->AsWeakPtr(); |
| 53 provider_host2_ = host2->AsWeakPtr(); | 48 provider_host2_ = host2->AsWeakPtr(); |
| 54 provider_host3_ = host3->AsWeakPtr(); | 49 provider_host3_ = host3->AsWeakPtr(); |
| 55 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 50 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
| 56 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 51 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
| 57 context_->AddProviderHost(make_scoped_ptr(host3.release())); | 52 context_->AddProviderHost(make_scoped_ptr(host3.release())); |
| 58 } | 53 } |
| 59 | 54 |
| 60 virtual void TearDown() OVERRIDE { | 55 virtual void TearDown() OVERRIDE { |
| 61 version_ = 0; | 56 version_ = 0; |
| 62 registration_ = 0; | 57 registration_ = 0; |
| 63 context_.reset(); | 58 helper_.reset(); |
| 59 } |
| 60 |
| 61 bool HasProcessToRun() const { |
| 62 return context_->process_manager()->ScopeHasProcessToRun(scope_); |
| 64 } | 63 } |
| 65 | 64 |
| 66 content::TestBrowserThreadBundle thread_bundle_; | 65 content::TestBrowserThreadBundle thread_bundle_; |
| 67 scoped_ptr<ServiceWorkerContextCore> context_; | 66 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 67 ServiceWorkerContextCore* context_; |
| 68 scoped_refptr<ServiceWorkerRegistration> registration_; | 68 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 69 scoped_refptr<ServiceWorkerVersion> version_; | 69 scoped_refptr<ServiceWorkerVersion> version_; |
| 70 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 70 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 71 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 71 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 72 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | 72 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; |
| 73 GURL scope_; | 73 GURL scope_; |
| 74 GURL script_url_; | 74 GURL script_url_; |
| 75 | 75 |
| 76 private: | 76 private: |
| 77 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 77 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 78 }; | 78 }; |
| 79 | 79 |
| 80 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 80 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { |
| 81 ASSERT_FALSE(version_->HasProcessToRun()); | 81 ASSERT_FALSE(HasProcessToRun()); |
| 82 | 82 |
| 83 // Associating version_ to a provider_host's active version will internally | 83 // Associating version_ to a provider_host's active version will internally |
| 84 // add the provider_host's process ref to the version. | 84 // add the provider_host's process ref to the version. |
| 85 provider_host1_->SetActiveVersion(version_); | 85 provider_host1_->SetActiveVersion(version_); |
| 86 ASSERT_TRUE(version_->HasProcessToRun()); | 86 ASSERT_TRUE(HasProcessToRun()); |
| 87 | 87 |
| 88 // Re-associating the same version and provider_host should just work too. | 88 // Re-associating the same version and provider_host should just work too. |
| 89 provider_host1_->SetActiveVersion(version_); | 89 provider_host1_->SetActiveVersion(version_); |
| 90 ASSERT_TRUE(version_->HasProcessToRun()); | 90 ASSERT_TRUE(HasProcessToRun()); |
| 91 | 91 |
| 92 // Resetting the provider_host's active version should remove process refs | 92 // Resetting the provider_host's active version should remove process refs |
| 93 // from the version. | 93 // from the version. |
| 94 provider_host1_->SetActiveVersion(NULL); | 94 provider_host1_->SetActiveVersion(NULL); |
| 95 ASSERT_FALSE(version_->HasProcessToRun()); | 95 ASSERT_FALSE(HasProcessToRun()); |
| 96 } | 96 } |
| 97 | 97 |
| 98 TEST_F(ServiceWorkerProviderHostTest, | 98 TEST_F(ServiceWorkerProviderHostTest, |
| 99 SetActiveVersion_MultipleHostsForSameProcess) { | 99 SetActiveVersion_MultipleHostsForSameProcess) { |
| 100 ASSERT_FALSE(version_->HasProcessToRun()); | 100 ASSERT_FALSE(HasProcessToRun()); |
| 101 | 101 |
| 102 // Associating version_ to two providers as active version. | 102 // Associating version_ to two providers as active version. |
| 103 provider_host1_->SetActiveVersion(version_); | 103 provider_host1_->SetActiveVersion(version_); |
| 104 provider_host2_->SetActiveVersion(version_); | 104 provider_host2_->SetActiveVersion(version_); |
| 105 ASSERT_TRUE(version_->HasProcessToRun()); | 105 ASSERT_TRUE(HasProcessToRun()); |
| 106 | 106 |
| 107 // Disassociating one provider_host shouldn't remove all process refs | 107 // Disassociating one provider_host shouldn't remove all process refs |
| 108 // from the version yet. | 108 // from the version yet. |
| 109 provider_host1_->SetActiveVersion(NULL); | 109 provider_host1_->SetActiveVersion(NULL); |
| 110 ASSERT_TRUE(version_->HasProcessToRun()); | 110 ASSERT_TRUE(HasProcessToRun()); |
| 111 | 111 |
| 112 // Disassociating the other provider_host will remove all process refs. | 112 // Disassociating the other provider_host will remove all process refs. |
| 113 provider_host2_->SetActiveVersion(NULL); | 113 provider_host2_->SetActiveVersion(NULL); |
| 114 ASSERT_FALSE(version_->HasProcessToRun()); | 114 ASSERT_FALSE(HasProcessToRun()); |
| 115 } | 115 } |
| 116 | 116 |
| 117 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 117 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { |
| 118 ASSERT_FALSE(version_->HasProcessToRun()); | 118 ASSERT_FALSE(HasProcessToRun()); |
| 119 | 119 |
| 120 // Associating version_ to a provider_host's waiting version will internally | 120 // Associating version_ to a provider_host's waiting version will internally |
| 121 // add the provider_host's process ref to the version. | 121 // add the provider_host's process ref to the version. |
| 122 provider_host1_->SetWaitingVersion(version_); | 122 provider_host1_->SetWaitingVersion(version_); |
| 123 ASSERT_TRUE(version_->HasProcessToRun()); | 123 ASSERT_TRUE(HasProcessToRun()); |
| 124 | 124 |
| 125 // Re-associating the same version and provider_host should just work too. | 125 // Re-associating the same version and provider_host should just work too. |
| 126 provider_host1_->SetWaitingVersion(version_); | 126 provider_host1_->SetWaitingVersion(version_); |
| 127 ASSERT_TRUE(version_->HasProcessToRun()); | 127 ASSERT_TRUE(HasProcessToRun()); |
| 128 | 128 |
| 129 // Resetting the provider_host's waiting version should remove process refs | 129 // Resetting the provider_host's waiting version should remove process refs |
| 130 // from the version. | 130 // from the version. |
| 131 provider_host1_->SetWaitingVersion(NULL); | 131 provider_host1_->SetWaitingVersion(NULL); |
| 132 ASSERT_FALSE(version_->HasProcessToRun()); | 132 ASSERT_FALSE(HasProcessToRun()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 TEST_F(ServiceWorkerProviderHostTest, | 135 TEST_F(ServiceWorkerProviderHostTest, |
| 136 SetWaitingVersion_MultipleHostsForSameProcess) { | 136 SetWaitingVersion_MultipleHostsForSameProcess) { |
| 137 ASSERT_FALSE(version_->HasProcessToRun()); | 137 ASSERT_FALSE(HasProcessToRun()); |
| 138 | 138 |
| 139 // Associating version_ to two providers as active version. | 139 // Associating version_ to two providers as active version. |
| 140 provider_host1_->SetWaitingVersion(version_); | 140 provider_host1_->SetWaitingVersion(version_); |
| 141 provider_host2_->SetWaitingVersion(version_); | 141 provider_host2_->SetWaitingVersion(version_); |
| 142 ASSERT_TRUE(version_->HasProcessToRun()); | 142 ASSERT_TRUE(HasProcessToRun()); |
| 143 | 143 |
| 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(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(HasProcessToRun()); |
| 152 } | 152 } |
| 153 | 153 |
| 154 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { | 154 class ServiceWorkerProviderHostWaitingVersionTest : public testing::Test { |
| 155 protected: | 155 protected: |
| 156 ServiceWorkerProviderHostWaitingVersionTest() | 156 ServiceWorkerProviderHostWaitingVersionTest() |
| 157 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 157 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| 158 next_provider_id_(1L) {} | 158 next_provider_id_(1L) {} |
| 159 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} | 159 virtual ~ServiceWorkerProviderHostWaitingVersionTest() {} |
| 160 | 160 |
| 161 virtual void SetUp() OVERRIDE { | 161 virtual void SetUp() OVERRIDE { |
| 162 context_.reset( | 162 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 163 new ServiceWorkerContextCore(base::FilePath(), | 163 context_ = helper_->context(); |
| 164 base::MessageLoopProxy::current(), | |
| 165 base::MessageLoopProxy::current(), | |
| 166 NULL, | |
| 167 NULL, | |
| 168 NULL)); | |
| 169 | 164 |
| 170 // Prepare provider hosts (for the same process). | 165 // Prepare provider hosts (for the same process). |
| 171 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); | 166 provider_host1_ = CreateProviderHost(GURL("http://www.example.com/foo")); |
| 172 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); | 167 provider_host2_ = CreateProviderHost(GURL("http://www.example.com/bar")); |
| 173 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); | 168 provider_host3_ = CreateProviderHost(GURL("http://www.example.ca/foo")); |
| 174 } | 169 } |
| 175 | 170 |
| 176 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( | 171 base::WeakPtr<ServiceWorkerProviderHost> CreateProviderHost( |
| 177 const GURL& document_url) { | 172 const GURL& document_url) { |
| 178 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | 173 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( |
| 179 kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL)); | 174 kRenderProcessId, next_provider_id_++, context_->AsWeakPtr(), NULL)); |
| 180 host->SetDocumentUrl(document_url); | 175 host->SetDocumentUrl(document_url); |
| 181 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); | 176 base::WeakPtr<ServiceWorkerProviderHost> provider_host = host->AsWeakPtr(); |
| 182 context_->AddProviderHost(host.Pass()); | 177 context_->AddProviderHost(host.Pass()); |
| 183 return provider_host; | 178 return provider_host; |
| 184 } | 179 } |
| 185 | 180 |
| 186 virtual void TearDown() OVERRIDE { | 181 virtual void TearDown() OVERRIDE { |
| 187 context_.reset(); | 182 helper_.reset(); |
| 188 } | 183 } |
| 189 | 184 |
| 190 content::TestBrowserThreadBundle thread_bundle_; | 185 content::TestBrowserThreadBundle thread_bundle_; |
| 191 scoped_ptr<ServiceWorkerContextCore> context_; | 186 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 187 ServiceWorkerContextCore* context_; |
| 192 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 188 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 193 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 189 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 194 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; | 190 base::WeakPtr<ServiceWorkerProviderHost> provider_host3_; |
| 195 | 191 |
| 196 private: | 192 private: |
| 197 int64 next_provider_id_; | 193 int64 next_provider_id_; |
| 198 | 194 |
| 199 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); | 195 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostWaitingVersionTest); |
| 200 }; | 196 }; |
| 201 | 197 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 | 270 |
| 275 // Disassociate version1 from host1 and host2. | 271 // Disassociate version1 from host1 and host2. |
| 276 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( | 272 ServiceWorkerRegisterJob::DisassociateVersionFromDocuments( |
| 277 context_->AsWeakPtr(), version1); | 273 context_->AsWeakPtr(), version1); |
| 278 EXPECT_EQ(NULL, provider_host1_->installing_version()); | 274 EXPECT_EQ(NULL, provider_host1_->installing_version()); |
| 279 EXPECT_EQ(NULL, provider_host2_->installing_version()); | 275 EXPECT_EQ(NULL, provider_host2_->installing_version()); |
| 280 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); | 276 EXPECT_EQ(version2.get(), provider_host3_->installing_version()); |
| 281 } | 277 } |
| 282 | 278 |
| 283 } // namespace content | 279 } // namespace content |
| OLD | NEW |