| 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/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "content/browser/service_worker/embedded_worker_registry.h" | 7 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 8 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
| 10 #include "content/browser/service_worker/service_worker_registration.h" | 10 #include "content/browser/service_worker/service_worker_registration.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]); | 356 ASSERT_EQ(ServiceWorkerVersion::ACTIVATING, statuses[2]); |
| 357 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]); | 357 ASSERT_EQ(ServiceWorkerVersion::ACTIVATED, statuses[3]); |
| 358 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]); | 358 ASSERT_EQ(ServiceWorkerVersion::REDUNDANT, statuses[4]); |
| 359 } | 359 } |
| 360 | 360 |
| 361 TEST_F(ServiceWorkerVersionTest, ScheduleStopWorker) { | 361 TEST_F(ServiceWorkerVersionTest, ScheduleStopWorker) { |
| 362 // Verify the timer is not running when version initializes its status. | 362 // Verify the timer is not running when version initializes its status. |
| 363 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | 363 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 364 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); | 364 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 365 | 365 |
| 366 // Verify the timer is running when version status changes frome ACTIVATING | 366 // Verify the timer is running after the worker is started. |
| 367 // to ACTIVATED. | |
| 368 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 367 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 369 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); | 368 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
| 370 base::RunLoop().RunUntilIdle(); | 369 base::RunLoop().RunUntilIdle(); |
| 371 EXPECT_EQ(SERVICE_WORKER_OK, status); | 370 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 372 version_->SetStatus(ServiceWorkerVersion::ACTIVATING); | |
| 373 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | |
| 374 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); | 371 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); |
| 375 | 372 |
| 376 // The timer should be running if the worker is restarted without controllee. | 373 // The timer should be running if the worker is restarted without controllee. |
| 377 status = SERVICE_WORKER_ERROR_FAILED; | 374 status = SERVICE_WORKER_ERROR_FAILED; |
| 378 version_->StopWorker(CreateReceiverOnCurrentThread(&status)); | 375 version_->StopWorker(CreateReceiverOnCurrentThread(&status)); |
| 379 base::RunLoop().RunUntilIdle(); | 376 base::RunLoop().RunUntilIdle(); |
| 380 EXPECT_EQ(SERVICE_WORKER_OK, status); | 377 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 381 status = SERVICE_WORKER_ERROR_FAILED; | 378 status = SERVICE_WORKER_ERROR_FAILED; |
| 382 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); | 379 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
| 383 base::RunLoop().RunUntilIdle(); | 380 base::RunLoop().RunUntilIdle(); |
| 384 EXPECT_EQ(SERVICE_WORKER_OK, status); | 381 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 385 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); | 382 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); |
| 386 | 383 |
| 387 // The timer should not be running if a controllee is added. | 384 // The timer should not be running if a controllee is added. |
| 388 scoped_ptr<ServiceWorkerProviderHost> host( | 385 scoped_ptr<ServiceWorkerProviderHost> host( |
| 389 new ServiceWorkerProviderHost(33 /* dummy render process id */, | 386 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 390 MSG_ROUTING_NONE /* render_frame_id */, | 387 MSG_ROUTING_NONE /* render_frame_id */, |
| 391 1 /* dummy provider_id */, | 388 1 /* dummy provider_id */, |
| 392 helper_->context()->AsWeakPtr(), | 389 helper_->context()->AsWeakPtr(), |
| 393 NULL)); | 390 NULL)); |
| 394 version_->AddControllee(host.get()); | 391 version_->AddControllee(host.get()); |
| 395 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); | 392 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 396 | 393 |
| 397 // The timer should be running if the controllee is removed. | 394 // The timer should be running if the controllee is removed. |
| 398 version_->RemoveControllee(host.get()); | 395 version_->RemoveControllee(host.get()); |
| 399 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); | 396 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); |
| 400 } | 397 } |
| 401 | 398 |
| 399 TEST_F(ServiceWorkerVersionTest, KeepAlive) { |
| 400 // Verify the timer is not running when version initializes its status. |
| 401 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); |
| 402 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 403 |
| 404 // Verify the timer is running after the worker is started. |
| 405 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 406 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
| 407 base::RunLoop().RunUntilIdle(); |
| 408 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 409 EXPECT_TRUE(version_->stop_worker_timer_.IsRunning()); |
| 410 |
| 411 // Set keep alive true. |
| 412 helper_->context()->SetKeepAliveMode(true); |
| 413 |
| 414 // This should have stopped the stop worker timer. |
| 415 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 416 |
| 417 // The timer should not be running even after a controllee is added |
| 418 // and then removed (i.e. so there's no controllee) if the keep_alive |
| 419 // mode is true. |
| 420 scoped_ptr<ServiceWorkerProviderHost> host( |
| 421 new ServiceWorkerProviderHost(33 /* dummy render process id */, |
| 422 MSG_ROUTING_NONE /* render_frame_id */, |
| 423 1 /* dummy provider_id */, |
| 424 helper_->context()->AsWeakPtr(), |
| 425 NULL)); |
| 426 version_->AddControllee(host.get()); |
| 427 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 428 version_->RemoveControllee(host.get()); |
| 429 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 430 |
| 431 // Stopping the worker. |
| 432 status = SERVICE_WORKER_ERROR_FAILED; |
| 433 version_->StopWorker(CreateReceiverOnCurrentThread(&status)); |
| 434 base::RunLoop().RunUntilIdle(); |
| 435 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 436 |
| 437 // Restart the worker. Usually this should start the stop worker timer, |
| 438 // but not now as keep_alive mode is true. |
| 439 status = SERVICE_WORKER_ERROR_FAILED; |
| 440 version_->StartWorker(CreateReceiverOnCurrentThread(&status)); |
| 441 base::RunLoop().RunUntilIdle(); |
| 442 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 443 EXPECT_FALSE(version_->stop_worker_timer_.IsRunning()); |
| 444 } |
| 445 |
| 402 TEST_F(ServiceWorkerVersionTest, ListenerAvailability) { | 446 TEST_F(ServiceWorkerVersionTest, ListenerAvailability) { |
| 403 // Initially the worker is not running. There should be no cache_listener_. | 447 // Initially the worker is not running. There should be no cache_listener_. |
| 404 EXPECT_FALSE(version_->cache_listener_.get()); | 448 EXPECT_FALSE(version_->cache_listener_.get()); |
| 405 | 449 |
| 406 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 450 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
| 407 version_->StartWorker( | 451 version_->StartWorker( |
| 408 CreateReceiverOnCurrentThread(&status)); | 452 CreateReceiverOnCurrentThread(&status)); |
| 409 base::RunLoop().RunUntilIdle(); | 453 base::RunLoop().RunUntilIdle(); |
| 410 | 454 |
| 411 // A new cache listener should be available once the worker starts. | 455 // A new cache listener should be available once the worker starts. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 helper_->context()->embedded_worker_registry()->RemoveChildProcessSender( | 487 helper_->context()->embedded_worker_registry()->RemoveChildProcessSender( |
| 444 process_id); | 488 process_id); |
| 445 base::RunLoop().RunUntilIdle(); | 489 base::RunLoop().RunUntilIdle(); |
| 446 | 490 |
| 447 // Callback completed. | 491 // Callback completed. |
| 448 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); | 492 EXPECT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status); |
| 449 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); | 493 EXPECT_EQ(ServiceWorkerVersion::STOPPED, version_->running_status()); |
| 450 } | 494 } |
| 451 | 495 |
| 452 } // namespace content | 496 } // namespace content |
| OLD | NEW |