OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 5 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "content/browser/service_worker/embedded_worker_instance.h" | 11 #include "content/browser/service_worker/embedded_worker_instance.h" |
12 #include "content/browser/service_worker/embedded_worker_registry.h" | 12 #include "content/browser/service_worker/embedded_worker_registry.h" |
13 #include "content/browser/service_worker/service_worker_context_core.h" | 13 #include "content/browser/service_worker/service_worker_context_core.h" |
14 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 14 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
15 #include "content/common/service_worker/embedded_worker_messages.h" | 15 #include "content/common/service_worker/embedded_worker_messages.h" |
16 #include "content/common/service_worker/service_worker_messages.h" | 16 #include "content/common/service_worker/service_worker_messages.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id) | 21 EmbeddedWorkerTestHelper::EmbeddedWorkerTestHelper(int mock_render_process_id) |
22 : wrapper_(new ServiceWorkerContextWrapper(NULL)), | 22 : wrapper_(new ServiceWorkerContextWrapper(NULL)), |
23 next_thread_id_(0), | 23 next_thread_id_(0), |
| 24 mock_render_process_id_(mock_render_process_id), |
24 weak_factory_(this) { | 25 weak_factory_(this) { |
25 wrapper_->InitInternal(base::FilePath(), | 26 wrapper_->InitInternal(base::FilePath(), |
26 base::MessageLoopProxy::current(), | 27 base::MessageLoopProxy::current(), |
27 base::MessageLoopProxy::current(), | 28 base::MessageLoopProxy::current(), |
28 NULL); | 29 NULL); |
29 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id); | 30 wrapper_->process_manager()->SetProcessIdForTest(mock_render_process_id); |
30 registry()->AddChildProcessSender(mock_render_process_id, this); | 31 registry()->AddChildProcessSender(mock_render_process_id, this); |
31 } | 32 } |
32 | 33 |
33 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { | 34 EmbeddedWorkerTestHelper::~EmbeddedWorkerTestHelper() { |
(...skipping 16 matching lines...) Expand all Loading... |
50 return true; | 51 return true; |
51 } | 52 } |
52 | 53 |
53 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) { | 54 bool EmbeddedWorkerTestHelper::OnMessageReceived(const IPC::Message& message) { |
54 bool handled = true; | 55 bool handled = true; |
55 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message) | 56 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerTestHelper, message) |
56 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub) | 57 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorkerStub) |
57 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub) | 58 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorkerStub) |
58 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker, | 59 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker, |
59 OnMessageToWorkerStub) | 60 OnMessageToWorkerStub) |
| 61 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload, |
| 62 OnResumeAfterDownloadStub) |
60 IPC_MESSAGE_UNHANDLED(handled = false) | 63 IPC_MESSAGE_UNHANDLED(handled = false) |
61 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
62 | 65 |
63 // IPC::TestSink only records messages that are not handled by filters, | 66 // IPC::TestSink only records messages that are not handled by filters, |
64 // so we just forward all messages to the separate sink. | 67 // so we just forward all messages to the separate sink. |
65 sink_.OnMessageReceived(message); | 68 sink_.OnMessageReceived(message); |
66 | 69 |
67 return handled; | 70 return handled; |
68 } | 71 } |
69 | 72 |
70 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() { | 73 ServiceWorkerContextCore* EmbeddedWorkerTestHelper::context() { |
71 return wrapper_->context(); | 74 return wrapper_->context(); |
72 } | 75 } |
73 | 76 |
74 void EmbeddedWorkerTestHelper::ShutdownContext() { | 77 void EmbeddedWorkerTestHelper::ShutdownContext() { |
75 wrapper_->Shutdown(); | 78 wrapper_->Shutdown(); |
76 wrapper_ = NULL; | 79 wrapper_ = NULL; |
77 } | 80 } |
78 | 81 |
79 void EmbeddedWorkerTestHelper::OnStartWorker( | 82 void EmbeddedWorkerTestHelper::OnStartWorker( |
80 int embedded_worker_id, | 83 int embedded_worker_id, |
81 int64 service_worker_version_id, | 84 int64 service_worker_version_id, |
82 const GURL& scope, | 85 const GURL& scope, |
83 const GURL& script_url) { | 86 const GURL& script_url, |
84 // By default just notify the sender that the worker is started. | 87 bool pause_after_download) { |
| 88 if (pause_after_download) { |
| 89 SimulatePausedAfterDownload(embedded_worker_id); |
| 90 return; |
| 91 } |
| 92 SimulateWorkerScriptLoaded(embedded_worker_id); |
85 SimulateWorkerStarted(next_thread_id_++, embedded_worker_id); | 93 SimulateWorkerStarted(next_thread_id_++, embedded_worker_id); |
86 } | 94 } |
87 | 95 |
| 96 void EmbeddedWorkerTestHelper::OnResumeAfterDownload(int embedded_worker_id) { |
| 97 SimulateWorkerScriptLoaded(embedded_worker_id); |
| 98 SimulateWorkerStarted(next_thread_id_++, embedded_worker_id); |
| 99 } |
| 100 |
88 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { | 101 void EmbeddedWorkerTestHelper::OnStopWorker(int embedded_worker_id) { |
89 // By default just notify the sender that the worker is stopped. | 102 // By default just notify the sender that the worker is stopped. |
90 SimulateWorkerStopped(embedded_worker_id); | 103 SimulateWorkerStopped(embedded_worker_id); |
91 } | 104 } |
92 | 105 |
93 bool EmbeddedWorkerTestHelper::OnMessageToWorker( | 106 bool EmbeddedWorkerTestHelper::OnMessageToWorker( |
94 int thread_id, | 107 int thread_id, |
95 int embedded_worker_id, | 108 int embedded_worker_id, |
96 const IPC::Message& message) { | 109 const IPC::Message& message) { |
97 bool handled = true; | 110 bool handled = true; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 SimulateSend( | 144 SimulateSend( |
132 new ServiceWorkerHostMsg_FetchEventFinished( | 145 new ServiceWorkerHostMsg_FetchEventFinished( |
133 embedded_worker_id, | 146 embedded_worker_id, |
134 request_id, | 147 request_id, |
135 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, | 148 SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, |
136 ServiceWorkerResponse(200, "OK", | 149 ServiceWorkerResponse(200, "OK", |
137 std::map<std::string, std::string>(), | 150 std::map<std::string, std::string>(), |
138 std::string()))); | 151 std::string()))); |
139 } | 152 } |
140 | 153 |
| 154 void EmbeddedWorkerTestHelper::SimulatePausedAfterDownload( |
| 155 int embedded_worker_id) { |
| 156 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
| 157 ASSERT_TRUE(worker != NULL); |
| 158 registry()->OnPausedAfterDownload(worker->process_id(), embedded_worker_id); |
| 159 } |
| 160 |
| 161 void EmbeddedWorkerTestHelper::SimulateWorkerScriptLoaded( |
| 162 int embedded_worker_id) { |
| 163 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
| 164 ASSERT_TRUE(worker != NULL); |
| 165 registry()->OnWorkerScriptLoaded(worker->process_id(), embedded_worker_id); |
| 166 } |
| 167 |
141 void EmbeddedWorkerTestHelper::SimulateWorkerStarted( | 168 void EmbeddedWorkerTestHelper::SimulateWorkerStarted( |
142 int thread_id, int embedded_worker_id) { | 169 int thread_id, int embedded_worker_id) { |
143 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 170 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
144 ASSERT_TRUE(worker != NULL); | 171 ASSERT_TRUE(worker != NULL); |
145 registry()->OnWorkerStarted( | 172 registry()->OnWorkerStarted( |
146 worker->process_id(), | 173 worker->process_id(), |
147 thread_id, | 174 thread_id, |
148 embedded_worker_id); | 175 embedded_worker_id); |
149 } | 176 } |
150 | 177 |
(...skipping 12 matching lines...) Expand all Loading... |
163 | 190 |
164 void EmbeddedWorkerTestHelper::OnStartWorkerStub( | 191 void EmbeddedWorkerTestHelper::OnStartWorkerStub( |
165 const EmbeddedWorkerMsg_StartWorker_Params& params) { | 192 const EmbeddedWorkerMsg_StartWorker_Params& params) { |
166 EmbeddedWorkerInstance* worker = | 193 EmbeddedWorkerInstance* worker = |
167 registry()->GetWorker(params.embedded_worker_id); | 194 registry()->GetWorker(params.embedded_worker_id); |
168 ASSERT_TRUE(worker != NULL); | 195 ASSERT_TRUE(worker != NULL); |
169 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 196 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
170 base::MessageLoopProxy::current()->PostTask( | 197 base::MessageLoopProxy::current()->PostTask( |
171 FROM_HERE, | 198 FROM_HERE, |
172 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, | 199 base::Bind(&EmbeddedWorkerTestHelper::OnStartWorker, |
| 200 weak_factory_.GetWeakPtr(), |
| 201 params.embedded_worker_id, |
| 202 params.service_worker_version_id, |
| 203 params.scope, |
| 204 params.script_url, |
| 205 params.pause_after_download)); |
| 206 } |
| 207 |
| 208 void EmbeddedWorkerTestHelper::OnResumeAfterDownloadStub( |
| 209 int embedded_worker_id) { |
| 210 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
| 211 ASSERT_TRUE(worker != NULL); |
| 212 base::MessageLoopProxy::current()->PostTask( |
| 213 FROM_HERE, |
| 214 base::Bind(&EmbeddedWorkerTestHelper::OnResumeAfterDownload, |
173 weak_factory_.GetWeakPtr(), | 215 weak_factory_.GetWeakPtr(), |
174 params.embedded_worker_id, | 216 embedded_worker_id)); |
175 params.service_worker_version_id, | |
176 params.scope, | |
177 params.script_url)); | |
178 } | 217 } |
179 | 218 |
180 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) { | 219 void EmbeddedWorkerTestHelper::OnStopWorkerStub(int embedded_worker_id) { |
181 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 220 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); |
182 ASSERT_TRUE(worker != NULL); | 221 ASSERT_TRUE(worker != NULL); |
183 base::MessageLoopProxy::current()->PostTask( | 222 base::MessageLoopProxy::current()->PostTask( |
184 FROM_HERE, | 223 FROM_HERE, |
185 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker, | 224 base::Bind(&EmbeddedWorkerTestHelper::OnStopWorker, |
186 weak_factory_.GetWeakPtr(), | 225 weak_factory_.GetWeakPtr(), |
187 embedded_worker_id)); | 226 embedded_worker_id)); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 request_id, | 274 request_id, |
236 request)); | 275 request)); |
237 } | 276 } |
238 | 277 |
239 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { | 278 EmbeddedWorkerRegistry* EmbeddedWorkerTestHelper::registry() { |
240 DCHECK(context()); | 279 DCHECK(context()); |
241 return context()->embedded_worker_registry(); | 280 return context()->embedded_worker_registry(); |
242 } | 281 } |
243 | 282 |
244 } // namespace content | 283 } // namespace content |
OLD | NEW |