Save running SW instance info, including its SiteInstance, into the ProcessManager.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_test_helper.cc
blob26c4d0eadf112f0ca47f4f78b2700d76bade2eec
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 "base/bind.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"
16 namespace content {
18 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id)
19 : wrapper_(new ServiceWorkerContextWrapper(NULL)),
20 next_thread_id_(0),
21 weak_factory_(this) {
22 wrapper_->InitInternal(base::FilePath(),
23 base::MessageLoopProxy::current(),
24 base::MessageLoopProxy::current(),
25 NULL);
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() {
34 if (wrapper_)
35 wrapper_->Shutdown();
38 void EmbeddedWorkerTestHelper::SimulateAddProcessToWorker(
39 int embedded_worker_id,
40 int process_id) {
41 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
42 ASSERT_TRUE(worker);
43 registry()->AddChildProcessSender(process_id, this);
44 worker->AddProcessReference(process_id);
47 bool EmbeddedWorkerTestHelper::Send(IPC::Message* message) {
48 OnMessageReceived(*message);
49 delete message;
50 return true;
53 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) {
54 bool handled = true;
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)
61 IPC_END_MESSAGE_MAP()
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);
67 return handled;
70 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() {
71 return wrapper_->context();
74 void EmbeddedWorkerTestHelper::ShutdownContext() {
75 wrapper_->Shutdown();
76 wrapper_ = NULL;
79 void EmbeddedWorkerTestHelper::OnStartWorker(
80 int embedded_worker_id,
81 int64 service_worker_version_id,
82 const GURL& scope,
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(
94 int thread_id,
95 int embedded_worker_id,
96 const IPC::Message& message) {
97 bool handled = true;
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);
107 return handled;
110 void EmbeddedWorkerTestHelper::OnActivateEvent(int embedded_worker_id,
111 int request_id) {
112 SimulateSend(
113 new ServiceWorkerHostMsg_ActivateEventFinished(
114 embedded_worker_id, request_id,
115 blink::WebServiceWorkerEventResultCompleted));
118 void EmbeddedWorkerTestHelper::OnInstallEvent(int embedded_worker_id,
119 int request_id,
120 int active_version_id) {
121 SimulateSend(
122 new ServiceWorkerHostMsg_InstallEventFinished(
123 embedded_worker_id, request_id,
124 blink::WebServiceWorkerEventResultCompleted));
127 void EmbeddedWorkerTestHelper::OnFetchEvent(
128 int embedded_worker_id,
129 int request_id,
130 const ServiceWorkerFetchRequest& request) {
131 SimulateSend(
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(),
145 thread_id,
146 embedded_worker_id);
149 void EmbeddedWorkerTestHelper::SimulateWorkerStopped(
150 int embedded_worker_id) {
151 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id);
152 if (worker != NULL)
153 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id);
156 void EmbeddedWorkerTestHelper::SimulateSend(
157 IPC::Message* message) {
158 registry()->OnMessageReceived(*message);
159 delete 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(
169 FROM_HERE,
170 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker,
171 weak_factory_.GetWeakPtr(),
172 params.embedded_worker_id,
173 params.service_worker_version_id,
174 params.scope,
175 params.script_url));
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(
182 FROM_HERE,
183 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker,
184 weak_factory_.GetWeakPtr(),
185 embedded_worker_id));
188 void EmbeddedWorkerTestHelper::OnMessageToWorkerStub(
189 int thread_id,
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(
196 FROM_HERE,
197 base::Bind(
198 base::IgnoreResult(&EmbeddedWorkerTestHelper::OnMessageToWorker),
199 weak_factory_.GetWeakPtr(),
200 thread_id,
201 embedded_worker_id,
202 message));
205 void EmbeddedWorkerTestHelper::OnActivateEventStub(int request_id) {
206 base::MessageLoopProxy::current()->PostTask(
207 FROM_HERE,
208 base::Bind(&EmbeddedWorkerTestHelper::OnActivateEvent,
209 weak_factory_.GetWeakPtr(),
210 current_embedded_worker_id_,
211 request_id));
214 void EmbeddedWorkerTestHelper::OnInstallEventStub(int request_id,
215 int active_version_id) {
216 base::MessageLoopProxy::current()->PostTask(
217 FROM_HERE,
218 base::Bind(&EmbeddedWorkerTestHelper::OnInstallEvent,
219 weak_factory_.GetWeakPtr(),
220 current_embedded_worker_id_,
221 request_id,
222 active_version_id));
225 void EmbeddedWorkerTestHelper::OnFetchEventStub(
226 int request_id,
227 const ServiceWorkerFetchRequest& request) {
228 base::MessageLoopProxy::current()->PostTask(
229 FROM_HERE,
230 base::Bind(&EmbeddedWorkerTestHelper::OnFetchEvent,
231 weak_factory_.GetWeakPtr(),
232 current_embedded_worker_id_,
233 request_id,
234 request));
237 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() {
238 DCHECK(context());
239 return context()->embedded_worker_registry();
242 } // namespace content