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> |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 55 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
56 // | 56 // |
57 // Alternatively consumers can subclass this helper and override On*() | 57 // Alternatively consumers can subclass this helper and override On*() |
58 // methods to add their own logic/verification code. | 58 // methods to add their own logic/verification code. |
59 // | 59 // |
60 // See embedded_worker_instance_unittest.cc for example usages. | 60 // See embedded_worker_instance_unittest.cc for example usages. |
61 // | 61 // |
62 class EmbeddedWorkerTestHelper : public IPC::Sender, | 62 class EmbeddedWorkerTestHelper : public IPC::Sender, |
63 public IPC::Listener { | 63 public IPC::Listener { |
64 public: | 64 public: |
65 enum class Event { Activate }; | 65 enum class Event { Install, Activate }; |
66 using FetchCallback = | 66 using FetchCallback = |
67 base::OnceCallback<void(ServiceWorkerStatusCode, | 67 base::OnceCallback<void(ServiceWorkerStatusCode, |
68 base::Time /* dispatch_event_time */)>; | 68 base::Time /* dispatch_event_time */)>; |
69 | 69 |
70 class MockEmbeddedWorkerInstanceClient | 70 class MockEmbeddedWorkerInstanceClient |
71 : public mojom::EmbeddedWorkerInstanceClient { | 71 : public mojom::EmbeddedWorkerInstanceClient { |
72 public: | 72 public: |
73 explicit MockEmbeddedWorkerInstanceClient( | 73 explicit MockEmbeddedWorkerInstanceClient( |
74 base::WeakPtr<EmbeddedWorkerTestHelper> helper); | 74 base::WeakPtr<EmbeddedWorkerTestHelper> helper); |
75 ~MockEmbeddedWorkerInstanceClient() override; | 75 ~MockEmbeddedWorkerInstanceClient() override; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 // Register a mojo endpoint object derived from | 116 // Register a mojo endpoint object derived from |
117 // MockEmbeddedWorkerInstanceClient. | 117 // MockEmbeddedWorkerInstanceClient. |
118 void RegisterMockInstanceClient( | 118 void RegisterMockInstanceClient( |
119 std::unique_ptr<MockEmbeddedWorkerInstanceClient> client); | 119 std::unique_ptr<MockEmbeddedWorkerInstanceClient> client); |
120 | 120 |
121 template <typename MockType, typename... Args> | 121 template <typename MockType, typename... Args> |
122 MockType* CreateAndRegisterMockInstanceClient(Args&&... args); | 122 MockType* CreateAndRegisterMockInstanceClient(Args&&... args); |
123 | 123 |
124 // IPC sink for EmbeddedWorker messages. | 124 // IPC sink for EmbeddedWorker messages. |
125 IPC::TestSink* ipc_sink() { return &sink_; } | 125 IPC::TestSink* ipc_sink() { return &sink_; } |
126 // Inner IPC sink for script context messages sent via EmbeddedWorker. | |
127 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } | |
128 | 126 |
129 std::vector<Event>* dispatched_events() { return &events_; } | 127 std::vector<Event>* dispatched_events() { return &events_; } |
130 | 128 |
131 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>* | 129 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>* |
132 mock_instance_clients() { | 130 mock_instance_clients() { |
133 return &mock_instance_clients_; | 131 return &mock_instance_clients_; |
134 } | 132 } |
135 | 133 |
136 ServiceWorkerContextCore* context(); | 134 ServiceWorkerContextCore* context(); |
137 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } | 135 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 int64_t service_worker_version_id, | 167 int64_t service_worker_version_id, |
170 const GURL& scope, | 168 const GURL& scope, |
171 const GURL& script_url, | 169 const GURL& script_url, |
172 bool pause_after_download, | 170 bool pause_after_download, |
173 mojom::ServiceWorkerEventDispatcherRequest request, | 171 mojom::ServiceWorkerEventDispatcherRequest request, |
174 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host); | 172 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host); |
175 virtual void OnResumeAfterDownload(int embedded_worker_id); | 173 virtual void OnResumeAfterDownload(int embedded_worker_id); |
176 // StopWorker IPC handler routed through MockEmbeddedWorkerInstanceClient. | 174 // StopWorker IPC handler routed through MockEmbeddedWorkerInstanceClient. |
177 // This calls SimulateWorkerStopped() by default. | 175 // This calls SimulateWorkerStopped() by default. |
178 virtual void OnStopWorker(int embedded_worker_id); | 176 virtual void OnStopWorker(int embedded_worker_id); |
179 // The legacy IPC message handler. This passes the messages to their | |
180 // respective On*Event handler by default. | |
181 virtual bool OnMessageToWorker(int thread_id, | |
182 int embedded_worker_id, | |
183 const IPC::Message& message); | |
184 | 177 |
185 // On*Event handlers. Called by the default implementation of | 178 // On*Event handlers. By default they just return success via |
186 // OnMessageToWorker when events are sent to the embedded | |
187 // worker. By default they just return success via | |
188 // SimulateSendReplyToBrowser. | 179 // SimulateSendReplyToBrowser. |
189 virtual void OnActivateEvent( | 180 virtual void OnActivateEvent( |
190 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback | 181 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback |
191 callback); | 182 callback); |
192 virtual void OnBackgroundFetchAbortEvent( | 183 virtual void OnBackgroundFetchAbortEvent( |
193 const std::string& tag, | 184 const std::string& tag, |
194 mojom::ServiceWorkerEventDispatcher:: | 185 mojom::ServiceWorkerEventDispatcher:: |
195 DispatchBackgroundFetchAbortEventCallback callback); | 186 DispatchBackgroundFetchAbortEventCallback callback); |
196 virtual void OnBackgroundFetchClickEvent( | 187 virtual void OnBackgroundFetchClickEvent( |
197 const std::string& tag, | 188 const std::string& tag, |
198 mojom::BackgroundFetchState state, | 189 mojom::BackgroundFetchState state, |
199 mojom::ServiceWorkerEventDispatcher:: | 190 mojom::ServiceWorkerEventDispatcher:: |
200 DispatchBackgroundFetchClickEventCallback callback); | 191 DispatchBackgroundFetchClickEventCallback callback); |
201 virtual void OnBackgroundFetchFailEvent( | 192 virtual void OnBackgroundFetchFailEvent( |
202 const std::string& tag, | 193 const std::string& tag, |
203 const std::vector<BackgroundFetchSettledFetch>& fetches, | 194 const std::vector<BackgroundFetchSettledFetch>& fetches, |
204 mojom::ServiceWorkerEventDispatcher:: | 195 mojom::ServiceWorkerEventDispatcher:: |
205 DispatchBackgroundFetchFailEventCallback callback); | 196 DispatchBackgroundFetchFailEventCallback callback); |
206 virtual void OnBackgroundFetchedEvent( | 197 virtual void OnBackgroundFetchedEvent( |
207 const std::string& tag, | 198 const std::string& tag, |
208 const std::vector<BackgroundFetchSettledFetch>& fetches, | 199 const std::vector<BackgroundFetchSettledFetch>& fetches, |
209 mojom::ServiceWorkerEventDispatcher:: | 200 mojom::ServiceWorkerEventDispatcher:: |
210 DispatchBackgroundFetchedEventCallback callback); | 201 DispatchBackgroundFetchedEventCallback callback); |
211 virtual void OnExtendableMessageEvent( | 202 virtual void OnExtendableMessageEvent( |
212 mojom::ExtendableMessageEventPtr event, | 203 mojom::ExtendableMessageEventPtr event, |
213 mojom::ServiceWorkerEventDispatcher:: | 204 mojom::ServiceWorkerEventDispatcher:: |
214 DispatchExtendableMessageEventCallback callback); | 205 DispatchExtendableMessageEventCallback callback); |
215 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 206 virtual void OnInstallEvent( |
| 207 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client, |
| 208 mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback |
| 209 callback); |
216 virtual void OnFetchEvent( | 210 virtual void OnFetchEvent( |
217 int embedded_worker_id, | 211 int embedded_worker_id, |
218 int fetch_event_id, | 212 int fetch_event_id, |
219 const ServiceWorkerFetchRequest& request, | 213 const ServiceWorkerFetchRequest& request, |
220 mojom::FetchEventPreloadHandlePtr preload_handle, | 214 mojom::FetchEventPreloadHandlePtr preload_handle, |
221 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 215 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |
222 FetchCallback finish_callback); | 216 FetchCallback finish_callback); |
223 virtual void OnNotificationClickEvent( | 217 virtual void OnNotificationClickEvent( |
224 const std::string& notification_id, | 218 const std::string& notification_id, |
225 const PlatformNotificationData& notification_data, | 219 const PlatformNotificationData& notification_data, |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 DispatchBackgroundFetchFailEventCallback callback); | 281 DispatchBackgroundFetchFailEventCallback callback); |
288 void OnBackgroundFetchedEventStub( | 282 void OnBackgroundFetchedEventStub( |
289 const std::string& tag, | 283 const std::string& tag, |
290 const std::vector<BackgroundFetchSettledFetch>& fetches, | 284 const std::vector<BackgroundFetchSettledFetch>& fetches, |
291 mojom::ServiceWorkerEventDispatcher:: | 285 mojom::ServiceWorkerEventDispatcher:: |
292 DispatchBackgroundFetchedEventCallback callback); | 286 DispatchBackgroundFetchedEventCallback callback); |
293 void OnExtendableMessageEventStub( | 287 void OnExtendableMessageEventStub( |
294 mojom::ExtendableMessageEventPtr event, | 288 mojom::ExtendableMessageEventPtr event, |
295 mojom::ServiceWorkerEventDispatcher:: | 289 mojom::ServiceWorkerEventDispatcher:: |
296 DispatchExtendableMessageEventCallback callback); | 290 DispatchExtendableMessageEventCallback callback); |
297 void OnInstallEventStub(int request_id); | 291 void OnInstallEventStub( |
| 292 mojom::ServiceWorkerInstallEventMethodsAssociatedPtrInfo client, |
| 293 mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback |
| 294 callback); |
298 void OnFetchEventStub( | 295 void OnFetchEventStub( |
299 int thread_id, | 296 int thread_id, |
300 int fetch_event_id, | 297 int fetch_event_id, |
301 const ServiceWorkerFetchRequest& request, | 298 const ServiceWorkerFetchRequest& request, |
302 mojom::FetchEventPreloadHandlePtr preload_handle, | 299 mojom::FetchEventPreloadHandlePtr preload_handle, |
303 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 300 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |
304 FetchCallback finish_callback); | 301 FetchCallback finish_callback); |
305 void OnNotificationClickEventStub( | 302 void OnNotificationClickEventStub( |
306 const std::string& notification_id, | 303 const std::string& notification_id, |
307 const PlatformNotificationData& notification_data, | 304 const PlatformNotificationData& notification_data, |
(...skipping 15 matching lines...) Expand all Loading... |
323 mojom::ServiceWorkerEventDispatcher::DispatchPaymentRequestEventCallback | 320 mojom::ServiceWorkerEventDispatcher::DispatchPaymentRequestEventCallback |
324 callback); | 321 callback); |
325 | 322 |
326 std::unique_ptr<TestBrowserContext> browser_context_; | 323 std::unique_ptr<TestBrowserContext> browser_context_; |
327 std::unique_ptr<MockRenderProcessHost> render_process_host_; | 324 std::unique_ptr<MockRenderProcessHost> render_process_host_; |
328 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; | 325 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; |
329 | 326 |
330 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 327 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
331 | 328 |
332 IPC::TestSink sink_; | 329 IPC::TestSink sink_; |
333 IPC::TestSink inner_sink_; | |
334 | 330 |
335 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> | 331 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> |
336 mock_instance_clients_; | 332 mock_instance_clients_; |
337 size_t mock_instance_clients_next_index_; | 333 size_t mock_instance_clients_next_index_; |
338 | 334 |
339 int next_thread_id_; | 335 int next_thread_id_; |
340 int next_provider_id_; | 336 int next_provider_id_; |
341 int mock_render_process_id_; | 337 int mock_render_process_id_; |
342 int new_mock_render_process_id_; | 338 int new_mock_render_process_id_; |
343 | 339 |
344 std::map<int /* process_id */, scoped_refptr<ServiceWorkerDispatcherHost>> | 340 std::map<int /* process_id */, scoped_refptr<ServiceWorkerDispatcherHost>> |
345 dispatcher_hosts_; | 341 dispatcher_hosts_; |
346 | 342 |
347 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 343 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
348 std::map<int /* thread_id */, int /* embedded_worker_id */> | 344 std::map<int /* thread_id */, int /* embedded_worker_id */> |
349 thread_id_embedded_worker_id_map_; | 345 thread_id_embedded_worker_id_map_; |
350 | 346 |
351 std::map< | 347 std::map< |
352 int /* embedded_worker_id */, | 348 int /* embedded_worker_id */, |
353 mojom::EmbeddedWorkerInstanceHostAssociatedPtr /* instance_host_ptr */> | 349 mojom::EmbeddedWorkerInstanceHostAssociatedPtr /* instance_host_ptr */> |
354 embedded_worker_id_instance_host_ptr_map_; | 350 embedded_worker_id_instance_host_ptr_map_; |
355 | 351 |
356 // Updated each time MessageToWorker message is received. | |
357 int current_embedded_worker_id_; | |
358 | |
359 std::vector<Event> events_; | 352 std::vector<Event> events_; |
360 | 353 |
361 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 354 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
362 | 355 |
363 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 356 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
364 }; | 357 }; |
365 | 358 |
366 template <typename MockType, typename... Args> | 359 template <typename MockType, typename... Args> |
367 MockType* EmbeddedWorkerTestHelper::CreateAndRegisterMockInstanceClient( | 360 MockType* EmbeddedWorkerTestHelper::CreateAndRegisterMockInstanceClient( |
368 Args&&... args) { | 361 Args&&... args) { |
369 std::unique_ptr<MockType> mock = | 362 std::unique_ptr<MockType> mock = |
370 base::MakeUnique<MockType>(std::forward<Args>(args)...); | 363 base::MakeUnique<MockType>(std::forward<Args>(args)...); |
371 MockType* mock_rawptr = mock.get(); | 364 MockType* mock_rawptr = mock.get(); |
372 RegisterMockInstanceClient(std::move(mock)); | 365 RegisterMockInstanceClient(std::move(mock)); |
373 return mock_rawptr; | 366 return mock_rawptr; |
374 } | 367 } |
375 | 368 |
376 } // namespace content | 369 } // namespace content |
377 | 370 |
378 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 371 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
OLD | NEW |