Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blobb0f05c7296ac08f373e6aabe72ca7e0635295111
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::SimulateAddProcessToWorker(
41 int embedded_worker_id,
42 int process_id) {
43 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
44 ASSERT_TRUE(worker);
45 registry()->AddChildProcessSender(process_id, this);
46 worker->AddProcessReference(process_id);
49 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
50 OnMessageReceived(*message);
51 delete message;
52 return true;
55 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
56 bool handled = true;
57 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message)
58 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub)
59 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub)
60 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker,
61 OnMessageToWorkerStub)
62 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload,
63 OnResumeAfterDownloadStub)
64 IPC_MESSAGE_UNHANDLED(handled = false)
65 IPC_END_MESSAGE_MAP()
67 // IPC::TestSink only records messages that are not handled by filters,
68 // so we just forward all messages to the separate sink.
69 sink_.OnMessageReceived(message);
71 return handled;
74 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() {
75 return wrapper_->context();
78 void EmbeddedWorkerTestHelper::ShutdownContext() {
79 wrapper_->Shutdown();
80 wrapper_ = NULL;
83 void EmbeddedWorkerTestHelper::OnStartWorker(
84 int embedded_worker_id,
85 int64 service_worker_version_id,
86 const GURL& scope,
87 const GURL& script_url,
88 bool pause_after_download) {
89 if (pause_after_download) {
90 SimulatePausedAfterDownload(embedded_worker_id);
91 return;
93 SimulateWorkerReadyForInspection(embedded_worker_id);
94 SimulateWorkerScriptLoaded(next_thread_id_++, embedded_worker_id);
95 SimulateWorkerStarted(embedded_worker_id);
98 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) {
99 SimulateWorkerReadyForInspection(embedded_worker_id);
100 SimulateWorkerScriptLoaded(next_thread_id_++, embedded_worker_id);
101 SimulateWorkerStarted(embedded_worker_id);
104 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) {
105 // By default just notify the sender that the worker is stopped.
106 SimulateWorkerStopped(embedded_worker_id);
109 bool EmbeddedWorkerTestHelper::OnMessageToWorker(
110 int thread_id,
111 int embedded_worker_id,
112 const IPC::Message& message) {
113 bool handled = true;
114 current_embedded_worker_id_ = embedded_worker_id;
115 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message)
116 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEventStub)
117 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEventStub)
118 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEventStub)
119 IPC_MESSAGE_UNHANDLED(handled = false)
120 IPC_END_MESSAGE_MAP()
121 // Record all messages directed to inner script context.
122 inner_sink_.OnMessageReceived(message);
123 return handled;
126 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id,
127 int request_id) {
128 SimulateSend(
129 new ServiceWorkerHostMsg_ActivateEventFinished(
130 embedded_worker_id, request_id,
131 blink::WebServiceWorkerEventResultCompleted));
134 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id,
135 int request_id,
136 int active_version_id) {
137 SimulateSend(
138 new ServiceWorkerHostMsg_InstallEventFinished(
139 embedded_worker_id, request_id,
140 blink::WebServiceWorkerEventResultCompleted));
143 void EmbeddedWorkerTestHelper::OnFetchEvent(
144 int embedded_worker_id,
145 int request_id,
146 const ServiceWorkerFetchRequest& request) {
147 SimulateSend(
148 new ServiceWorkerHostMsg_FetchEventFinished(
149 embedded_worker_id,
150 request_id,
151 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE,
152 ServiceWorkerResponse(GURL(""),
153 200,
154 "OK",
155 std::map<std::string, std::string>(),
156 std::string())));
159 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload(
160 int embedded_worker_id) {
161 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
162 ASSERT_TRUE(worker != NULL);
163 registry()->OnPausedAfterDownload(worker->process_id(), embedded_worker_id);
166 void EmbeddedWorkerTestHelper::SimulateWorkerReadyForInspection(
167 int embedded_worker_id) {
168 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
169 ASSERT_TRUE(worker != NULL);
170 registry()->OnWorkerReadyForInspection(worker->process_id(),
171 embedded_worker_id);
174 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded(
175 int thread_id, int embedded_worker_id) {
176 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
177 ASSERT_TRUE(worker != NULL);
178 registry()->OnWorkerScriptLoaded(
179 worker->process_id(), thread_id, embedded_worker_id);
182 void EmbeddedWorkerTestHelper::SimulateWorkerStarted(
183 int embedded_worker_id) {
184 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
185 ASSERT_TRUE(worker != NULL);
186 registry()->OnWorkerStarted(
187 worker->process_id(),
188 embedded_worker_id);
191 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
192 int embedded_worker_id) {
193 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
194 if (worker != NULL)
195 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
198 void EmbeddedWorkerTestHelper::SimulateSend(
199 IPC::Message* message) {
200 registry()->OnMessageReceived(*message);
201 delete message;
204 void EmbeddedWorkerTestHelper::OnStartWorkerStub(
205 const EmbeddedWorkerMsg_StartWorker_Params& params) {
206 EmbeddedWorkerInstance* worker =
207 registry()->GetWorker(params.embedded_worker_id);
208 ASSERT_TRUE(worker != NULL);
209 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status());
210 base::MessageLoopProxy::current()->PostTask(
211 FROM_HERE,
212 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
213 weak_factory_.GetWeakPtr(),
214 params.embedded_worker_id,
215 params.service_worker_version_id,
216 params.scope,
217 params.script_url,
218 params.pause_after_download));
221 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub(
222 int embedded_worker_id) {
223 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
224 ASSERT_TRUE(worker != NULL);
225 base::MessageLoopProxy::current()->PostTask(
226 FROM_HERE,
227 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload,
228 weak_factory_.GetWeakPtr(),
229 embedded_worker_id));
232 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) {
233 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
234 ASSERT_TRUE(worker != NULL);
235 base::MessageLoopProxy::current()->PostTask(
236 FROM_HERE,
237 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
238 weak_factory_.GetWeakPtr(),
239 embedded_worker_id));
242 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
243 int thread_id,
244 int embedded_worker_id,
245 const IPC::Message& message) {
246 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
247 ASSERT_TRUE(worker != NULL);
248 EXPECT_EQ(worker->thread_id(), thread_id);
249 base::MessageLoopProxy::current()->PostTask(
250 FROM_HERE,
251 base::Bind(
252 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker),
253 weak_factory_.GetWeakPtr(),
254 thread_id,
255 embedded_worker_id,
256 message));
259 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) {
260 base::MessageLoopProxy::current()->PostTask(
261 FROM_HERE,
262 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent,
263 weak_factory_.GetWeakPtr(),
264 current_embedded_worker_id_,
265 request_id));
268 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id,
269 int active_version_id) {
270 base::MessageLoopProxy::current()->PostTask(
271 FROM_HERE,
272 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent,
273 weak_factory_.GetWeakPtr(),
274 current_embedded_worker_id_,
275 request_id,
276 active_version_id));
279 void EmbeddedWorkerTestHelper::OnFetchEventStub(
280 int request_id,
281 const ServiceWorkerFetchRequest& request) {
282 base::MessageLoopProxy::current()->PostTask(
283 FROM_HERE,
284 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent,
285 weak_factory_.GetWeakPtr(),
286 current_embedded_worker_id_,
287 request_id,
288 request));
291 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
292 DCHECK(context());
293 return context()->embedded_worker_registry();
296 } // namespace content