Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blob4669069e8527f5dda47990b00cdc8517208d3e79
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"
7 #include <map>
8 #include <string>
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"
19 namespace content {
21 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id)
22 : wrapper_(new ServiceWorkerContextWrapper(NULL)),
23 next_thread_id_(0),
24 mock_render_process_id_(mock_render_process_id),
25 weak_factory_(this) {
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(),
33 NULL,
34 NULL);
35 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id);
36 registry()->AddChildProcessSender(mock_render_process_id, this, nullptr);
39 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
40 if (wrapper_.get())
41 wrapper_->Shutdown();
44 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(
45 const GURL& pattern,
46 int process_id) {
47 registry()->AddChildProcessSender(process_id, this, nullptr);
48 wrapper_->process_manager()->AddProcessReferenceToPattern(
49 pattern, process_id);
52 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
53 OnMessageReceived(*message);
54 delete message;
55 return true;
58 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
59 bool handled = true;
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)
68 IPC_END_MESSAGE_MAP()
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);
74 return handled;
77 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() {
78 return wrapper_->context();
81 void EmbeddedWorkerTestHelper::ShutdownContext() {
82 wrapper_->Shutdown();
83 wrapper_ = NULL;
86 void EmbeddedWorkerTestHelper::OnStartWorker(
87 int embedded_worker_id,
88 int64 service_worker_version_id,
89 const GURL& scope,
90 const GURL& script_url,
91 bool pause_after_download) {
92 if (pause_after_download) {
93 SimulatePausedAfterDownload(embedded_worker_id);
94 return;
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(
115 int thread_id,
116 int embedded_worker_id,
117 const IPC::Message& message) {
118 bool handled = true;
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);
128 return handled;
131 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id,
132 int request_id) {
133 SimulateSend(
134 new ServiceWorkerHostMsg_ActivateEventFinished(
135 embedded_worker_id, request_id,
136 blink::WebServiceWorkerEventResultCompleted));
139 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id,
140 int request_id,
141 int active_version_id) {
142 SimulateSend(
143 new ServiceWorkerHostMsg_InstallEventFinished(
144 embedded_worker_id, request_id,
145 blink::WebServiceWorkerEventResultCompleted));
148 void EmbeddedWorkerTestHelper::OnFetchEvent(
149 int embedded_worker_id,
150 int request_id,
151 const ServiceWorkerFetchRequest& request) {
152 SimulateSend(new ServiceWorkerHostMsg_FetchEventFinished(
153 embedded_worker_id,
154 request_id,
155 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
156 ServiceWorkerResponse(GURL(""),
157 200,
158 "OK",
159 blink::WebServiceWorkerResponseTypeDefault,
160 ServiceWorkerHeaderMap(),
161 std::string(),
162 0)));
165 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload(
166 int embedded_worker_id) {
167 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
168 ASSERT_TRUE(worker != NULL);
169 registry()->OnPausedAfterDownload(worker->process_id(), embedded_worker_id);
172 void EmbeddedWorkerTestHelper::SimulateWorkerReadyForInspection(
173 int embedded_worker_id) {
174 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
175 ASSERT_TRUE(worker != NULL);
176 registry()->OnWorkerReadyForInspection(worker->process_id(),
177 embedded_worker_id);
180 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded(
181 int thread_id, int embedded_worker_id) {
182 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
183 ASSERT_TRUE(worker != NULL);
184 registry()->OnWorkerScriptLoaded(
185 worker->process_id(), thread_id, embedded_worker_id);
188 void EmbeddedWorkerTestHelper::SimulateWorkerScriptEvaluated(
189 int embedded_worker_id) {
190 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
191 ASSERT_TRUE(worker != NULL);
192 registry()->OnWorkerScriptEvaluated(
193 worker->process_id(), embedded_worker_id, true /* success */);
196 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
197 int embedded_worker_id) {
198 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
199 ASSERT_TRUE(worker != NULL);
200 registry()->OnWorkerStarted(
201 worker->process_id(),
202 embedded_worker_id);
205 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
206 int embedded_worker_id) {
207 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
208 if (worker != NULL)
209 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
212 void EmbeddedWorkerTestHelper::SimulateSend(
213 IPC::Message* message) {
214 registry()->OnMessageReceived(*message);
215 delete message;
218 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
219 const EmbeddedWorkerMsg_StartWorker_Params& params) {
220 EmbeddedWorkerInstance* worker =
221 registry()->GetWorker(params.embedded_worker_id);
222 ASSERT_TRUE(worker != NULL);
223 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status());
224 base::MessageLoopProxy::current()->PostTask(
225 FROM_HERE,
226 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
227 weak_factory_.GetWeakPtr(),
228 params.embedded_worker_id,
229 params.service_worker_version_id,
230 params.scope,
231 params.script_url,
232 params.pause_after_download));
235 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
236 int embedded_worker_id) {
237 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
238 ASSERT_TRUE(worker != NULL);
239 base::MessageLoopProxy::current()->PostTask(
240 FROM_HERE,
241 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload,
242 weak_factory_.GetWeakPtr(),
243 embedded_worker_id));
246 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) {
247 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
248 ASSERT_TRUE(worker != NULL);
249 base::MessageLoopProxy::current()->PostTask(
250 FROM_HERE,
251 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
252 weak_factory_.GetWeakPtr(),
253 embedded_worker_id));
256 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
257 int thread_id,
258 int embedded_worker_id,
259 const IPC::Message& message) {
260 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
261 ASSERT_TRUE(worker != NULL);
262 EXPECT_EQ(worker->thread_id(), thread_id);
263 base::MessageLoopProxy::current()->PostTask(
264 FROM_HERE,
265 base::Bind(
266 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker),
267 weak_factory_.GetWeakPtr(),
268 thread_id,
269 embedded_worker_id,
270 message));
273 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) {
274 base::MessageLoopProxy::current()->PostTask(
275 FROM_HERE,
276 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent,
277 weak_factory_.GetWeakPtr(),
278 current_embedded_worker_id_,
279 request_id));
282 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id,
283 int active_version_id) {
284 base::MessageLoopProxy::current()->PostTask(
285 FROM_HERE,
286 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent,
287 weak_factory_.GetWeakPtr(),
288 current_embedded_worker_id_,
289 request_id,
290 active_version_id));
293 void EmbeddedWorkerTestHelper::OnFetchEventStub(
294 int request_id,
295 const ServiceWorkerFetchRequest& request) {
296 base::MessageLoopProxy::current()->PostTask(
297 FROM_HERE,
298 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent,
299 weak_factory_.GetWeakPtr(),
300 current_embedded_worker_id_,
301 request_id,
302 request));
305 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
306 DCHECK(context());
307 return context()->embedded_worker_registry();
310 } // namespace content