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"
10 #include "base/bind.h"
11 #include "content/browser/service_worker/embedded_worker_instance.h"
12 #include "content/browser/service_worker/embedded_worker_registry.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_context_wrapper.h"
15 #include "content/common/service_worker/embedded_worker_messages.h"
16 #include "content/common/service_worker/service_worker_messages.h"
17 #include "testing/gtest/include/gtest/gtest.h"
21 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id
)
22 : wrapper_(new ServiceWorkerContextWrapper(NULL
)),
24 mock_render_process_id_(mock_render_process_id
),
26 wrapper_
->InitInternal(base::FilePath(),
27 base::MessageLoopProxy::current(),
28 base::MessageLoopProxy::current(),
29 base::MessageLoopProxy::current(),
31 wrapper_
->process_manager()->SetProcessIdForTest(mock_render_process_id
);
32 registry()->AddChildProcessSender(mock_render_process_id
, this);
35 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
40 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker(
41 int embedded_worker_id
,
43 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
45 registry()->AddChildProcessSender(process_id
, this);
46 worker
->AddProcessReference(process_id
);
49 bool EmbeddedWorkerTestHelper::Send(IPC::Message
* message
) {
50 OnMessageReceived(*message
);
55 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message
& message
) {
57 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
58 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorkerStub
)
59 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorkerStub
)
60 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker
,
61 OnMessageToWorkerStub
)
62 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload
,
63 OnResumeAfterDownloadStub
)
64 IPC_MESSAGE_UNHANDLED(handled
= false)
67 // IPC::TestSink only records messages that are not handled by filters,
68 // so we just forward all messages to the separate sink.
69 sink_
.OnMessageReceived(message
);
74 ServiceWorkerContextCore
* EmbeddedWorkerTestHelper::context() {
75 return wrapper_
->context();
78 void EmbeddedWorkerTestHelper::ShutdownContext() {
83 void EmbeddedWorkerTestHelper::OnStartWorker(
84 int embedded_worker_id
,
85 int64 service_worker_version_id
,
87 const GURL
& script_url
,
88 bool pause_after_download
) {
89 if (pause_after_download
) {
90 SimulatePausedAfterDownload(embedded_worker_id
);
93 SimulateWorkerReadyForInspection(embedded_worker_id
);
94 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
95 SimulateWorkerStarted(embedded_worker_id
);
98 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id
) {
99 SimulateWorkerReadyForInspection(embedded_worker_id
);
100 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
101 SimulateWorkerStarted(embedded_worker_id
);
104 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id
) {
105 // By default just notify the sender that the worker is stopped.
106 SimulateWorkerStopped(embedded_worker_id
);
109 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
111 int embedded_worker_id
,
112 const IPC::Message
& message
) {
114 current_embedded_worker_id_
= embedded_worker_id
;
115 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
116 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent
, OnActivateEventStub
)
117 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent
, OnInstallEventStub
)
118 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent
, OnFetchEventStub
)
119 IPC_MESSAGE_UNHANDLED(handled
= false)
120 IPC_END_MESSAGE_MAP()
121 // Record all messages directed to inner script context.
122 inner_sink_
.OnMessageReceived(message
);
126 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id
,
129 new ServiceWorkerHostMsg_ActivateEventFinished(
130 embedded_worker_id
, request_id
,
131 blink::WebServiceWorkerEventResultCompleted
));
134 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id
,
136 int active_version_id
) {
138 new ServiceWorkerHostMsg_InstallEventFinished(
139 embedded_worker_id
, request_id
,
140 blink::WebServiceWorkerEventResultCompleted
));
143 void EmbeddedWorkerTestHelper::OnFetchEvent(
144 int embedded_worker_id
,
146 const ServiceWorkerFetchRequest
& request
) {
148 new ServiceWorkerHostMsg_FetchEventFinished(
151 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
,
152 ServiceWorkerResponse(GURL(""),
155 std::map
<std::string
, std::string
>(),
159 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload(
160 int embedded_worker_id
) {
161 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
162 ASSERT_TRUE(worker
!= NULL
);
163 registry()->OnPausedAfterDownload(worker
->process_id(), embedded_worker_id
);
166 void EmbeddedWorkerTestHelper::SimulateWorkerReadyForInspection(
167 int embedded_worker_id
) {
168 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
169 ASSERT_TRUE(worker
!= NULL
);
170 registry()->OnWorkerReadyForInspection(worker
->process_id(),
174 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded(
175 int thread_id
, int embedded_worker_id
) {
176 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
177 ASSERT_TRUE(worker
!= NULL
);
178 registry()->OnWorkerScriptLoaded(
179 worker
->process_id(), thread_id
, embedded_worker_id
);
182 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
183 int embedded_worker_id
) {
184 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
185 ASSERT_TRUE(worker
!= NULL
);
186 registry()->OnWorkerStarted(
187 worker
->process_id(),
191 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
192 int embedded_worker_id
) {
193 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
195 registry()->OnWorkerStopped(worker
->process_id(), embedded_worker_id
);
198 void EmbeddedWorkerTestHelper::SimulateSend(
199 IPC::Message
* message
) {
200 registry()->OnMessageReceived(*message
);
204 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
205 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
206 EmbeddedWorkerInstance
* worker
=
207 registry()->GetWorker(params
.embedded_worker_id
);
208 ASSERT_TRUE(worker
!= NULL
);
209 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
210 base::MessageLoopProxy::current()->PostTask(
212 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker
,
213 weak_factory_
.GetWeakPtr(),
214 params
.embedded_worker_id
,
215 params
.service_worker_version_id
,
218 params
.pause_after_download
));
221 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
222 int embedded_worker_id
) {
223 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
224 ASSERT_TRUE(worker
!= NULL
);
225 base::MessageLoopProxy::current()->PostTask(
227 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload
,
228 weak_factory_
.GetWeakPtr(),
229 embedded_worker_id
));
232 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id
) {
233 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
234 ASSERT_TRUE(worker
!= NULL
);
235 base::MessageLoopProxy::current()->PostTask(
237 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker
,
238 weak_factory_
.GetWeakPtr(),
239 embedded_worker_id
));
242 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
244 int embedded_worker_id
,
245 const IPC::Message
& message
) {
246 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
247 ASSERT_TRUE(worker
!= NULL
);
248 EXPECT_EQ(worker
->thread_id(), thread_id
);
249 base::MessageLoopProxy::current()->PostTask(
252 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker
),
253 weak_factory_
.GetWeakPtr(),
259 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id
) {
260 base::MessageLoopProxy::current()->PostTask(
262 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent
,
263 weak_factory_
.GetWeakPtr(),
264 current_embedded_worker_id_
,
268 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id
,
269 int active_version_id
) {
270 base::MessageLoopProxy::current()->PostTask(
272 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent
,
273 weak_factory_
.GetWeakPtr(),
274 current_embedded_worker_id_
,
279 void EmbeddedWorkerTestHelper::OnFetchEventStub(
281 const ServiceWorkerFetchRequest
& request
) {
282 base::MessageLoopProxy::current()->PostTask(
284 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent
,
285 weak_factory_
.GetWeakPtr(),
286 current_embedded_worker_id_
,
291 EmbeddedWorkerRegistry
* EmbeddedWorkerTestHelper::registry() {
293 return context()->embedded_worker_registry();
296 } // namespace content