Implement SSLKEYLOGFILE for OpenSSL.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blob97adcf172536d59fb9b76d43c43245026e0882bd
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 wrapper_->InitInternal(base::FilePath(),
27 base::MessageLoopProxy::current(),
28 base::MessageLoopProxy::current(),
29 base::MessageLoopProxy::current(),
30 NULL);
31 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id);
32 registry()->AddChildProcessSender(mock_render_process_id, this);
35 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() {
36 if (wrapper_.get())
37 wrapper_->Shutdown();
40 void EmbeddedWorkerTestHelper::SimulateAddProcessToPattern(
41 const GURL& pattern,
42 int process_id) {
43 registry()->AddChildProcessSender(process_id, this);
44 wrapper_->process_manager()->AddProcessReferenceToPattern(
45 pattern, process_id);
48 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
49 OnMessageReceived(*message);
50 delete message;
51 return true;
54 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
55 bool handled = true;
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)
64 IPC_END_MESSAGE_MAP()
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);
70 return handled;
73 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() {
74 return wrapper_->context();
77 void EmbeddedWorkerTestHelper::ShutdownContext() {
78 wrapper_->Shutdown();
79 wrapper_ = NULL;
82 void EmbeddedWorkerTestHelper::OnStartWorker(
83 int embedded_worker_id,
84 int64 service_worker_version_id,
85 const GURL& scope,
86 const GURL& script_url,
87 bool pause_after_download) {
88 if (pause_after_download) {
89 SimulatePausedAfterDownload(embedded_worker_id);
90 return;
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(
109 int thread_id,
110 int embedded_worker_id,
111 const IPC::Message& message) {
112 bool handled = true;
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);
122 return handled;
125 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id,
126 int request_id) {
127 SimulateSend(
128 new ServiceWorkerHostMsg_ActivateEventFinished(
129 embedded_worker_id, request_id,
130 blink::WebServiceWorkerEventResultCompleted));
133 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id,
134 int request_id,
135 int active_version_id) {
136 SimulateSend(
137 new ServiceWorkerHostMsg_InstallEventFinished(
138 embedded_worker_id, request_id,
139 blink::WebServiceWorkerEventResultCompleted));
142 void EmbeddedWorkerTestHelper::OnFetchEvent(
143 int embedded_worker_id,
144 int request_id,
145 const ServiceWorkerFetchRequest& request) {
146 SimulateSend(
147 new ServiceWorkerHostMsg_FetchEventFinished(
148 embedded_worker_id,
149 request_id,
150 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
151 ServiceWorkerResponse(GURL(""),
152 200,
153 "OK",
154 std::map<std::string, std::string>(),
155 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(),
170 embedded_worker_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(),
187 embedded_worker_id);
190 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
191 int embedded_worker_id) {
192 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
193 if (worker != NULL)
194 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
197 void EmbeddedWorkerTestHelper::SimulateSend(
198 IPC::Message* message) {
199 registry()->OnMessageReceived(*message);
200 delete 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(
210 FROM_HERE,
211 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
212 weak_factory_.GetWeakPtr(),
213 params.embedded_worker_id,
214 params.service_worker_version_id,
215 params.scope,
216 params.script_url,
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(
225 FROM_HERE,
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(
235 FROM_HERE,
236 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
237 weak_factory_.GetWeakPtr(),
238 embedded_worker_id));
241 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
242 int thread_id,
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(
249 FROM_HERE,
250 base::Bind(
251 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker),
252 weak_factory_.GetWeakPtr(),
253 thread_id,
254 embedded_worker_id,
255 message));
258 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) {
259 base::MessageLoopProxy::current()->PostTask(
260 FROM_HERE,
261 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent,
262 weak_factory_.GetWeakPtr(),
263 current_embedded_worker_id_,
264 request_id));
267 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id,
268 int active_version_id) {
269 base::MessageLoopProxy::current()->PostTask(
270 FROM_HERE,
271 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent,
272 weak_factory_.GetWeakPtr(),
273 current_embedded_worker_id_,
274 request_id,
275 active_version_id));
278 void EmbeddedWorkerTestHelper::OnFetchEventStub(
279 int request_id,
280 const ServiceWorkerFetchRequest& request) {
281 base::MessageLoopProxy::current()->PostTask(
282 FROM_HERE,
283 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent,
284 weak_factory_.GetWeakPtr(),
285 current_embedded_worker_id_,
286 request_id,
287 request));
290 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
291 DCHECK(context());
292 return context()->embedded_worker_registry();
295 } // namespace content