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::SimulateAddProcessToPattern(
43 registry()->AddChildProcessSender(process_id
, this);
44 wrapper_
->process_manager()->AddProcessReferenceToPattern(
48 bool EmbeddedWorkerTestHelper::Send(IPC::Message
* message
) {
49 OnMessageReceived(*message
);
54 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message
& message
) {
56 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
57 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorkerStub
)
58 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorkerStub
)
59 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker
,
60 OnMessageToWorkerStub
)
61 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload
,
62 OnResumeAfterDownloadStub
)
63 IPC_MESSAGE_UNHANDLED(handled
= false)
66 // IPC::TestSink only records messages that are not handled by filters,
67 // so we just forward all messages to the separate sink.
68 sink_
.OnMessageReceived(message
);
73 ServiceWorkerContextCore
* EmbeddedWorkerTestHelper::context() {
74 return wrapper_
->context();
77 void EmbeddedWorkerTestHelper::ShutdownContext() {
82 void EmbeddedWorkerTestHelper::OnStartWorker(
83 int embedded_worker_id
,
84 int64 service_worker_version_id
,
86 const GURL
& script_url
,
87 bool pause_after_download
) {
88 if (pause_after_download
) {
89 SimulatePausedAfterDownload(embedded_worker_id
);
92 SimulateWorkerReadyForInspection(embedded_worker_id
);
93 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
94 SimulateWorkerStarted(embedded_worker_id
);
97 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id
) {
98 SimulateWorkerReadyForInspection(embedded_worker_id
);
99 SimulateWorkerScriptLoaded(next_thread_id_
++, embedded_worker_id
);
100 SimulateWorkerStarted(embedded_worker_id
);
103 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id
) {
104 // By default just notify the sender that the worker is stopped.
105 SimulateWorkerStopped(embedded_worker_id
);
108 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
110 int embedded_worker_id
,
111 const IPC::Message
& message
) {
113 current_embedded_worker_id_
= embedded_worker_id
;
114 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
115 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent
, OnActivateEventStub
)
116 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent
, OnInstallEventStub
)
117 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent
, OnFetchEventStub
)
118 IPC_MESSAGE_UNHANDLED(handled
= false)
119 IPC_END_MESSAGE_MAP()
120 // Record all messages directed to inner script context.
121 inner_sink_
.OnMessageReceived(message
);
125 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id
,
128 new ServiceWorkerHostMsg_ActivateEventFinished(
129 embedded_worker_id
, request_id
,
130 blink::WebServiceWorkerEventResultCompleted
));
133 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id
,
135 int active_version_id
) {
137 new ServiceWorkerHostMsg_InstallEventFinished(
138 embedded_worker_id
, request_id
,
139 blink::WebServiceWorkerEventResultCompleted
));
142 void EmbeddedWorkerTestHelper::OnFetchEvent(
143 int embedded_worker_id
,
145 const ServiceWorkerFetchRequest
& request
) {
147 new ServiceWorkerHostMsg_FetchEventFinished(
150 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
,
151 ServiceWorkerResponse(GURL(""),
154 std::map
<std::string
, std::string
>(),
158 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload(
159 int embedded_worker_id
) {
160 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
161 ASSERT_TRUE(worker
!= NULL
);
162 registry()->OnPausedAfterDownload(worker
->process_id(), embedded_worker_id
);
165 void EmbeddedWorkerTestHelper::SimulateWorkerReadyForInspection(
166 int embedded_worker_id
) {
167 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
168 ASSERT_TRUE(worker
!= NULL
);
169 registry()->OnWorkerReadyForInspection(worker
->process_id(),
173 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded(
174 int thread_id
, int embedded_worker_id
) {
175 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
176 ASSERT_TRUE(worker
!= NULL
);
177 registry()->OnWorkerScriptLoaded(
178 worker
->process_id(), thread_id
, embedded_worker_id
);
181 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
182 int embedded_worker_id
) {
183 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
184 ASSERT_TRUE(worker
!= NULL
);
185 registry()->OnWorkerStarted(
186 worker
->process_id(),
190 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
191 int embedded_worker_id
) {
192 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
194 registry()->OnWorkerStopped(worker
->process_id(), embedded_worker_id
);
197 void EmbeddedWorkerTestHelper::SimulateSend(
198 IPC::Message
* message
) {
199 registry()->OnMessageReceived(*message
);
203 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
204 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
205 EmbeddedWorkerInstance
* worker
=
206 registry()->GetWorker(params
.embedded_worker_id
);
207 ASSERT_TRUE(worker
!= NULL
);
208 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
209 base::MessageLoopProxy::current()->PostTask(
211 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker
,
212 weak_factory_
.GetWeakPtr(),
213 params
.embedded_worker_id
,
214 params
.service_worker_version_id
,
217 params
.pause_after_download
));
220 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
221 int embedded_worker_id
) {
222 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
223 ASSERT_TRUE(worker
!= NULL
);
224 base::MessageLoopProxy::current()->PostTask(
226 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload
,
227 weak_factory_
.GetWeakPtr(),
228 embedded_worker_id
));
231 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id
) {
232 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
233 ASSERT_TRUE(worker
!= NULL
);
234 base::MessageLoopProxy::current()->PostTask(
236 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker
,
237 weak_factory_
.GetWeakPtr(),
238 embedded_worker_id
));
241 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
243 int embedded_worker_id
,
244 const IPC::Message
& message
) {
245 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
246 ASSERT_TRUE(worker
!= NULL
);
247 EXPECT_EQ(worker
->thread_id(), thread_id
);
248 base::MessageLoopProxy::current()->PostTask(
251 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker
),
252 weak_factory_
.GetWeakPtr(),
258 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id
) {
259 base::MessageLoopProxy::current()->PostTask(
261 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent
,
262 weak_factory_
.GetWeakPtr(),
263 current_embedded_worker_id_
,
267 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id
,
268 int active_version_id
) {
269 base::MessageLoopProxy::current()->PostTask(
271 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent
,
272 weak_factory_
.GetWeakPtr(),
273 current_embedded_worker_id_
,
278 void EmbeddedWorkerTestHelper::OnFetchEventStub(
280 const ServiceWorkerFetchRequest
& request
) {
281 base::MessageLoopProxy::current()->PostTask(
283 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent
,
284 weak_factory_
.GetWeakPtr(),
285 current_embedded_worker_id_
,
290 EmbeddedWorkerRegistry
* EmbeddedWorkerTestHelper::registry() {
292 return context()->embedded_worker_registry();
295 } // namespace content