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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 { Activate }; |
66 using FetchCallback = | 66 using FetchCallback = |
67 base::Callback<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; |
76 | 76 |
77 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, | 77 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, |
78 mojo::ScopedMessagePipeHandle request); | 78 mojo::ScopedMessagePipeHandle request); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 // respective On*Event handler by default. | 180 // respective On*Event handler by default. |
181 virtual bool OnMessageToWorker(int thread_id, | 181 virtual bool OnMessageToWorker(int thread_id, |
182 int embedded_worker_id, | 182 int embedded_worker_id, |
183 const IPC::Message& message); | 183 const IPC::Message& message); |
184 | 184 |
185 // On*Event handlers. Called by the default implementation of | 185 // On*Event handlers. Called by the default implementation of |
186 // OnMessageToWorker when events are sent to the embedded | 186 // OnMessageToWorker when events are sent to the embedded |
187 // worker. By default they just return success via | 187 // worker. By default they just return success via |
188 // SimulateSendReplyToBrowser. | 188 // SimulateSendReplyToBrowser. |
189 virtual void OnActivateEvent( | 189 virtual void OnActivateEvent( |
190 const mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback& | 190 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback |
191 callback); | 191 callback); |
192 virtual void OnBackgroundFetchAbortEvent( | 192 virtual void OnBackgroundFetchAbortEvent( |
193 const std::string& tag, | 193 const std::string& tag, |
194 const mojom::ServiceWorkerEventDispatcher:: | 194 mojom::ServiceWorkerEventDispatcher:: |
195 DispatchBackgroundFetchAbortEventCallback& callback); | 195 DispatchBackgroundFetchAbortEventCallback callback); |
196 virtual void OnBackgroundFetchClickEvent( | 196 virtual void OnBackgroundFetchClickEvent( |
197 const std::string& tag, | 197 const std::string& tag, |
198 mojom::BackgroundFetchState state, | 198 mojom::BackgroundFetchState state, |
199 const mojom::ServiceWorkerEventDispatcher:: | 199 mojom::ServiceWorkerEventDispatcher:: |
200 DispatchBackgroundFetchClickEventCallback& callback); | 200 DispatchBackgroundFetchClickEventCallback callback); |
201 virtual void OnBackgroundFetchFailEvent( | 201 virtual void OnBackgroundFetchFailEvent( |
202 const std::string& tag, | 202 const std::string& tag, |
203 const std::vector<BackgroundFetchSettledFetch>& fetches, | 203 const std::vector<BackgroundFetchSettledFetch>& fetches, |
204 const mojom::ServiceWorkerEventDispatcher:: | 204 mojom::ServiceWorkerEventDispatcher:: |
205 DispatchBackgroundFetchFailEventCallback& callback); | 205 DispatchBackgroundFetchFailEventCallback callback); |
206 virtual void OnBackgroundFetchedEvent( | 206 virtual void OnBackgroundFetchedEvent( |
207 const std::string& tag, | 207 const std::string& tag, |
208 const std::vector<BackgroundFetchSettledFetch>& fetches, | 208 const std::vector<BackgroundFetchSettledFetch>& fetches, |
209 const mojom::ServiceWorkerEventDispatcher:: | 209 mojom::ServiceWorkerEventDispatcher:: |
210 DispatchBackgroundFetchedEventCallback& callback); | 210 DispatchBackgroundFetchedEventCallback callback); |
211 virtual void OnExtendableMessageEvent( | 211 virtual void OnExtendableMessageEvent( |
212 mojom::ExtendableMessageEventPtr event, | 212 mojom::ExtendableMessageEventPtr event, |
213 const mojom::ServiceWorkerEventDispatcher:: | 213 mojom::ServiceWorkerEventDispatcher:: |
214 DispatchExtendableMessageEventCallback& callback); | 214 DispatchExtendableMessageEventCallback callback); |
215 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 215 virtual void OnInstallEvent(int embedded_worker_id, int request_id); |
216 virtual void OnFetchEvent( | 216 virtual void OnFetchEvent( |
217 int embedded_worker_id, | 217 int embedded_worker_id, |
218 int fetch_event_id, | 218 int fetch_event_id, |
219 const ServiceWorkerFetchRequest& request, | 219 const ServiceWorkerFetchRequest& request, |
220 mojom::FetchEventPreloadHandlePtr preload_handle, | 220 mojom::FetchEventPreloadHandlePtr preload_handle, |
221 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 221 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |
222 const FetchCallback& finish_callback); | 222 FetchCallback finish_callback); |
223 virtual void OnNotificationClickEvent( | 223 virtual void OnNotificationClickEvent( |
224 const std::string& notification_id, | 224 const std::string& notification_id, |
225 const PlatformNotificationData& notification_data, | 225 const PlatformNotificationData& notification_data, |
226 int action_index, | 226 int action_index, |
227 const base::Optional<base::string16>& reply, | 227 const base::Optional<base::string16>& reply, |
228 const mojom::ServiceWorkerEventDispatcher:: | 228 mojom::ServiceWorkerEventDispatcher:: |
229 DispatchNotificationClickEventCallback& callback); | 229 DispatchNotificationClickEventCallback callback); |
230 virtual void OnNotificationCloseEvent( | 230 virtual void OnNotificationCloseEvent( |
231 const std::string& notification_id, | 231 const std::string& notification_id, |
232 const PlatformNotificationData& notification_data, | 232 const PlatformNotificationData& notification_data, |
233 const mojom::ServiceWorkerEventDispatcher:: | 233 mojom::ServiceWorkerEventDispatcher:: |
234 DispatchNotificationCloseEventCallback& callback); | 234 DispatchNotificationCloseEventCallback callback); |
235 virtual void OnPushEvent( | 235 virtual void OnPushEvent( |
236 const PushEventPayload& payload, | 236 const PushEventPayload& payload, |
237 const mojom::ServiceWorkerEventDispatcher::DispatchPushEventCallback& | 237 mojom::ServiceWorkerEventDispatcher::DispatchPushEventCallback callback); |
238 callback); | |
239 virtual void OnPaymentRequestEvent( | 238 virtual void OnPaymentRequestEvent( |
240 payments::mojom::PaymentAppRequestPtr data, | 239 payments::mojom::PaymentAppRequestPtr data, |
241 payments::mojom::PaymentAppResponseCallbackPtr response_callback, | 240 payments::mojom::PaymentAppResponseCallbackPtr response_callback, |
242 const mojom::ServiceWorkerEventDispatcher:: | 241 mojom::ServiceWorkerEventDispatcher::DispatchPaymentRequestEventCallback |
243 DispatchPaymentRequestEventCallback& callback); | 242 callback); |
244 | 243 |
245 // These functions simulate sending an EmbeddedHostMsg message through the | 244 // These functions simulate sending an EmbeddedHostMsg message through the |
246 // legacy IPC system to the browser. | 245 // legacy IPC system to the browser. |
247 void SimulateWorkerReadyForInspection(int embedded_worker_id); | 246 void SimulateWorkerReadyForInspection(int embedded_worker_id); |
248 void SimulateWorkerScriptCached(int embedded_worker_id); | 247 void SimulateWorkerScriptCached(int embedded_worker_id); |
249 void SimulateWorkerScriptLoaded(int embedded_worker_id); | 248 void SimulateWorkerScriptLoaded(int embedded_worker_id); |
250 void SimulateWorkerThreadStarted(int thread_id, | 249 void SimulateWorkerThreadStarted(int thread_id, |
251 int embedded_worker_id, | 250 int embedded_worker_id, |
252 int provider_id); | 251 int provider_id); |
253 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); | 252 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); |
254 void SimulateWorkerStarted(int embedded_worker_id); | 253 void SimulateWorkerStarted(int embedded_worker_id); |
255 void SimulateWorkerStopped(int embedded_worker_id); | 254 void SimulateWorkerStopped(int embedded_worker_id); |
256 void SimulateSend(IPC::Message* message); | 255 void SimulateSend(IPC::Message* message); |
257 | 256 |
258 EmbeddedWorkerRegistry* registry(); | 257 EmbeddedWorkerRegistry* registry(); |
259 | 258 |
260 private: | 259 private: |
261 class MockServiceWorkerEventDispatcher; | 260 class MockServiceWorkerEventDispatcher; |
262 | 261 |
263 void OnStartWorkerStub( | 262 void OnStartWorkerStub( |
264 const EmbeddedWorkerStartParams& params, | 263 const EmbeddedWorkerStartParams& params, |
265 mojom::ServiceWorkerEventDispatcherRequest request, | 264 mojom::ServiceWorkerEventDispatcherRequest request, |
266 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host); | 265 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host); |
267 void OnResumeAfterDownloadStub(int embedded_worker_id); | 266 void OnResumeAfterDownloadStub(int embedded_worker_id); |
268 void OnStopWorkerStub(int embedded_worker_id); | 267 void OnStopWorkerStub(int embedded_worker_id); |
269 void OnMessageToWorkerStub(int thread_id, | 268 void OnMessageToWorkerStub(int thread_id, |
270 int embedded_worker_id, | 269 int embedded_worker_id, |
271 const IPC::Message& message); | 270 const IPC::Message& message); |
272 void OnActivateEventStub( | 271 void OnActivateEventStub( |
273 const mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback& | 272 mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback |
274 callback); | 273 callback); |
275 void OnBackgroundFetchAbortEventStub( | 274 void OnBackgroundFetchAbortEventStub( |
276 const std::string& tag, | 275 const std::string& tag, |
277 const mojom::ServiceWorkerEventDispatcher:: | 276 mojom::ServiceWorkerEventDispatcher:: |
278 DispatchBackgroundFetchAbortEventCallback& callback); | 277 DispatchBackgroundFetchAbortEventCallback callback); |
279 void OnBackgroundFetchClickEventStub( | 278 void OnBackgroundFetchClickEventStub( |
280 const std::string& tag, | 279 const std::string& tag, |
281 mojom::BackgroundFetchState state, | 280 mojom::BackgroundFetchState state, |
282 const mojom::ServiceWorkerEventDispatcher:: | 281 mojom::ServiceWorkerEventDispatcher:: |
283 DispatchBackgroundFetchClickEventCallback& callback); | 282 DispatchBackgroundFetchClickEventCallback callback); |
284 void OnBackgroundFetchFailEventStub( | 283 void OnBackgroundFetchFailEventStub( |
285 const std::string& tag, | 284 const std::string& tag, |
286 const std::vector<BackgroundFetchSettledFetch>& fetches, | 285 const std::vector<BackgroundFetchSettledFetch>& fetches, |
287 const mojom::ServiceWorkerEventDispatcher:: | 286 mojom::ServiceWorkerEventDispatcher:: |
288 DispatchBackgroundFetchFailEventCallback& callback); | 287 DispatchBackgroundFetchFailEventCallback callback); |
289 void OnBackgroundFetchedEventStub( | 288 void OnBackgroundFetchedEventStub( |
290 const std::string& tag, | 289 const std::string& tag, |
291 const std::vector<BackgroundFetchSettledFetch>& fetches, | 290 const std::vector<BackgroundFetchSettledFetch>& fetches, |
292 const mojom::ServiceWorkerEventDispatcher:: | 291 mojom::ServiceWorkerEventDispatcher:: |
293 DispatchBackgroundFetchedEventCallback& callback); | 292 DispatchBackgroundFetchedEventCallback callback); |
294 void OnExtendableMessageEventStub( | 293 void OnExtendableMessageEventStub( |
295 mojom::ExtendableMessageEventPtr event, | 294 mojom::ExtendableMessageEventPtr event, |
296 const mojom::ServiceWorkerEventDispatcher:: | 295 mojom::ServiceWorkerEventDispatcher:: |
297 DispatchExtendableMessageEventCallback& callback); | 296 DispatchExtendableMessageEventCallback callback); |
298 void OnInstallEventStub(int request_id); | 297 void OnInstallEventStub(int request_id); |
299 void OnFetchEventStub( | 298 void OnFetchEventStub( |
300 int thread_id, | 299 int thread_id, |
301 int fetch_event_id, | 300 int fetch_event_id, |
302 const ServiceWorkerFetchRequest& request, | 301 const ServiceWorkerFetchRequest& request, |
303 mojom::FetchEventPreloadHandlePtr preload_handle, | 302 mojom::FetchEventPreloadHandlePtr preload_handle, |
304 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 303 mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |
305 const FetchCallback& finish_callback); | 304 FetchCallback finish_callback); |
306 void OnNotificationClickEventStub( | 305 void OnNotificationClickEventStub( |
307 const std::string& notification_id, | 306 const std::string& notification_id, |
308 const PlatformNotificationData& notification_data, | 307 const PlatformNotificationData& notification_data, |
309 int action_index, | 308 int action_index, |
310 const base::Optional<base::string16>& reply, | 309 const base::Optional<base::string16>& reply, |
311 const mojom::ServiceWorkerEventDispatcher:: | 310 mojom::ServiceWorkerEventDispatcher:: |
312 DispatchNotificationClickEventCallback& callback); | 311 DispatchNotificationClickEventCallback callback); |
313 void OnNotificationCloseEventStub( | 312 void OnNotificationCloseEventStub( |
314 const std::string& notification_id, | 313 const std::string& notification_id, |
315 const PlatformNotificationData& notification_data, | 314 const PlatformNotificationData& notification_data, |
316 const mojom::ServiceWorkerEventDispatcher:: | 315 mojom::ServiceWorkerEventDispatcher:: |
317 DispatchNotificationCloseEventCallback& callback); | 316 DispatchNotificationCloseEventCallback callback); |
318 void OnPushEventStub( | 317 void OnPushEventStub( |
319 const PushEventPayload& payload, | 318 const PushEventPayload& payload, |
320 const mojom::ServiceWorkerEventDispatcher::DispatchPushEventCallback& | 319 mojom::ServiceWorkerEventDispatcher::DispatchPushEventCallback callback); |
321 callback); | |
322 void OnPaymentRequestEventStub( | 320 void OnPaymentRequestEventStub( |
323 payments::mojom::PaymentAppRequestPtr data, | 321 payments::mojom::PaymentAppRequestPtr data, |
324 payments::mojom::PaymentAppResponseCallbackPtr response_callback, | 322 payments::mojom::PaymentAppResponseCallbackPtr response_callback, |
325 const mojom::ServiceWorkerEventDispatcher:: | 323 mojom::ServiceWorkerEventDispatcher::DispatchPaymentRequestEventCallback |
326 DispatchPaymentRequestEventCallback& callback); | 324 callback); |
327 | 325 |
328 std::unique_ptr<TestBrowserContext> browser_context_; | 326 std::unique_ptr<TestBrowserContext> browser_context_; |
329 std::unique_ptr<MockRenderProcessHost> render_process_host_; | 327 std::unique_ptr<MockRenderProcessHost> render_process_host_; |
330 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; | 328 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; |
331 | 329 |
332 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 330 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
333 | 331 |
334 IPC::TestSink sink_; | 332 IPC::TestSink sink_; |
335 IPC::TestSink inner_sink_; | 333 IPC::TestSink inner_sink_; |
336 | 334 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
371 std::unique_ptr<MockType> mock = | 369 std::unique_ptr<MockType> mock = |
372 base::MakeUnique<MockType>(std::forward<Args>(args)...); | 370 base::MakeUnique<MockType>(std::forward<Args>(args)...); |
373 MockType* mock_rawptr = mock.get(); | 371 MockType* mock_rawptr = mock.get(); |
374 RegisterMockInstanceClient(std::move(mock)); | 372 RegisterMockInstanceClient(std::move(mock)); |
375 return mock_rawptr; | 373 return mock_rawptr; |
376 } | 374 } |
377 | 375 |
378 } // namespace content | 376 } // namespace content |
379 | 377 |
380 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 378 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
OLD | NEW |