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
3 // found in the LICENSE file.
5 #include "content/browser/service_worker/embedded_worker_test_helper.h"
8 #include "content/browser/service_worker/embedded_worker_instance.h"
9 #include "content/browser/service_worker/embedded_worker_registry.h"
10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/common/service_worker/embedded_worker_messages.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(
17 ServiceWorkerContextCore
* context
,
18 int mock_render_process_id
)
19 : context_(context
->AsWeakPtr()),
22 registry()->AddChildProcessSender(mock_render_process_id
, this);
25 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
28 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker(
29 int embedded_worker_id
,
31 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
33 registry()->AddChildProcessSender(process_id
, this);
34 worker
->AddProcessReference(process_id
);
37 bool EmbeddedWorkerTestHelper::Send(IPC::Message
* message
) {
38 OnMessageReceived(*message
);
43 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message
& message
) {
45 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
46 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorkerStub
)
47 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorkerStub
)
48 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_SendMessageToWorker
,
49 OnSendMessageToWorkerStub
)
50 IPC_MESSAGE_UNHANDLED(handled
= false)
53 // IPC::TestSink only records messages that are not handled by filters,
54 // so we just forward all messages to the separate sink.
55 sink_
.OnMessageReceived(message
);
60 void EmbeddedWorkerTestHelper::OnStartWorker(
61 int embedded_worker_id
,
62 int64 service_worker_version_id
,
63 const GURL
& script_url
) {
64 // By default just notify the sender that the worker is started.
65 SimulateWorkerStarted(next_thread_id_
++, embedded_worker_id
);
68 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id
) {
69 // By default just notify the sender that the worker is stopped.
70 SimulateWorkerStopped(embedded_worker_id
);
73 void EmbeddedWorkerTestHelper::OnSendMessageToWorker(
75 int embedded_worker_id
,
77 const IPC::Message
& message
) {
78 // Do nothing on the message by default.
81 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
82 int thread_id
, int embedded_worker_id
) {
83 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
84 ASSERT_TRUE(worker
!= NULL
);
85 registry()->OnWorkerStarted(
91 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
92 int embedded_worker_id
) {
93 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
94 ASSERT_TRUE(worker
!= NULL
);
95 registry()->OnWorkerStopped(worker
->process_id(), embedded_worker_id
);
98 void EmbeddedWorkerTestHelper::SimulateSendMessageToBrowser(
99 int embedded_worker_id
, int request_id
, const IPC::Message
& message
) {
100 registry()->OnSendMessageToBrowser(embedded_worker_id
, request_id
, message
);
103 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
104 int embedded_worker_id
,
105 int64 service_worker_version_id
,
106 const GURL
& script_url
) {
107 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
108 ASSERT_TRUE(worker
!= NULL
);
109 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
110 base::MessageLoopProxy::current()->PostTask(
112 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker
,
113 weak_factory_
.GetWeakPtr(),
115 service_worker_version_id
,
119 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id
) {
120 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
121 ASSERT_TRUE(worker
!= NULL
);
122 base::MessageLoopProxy::current()->PostTask(
124 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker
,
125 weak_factory_
.GetWeakPtr(),
126 embedded_worker_id
));
129 void EmbeddedWorkerTestHelper::OnSendMessageToWorkerStub(
131 int embedded_worker_id
,
133 const IPC::Message
& message
) {
134 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
135 ASSERT_TRUE(worker
!= NULL
);
136 EXPECT_EQ(worker
->thread_id(), thread_id
);
137 base::MessageLoopProxy::current()->PostTask(
139 base::Bind(&EmbeddedWorkerTestHelper::OnSendMessageToWorker
,
140 weak_factory_
.GetWeakPtr(),
147 EmbeddedWorkerRegistry
* EmbeddedWorkerTestHelper::registry() {
149 return context_
->embedded_worker_registry();
152 } // namespace content