| 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 "base/thread_task_runner_handle.h" | 7 #include "base/thread_task_runner_handle.h" |
| 8 #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_provider_host.h" | 9 #include "content/browser/service_worker/service_worker_provider_host.h" |
| 10 #include "content/browser/service_worker/service_worker_register_job.h" | 10 #include "content/browser/service_worker/service_worker_register_job.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 base::ThreadTaskRunnerHandle::Get(), | 31 base::ThreadTaskRunnerHandle::Get(), |
| 32 NULL, | 32 NULL, |
| 33 NULL, | 33 NULL, |
| 34 NULL)); | 34 NULL)); |
| 35 | 35 |
| 36 scope_ = GURL("http://www.example.com/"); | 36 scope_ = GURL("http://www.example.com/"); |
| 37 script_url_ = GURL("http://www.example.com/service_worker.js"); | 37 script_url_ = GURL("http://www.example.com/service_worker.js"); |
| 38 registration_ = new ServiceWorkerRegistration( | 38 registration_ = new ServiceWorkerRegistration( |
| 39 scope_, script_url_, 1L, context_->AsWeakPtr()); | 39 scope_, script_url_, 1L, context_->AsWeakPtr()); |
| 40 version_ = new ServiceWorkerVersion( | 40 version_ = new ServiceWorkerVersion( |
| 41 registration_, | 41 registration_.get(), 1L, context_->AsWeakPtr()); |
| 42 1L, context_->AsWeakPtr()); | |
| 43 | 42 |
| 44 // Prepare provider hosts (for the same process). | 43 // Prepare provider hosts (for the same process). |
| 45 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( | 44 scoped_ptr<ServiceWorkerProviderHost> host1(new ServiceWorkerProviderHost( |
| 46 kRenderProcessId, 1 /* provider_id */, | 45 kRenderProcessId, 1 /* provider_id */, |
| 47 context_->AsWeakPtr(), NULL)); | 46 context_->AsWeakPtr(), NULL)); |
| 48 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( | 47 scoped_ptr<ServiceWorkerProviderHost> host2(new ServiceWorkerProviderHost( |
| 49 kRenderProcessId, 2 /* provider_id */, | 48 kRenderProcessId, 2 /* provider_id */, |
| 50 context_->AsWeakPtr(), NULL)); | 49 context_->AsWeakPtr(), NULL)); |
| 51 provider_host1_ = host1->AsWeakPtr(); | 50 provider_host1_ = host1->AsWeakPtr(); |
| 52 provider_host2_ = host2->AsWeakPtr(); | 51 provider_host2_ = host2->AsWeakPtr(); |
| 53 context_->AddProviderHost(make_scoped_ptr(host1.release())); | 52 context_->AddProviderHost(make_scoped_ptr(host1.release())); |
| 54 context_->AddProviderHost(make_scoped_ptr(host2.release())); | 53 context_->AddProviderHost(make_scoped_ptr(host2.release())); |
| 55 } | 54 } |
| 56 | 55 |
| 57 virtual void TearDown() OVERRIDE { | 56 virtual void TearDown() OVERRIDE { |
| 58 version_ = 0; | 57 version_ = 0; |
| 59 registration_ = 0; | 58 registration_ = 0; |
| 60 context_.reset(); | 59 context_.reset(); |
| 61 } | 60 } |
| 62 | 61 |
| 63 void VerifyVersionAttributes( | 62 void VerifyVersionAttributes( |
| 64 base::WeakPtr<ServiceWorkerProviderHost> provider_host, | 63 base::WeakPtr<ServiceWorkerProviderHost> provider_host, |
| 65 ServiceWorkerVersion* installing, | 64 ServiceWorkerVersion* installing, |
| 66 ServiceWorkerVersion* waiting, | 65 ServiceWorkerVersion* waiting, |
| 67 ServiceWorkerVersion* active) { | 66 ServiceWorkerVersion* active) { |
| 68 EXPECT_EQ(installing, provider_host->installing_version_); | 67 EXPECT_EQ(installing, provider_host->installing_version_.get()); |
| 69 EXPECT_EQ(waiting, provider_host->waiting_version_); | 68 EXPECT_EQ(waiting, provider_host->waiting_version_.get()); |
| 70 EXPECT_EQ(active, provider_host->active_version_); | 69 EXPECT_EQ(active, provider_host->active_version_.get()); |
| 71 EXPECT_FALSE(provider_host->controlling_version_); | 70 EXPECT_FALSE(provider_host->controlling_version_.get()); |
| 72 } | 71 } |
| 73 | 72 |
| 74 content::TestBrowserThreadBundle thread_bundle_; | 73 content::TestBrowserThreadBundle thread_bundle_; |
| 75 scoped_ptr<ServiceWorkerContextCore> context_; | 74 scoped_ptr<ServiceWorkerContextCore> context_; |
| 76 scoped_refptr<ServiceWorkerRegistration> registration_; | 75 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 77 scoped_refptr<ServiceWorkerVersion> version_; | 76 scoped_refptr<ServiceWorkerVersion> version_; |
| 78 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; | 77 base::WeakPtr<ServiceWorkerProviderHost> provider_host1_; |
| 79 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; | 78 base::WeakPtr<ServiceWorkerProviderHost> provider_host2_; |
| 80 GURL scope_; | 79 GURL scope_; |
| 81 GURL script_url_; | 80 GURL script_url_; |
| 82 | 81 |
| 83 private: | 82 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); | 83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderHostTest); |
| 85 }; | 84 }; |
| 86 | 85 |
| 87 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { | 86 TEST_F(ServiceWorkerProviderHostTest, SetActiveVersion_ProcessStatus) { |
| 88 provider_host1_->AssociateRegistration(registration_); | 87 provider_host1_->AssociateRegistration(registration_.get()); |
| 89 ASSERT_FALSE(version_->HasProcessToRun()); | 88 ASSERT_FALSE(version_->HasProcessToRun()); |
| 90 | 89 |
| 91 // Associating version_ to a provider_host's active version will internally | 90 // Associating version_ to a provider_host's active version will internally |
| 92 // add the provider_host's process ref to the version. | 91 // add the provider_host's process ref to the version. |
| 93 registration_->SetActiveVersion(version_); | 92 registration_->SetActiveVersion(version_.get()); |
| 94 ASSERT_TRUE(version_->HasProcessToRun()); | 93 ASSERT_TRUE(version_->HasProcessToRun()); |
| 95 | 94 |
| 96 // 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. |
| 97 registration_->SetActiveVersion(version_); | 96 registration_->SetActiveVersion(version_.get()); |
| 98 ASSERT_TRUE(version_->HasProcessToRun()); | 97 ASSERT_TRUE(version_->HasProcessToRun()); |
| 99 | 98 |
| 100 // Resetting the provider_host's active version should remove process refs | 99 // Resetting the provider_host's active version should remove process refs |
| 101 // from the version. | 100 // from the version. |
| 102 provider_host1_->UnassociateRegistration(); | 101 provider_host1_->UnassociateRegistration(); |
| 103 ASSERT_FALSE(version_->HasProcessToRun()); | 102 ASSERT_FALSE(version_->HasProcessToRun()); |
| 104 } | 103 } |
| 105 | 104 |
| 106 TEST_F(ServiceWorkerProviderHostTest, | 105 TEST_F(ServiceWorkerProviderHostTest, |
| 107 SetActiveVersion_MultipleHostsForSameProcess) { | 106 SetActiveVersion_MultipleHostsForSameProcess) { |
| 108 provider_host1_->AssociateRegistration(registration_); | 107 provider_host1_->AssociateRegistration(registration_.get()); |
| 109 provider_host2_->AssociateRegistration(registration_); | 108 provider_host2_->AssociateRegistration(registration_.get()); |
| 110 ASSERT_FALSE(version_->HasProcessToRun()); | 109 ASSERT_FALSE(version_->HasProcessToRun()); |
| 111 | 110 |
| 112 // Associating version_ to two providers as active version. | 111 // Associating version_ to two providers as active version. |
| 113 registration_->SetActiveVersion(version_); | 112 registration_->SetActiveVersion(version_.get()); |
| 114 ASSERT_TRUE(version_->HasProcessToRun()); | 113 ASSERT_TRUE(version_->HasProcessToRun()); |
| 115 | 114 |
| 116 // Disassociating one provider_host shouldn't remove all process refs | 115 // Disassociating one provider_host shouldn't remove all process refs |
| 117 // from the version yet. | 116 // from the version yet. |
| 118 provider_host1_->UnassociateRegistration(); | 117 provider_host1_->UnassociateRegistration(); |
| 119 ASSERT_TRUE(version_->HasProcessToRun()); | 118 ASSERT_TRUE(version_->HasProcessToRun()); |
| 120 | 119 |
| 121 // Disassociating the other provider_host will remove all process refs. | 120 // Disassociating the other provider_host will remove all process refs. |
| 122 provider_host2_->UnassociateRegistration(); | 121 provider_host2_->UnassociateRegistration(); |
| 123 ASSERT_FALSE(version_->HasProcessToRun()); | 122 ASSERT_FALSE(version_->HasProcessToRun()); |
| 124 } | 123 } |
| 125 | 124 |
| 126 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { | 125 TEST_F(ServiceWorkerProviderHostTest, SetWaitingVersion_ProcessStatus) { |
| 127 provider_host1_->AssociateRegistration(registration_); | 126 provider_host1_->AssociateRegistration(registration_.get()); |
| 128 ASSERT_FALSE(version_->HasProcessToRun()); | 127 ASSERT_FALSE(version_->HasProcessToRun()); |
| 129 | 128 |
| 130 // Associating version_ to a provider_host's waiting version will internally | 129 // Associating version_ to a provider_host's waiting version will internally |
| 131 // add the provider_host's process ref to the version. | 130 // add the provider_host's process ref to the version. |
| 132 registration_->SetWaitingVersion(version_); | 131 registration_->SetWaitingVersion(version_.get()); |
| 133 ASSERT_TRUE(version_->HasProcessToRun()); | 132 ASSERT_TRUE(version_->HasProcessToRun()); |
| 134 | 133 |
| 135 // Re-associating the same version and provider_host should just work too. | 134 // Re-associating the same version and provider_host should just work too. |
| 136 registration_->SetWaitingVersion(version_); | 135 registration_->SetWaitingVersion(version_.get()); |
| 137 ASSERT_TRUE(version_->HasProcessToRun()); | 136 ASSERT_TRUE(version_->HasProcessToRun()); |
| 138 | 137 |
| 139 // Resetting the provider_host's waiting version should remove process refs | 138 // Resetting the provider_host's waiting version should remove process refs |
| 140 // from the version. | 139 // from the version. |
| 141 provider_host1_->UnassociateRegistration(); | 140 provider_host1_->UnassociateRegistration(); |
| 142 ASSERT_FALSE(version_->HasProcessToRun()); | 141 ASSERT_FALSE(version_->HasProcessToRun()); |
| 143 } | 142 } |
| 144 | 143 |
| 145 TEST_F(ServiceWorkerProviderHostTest, | 144 TEST_F(ServiceWorkerProviderHostTest, |
| 146 SetWaitingVersion_MultipleHostsForSameProcess) { | 145 SetWaitingVersion_MultipleHostsForSameProcess) { |
| 147 provider_host1_->AssociateRegistration(registration_); | 146 provider_host1_->AssociateRegistration(registration_.get()); |
| 148 provider_host2_->AssociateRegistration(registration_); | 147 provider_host2_->AssociateRegistration(registration_.get()); |
| 149 ASSERT_FALSE(version_->HasProcessToRun()); | 148 ASSERT_FALSE(version_->HasProcessToRun()); |
| 150 | 149 |
| 151 // Associating version_ to two providers as waiting version. | 150 // Associating version_ to two providers as waiting version. |
| 152 registration_->SetWaitingVersion(version_); | 151 registration_->SetWaitingVersion(version_.get()); |
| 153 ASSERT_TRUE(version_->HasProcessToRun()); | 152 ASSERT_TRUE(version_->HasProcessToRun()); |
| 154 | 153 |
| 155 // Disassociating one provider_host shouldn't remove all process refs | 154 // Disassociating one provider_host shouldn't remove all process refs |
| 156 // from the version yet. | 155 // from the version yet. |
| 157 provider_host1_->UnassociateRegistration(); | 156 provider_host1_->UnassociateRegistration(); |
| 158 ASSERT_TRUE(version_->HasProcessToRun()); | 157 ASSERT_TRUE(version_->HasProcessToRun()); |
| 159 | 158 |
| 160 // Disassociating the other provider_host will remove all process refs. | 159 // Disassociating the other provider_host will remove all process refs. |
| 161 provider_host2_->UnassociateRegistration(); | 160 provider_host2_->UnassociateRegistration(); |
| 162 ASSERT_FALSE(version_->HasProcessToRun()); | 161 ASSERT_FALSE(version_->HasProcessToRun()); |
| 163 } | 162 } |
| 164 | 163 |
| 165 TEST_F(ServiceWorkerProviderHostTest, | 164 TEST_F(ServiceWorkerProviderHostTest, |
| 166 ObserveVersionAttributesChanged_Basic) { | 165 ObserveVersionAttributesChanged_Basic) { |
| 167 provider_host1_->AssociateRegistration(registration_); | 166 provider_host1_->AssociateRegistration(registration_.get()); |
| 168 provider_host2_->AssociateRegistration(registration_); | 167 provider_host2_->AssociateRegistration(registration_.get()); |
| 169 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); | 168 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); |
| 170 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 169 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 171 | 170 |
| 172 registration_->SetInstallingVersion(version_); | 171 registration_->SetInstallingVersion(version_.get()); |
| 173 VerifyVersionAttributes(provider_host1_, version_, NULL, NULL); | 172 VerifyVersionAttributes(provider_host1_, version_.get(), NULL, NULL); |
| 174 VerifyVersionAttributes(provider_host2_, version_, NULL, NULL); | 173 VerifyVersionAttributes(provider_host2_, version_.get(), NULL, NULL); |
| 175 | 174 |
| 176 registration_->SetWaitingVersion(version_); | 175 registration_->SetWaitingVersion(version_.get()); |
| 177 VerifyVersionAttributes(provider_host1_, NULL, version_, NULL); | 176 VerifyVersionAttributes(provider_host1_, NULL, version_.get(), NULL); |
| 178 VerifyVersionAttributes(provider_host2_, NULL, version_, NULL); | 177 VerifyVersionAttributes(provider_host2_, NULL, version_.get(), NULL); |
| 179 | 178 |
| 180 // Disassociating the registration should clear all version attributes. | 179 // Disassociating the registration should clear all version attributes. |
| 181 provider_host2_->UnassociateRegistration(); | 180 provider_host2_->UnassociateRegistration(); |
| 182 VerifyVersionAttributes(provider_host1_, NULL, version_, NULL); | 181 VerifyVersionAttributes(provider_host1_, NULL, version_.get(), NULL); |
| 183 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 182 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 184 | 183 |
| 185 // Shouldn't notify the disassociated provider of the change. | 184 // Shouldn't notify the disassociated provider of the change. |
| 186 registration_->SetActiveVersion(version_); | 185 registration_->SetActiveVersion(version_.get()); |
| 187 VerifyVersionAttributes(provider_host1_, NULL, NULL, version_); | 186 VerifyVersionAttributes(provider_host1_, NULL, NULL, version_.get()); |
| 188 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 187 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 189 } | 188 } |
| 190 | 189 |
| 191 TEST_F(ServiceWorkerProviderHostTest, | 190 TEST_F(ServiceWorkerProviderHostTest, |
| 192 ObserveVersionAttributesChanged_MultipleVersions) { | 191 ObserveVersionAttributesChanged_MultipleVersions) { |
| 193 provider_host1_->AssociateRegistration(registration_); | 192 provider_host1_->AssociateRegistration(registration_.get()); |
| 194 provider_host2_->AssociateRegistration(registration_); | 193 provider_host2_->AssociateRegistration(registration_.get()); |
| 195 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); | 194 VerifyVersionAttributes(provider_host1_, NULL, NULL, NULL); |
| 196 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 195 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 197 | 196 |
| 198 scoped_refptr<ServiceWorkerVersion> version1 = | 197 scoped_refptr<ServiceWorkerVersion> version1 = |
| 199 new ServiceWorkerVersion(registration_, 10L, context_->AsWeakPtr()); | 198 new ServiceWorkerVersion(registration_.get(), 10L, context_->AsWeakPtr()); |
| 200 scoped_refptr<ServiceWorkerVersion> version2 = | 199 scoped_refptr<ServiceWorkerVersion> version2 = |
| 201 new ServiceWorkerVersion(registration_, 20L, context_->AsWeakPtr()); | 200 new ServiceWorkerVersion(registration_.get(), 20L, context_->AsWeakPtr()); |
| 202 | 201 |
| 203 registration_->SetInstallingVersion(version1); | 202 registration_->SetInstallingVersion(version1.get()); |
| 204 VerifyVersionAttributes(provider_host1_, version1, NULL, NULL); | 203 VerifyVersionAttributes(provider_host1_, version1.get(), NULL, NULL); |
| 205 VerifyVersionAttributes(provider_host2_, version1, NULL, NULL); | 204 VerifyVersionAttributes(provider_host2_, version1.get(), NULL, NULL); |
| 206 | 205 |
| 207 registration_->SetWaitingVersion(version1); | 206 registration_->SetWaitingVersion(version1.get()); |
| 208 VerifyVersionAttributes(provider_host1_, NULL, version1, NULL); | 207 VerifyVersionAttributes(provider_host1_, NULL, version1.get(), NULL); |
| 209 VerifyVersionAttributes(provider_host2_, NULL, version1, NULL); | 208 VerifyVersionAttributes(provider_host2_, NULL, version1.get(), NULL); |
| 210 | 209 |
| 211 registration_->SetInstallingVersion(version2); | 210 registration_->SetInstallingVersion(version2.get()); |
| 212 VerifyVersionAttributes(provider_host1_, version2, version1, NULL); | 211 VerifyVersionAttributes( |
| 213 VerifyVersionAttributes(provider_host2_, version2, version1, NULL); | 212 provider_host1_, version2.get(), version1.get(), NULL); |
| 213 VerifyVersionAttributes( |
| 214 provider_host2_, version2.get(), version1.get(), NULL); |
| 214 | 215 |
| 215 // Disassociating the registration should clear all version attributes. | 216 // Disassociating the registration should clear all version attributes. |
| 216 provider_host2_->UnassociateRegistration(); | 217 provider_host2_->UnassociateRegistration(); |
| 217 VerifyVersionAttributes(provider_host1_, version2, version1, NULL); | 218 VerifyVersionAttributes( |
| 219 provider_host1_, version2.get(), version1.get(), NULL); |
| 218 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 220 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 219 | 221 |
| 220 // Shouldn't notify the disassociated provider of the change. | 222 // Shouldn't notify the disassociated provider of the change. |
| 221 registration_->SetActiveVersion(version1); | 223 registration_->SetActiveVersion(version1.get()); |
| 222 VerifyVersionAttributes(provider_host1_, version2, NULL, version1); | 224 VerifyVersionAttributes( |
| 225 provider_host1_, version2.get(), NULL, version1.get()); |
| 223 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 226 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 224 | 227 |
| 225 registration_->SetActiveVersion(version2); | 228 registration_->SetActiveVersion(version2.get()); |
| 226 VerifyVersionAttributes(provider_host1_, NULL, NULL, version2); | 229 VerifyVersionAttributes(provider_host1_, NULL, NULL, version2.get()); |
| 227 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); | 230 VerifyVersionAttributes(provider_host2_, NULL, NULL, NULL); |
| 228 } | 231 } |
| 229 | 232 |
| 230 } // namespace content | 233 } // namespace content |
| OLD | NEW |