| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/service_worker/service_worker_registration.h" | 5 #include "content/browser/service_worker/service_worker_registration.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 scoped_refptr<ServiceWorkerRegistration> observed_registration_; | 92 scoped_refptr<ServiceWorkerRegistration> observed_registration_; |
| 93 ChangedVersionAttributesMask observed_changed_mask_; | 93 ChangedVersionAttributesMask observed_changed_mask_; |
| 94 ServiceWorkerRegistrationInfo observed_info_; | 94 ServiceWorkerRegistrationInfo observed_info_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 private: | 97 private: |
| 98 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 98 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 99 TestBrowserThreadBundle thread_bundle_; | 99 TestBrowserThreadBundle thread_bundle_; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 class ServiceWorkerRegistrationTestP | 102 TEST_F(ServiceWorkerRegistrationTest, SetAndUnsetVersions) { |
| 103 : public MojoServiceWorkerTestP<ServiceWorkerRegistrationTest> {}; | |
| 104 | |
| 105 TEST_P(ServiceWorkerRegistrationTestP, SetAndUnsetVersions) { | |
| 106 const GURL kScope("http://www.example.not/"); | 103 const GURL kScope("http://www.example.not/"); |
| 107 const GURL kScript("http://www.example.not/service_worker.js"); | 104 const GURL kScript("http://www.example.not/service_worker.js"); |
| 108 int64_t kRegistrationId = 1L; | 105 int64_t kRegistrationId = 1L; |
| 109 scoped_refptr<ServiceWorkerRegistration> registration = | 106 scoped_refptr<ServiceWorkerRegistration> registration = |
| 110 new ServiceWorkerRegistration(kScope, kRegistrationId, | 107 new ServiceWorkerRegistration(kScope, kRegistrationId, |
| 111 context()->AsWeakPtr()); | 108 context()->AsWeakPtr()); |
| 112 | 109 |
| 113 const int64_t version_1_id = 1L; | 110 const int64_t version_1_id = 1L; |
| 114 const int64_t version_2_id = 2L; | 111 const int64_t version_2_id = 2L; |
| 115 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion( | 112 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 EXPECT_FALSE(registration->waiting_version()); | 160 EXPECT_FALSE(registration->waiting_version()); |
| 164 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION, | 161 EXPECT_EQ(ChangedVersionAttributesMask::WAITING_VERSION, |
| 165 listener.observed_changed_mask_.changed()); | 162 listener.observed_changed_mask_.changed()); |
| 166 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); | 163 EXPECT_EQ(version_1_id, listener.observed_info_.active_version.version_id); |
| 167 EXPECT_EQ(listener.observed_info_.waiting_version.version_id, | 164 EXPECT_EQ(listener.observed_info_.waiting_version.version_id, |
| 168 kInvalidServiceWorkerVersionId); | 165 kInvalidServiceWorkerVersionId); |
| 169 EXPECT_EQ(listener.observed_info_.installing_version.version_id, | 166 EXPECT_EQ(listener.observed_info_.installing_version.version_id, |
| 170 kInvalidServiceWorkerVersionId); | 167 kInvalidServiceWorkerVersionId); |
| 171 } | 168 } |
| 172 | 169 |
| 173 TEST_P(ServiceWorkerRegistrationTestP, FailedRegistrationNoCrash) { | 170 TEST_F(ServiceWorkerRegistrationTest, FailedRegistrationNoCrash) { |
| 174 const GURL kScope("http://www.example.not/"); | 171 const GURL kScope("http://www.example.not/"); |
| 175 int64_t kRegistrationId = 1L; | 172 int64_t kRegistrationId = 1L; |
| 176 scoped_refptr<ServiceWorkerRegistration> registration = | 173 scoped_refptr<ServiceWorkerRegistration> registration = |
| 177 new ServiceWorkerRegistration(kScope, kRegistrationId, | 174 new ServiceWorkerRegistration(kScope, kRegistrationId, |
| 178 context()->AsWeakPtr()); | 175 context()->AsWeakPtr()); |
| 179 std::unique_ptr<ServiceWorkerRegistrationHandle> handle( | 176 std::unique_ptr<ServiceWorkerRegistrationHandle> handle( |
| 180 new ServiceWorkerRegistrationHandle( | 177 new ServiceWorkerRegistrationHandle( |
| 181 context()->AsWeakPtr(), base::WeakPtr<ServiceWorkerProviderHost>(), | 178 context()->AsWeakPtr(), base::WeakPtr<ServiceWorkerProviderHost>(), |
| 182 registration.get())); | 179 registration.get())); |
| 183 registration->NotifyRegistrationFailed(); | 180 registration->NotifyRegistrationFailed(); |
| 184 // Don't crash when handle gets destructed. | 181 // Don't crash when handle gets destructed. |
| 185 } | 182 } |
| 186 | 183 |
| 187 TEST_P(ServiceWorkerRegistrationTestP, NavigationPreload) { | 184 TEST_F(ServiceWorkerRegistrationTest, NavigationPreload) { |
| 188 const GURL kScope("http://www.example.not/"); | 185 const GURL kScope("http://www.example.not/"); |
| 189 const GURL kScript("https://www.example.not/service_worker.js"); | 186 const GURL kScript("https://www.example.not/service_worker.js"); |
| 190 // Setup. | 187 // Setup. |
| 191 scoped_refptr<ServiceWorkerRegistration> registration = | 188 scoped_refptr<ServiceWorkerRegistration> registration = |
| 192 new ServiceWorkerRegistration(kScope, storage()->NewRegistrationId(), | 189 new ServiceWorkerRegistration(kScope, storage()->NewRegistrationId(), |
| 193 context()->AsWeakPtr()); | 190 context()->AsWeakPtr()); |
| 194 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion( | 191 scoped_refptr<ServiceWorkerVersion> version_1 = new ServiceWorkerVersion( |
| 195 registration.get(), kScript, storage()->NewVersionId(), | 192 registration.get(), kScript, storage()->NewVersionId(), |
| 196 context()->AsWeakPtr()); | 193 context()->AsWeakPtr()); |
| 197 version_1->set_fetch_handler_existence( | 194 version_1->set_fetch_handler_existence( |
| (...skipping 17 matching lines...) Expand all Loading... |
| 215 registration->SetActiveVersion(version_2); | 212 registration->SetActiveVersion(version_2); |
| 216 version_2->SetStatus(ServiceWorkerVersion::ACTIVATING); | 213 version_2->SetStatus(ServiceWorkerVersion::ACTIVATING); |
| 217 EXPECT_TRUE(version_2->navigation_preload_state().enabled); | 214 EXPECT_TRUE(version_2->navigation_preload_state().enabled); |
| 218 // Disabling it unsets the flag on the active version. | 215 // Disabling it unsets the flag on the active version. |
| 219 registration->EnableNavigationPreload(false); | 216 registration->EnableNavigationPreload(false); |
| 220 EXPECT_FALSE(version_2->navigation_preload_state().enabled); | 217 EXPECT_FALSE(version_2->navigation_preload_state().enabled); |
| 221 } | 218 } |
| 222 | 219 |
| 223 // Sets up a registration with a waiting worker, and an active worker | 220 // Sets up a registration with a waiting worker, and an active worker |
| 224 // with a controllee and an inflight request. | 221 // with a controllee and an inflight request. |
| 225 class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTestP { | 222 class ServiceWorkerActivationTest : public ServiceWorkerRegistrationTest { |
| 226 public: | 223 public: |
| 227 ServiceWorkerActivationTest() : ServiceWorkerRegistrationTestP() {} | 224 ServiceWorkerActivationTest() : ServiceWorkerRegistrationTest() {} |
| 228 | 225 |
| 229 void SetUp() override { | 226 void SetUp() override { |
| 230 ServiceWorkerRegistrationTest::SetUp(); | 227 ServiceWorkerRegistrationTest::SetUp(); |
| 231 | 228 |
| 232 const GURL kScope("https://www.example.not/"); | 229 const GURL kScope("https://www.example.not/"); |
| 233 const GURL kScript("https://www.example.not/service_worker.js"); | 230 const GURL kScript("https://www.example.not/service_worker.js"); |
| 234 | 231 |
| 235 registration_ = new ServiceWorkerRegistration( | 232 registration_ = new ServiceWorkerRegistration( |
| 236 kScope, storage()->NewRegistrationId(), context()->AsWeakPtr()); | 233 kScope, storage()->NewRegistrationId(), context()->AsWeakPtr()); |
| 237 | 234 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 ServiceWorkerProviderHost* controllee() { return host_.get(); } | 292 ServiceWorkerProviderHost* controllee() { return host_.get(); } |
| 296 int inflight_request_id() const { return inflight_request_id_; } | 293 int inflight_request_id() const { return inflight_request_id_; } |
| 297 | 294 |
| 298 private: | 295 private: |
| 299 scoped_refptr<ServiceWorkerRegistration> registration_; | 296 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 300 std::unique_ptr<ServiceWorkerProviderHost> host_; | 297 std::unique_ptr<ServiceWorkerProviderHost> host_; |
| 301 int inflight_request_id_ = -1; | 298 int inflight_request_id_ = -1; |
| 302 }; | 299 }; |
| 303 | 300 |
| 304 // Test activation triggered by finishing all requests. | 301 // Test activation triggered by finishing all requests. |
| 305 TEST_P(ServiceWorkerActivationTest, NoInflightRequest) { | 302 TEST_F(ServiceWorkerActivationTest, NoInflightRequest) { |
| 306 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 303 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 307 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 304 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 308 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 305 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 309 | 306 |
| 310 // Remove the controllee. Since there is an in-flight request, | 307 // Remove the controllee. Since there is an in-flight request, |
| 311 // activation should not yet happen. | 308 // activation should not yet happen. |
| 312 version_1->RemoveControllee(controllee()); | 309 version_1->RemoveControllee(controllee()); |
| 313 base::RunLoop().RunUntilIdle(); | 310 base::RunLoop().RunUntilIdle(); |
| 314 EXPECT_EQ(version_1.get(), reg->active_version()); | 311 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 315 | 312 |
| 316 // Finish the request. Activation should happen. | 313 // Finish the request. Activation should happen. |
| 317 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, | 314 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 318 base::Time::Now()); | 315 base::Time::Now()); |
| 319 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 320 EXPECT_EQ(version_2.get(), reg->active_version()); | 317 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 321 } | 318 } |
| 322 | 319 |
| 323 // Test activation triggered by loss of controllee. | 320 // Test activation triggered by loss of controllee. |
| 324 TEST_P(ServiceWorkerActivationTest, NoControllee) { | 321 TEST_F(ServiceWorkerActivationTest, NoControllee) { |
| 325 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 322 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 326 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 323 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 327 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 324 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 328 | 325 |
| 329 // Finish the request. Since there is a controllee, activation should not yet | 326 // Finish the request. Since there is a controllee, activation should not yet |
| 330 // happen. | 327 // happen. |
| 331 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, | 328 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 332 base::Time::Now()); | 329 base::Time::Now()); |
| 333 base::RunLoop().RunUntilIdle(); | 330 base::RunLoop().RunUntilIdle(); |
| 334 EXPECT_EQ(version_1.get(), reg->active_version()); | 331 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 335 | 332 |
| 336 // Remove the controllee. Activation should happen. | 333 // Remove the controllee. Activation should happen. |
| 337 version_1->RemoveControllee(controllee()); | 334 version_1->RemoveControllee(controllee()); |
| 338 base::RunLoop().RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
| 339 EXPECT_EQ(version_2.get(), reg->active_version()); | 336 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 340 } | 337 } |
| 341 | 338 |
| 342 // Test activation triggered by skipWaiting. | 339 // Test activation triggered by skipWaiting. |
| 343 TEST_P(ServiceWorkerActivationTest, SkipWaiting) { | 340 TEST_F(ServiceWorkerActivationTest, SkipWaiting) { |
| 344 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 341 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 345 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 342 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 346 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 343 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 347 | 344 |
| 348 // Finish the in-flight request. Since there is a controllee, | 345 // Finish the in-flight request. Since there is a controllee, |
| 349 // activation should not happen. | 346 // activation should not happen. |
| 350 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, | 347 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 351 base::Time::Now()); | 348 base::Time::Now()); |
| 352 base::RunLoop().RunUntilIdle(); | 349 base::RunLoop().RunUntilIdle(); |
| 353 EXPECT_EQ(version_1.get(), reg->active_version()); | 350 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 354 | 351 |
| 355 // Call skipWaiting. Activation should happen. | 352 // Call skipWaiting. Activation should happen. |
| 356 version_2->OnSkipWaiting(77 /* dummy request_id */); | 353 version_2->OnSkipWaiting(77 /* dummy request_id */); |
| 357 base::RunLoop().RunUntilIdle(); | 354 base::RunLoop().RunUntilIdle(); |
| 358 EXPECT_EQ(version_2.get(), reg->active_version()); | 355 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 359 } | 356 } |
| 360 | 357 |
| 361 // Test activation triggered by skipWaiting and finishing requests. | 358 // Test activation triggered by skipWaiting and finishing requests. |
| 362 TEST_P(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) { | 359 TEST_F(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) { |
| 363 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 360 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 364 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 361 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 365 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 362 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 366 | 363 |
| 367 // Set skip waiting flag. Since there is still an in-flight request, | 364 // Set skip waiting flag. Since there is still an in-flight request, |
| 368 // activation should not happen. | 365 // activation should not happen. |
| 369 version_2->OnSkipWaiting(77 /* dummy request_id */); | 366 version_2->OnSkipWaiting(77 /* dummy request_id */); |
| 370 base::RunLoop().RunUntilIdle(); | 367 base::RunLoop().RunUntilIdle(); |
| 371 EXPECT_EQ(version_1.get(), reg->active_version()); | 368 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 372 | 369 |
| 373 // Finish the request. Activation should happen. | 370 // Finish the request. Activation should happen. |
| 374 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, | 371 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 375 base::Time::Now()); | 372 base::Time::Now()); |
| 376 base::RunLoop().RunUntilIdle(); | 373 base::RunLoop().RunUntilIdle(); |
| 377 EXPECT_EQ(version_2.get(), reg->active_version()); | 374 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 378 } | 375 } |
| 379 | 376 |
| 380 INSTANTIATE_TEST_CASE_P(ServiceWorkerRegistrationTest, | |
| 381 ServiceWorkerRegistrationTestP, | |
| 382 testing::Bool()); | |
| 383 | |
| 384 INSTANTIATE_TEST_CASE_P(ServiceWorkerActivationTest, | |
| 385 ServiceWorkerActivationTest, | |
| 386 testing::Bool()); | |
| 387 | |
| 388 } // namespace content | 377 } // namespace content |
| OLD | NEW |