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/browser/service_worker/service_worker_context_wrapper.h"
12 #include "content/common/service_worker/embedded_worker_messages.h"
13 #include "content/common/service_worker/service_worker_messages.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id
)
19 : wrapper_(new ServiceWorkerContextWrapper(NULL
)),
22 wrapper_
->InitInternal(base::FilePath(),
23 base::MessageLoopProxy::current(),
24 base::MessageLoopProxy::current(),
26 scoped_ptr
<ServiceWorkerProcessManager
> process_manager(
27 new ServiceWorkerProcessManager(wrapper_
));
28 process_manager
->SetProcessIdForTest(mock_render_process_id
);
29 wrapper_
->context()->SetProcessManagerForTest(process_manager
.Pass());
30 registry()->AddChildProcessSender(mock_render_process_id
, this);
33 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
38 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker(
39 int embedded_worker_id
,
41 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
43 registry()->AddChildProcessSender(process_id
, this);
44 worker
->AddProcessReference(process_id
);
47 bool EmbeddedWorkerTestHelper::Send(IPC::Message
* message
) {
48 OnMessageReceived(*message
);
53 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message
& message
) {
55 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
56 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorkerStub
)
57 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorkerStub
)
58 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker
,
59 OnMessageToWorkerStub
)
60 IPC_MESSAGE_UNHANDLED(handled
= false)
63 // IPC::TestSink only records messages that are not handled by filters,
64 // so we just forward all messages to the separate sink.
65 sink_
.OnMessageReceived(message
);
70 ServiceWorkerContextCore
* EmbeddedWorkerTestHelper::context() {
71 return wrapper_
->context();
74 void EmbeddedWorkerTestHelper::ShutdownContext() {
79 void EmbeddedWorkerTestHelper::OnStartWorker(
80 int embedded_worker_id
,
81 int64 service_worker_version_id
,
83 const GURL
& script_url
) {
84 // By default just notify the sender that the worker is started.
85 SimulateWorkerStarted(next_thread_id_
++, embedded_worker_id
);
88 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id
) {
89 // By default just notify the sender that the worker is stopped.
90 SimulateWorkerStopped(embedded_worker_id
);
93 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
95 int embedded_worker_id
,
96 const IPC::Message
& message
) {
98 current_embedded_worker_id_
= embedded_worker_id
;
99 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper
, message
)
100 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent
, OnActivateEventStub
)
101 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent
, OnInstallEventStub
)
102 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent
, OnFetchEventStub
)
103 IPC_MESSAGE_UNHANDLED(handled
= false)
104 IPC_END_MESSAGE_MAP()
105 // Record all messages directed to inner script context.
106 inner_sink_
.OnMessageReceived(message
);
110 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id
,
113 new ServiceWorkerHostMsg_ActivateEventFinished(
114 embedded_worker_id
, request_id
,
115 blink::WebServiceWorkerEventResultCompleted
));
118 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id
,
120 int active_version_id
) {
122 new ServiceWorkerHostMsg_InstallEventFinished(
123 embedded_worker_id
, request_id
,
124 blink::WebServiceWorkerEventResultCompleted
));
127 void EmbeddedWorkerTestHelper::OnFetchEvent(
128 int embedded_worker_id
,
130 const ServiceWorkerFetchRequest
& request
) {
132 new ServiceWorkerHostMsg_FetchEventFinished(
133 embedded_worker_id
, request_id
,
134 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE
,
135 ServiceWorkerResponse(200, "OK", "GET",
136 std::map
<std::string
, std::string
>())));
139 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
140 int thread_id
, int embedded_worker_id
) {
141 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
142 ASSERT_TRUE(worker
!= NULL
);
143 registry()->OnWorkerStarted(
144 worker
->process_id(),
149 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
150 int embedded_worker_id
) {
151 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
153 registry()->OnWorkerStopped(worker
->process_id(), embedded_worker_id
);
156 void EmbeddedWorkerTestHelper::SimulateSend(
157 IPC::Message
* message
) {
158 registry()->OnMessageReceived(*message
);
162 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
163 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
164 EmbeddedWorkerInstance
* worker
=
165 registry()->GetWorker(params
.embedded_worker_id
);
166 ASSERT_TRUE(worker
!= NULL
);
167 EXPECT_EQ(EmbeddedWorkerInstance::STARTING
, worker
->status());
168 base::MessageLoopProxy::current()->PostTask(
170 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker
,
171 weak_factory_
.GetWeakPtr(),
172 params
.embedded_worker_id
,
173 params
.service_worker_version_id
,
178 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id
) {
179 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
180 ASSERT_TRUE(worker
!= NULL
);
181 base::MessageLoopProxy::current()->PostTask(
183 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker
,
184 weak_factory_
.GetWeakPtr(),
185 embedded_worker_id
));
188 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
190 int embedded_worker_id
,
191 const IPC::Message
& message
) {
192 EmbeddedWorkerInstance
* worker
= registry()->GetWorker(embedded_worker_id
);
193 ASSERT_TRUE(worker
!= NULL
);
194 EXPECT_EQ(worker
->thread_id(), thread_id
);
195 base::MessageLoopProxy::current()->PostTask(
198 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker
),
199 weak_factory_
.GetWeakPtr(),
205 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id
) {
206 base::MessageLoopProxy::current()->PostTask(
208 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent
,
209 weak_factory_
.GetWeakPtr(),
210 current_embedded_worker_id_
,
214 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id
,
215 int active_version_id
) {
216 base::MessageLoopProxy::current()->PostTask(
218 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent
,
219 weak_factory_
.GetWeakPtr(),
220 current_embedded_worker_id_
,
225 void EmbeddedWorkerTestHelper::OnFetchEventStub(
227 const ServiceWorkerFetchRequest
& request
) {
228 base::MessageLoopProxy::current()->PostTask(
230 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent
,
231 weak_factory_
.GetWeakPtr(),
232 current_embedded_worker_id_
,
237 EmbeddedWorkerRegistry
* EmbeddedWorkerTestHelper::registry() {
239 return context()->embedded_worker_registry();
242 } // namespace content