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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
18 #include "base/optional.h" | 18 #include "base/optional.h" |
19 #include "base/time/time.h" | |
19 #include "content/common/service_worker/embedded_worker.mojom.h" | 20 #include "content/common/service_worker/embedded_worker.mojom.h" |
21 #include "content/common/service_worker/service_worker_status_code.h" | |
20 #include "ipc/ipc_listener.h" | 22 #include "ipc/ipc_listener.h" |
21 #include "ipc/ipc_test_sink.h" | 23 #include "ipc/ipc_test_sink.h" |
22 #include "mojo/public/cpp/bindings/binding.h" | 24 #include "mojo/public/cpp/bindings/binding.h" |
23 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 25 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 #include "url/gurl.h" | 27 #include "url/gurl.h" |
26 | 28 |
27 class GURL; | 29 class GURL; |
28 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; | 30 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; |
29 | 31 |
(...skipping 26 matching lines...) Expand all Loading... | |
56 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 58 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
57 // | 59 // |
58 // Alternatively consumers can subclass this helper and override On*() | 60 // Alternatively consumers can subclass this helper and override On*() |
59 // methods to add their own logic/verification code. | 61 // methods to add their own logic/verification code. |
60 // | 62 // |
61 // See embedded_worker_instance_unittest.cc for example usages. | 63 // See embedded_worker_instance_unittest.cc for example usages. |
62 // | 64 // |
63 class EmbeddedWorkerTestHelper : public IPC::Sender, | 65 class EmbeddedWorkerTestHelper : public IPC::Sender, |
64 public IPC::Listener { | 66 public IPC::Listener { |
65 public: | 67 public: |
68 using FetchCallback = | |
69 base::Callback<void(ServiceWorkerStatusCode, | |
70 base::Time /* dispatch_event_time */)>; | |
71 | |
66 class MockEmbeddedWorkerInstanceClient | 72 class MockEmbeddedWorkerInstanceClient |
67 : public mojom::EmbeddedWorkerInstanceClient { | 73 : public mojom::EmbeddedWorkerInstanceClient { |
68 public: | 74 public: |
69 explicit MockEmbeddedWorkerInstanceClient( | 75 explicit MockEmbeddedWorkerInstanceClient( |
70 base::WeakPtr<EmbeddedWorkerTestHelper> helper); | 76 base::WeakPtr<EmbeddedWorkerTestHelper> helper); |
71 ~MockEmbeddedWorkerInstanceClient() override; | 77 ~MockEmbeddedWorkerInstanceClient() override; |
72 | 78 |
73 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, | 79 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, |
74 mojom::EmbeddedWorkerInstanceClientRequest request); | 80 mojom::EmbeddedWorkerInstanceClientRequest request); |
75 | 81 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 const GURL& script_url, | 149 const GURL& script_url, |
144 bool pause_after_download); | 150 bool pause_after_download); |
145 virtual void OnResumeAfterDownload(int embedded_worker_id); | 151 virtual void OnResumeAfterDownload(int embedded_worker_id); |
146 virtual void OnStopWorker(int embedded_worker_id); | 152 virtual void OnStopWorker(int embedded_worker_id); |
147 virtual bool OnMessageToWorker(int thread_id, | 153 virtual bool OnMessageToWorker(int thread_id, |
148 int embedded_worker_id, | 154 int embedded_worker_id, |
149 const IPC::Message& message); | 155 const IPC::Message& message); |
150 | 156 |
151 // Called to setup mojo for a new embedded worker. Override to register | 157 // Called to setup mojo for a new embedded worker. Override to register |
152 // interfaces the worker should expose to the browser. | 158 // interfaces the worker should expose to the browser. |
153 virtual void OnSetupMojo(shell::InterfaceRegistry* interface_registry); | 159 virtual void OnSetupMojo(int thread_id, |
160 shell::InterfaceRegistry* interface_registry); | |
154 | 161 |
155 // On*Event handlers. Called by the default implementation of | 162 // On*Event handlers. Called by the default implementation of |
156 // OnMessageToWorker when events are sent to the embedded | 163 // OnMessageToWorker when events are sent to the embedded |
157 // worker. By default they just return success via | 164 // worker. By default they just return success via |
158 // SimulateSendReplyToBrowser. | 165 // SimulateSendReplyToBrowser. |
159 virtual void OnActivateEvent(int embedded_worker_id, int request_id); | 166 virtual void OnActivateEvent(int embedded_worker_id, int request_id); |
160 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); | 167 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); |
161 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 168 virtual void OnInstallEvent(int embedded_worker_id, int request_id); |
162 virtual void OnFetchEvent(int embedded_worker_id, | 169 virtual void OnFetchEvent(int embedded_worker_id, |
163 int response_id, | 170 int response_id, |
164 int event_finish_id, | 171 const ServiceWorkerFetchRequest& request, |
165 const ServiceWorkerFetchRequest& request); | 172 const FetchCallback& callback); |
166 virtual void OnPushEvent(int embedded_worker_id, | 173 virtual void OnPushEvent(int embedded_worker_id, |
167 int request_id, | 174 int request_id, |
168 const PushEventPayload& payload); | 175 const PushEventPayload& payload); |
169 | 176 |
170 // These functions simulate sending an EmbeddedHostMsg message to the | 177 // These functions simulate sending an EmbeddedHostMsg message to the |
171 // browser. | 178 // browser. |
172 void SimulateWorkerReadyForInspection(int embedded_worker_id); | 179 void SimulateWorkerReadyForInspection(int embedded_worker_id); |
173 void SimulateWorkerScriptCached(int embedded_worker_id); | 180 void SimulateWorkerScriptCached(int embedded_worker_id); |
174 void SimulateWorkerScriptLoaded(int embedded_worker_id); | 181 void SimulateWorkerScriptLoaded(int embedded_worker_id); |
175 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); | 182 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); |
176 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); | 183 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); |
177 void SimulateWorkerStarted(int embedded_worker_id); | 184 void SimulateWorkerStarted(int embedded_worker_id); |
178 void SimulateWorkerStopped(int embedded_worker_id); | 185 void SimulateWorkerStopped(int embedded_worker_id); |
179 void SimulateSend(IPC::Message* message); | 186 void SimulateSend(IPC::Message* message); |
180 | 187 |
181 EmbeddedWorkerRegistry* registry(); | 188 EmbeddedWorkerRegistry* registry(); |
182 | 189 |
183 private: | 190 private: |
184 using InterfaceRegistryAndProvider = | 191 using InterfaceRegistryAndProvider = |
185 std::pair<std::unique_ptr<shell::InterfaceRegistry>, | 192 std::pair<std::unique_ptr<shell::InterfaceRegistry>, |
186 std::unique_ptr<shell::InterfaceProvider>>; | 193 std::unique_ptr<shell::InterfaceProvider>>; |
187 | 194 |
188 class MockEmbeddedWorkerSetup; | 195 class MockEmbeddedWorkerSetup; |
196 class MockFetchEventDispatcher; | |
189 | 197 |
190 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); | 198 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); |
191 void OnResumeAfterDownloadStub(int embedded_worker_id); | 199 void OnResumeAfterDownloadStub(int embedded_worker_id); |
192 void OnStopWorkerStub(int embedded_worker_id); | 200 void OnStopWorkerStub(int embedded_worker_id); |
193 void OnMessageToWorkerStub(int thread_id, | 201 void OnMessageToWorkerStub(int thread_id, |
194 int embedded_worker_id, | 202 int embedded_worker_id, |
195 const IPC::Message& message); | 203 const IPC::Message& message); |
196 void OnActivateEventStub(int request_id); | 204 void OnActivateEventStub(int request_id); |
197 void OnExtendableMessageEventStub( | 205 void OnExtendableMessageEventStub( |
198 int request_id, | 206 int request_id, |
199 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); | 207 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); |
200 void OnInstallEventStub(int request_id); | 208 void OnInstallEventStub(int request_id); |
201 void OnFetchEventStub(int response_id, | 209 void OnFetchEventStub(int thread_id, |
202 int event_finish_id, | 210 int response_id, |
203 const ServiceWorkerFetchRequest& request); | 211 const ServiceWorkerFetchRequest& request, |
212 const FetchCallback& callback); | |
204 void OnPushEventStub(int request_id, const PushEventPayload& payload); | 213 void OnPushEventStub(int request_id, const PushEventPayload& payload); |
205 void OnSetupMojoStub(int thread_id, | 214 void OnSetupMojoStub(int thread_id, |
206 shell::mojom::InterfaceProviderRequest services, | 215 shell::mojom::InterfaceProviderRequest services, |
207 shell::mojom::InterfaceProviderPtr exposed_services); | 216 shell::mojom::InterfaceProviderPtr exposed_services); |
208 | 217 |
209 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 218 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
210 | 219 |
211 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry( | 220 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry( |
212 MockRenderProcessHost* rph); | 221 MockRenderProcessHost* rph); |
213 | 222 |
(...skipping 12 matching lines...) Expand all Loading... | |
226 | 235 |
227 int next_thread_id_; | 236 int next_thread_id_; |
228 int mock_render_process_id_; | 237 int mock_render_process_id_; |
229 int new_mock_render_process_id_; | 238 int new_mock_render_process_id_; |
230 | 239 |
231 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; | 240 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; |
232 std::unique_ptr<shell::InterfaceRegistry> | 241 std::unique_ptr<shell::InterfaceRegistry> |
233 new_render_process_interface_registry_; | 242 new_render_process_interface_registry_; |
234 | 243 |
235 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 244 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
245 std::map<int, int> thread_id_embedded_worker_id_map_; | |
dcheng
2016/10/04 07:19:53
Nit: a comment here would be helpful to understand
horo
2016/10/04 12:16:40
Done.
| |
236 | 246 |
237 // Stores the InterfaceRegistry/InterfaceProviders that are associated with | 247 // Stores the InterfaceRegistry/InterfaceProviders that are associated with |
238 // each individual service worker. | 248 // each individual service worker. |
239 base::hash_map<int, InterfaceRegistryAndProvider> | 249 base::hash_map<int, InterfaceRegistryAndProvider> |
240 thread_id_service_registry_map_; | 250 thread_id_service_registry_map_; |
241 | 251 |
242 // Updated each time MessageToWorker message is received. | 252 // Updated each time MessageToWorker message is received. |
243 int current_embedded_worker_id_; | 253 int current_embedded_worker_id_; |
244 | 254 |
245 std::vector<scoped_refptr<MessagePortMessageFilter>> | 255 std::vector<scoped_refptr<MessagePortMessageFilter>> |
246 message_port_message_filters_; | 256 message_port_message_filters_; |
247 | 257 |
248 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 258 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
249 | 259 |
250 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 260 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
251 }; | 261 }; |
252 | 262 |
253 } // namespace content | 263 } // namespace content |
254 | 264 |
255 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 265 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
OLD | NEW |