| 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::Callback<void(ServiceWorkerStatusCode, |   67       base::Callback<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. | 
|  186   // OnMessageToWorker when events are sent to the embedded |  | 
|  187   // worker. By default they just return success via |  | 
|  188   // SimulateSendReplyToBrowser. |  | 
|  189   virtual void OnActivateEvent( |  179   virtual void OnActivateEvent( | 
|  190       const mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback& |  180       const mojom::ServiceWorkerEventDispatcher::DispatchActivateEventCallback& | 
|  191           callback); |  181           callback); | 
|  192   virtual void OnBackgroundFetchAbortEvent( |  182   virtual void OnBackgroundFetchAbortEvent( | 
|  193       const std::string& tag, |  183       const std::string& tag, | 
|  194       const mojom::ServiceWorkerEventDispatcher:: |  184       const mojom::ServiceWorkerEventDispatcher:: | 
|  195           DispatchBackgroundFetchAbortEventCallback& callback); |  185           DispatchBackgroundFetchAbortEventCallback& callback); | 
|  196   virtual void OnBackgroundFetchClickEvent( |  186   virtual void OnBackgroundFetchClickEvent( | 
|  197       const std::string& tag, |  187       const std::string& tag, | 
|  198       mojom::BackgroundFetchState state, |  188       mojom::BackgroundFetchState state, | 
|  199       const mojom::ServiceWorkerEventDispatcher:: |  189       const mojom::ServiceWorkerEventDispatcher:: | 
|  200           DispatchBackgroundFetchClickEventCallback& callback); |  190           DispatchBackgroundFetchClickEventCallback& callback); | 
|  201   virtual void OnBackgroundFetchFailEvent( |  191   virtual void OnBackgroundFetchFailEvent( | 
|  202       const std::string& tag, |  192       const std::string& tag, | 
|  203       const std::vector<BackgroundFetchSettledFetch>& fetches, |  193       const std::vector<BackgroundFetchSettledFetch>& fetches, | 
|  204       const mojom::ServiceWorkerEventDispatcher:: |  194       const mojom::ServiceWorkerEventDispatcher:: | 
|  205           DispatchBackgroundFetchFailEventCallback& callback); |  195           DispatchBackgroundFetchFailEventCallback& callback); | 
|  206   virtual void OnBackgroundFetchedEvent( |  196   virtual void OnBackgroundFetchedEvent( | 
|  207       const std::string& tag, |  197       const std::string& tag, | 
|  208       const std::vector<BackgroundFetchSettledFetch>& fetches, |  198       const std::vector<BackgroundFetchSettledFetch>& fetches, | 
|  209       const mojom::ServiceWorkerEventDispatcher:: |  199       const mojom::ServiceWorkerEventDispatcher:: | 
|  210           DispatchBackgroundFetchedEventCallback& callback); |  200           DispatchBackgroundFetchedEventCallback& callback); | 
|  211   virtual void OnExtendableMessageEvent( |  201   virtual void OnExtendableMessageEvent( | 
|  212       mojom::ExtendableMessageEventPtr event, |  202       mojom::ExtendableMessageEventPtr event, | 
|  213       const mojom::ServiceWorkerEventDispatcher:: |  203       const mojom::ServiceWorkerEventDispatcher:: | 
|  214           DispatchExtendableMessageEventCallback& callback); |  204           DispatchExtendableMessageEventCallback& callback); | 
|  215   virtual void OnInstallEvent(int embedded_worker_id, int request_id); |  205   virtual void OnInstallEvent( | 
 |  206       const mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback& | 
 |  207           callback); | 
|  216   virtual void OnFetchEvent( |  208   virtual void OnFetchEvent( | 
|  217       int embedded_worker_id, |  209       int embedded_worker_id, | 
|  218       int fetch_event_id, |  210       int fetch_event_id, | 
|  219       const ServiceWorkerFetchRequest& request, |  211       const ServiceWorkerFetchRequest& request, | 
|  220       mojom::FetchEventPreloadHandlePtr preload_handle, |  212       mojom::FetchEventPreloadHandlePtr preload_handle, | 
|  221       mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |  213       mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 
|  222       const FetchCallback& finish_callback); |  214       const FetchCallback& finish_callback); | 
|  223   virtual void OnNotificationClickEvent( |  215   virtual void OnNotificationClickEvent( | 
|  224       const std::string& notification_id, |  216       const std::string& notification_id, | 
|  225       const PlatformNotificationData& notification_data, |  217       const PlatformNotificationData& notification_data, | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  288           DispatchBackgroundFetchFailEventCallback& callback); |  280           DispatchBackgroundFetchFailEventCallback& callback); | 
|  289   void OnBackgroundFetchedEventStub( |  281   void OnBackgroundFetchedEventStub( | 
|  290       const std::string& tag, |  282       const std::string& tag, | 
|  291       const std::vector<BackgroundFetchSettledFetch>& fetches, |  283       const std::vector<BackgroundFetchSettledFetch>& fetches, | 
|  292       const mojom::ServiceWorkerEventDispatcher:: |  284       const mojom::ServiceWorkerEventDispatcher:: | 
|  293           DispatchBackgroundFetchedEventCallback& callback); |  285           DispatchBackgroundFetchedEventCallback& callback); | 
|  294   void OnExtendableMessageEventStub( |  286   void OnExtendableMessageEventStub( | 
|  295       mojom::ExtendableMessageEventPtr event, |  287       mojom::ExtendableMessageEventPtr event, | 
|  296       const mojom::ServiceWorkerEventDispatcher:: |  288       const mojom::ServiceWorkerEventDispatcher:: | 
|  297           DispatchExtendableMessageEventCallback& callback); |  289           DispatchExtendableMessageEventCallback& callback); | 
|  298   void OnInstallEventStub(int request_id); |  290   void OnInstallEventStub( | 
 |  291       const mojom::ServiceWorkerEventDispatcher::DispatchInstallEventCallback& | 
 |  292           callback); | 
|  299   void OnFetchEventStub( |  293   void OnFetchEventStub( | 
|  300       int thread_id, |  294       int thread_id, | 
|  301       int fetch_event_id, |  295       int fetch_event_id, | 
|  302       const ServiceWorkerFetchRequest& request, |  296       const ServiceWorkerFetchRequest& request, | 
|  303       mojom::FetchEventPreloadHandlePtr preload_handle, |  297       mojom::FetchEventPreloadHandlePtr preload_handle, | 
|  304       mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, |  298       mojom::ServiceWorkerFetchResponseCallbackPtr response_callback, | 
|  305       const FetchCallback& finish_callback); |  299       const FetchCallback& finish_callback); | 
|  306   void OnNotificationClickEventStub( |  300   void OnNotificationClickEventStub( | 
|  307       const std::string& notification_id, |  301       const std::string& notification_id, | 
|  308       const PlatformNotificationData& notification_data, |  302       const PlatformNotificationData& notification_data, | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
|  325       const mojom::ServiceWorkerEventDispatcher:: |  319       const mojom::ServiceWorkerEventDispatcher:: | 
|  326           DispatchPaymentRequestEventCallback& callback); |  320           DispatchPaymentRequestEventCallback& callback); | 
|  327  |  321  | 
|  328   std::unique_ptr<TestBrowserContext> browser_context_; |  322   std::unique_ptr<TestBrowserContext> browser_context_; | 
|  329   std::unique_ptr<MockRenderProcessHost> render_process_host_; |  323   std::unique_ptr<MockRenderProcessHost> render_process_host_; | 
|  330   std::unique_ptr<MockRenderProcessHost> new_render_process_host_; |  324   std::unique_ptr<MockRenderProcessHost> new_render_process_host_; | 
|  331  |  325  | 
|  332   scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |  326   scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 
|  333  |  327  | 
|  334   IPC::TestSink sink_; |  328   IPC::TestSink sink_; | 
|  335   IPC::TestSink inner_sink_; |  | 
|  336  |  329  | 
|  337   std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> |  330   std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> | 
|  338       mock_instance_clients_; |  331       mock_instance_clients_; | 
|  339   size_t mock_instance_clients_next_index_; |  332   size_t mock_instance_clients_next_index_; | 
|  340  |  333  | 
|  341   int next_thread_id_; |  334   int next_thread_id_; | 
|  342   int next_provider_id_; |  335   int next_provider_id_; | 
|  343   int mock_render_process_id_; |  336   int mock_render_process_id_; | 
|  344   int new_mock_render_process_id_; |  337   int new_mock_render_process_id_; | 
|  345  |  338  | 
|  346   std::map<int /* process_id */, scoped_refptr<ServiceWorkerDispatcherHost>> |  339   std::map<int /* process_id */, scoped_refptr<ServiceWorkerDispatcherHost>> | 
|  347       dispatcher_hosts_; |  340       dispatcher_hosts_; | 
|  348  |  341  | 
|  349   std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |  342   std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 
|  350   std::map<int /* thread_id */, int /* embedded_worker_id */> |  343   std::map<int /* thread_id */, int /* embedded_worker_id */> | 
|  351       thread_id_embedded_worker_id_map_; |  344       thread_id_embedded_worker_id_map_; | 
|  352  |  345  | 
|  353   std::map< |  346   std::map< | 
|  354       int /* embedded_worker_id */, |  347       int /* embedded_worker_id */, | 
|  355       mojom::EmbeddedWorkerInstanceHostAssociatedPtr /* instance_host_ptr */> |  348       mojom::EmbeddedWorkerInstanceHostAssociatedPtr /* instance_host_ptr */> | 
|  356       embedded_worker_id_instance_host_ptr_map_; |  349       embedded_worker_id_instance_host_ptr_map_; | 
|  357  |  350  | 
|  358   // Updated each time MessageToWorker message is received. |  | 
|  359   int current_embedded_worker_id_; |  | 
|  360  |  | 
|  361   std::vector<Event> events_; |  351   std::vector<Event> events_; | 
|  362  |  352  | 
|  363   base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |  353   base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 
|  364  |  354  | 
|  365   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |  355   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 
|  366 }; |  356 }; | 
|  367  |  357  | 
|  368 template <typename MockType, typename... Args> |  358 template <typename MockType, typename... Args> | 
|  369 MockType* EmbeddedWorkerTestHelper::CreateAndRegisterMockInstanceClient( |  359 MockType* EmbeddedWorkerTestHelper::CreateAndRegisterMockInstanceClient( | 
|  370     Args&&... args) { |  360     Args&&... args) { | 
|  371   std::unique_ptr<MockType> mock = |  361   std::unique_ptr<MockType> mock = | 
|  372       base::MakeUnique<MockType>(std::forward<Args>(args)...); |  362       base::MakeUnique<MockType>(std::forward<Args>(args)...); | 
|  373   MockType* mock_rawptr = mock.get(); |  363   MockType* mock_rawptr = mock.get(); | 
|  374   RegisterMockInstanceClient(std::move(mock)); |  364   RegisterMockInstanceClient(std::move(mock)); | 
|  375   return mock_rawptr; |  365   return mock_rawptr; | 
|  376 } |  366 } | 
|  377  |  367  | 
|  378 }  // namespace content |  368 }  // namespace content | 
|  379  |  369  | 
|  380 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |  370 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 
| OLD | NEW |