| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" |
| 14 #include "content/browser/service_worker/embedded_worker_status.h" | 15 #include "content/browser/service_worker/embedded_worker_status.h" |
| 15 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 16 #include "content/browser/service_worker/service_worker_context_core.h" | 17 #include "content/browser/service_worker/service_worker_context_core.h" |
| 17 #include "content/browser/service_worker/service_worker_registration_handle.h" | 18 #include "content/browser/service_worker/service_worker_registration_handle.h" |
| 18 #include "content/browser/service_worker/service_worker_test_utils.h" | 19 #include "content/browser/service_worker/service_worker_test_utils.h" |
| 19 #include "content/common/service_worker/service_worker_utils.h" | 20 #include "content/common/service_worker/service_worker_utils.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" | 21 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 23 | 24 |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 266 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 266 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 267 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 267 | 268 |
| 268 // Remove the controllee. Since there is an in-flight request, | 269 // Remove the controllee. Since there is an in-flight request, |
| 269 // activation should not yet happen. | 270 // activation should not yet happen. |
| 270 version_1->RemoveControllee(controllee()); | 271 version_1->RemoveControllee(controllee()); |
| 271 base::RunLoop().RunUntilIdle(); | 272 base::RunLoop().RunUntilIdle(); |
| 272 EXPECT_EQ(version_1.get(), reg->active_version()); | 273 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 273 | 274 |
| 274 // Finish the request. Activation should happen. | 275 // Finish the request. Activation should happen. |
| 275 version_1->FinishRequest(inflight_request_id(), true /* was_handled */); | 276 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 277 base::Time::Now()); |
| 276 base::RunLoop().RunUntilIdle(); | 278 base::RunLoop().RunUntilIdle(); |
| 277 EXPECT_EQ(version_2.get(), reg->active_version()); | 279 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 278 } | 280 } |
| 279 | 281 |
| 280 // Test activation triggered by loss of controllee. | 282 // Test activation triggered by loss of controllee. |
| 281 TEST_F(ServiceWorkerActivationTest, NoControllee) { | 283 TEST_F(ServiceWorkerActivationTest, NoControllee) { |
| 282 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 284 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 283 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 285 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 284 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 286 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 285 | 287 |
| 286 // Finish the request. Since there is a controllee, activation should not yet | 288 // Finish the request. Since there is a controllee, activation should not yet |
| 287 // happen. | 289 // happen. |
| 288 version_1->FinishRequest(inflight_request_id(), true /* was_handled */); | 290 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 291 base::Time::Now()); |
| 289 base::RunLoop().RunUntilIdle(); | 292 base::RunLoop().RunUntilIdle(); |
| 290 EXPECT_EQ(version_1.get(), reg->active_version()); | 293 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 291 | 294 |
| 292 // Remove the controllee. Activation should happen. | 295 // Remove the controllee. Activation should happen. |
| 293 version_1->RemoveControllee(controllee()); | 296 version_1->RemoveControllee(controllee()); |
| 294 base::RunLoop().RunUntilIdle(); | 297 base::RunLoop().RunUntilIdle(); |
| 295 EXPECT_EQ(version_2.get(), reg->active_version()); | 298 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 296 } | 299 } |
| 297 | 300 |
| 298 // Test activation triggered by skipWaiting. | 301 // Test activation triggered by skipWaiting. |
| 299 TEST_F(ServiceWorkerActivationTest, SkipWaiting) { | 302 TEST_F(ServiceWorkerActivationTest, SkipWaiting) { |
| 300 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 303 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 301 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 304 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 302 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 305 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 303 | 306 |
| 304 // Finish the in-flight request. Since there is a controllee, | 307 // Finish the in-flight request. Since there is a controllee, |
| 305 // activation should not happen. | 308 // activation should not happen. |
| 306 version_1->FinishRequest(inflight_request_id(), true /* was_handled */); | 309 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 310 base::Time::Now()); |
| 307 base::RunLoop().RunUntilIdle(); | 311 base::RunLoop().RunUntilIdle(); |
| 308 EXPECT_EQ(version_1.get(), reg->active_version()); | 312 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 309 | 313 |
| 310 // Call skipWaiting. Activation should happen. | 314 // Call skipWaiting. Activation should happen. |
| 311 version_2->OnSkipWaiting(77 /* dummy request_id */); | 315 version_2->OnSkipWaiting(77 /* dummy request_id */); |
| 312 base::RunLoop().RunUntilIdle(); | 316 base::RunLoop().RunUntilIdle(); |
| 313 EXPECT_EQ(version_2.get(), reg->active_version()); | 317 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 314 } | 318 } |
| 315 | 319 |
| 316 // Test activation triggered by skipWaiting and finishing requests. | 320 // Test activation triggered by skipWaiting and finishing requests. |
| 317 TEST_F(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) { | 321 TEST_F(ServiceWorkerActivationTest, SkipWaitingWithInflightRequest) { |
| 318 scoped_refptr<ServiceWorkerRegistration> reg = registration(); | 322 scoped_refptr<ServiceWorkerRegistration> reg = registration(); |
| 319 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); | 323 scoped_refptr<ServiceWorkerVersion> version_1 = reg->active_version(); |
| 320 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); | 324 scoped_refptr<ServiceWorkerVersion> version_2 = reg->waiting_version(); |
| 321 | 325 |
| 322 // Set skip waiting flag. Since there is still an in-flight request, | 326 // Set skip waiting flag. Since there is still an in-flight request, |
| 323 // activation should not happen. | 327 // activation should not happen. |
| 324 version_2->OnSkipWaiting(77 /* dummy request_id */); | 328 version_2->OnSkipWaiting(77 /* dummy request_id */); |
| 325 base::RunLoop().RunUntilIdle(); | 329 base::RunLoop().RunUntilIdle(); |
| 326 EXPECT_EQ(version_1.get(), reg->active_version()); | 330 EXPECT_EQ(version_1.get(), reg->active_version()); |
| 327 | 331 |
| 328 // Finish the request. Activation should happen. | 332 // Finish the request. Activation should happen. |
| 329 version_1->FinishRequest(inflight_request_id(), true /* was_handled */); | 333 version_1->FinishRequest(inflight_request_id(), true /* was_handled */, |
| 334 base::Time::Now()); |
| 330 base::RunLoop().RunUntilIdle(); | 335 base::RunLoop().RunUntilIdle(); |
| 331 EXPECT_EQ(version_2.get(), reg->active_version()); | 336 EXPECT_EQ(version_2.get(), reg->active_version()); |
| 332 } | 337 } |
| 333 | 338 |
| 334 } // namespace content | 339 } // namespace content |
| OLD | NEW |