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