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 scoped_ptr
<MockServiceWorkerDatabaseTaskManager
> database_task_manager(
27 new MockServiceWorkerDatabaseTaskManager(
28 base::MessageLoopProxy::current()));
29 wrapper_
->InitInternal(base::FilePath(),
30 base::MessageLoopProxy::current(),
31 database_task_manager
.Pass(),
32 base::MessageLoopProxy::current(),
35 wrapper_
->process_manager()->SetProcessIdForTest(mock_render_process_id
);
36 registry()->AddChildProcessSender(mock_render_process_id
, this, nullptr);
39 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
44 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(
47 registry()->AddChildProcessSender(process_id
, this, nullptr);
48 wrapper_
->process_manager()->AddProcessReferenceToPattern(
52 bool EmbeddedWorkerTestHelper::Send(IPC::Message
* message
) {
53 OnMessageReceived(*message
);
58 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message
& message
) {
60 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
61 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorkerStub
)
62 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorkerStub
)
63 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker
,
64 OnMessageToWorkerStub
)
65 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload
,
66 OnResumeAfterDownloadStub
)
67 IPC_MESSAGE_UNHANDLED(handled
= false)
70 // IPC::TestSink only records messages that are not handled by filters,
71 // so we just forward all messages to the separate sink.
72 sink_
.OnMessageReceived(message
);
77 ServiceWorkerContextCore
* EmbeddedWorkerTestHelper::context() {
78 return wrapper_
->context();
81 void EmbeddedWorkerTestHelper::ShutdownContext() {
86 void EmbeddedWorkerTestHelper::OnStartWorker(
87 int embedded_worker_id
,
88 int64 service_worker_version_id
,
90 const GURL
& script_url
,
91 bool pause_after_download
) {
92 if (pause_after_download
) {
93 SimulatePausedAfterDownload(embedded_worker_id
);
96 SimulateWorkerReadyForInspection(embedded_worker_id
);
97 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
98 SimulateWorkerScriptEvaluated(embedded_worker_id
);
99 SimulateWorkerStarted(embedded_worker_id
);
102 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id
) {
103 SimulateWorkerReadyForInspection(embedded_worker_id
);
104 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
105 SimulateWorkerScriptEvaluated(embedded_worker_id
);
106 SimulateWorkerStarted(embedded_worker_id
);
109 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id
) {
110 // By default just notify the sender that the worker is stopped.
111 SimulateWorkerStopped(embedded_worker_id
);
114 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
116 int embedded_worker_id
,
117 const IPC::Message
& message
) {
119 current_embedded_worker_id_
= embedded_worker_id
;
120 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
121 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent
, OnActivateEventStub
)
122 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent
, OnInstallEventStub
)
123 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent
, OnFetchEventStub
)
124 IPC_MESSAGE_UNHANDLED(handled
= false)
125 IPC_END_MESSAGE_MAP()
126 // Record all messages directed to inner script context.
127 inner_sink_
.OnMessageReceived(message
);
131 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id
,
134 new ServiceWorkerHostMsg_ActivateEventFinished(
135 embedded_worker_id
, request_id
,
136 blink::WebServiceWorkerEventResultCompleted
));
139 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id
,
141 int active_version_id
) {
143 new ServiceWorkerHostMsg_InstallEventFinished(
144 embedded_worker_id
, request_id
,
145 blink::WebServiceWorkerEventResultCompleted
));
148 void EmbeddedWorkerTestHelper::OnFetchEvent(
149 int embedded_worker_id
,
151 const ServiceWorkerFetchRequest
& request
) {
152 SimulateSend(new ServiceWorkerHostMsg_FetchEventFinished(
155 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
,
156 ServiceWorkerResponse(GURL(),
159 blink::WebServiceWorkerResponseTypeDefault
,
160 ServiceWorkerHeaderMap(),
166 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload(
167 int embedded_worker_id
) {
168 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
169 ASSERT_TRUE(worker
!= NULL
);
170 registry()->OnPausedAfterDownload(worker
->process_id(), embedded_worker_id
);
173 void EmbeddedWorkerTestHelper::SimulateWorkerReadyForInspection(
174 int embedded_worker_id
) {
175 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
176 ASSERT_TRUE(worker
!= NULL
);
177 registry()->OnWorkerReadyForInspection(worker
->process_id(),
181 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded(
182 int thread_id
, int embedded_worker_id
) {
183 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
184 ASSERT_TRUE(worker
!= NULL
);
185 registry()->OnWorkerScriptLoaded(
186 worker
->process_id(), thread_id
, embedded_worker_id
);
189 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated(
190 int embedded_worker_id
) {
191 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
192 ASSERT_TRUE(worker
!= NULL
);
193 registry()->OnWorkerScriptEvaluated(
194 worker
->process_id(), embedded_worker_id
, true /* success */);
197 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
198 int embedded_worker_id
) {
199 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
200 ASSERT_TRUE(worker
!= NULL
);
201 registry()->OnWorkerStarted(
202 worker
->process_id(),
206 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
207 int embedded_worker_id
) {
208 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
210 registry()->OnWorkerStopped(worker
->process_id(), embedded_worker_id
);
213 void EmbeddedWorkerTestHelper::SimulateSend(
214 IPC::Message
* message
) {
215 registry()->OnMessageReceived(*message
);
219 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
220 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
221 EmbeddedWorkerInstance
* worker
=
222 registry()->GetWorker(params
.embedded_worker_id
);
223 ASSERT_TRUE(worker
!= NULL
);
224 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
225 base::MessageLoopProxy::current()->PostTask(
227 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker
,
228 weak_factory_
.GetWeakPtr(),
229 params
.embedded_worker_id
,
230 params
.service_worker_version_id
,
233 params
.pause_after_download
));
236 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
237 int embedded_worker_id
) {
238 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
239 ASSERT_TRUE(worker
!= NULL
);
240 base::MessageLoopProxy::current()->PostTask(
242 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload
,
243 weak_factory_
.GetWeakPtr(),
244 embedded_worker_id
));
247 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id
) {
248 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
249 ASSERT_TRUE(worker
!= NULL
);
250 base::MessageLoopProxy::current()->PostTask(
252 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker
,
253 weak_factory_
.GetWeakPtr(),
254 embedded_worker_id
));
257 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
259 int embedded_worker_id
,
260 const IPC::Message
& message
) {
261 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
262 ASSERT_TRUE(worker
!= NULL
);
263 EXPECT_EQ(worker
->thread_id(), thread_id
);
264 base::MessageLoopProxy::current()->PostTask(
267 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker
),
268 weak_factory_
.GetWeakPtr(),
274 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id
) {
275 base::MessageLoopProxy::current()->PostTask(
277 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent
,
278 weak_factory_
.GetWeakPtr(),
279 current_embedded_worker_id_
,
283 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id
,
284 int active_version_id
) {
285 base::MessageLoopProxy::current()->PostTask(
287 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent
,
288 weak_factory_
.GetWeakPtr(),
289 current_embedded_worker_id_
,
294 void EmbeddedWorkerTestHelper::OnFetchEventStub(
296 const ServiceWorkerFetchRequest
& request
) {
297 base::MessageLoopProxy::current()->PostTask(
299 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent
,
300 weak_factory_
.GetWeakPtr(),
301 current_embedded_worker_id_
,
306 EmbeddedWorkerRegistry
* EmbeddedWorkerTestHelper::registry() {
308 return context()->embedded_worker_registry();
311 } // namespace content