Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blob5da4474d24409a23b773272fae24e6bf50884a70
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(
22 const base::FilePath& user_data_directory,
23 int mock_render_process_id)
24 : wrapper_(new ServiceWorkerContextWrapper(NULL)),
25 next_thread_id_(0),
26 mock_render_process_id_(mock_render_process_id),
27 weak_factory_(this) {
28 scoped_ptr<MockServiceWorkerDatabaseTaskManager> database_task_manager(
29 new MockServiceWorkerDatabaseTaskManager(
30 base::MessageLoopProxy::current()));
31 wrapper_->InitInternal(user_data_directory, database_task_manager.Pass(),
32 base::MessageLoopProxy::current(), NULL, NULL);
33 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id);
34 registry()->AddChildProcessSender(mock_render_process_id, this, nullptr);
37 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
38 if (wrapper_.get())
39 wrapper_->Shutdown();
42 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(
43 const GURL& pattern,
44 int process_id) {
45 registry()->AddChildProcessSender(process_id, this, nullptr);
46 wrapper_->process_manager()->AddProcessReferenceToPattern(
47 pattern, process_id);
50 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
51 OnMessageReceived(*message);
52 delete message;
53 return true;
56 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
57 bool handled = true;
58 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message)
59 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub)
60 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub)
61 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker,
62 OnMessageToWorkerStub)
63 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload,
64 OnResumeAfterDownloadStub)
65 IPC_MESSAGE_UNHANDLED(handled = false)
66 IPC_END_MESSAGE_MAP()
68 // IPC::TestSink only records messages that are not handled by filters,
69 // so we just forward all messages to the separate sink.
70 sink_.OnMessageReceived(message);
72 return handled;
75 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() {
76 return wrapper_->context();
79 void EmbeddedWorkerTestHelper::ShutdownContext() {
80 wrapper_->Shutdown();
81 wrapper_ = NULL;
84 void EmbeddedWorkerTestHelper::OnStartWorker(
85 int embedded_worker_id,
86 int64 service_worker_version_id,
87 const GURL& scope,
88 const GURL& script_url,
89 bool pause_after_download) {
90 if (pause_after_download) {
91 SimulatePausedAfterDownload(embedded_worker_id);
92 return;
94 SimulateWorkerReadyForInspection(embedded_worker_id);
95 SimulateWorkerScriptLoaded(next_thread_id_++, embedded_worker_id);
96 SimulateWorkerScriptEvaluated(embedded_worker_id);
97 SimulateWorkerStarted(embedded_worker_id);
100 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) {
101 SimulateWorkerReadyForInspection(embedded_worker_id);
102 SimulateWorkerScriptLoaded(next_thread_id_++, embedded_worker_id);
103 SimulateWorkerScriptEvaluated(embedded_worker_id);
104 SimulateWorkerStarted(embedded_worker_id);
107 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) {
108 // By default just notify the sender that the worker is stopped.
109 SimulateWorkerStopped(embedded_worker_id);
112 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
113 int thread_id,
114 int embedded_worker_id,
115 const IPC::Message& message) {
116 bool handled = true;
117 current_embedded_worker_id_ = embedded_worker_id;
118 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message)
119 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEventStub)
120 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEventStub)
121 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEventStub)
122 IPC_MESSAGE_UNHANDLED(handled = false)
123 IPC_END_MESSAGE_MAP()
124 // Record all messages directed to inner script context.
125 inner_sink_.OnMessageReceived(message);
126 return handled;
129 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id,
130 int request_id) {
131 SimulateSend(
132 new ServiceWorkerHostMsg_ActivateEventFinished(
133 embedded_worker_id, request_id,
134 blink::WebServiceWorkerEventResultCompleted));
137 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id,
138 int request_id) {
139 // The installing worker may have been doomed and terminated.
140 if (!registry()->GetWorker(embedded_worker_id))
141 return;
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(),
163 GURL())));
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(),
178 embedded_worker_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(),
203 embedded_worker_id);
206 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
207 int embedded_worker_id) {
208 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
209 if (worker != NULL)
210 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
213 void EmbeddedWorkerTestHelper::SimulateSend(
214 IPC::Message* message) {
215 registry()->OnMessageReceived(*message, mock_render_process_id_);
216 delete 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(
226 FROM_HERE,
227 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
228 weak_factory_.GetWeakPtr(),
229 params.embedded_worker_id,
230 params.service_worker_version_id,
231 params.scope,
232 params.script_url,
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(
241 FROM_HERE,
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(
251 FROM_HERE,
252 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
253 weak_factory_.GetWeakPtr(),
254 embedded_worker_id));
257 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
258 int thread_id,
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(
265 FROM_HERE,
266 base::Bind(
267 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker),
268 weak_factory_.GetWeakPtr(),
269 thread_id,
270 embedded_worker_id,
271 message));
274 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) {
275 base::MessageLoopProxy::current()->PostTask(
276 FROM_HERE,
277 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent,
278 weak_factory_.GetWeakPtr(),
279 current_embedded_worker_id_,
280 request_id));
283 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_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));
292 void EmbeddedWorkerTestHelper::OnFetchEventStub(
293 int request_id,
294 const ServiceWorkerFetchRequest& request) {
295 base::MessageLoopProxy::current()->PostTask(
296 FROM_HERE,
297 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent,
298 weak_factory_.GetWeakPtr(),
299 current_embedded_worker_id_,
300 request_id,
301 request));
304 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
305 DCHECK(context());
306 return context()->embedded_worker_registry();
309 } // namespace content