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 "content/browser/service_worker/embedded_worker_test_helper.h" | 5 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 MockEmbeddedWorkerInstanceClient( | 66 MockEmbeddedWorkerInstanceClient( |
67 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 67 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
68 : helper_(helper), binding_(this) {} | 68 : helper_(helper), binding_(this) {} |
69 | 69 |
70 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: | 70 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient:: |
71 ~MockEmbeddedWorkerInstanceClient() {} | 71 ~MockEmbeddedWorkerInstanceClient() {} |
72 | 72 |
73 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( | 73 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( |
74 const EmbeddedWorkerStartParams& params, | 74 const EmbeddedWorkerStartParams& params, |
75 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, | 75 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, |
76 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, | 76 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { |
77 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) { | |
78 if (!helper_) | 77 if (!helper_) |
79 return; | 78 return; |
80 | 79 |
81 embedded_worker_id_ = params.embedded_worker_id; | 80 embedded_worker_id_ = params.embedded_worker_id; |
82 | 81 |
83 EmbeddedWorkerInstance* worker = | 82 EmbeddedWorkerInstance* worker = |
84 helper_->registry()->GetWorker(params.embedded_worker_id); | 83 helper_->registry()->GetWorker(params.embedded_worker_id); |
85 ASSERT_TRUE(worker); | 84 ASSERT_TRUE(worker); |
86 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); | 85 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); |
87 | 86 |
88 helper_->OnStartWorkerStub(params, std::move(dispatcher_request), | 87 helper_->OnStartWorkerStub(params, std::move(dispatcher_request), |
89 std::move(instance_host), | 88 std::move(instance_host)); |
90 std::move(provider_info)); | |
91 } | 89 } |
92 | 90 |
93 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker() { | 91 void EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StopWorker() { |
94 if (!helper_) | 92 if (!helper_) |
95 return; | 93 return; |
96 | 94 |
97 ASSERT_TRUE(embedded_worker_id_); | 95 ASSERT_TRUE(embedded_worker_id_); |
98 EmbeddedWorkerInstance* worker = | 96 EmbeddedWorkerInstance* worker = |
99 helper_->registry()->GetWorker(embedded_worker_id_.value()); | 97 helper_->registry()->GetWorker(embedded_worker_id_.value()); |
100 // |worker| is possible to be null when corresponding EmbeddedWorkerInstance | 98 // |worker| is possible to be null when corresponding EmbeddedWorkerInstance |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 | 283 |
286 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper( | 284 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper( |
287 const base::FilePath& user_data_directory) | 285 const base::FilePath& user_data_directory) |
288 : browser_context_(new TestBrowserContext), | 286 : browser_context_(new TestBrowserContext), |
289 render_process_host_(new MockRenderProcessHost(browser_context_.get())), | 287 render_process_host_(new MockRenderProcessHost(browser_context_.get())), |
290 new_render_process_host_( | 288 new_render_process_host_( |
291 new MockRenderProcessHost(browser_context_.get())), | 289 new MockRenderProcessHost(browser_context_.get())), |
292 wrapper_(new ServiceWorkerContextWrapper(browser_context_.get())), | 290 wrapper_(new ServiceWorkerContextWrapper(browser_context_.get())), |
293 mock_instance_clients_next_index_(0), | 291 mock_instance_clients_next_index_(0), |
294 next_thread_id_(0), | 292 next_thread_id_(0), |
| 293 next_provider_id_(1000), |
295 mock_render_process_id_(render_process_host_->GetID()), | 294 mock_render_process_id_(render_process_host_->GetID()), |
296 new_mock_render_process_id_(new_render_process_host_->GetID()), | 295 new_mock_render_process_id_(new_render_process_host_->GetID()), |
297 weak_factory_(this) { | 296 weak_factory_(this) { |
298 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( | 297 std::unique_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager( |
299 new MockServiceWorkerDatabaseTaskManager( | 298 new MockServiceWorkerDatabaseTaskManager( |
300 base::ThreadTaskRunnerHandle::Get())); | 299 base::ThreadTaskRunnerHandle::Get())); |
301 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), | 300 wrapper_->InitInternal(user_data_directory, std::move(database_task_manager), |
302 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); | 301 base::ThreadTaskRunnerHandle::Get(), nullptr, nullptr); |
303 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); | 302 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id()); |
304 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); | 303 wrapper_->process_manager()->SetNewProcessIdForTest(new_render_process_id()); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 return info; | 388 return info; |
390 } | 389 } |
391 | 390 |
392 void EmbeddedWorkerTestHelper::OnStartWorker( | 391 void EmbeddedWorkerTestHelper::OnStartWorker( |
393 int embedded_worker_id, | 392 int embedded_worker_id, |
394 int64_t service_worker_version_id, | 393 int64_t service_worker_version_id, |
395 const GURL& scope, | 394 const GURL& scope, |
396 const GURL& script_url, | 395 const GURL& script_url, |
397 bool pause_after_download, | 396 bool pause_after_download, |
398 mojom::ServiceWorkerEventDispatcherRequest request, | 397 mojom::ServiceWorkerEventDispatcherRequest request, |
399 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, | 398 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { |
400 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) { | |
401 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 399 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
402 ASSERT_TRUE(worker); | 400 ASSERT_TRUE(worker); |
403 MockServiceWorkerEventDispatcher::Create(AsWeakPtr(), worker->thread_id(), | 401 MockServiceWorkerEventDispatcher::Create(AsWeakPtr(), worker->thread_id(), |
404 std::move(request)); | 402 std::move(request)); |
405 | 403 |
406 embedded_worker_id_service_worker_version_id_map_[embedded_worker_id] = | 404 embedded_worker_id_service_worker_version_id_map_[embedded_worker_id] = |
407 service_worker_version_id; | 405 service_worker_version_id; |
408 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id].Bind( | 406 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id].Bind( |
409 std::move(instance_host)); | 407 std::move(instance_host)); |
410 ServiceWorkerRemoteProviderEndpoint* provider_endpoint = | |
411 &embedded_worker_id_remote_provider_map_[embedded_worker_id]; | |
412 provider_endpoint->BindWithProviderInfo(std::move(provider_info)); | |
413 | 408 |
414 SimulateWorkerReadyForInspection(embedded_worker_id); | 409 SimulateWorkerReadyForInspection(embedded_worker_id); |
415 SimulateWorkerScriptCached(embedded_worker_id); | 410 SimulateWorkerScriptCached(embedded_worker_id); |
416 SimulateWorkerScriptLoaded(embedded_worker_id); | 411 SimulateWorkerScriptLoaded(embedded_worker_id); |
417 if (!pause_after_download) | 412 if (!pause_after_download) |
418 OnResumeAfterDownload(embedded_worker_id); | 413 OnResumeAfterDownload(embedded_worker_id); |
419 } | 414 } |
420 | 415 |
421 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) { | 416 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) { |
422 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); | 417 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id, |
| 418 GetNextProviderId()); |
423 SimulateWorkerScriptEvaluated(embedded_worker_id, true /* success */); | 419 SimulateWorkerScriptEvaluated(embedded_worker_id, true /* success */); |
424 SimulateWorkerStarted(embedded_worker_id); | 420 SimulateWorkerStarted(embedded_worker_id); |
425 } | 421 } |
426 | 422 |
427 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { | 423 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { |
428 // By default just notify the sender that the worker is stopped. | 424 // By default just notify the sender that the worker is stopped. |
429 SimulateWorkerStopped(embedded_worker_id); | 425 SimulateWorkerStopped(embedded_worker_id); |
430 } | 426 } |
431 | 427 |
432 void EmbeddedWorkerTestHelper::OnActivateEvent( | 428 void EmbeddedWorkerTestHelper::OnActivateEvent( |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 569 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
574 ASSERT_TRUE(worker); | 570 ASSERT_TRUE(worker); |
575 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); | 571 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); |
576 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] | 572 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] |
577 ->OnScriptLoaded(); | 573 ->OnScriptLoaded(); |
578 base::RunLoop().RunUntilIdle(); | 574 base::RunLoop().RunUntilIdle(); |
579 } | 575 } |
580 | 576 |
581 void EmbeddedWorkerTestHelper::SimulateWorkerThreadStarted( | 577 void EmbeddedWorkerTestHelper::SimulateWorkerThreadStarted( |
582 int thread_id, | 578 int thread_id, |
583 int embedded_worker_id) { | 579 int embedded_worker_id, |
| 580 int provider_id) { |
584 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 581 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
585 ASSERT_TRUE(worker); | 582 ASSERT_TRUE(worker); |
586 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); | 583 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); |
| 584 |
| 585 // Prepare a provider host to be used by following OnThreadStarted(). |
| 586 std::unique_ptr<ServiceWorkerProviderHost> host = |
| 587 CreateProviderHostForServiceWorkerContext( |
| 588 worker->process_id(), provider_id, true /* is_parent_frame_secure */, |
| 589 context()->AsWeakPtr(), |
| 590 &embedded_worker_id_remote_provider_map_[embedded_worker_id]); |
| 591 context()->AddProviderHost(std::move(host)); |
| 592 |
587 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] | 593 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] |
588 ->OnThreadStarted(thread_id); | 594 ->OnThreadStarted(thread_id, provider_id); |
589 base::RunLoop().RunUntilIdle(); | 595 base::RunLoop().RunUntilIdle(); |
590 } | 596 } |
591 | 597 |
592 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated( | 598 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated( |
593 int embedded_worker_id, | 599 int embedded_worker_id, |
594 bool success) { | 600 bool success) { |
595 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 601 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
596 ASSERT_TRUE(worker); | 602 ASSERT_TRUE(worker); |
597 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); | 603 ASSERT_TRUE(embedded_worker_id_instance_host_ptr_map_[embedded_worker_id]); |
598 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] | 604 embedded_worker_id_instance_host_ptr_map_[embedded_worker_id] |
(...skipping 19 matching lines...) Expand all Loading... |
618 } | 624 } |
619 | 625 |
620 void EmbeddedWorkerTestHelper::SimulateSend(IPC::Message* message) { | 626 void EmbeddedWorkerTestHelper::SimulateSend(IPC::Message* message) { |
621 registry()->OnMessageReceived(*message, mock_render_process_id_); | 627 registry()->OnMessageReceived(*message, mock_render_process_id_); |
622 delete message; | 628 delete message; |
623 } | 629 } |
624 | 630 |
625 void EmbeddedWorkerTestHelper::OnStartWorkerStub( | 631 void EmbeddedWorkerTestHelper::OnStartWorkerStub( |
626 const EmbeddedWorkerStartParams& params, | 632 const EmbeddedWorkerStartParams& params, |
627 mojom::ServiceWorkerEventDispatcherRequest request, | 633 mojom::ServiceWorkerEventDispatcherRequest request, |
628 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host, | 634 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) { |
629 mojom::ServiceWorkerProviderInfoForStartWorkerPtr provider_info) { | |
630 EmbeddedWorkerInstance* worker = | 635 EmbeddedWorkerInstance* worker = |
631 registry()->GetWorker(params.embedded_worker_id); | 636 registry()->GetWorker(params.embedded_worker_id); |
632 ASSERT_TRUE(worker); | 637 ASSERT_TRUE(worker); |
633 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); | 638 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); |
634 base::ThreadTaskRunnerHandle::Get()->PostTask( | 639 base::ThreadTaskRunnerHandle::Get()->PostTask( |
635 FROM_HERE, | 640 FROM_HERE, |
636 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, AsWeakPtr(), | 641 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, AsWeakPtr(), |
637 params.embedded_worker_id, params.service_worker_version_id, | 642 params.embedded_worker_id, params.service_worker_version_id, |
638 params.scope, params.script_url, params.pause_after_download, | 643 params.scope, params.script_url, params.pause_after_download, |
639 base::Passed(&request), base::Passed(&instance_host), | 644 base::Passed(&request), base::Passed(&instance_host))); |
640 base::Passed(&provider_info))); | |
641 } | 645 } |
642 | 646 |
643 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub( | 647 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub( |
644 int embedded_worker_id) { | 648 int embedded_worker_id) { |
645 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 649 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
646 ASSERT_TRUE(worker); | 650 ASSERT_TRUE(worker); |
647 base::ThreadTaskRunnerHandle::Get()->PostTask( | 651 base::ThreadTaskRunnerHandle::Get()->PostTask( |
648 FROM_HERE, base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload, | 652 FROM_HERE, base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload, |
649 AsWeakPtr(), embedded_worker_id)); | 653 AsWeakPtr(), embedded_worker_id)); |
650 } | 654 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
794 base::Passed(&event_data), base::Passed(&response_callback), | 798 base::Passed(&event_data), base::Passed(&response_callback), |
795 base::Passed(&callback))); | 799 base::Passed(&callback))); |
796 } | 800 } |
797 | 801 |
798 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { | 802 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { |
799 DCHECK(context()); | 803 DCHECK(context()); |
800 return context()->embedded_worker_registry(); | 804 return context()->embedded_worker_registry(); |
801 } | 805 } |
802 | 806 |
803 } // namespace content | 807 } // namespace content |
OLD | NEW |