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/time/time.h" | |
18 #include "ipc/ipc_listener.h" | 19 #include "ipc/ipc_listener.h" |
19 #include "ipc/ipc_test_sink.h" | 20 #include "ipc/ipc_test_sink.h" |
20 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 21 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "third_party/WebKit/public/platform/modules/serviceworker/service_worke r_event_status.mojom.h" | |
22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
23 | 25 |
24 class GURL; | 26 class GURL; |
25 struct EmbeddedWorkerMsg_StartWorker_Params; | 27 struct EmbeddedWorkerMsg_StartWorker_Params; |
26 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; | 28 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; |
27 | 29 |
28 namespace shell { | 30 namespace shell { |
29 class InterfaceProvider; | 31 class InterfaceProvider; |
30 class InterfaceRegistry; | 32 class InterfaceRegistry; |
31 } | 33 } |
(...skipping 21 matching lines...) Expand all Loading... | |
53 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 55 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
54 // | 56 // |
55 // Alternatively consumers can subclass this helper and override On*() | 57 // Alternatively consumers can subclass this helper and override On*() |
56 // methods to add their own logic/verification code. | 58 // methods to add their own logic/verification code. |
57 // | 59 // |
58 // See embedded_worker_instance_unittest.cc for example usages. | 60 // See embedded_worker_instance_unittest.cc for example usages. |
59 // | 61 // |
60 class EmbeddedWorkerTestHelper : public IPC::Sender, | 62 class EmbeddedWorkerTestHelper : public IPC::Sender, |
61 public IPC::Listener { | 63 public IPC::Listener { |
62 public: | 64 public: |
65 using FetchCallback = | |
66 base::Callback<void(blink::mojom::ServiceWorkerEventStatus, | |
shimazu
2016/09/23 03:26:11
Now we can use EnumTraits<blink::mojom::ServiceWor
horo
2016/09/23 05:57:56
Is it possible to convert 'const Callback<void (::
horo
2016/09/23 09:13:35
Ah, I misunderstood about typemap and EnumTraits.
| |
67 base::Time /* dispatch_event_time */)>; | |
68 | |
63 // If |user_data_directory| is empty, the context makes storage stuff in | 69 // If |user_data_directory| is empty, the context makes storage stuff in |
64 // memory. | 70 // memory. |
65 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); | 71 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); |
66 ~EmbeddedWorkerTestHelper() override; | 72 ~EmbeddedWorkerTestHelper() override; |
67 | 73 |
68 // Call this to simulate add/associate a process to a pattern. | 74 // Call this to simulate add/associate a process to a pattern. |
69 // This also registers this sender for the process. | 75 // This also registers this sender for the process. |
70 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); | 76 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); |
71 | 77 |
72 // IPC::Sender implementation. | 78 // IPC::Sender implementation. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 const GURL& script_url, | 120 const GURL& script_url, |
115 bool pause_after_download); | 121 bool pause_after_download); |
116 virtual void OnResumeAfterDownload(int embedded_worker_id); | 122 virtual void OnResumeAfterDownload(int embedded_worker_id); |
117 virtual void OnStopWorker(int embedded_worker_id); | 123 virtual void OnStopWorker(int embedded_worker_id); |
118 virtual bool OnMessageToWorker(int thread_id, | 124 virtual bool OnMessageToWorker(int thread_id, |
119 int embedded_worker_id, | 125 int embedded_worker_id, |
120 const IPC::Message& message); | 126 const IPC::Message& message); |
121 | 127 |
122 // Called to setup mojo for a new embedded worker. Override to register | 128 // Called to setup mojo for a new embedded worker. Override to register |
123 // interfaces the worker should expose to the browser. | 129 // interfaces the worker should expose to the browser. |
124 virtual void OnSetupMojo(shell::InterfaceRegistry* interface_registry); | 130 virtual void OnSetupMojo(int thread_id, |
131 shell::InterfaceRegistry* interface_registry); | |
125 | 132 |
126 // On*Event handlers. Called by the default implementation of | 133 // On*Event handlers. Called by the default implementation of |
127 // OnMessageToWorker when events are sent to the embedded | 134 // OnMessageToWorker when events are sent to the embedded |
128 // worker. By default they just return success via | 135 // worker. By default they just return success via |
129 // SimulateSendReplyToBrowser. | 136 // SimulateSendReplyToBrowser. |
130 virtual void OnActivateEvent(int embedded_worker_id, int request_id); | 137 virtual void OnActivateEvent(int embedded_worker_id, int request_id); |
131 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); | 138 virtual void OnExtendableMessageEvent(int embedded_worker_id, int request_id); |
132 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 139 virtual void OnInstallEvent(int embedded_worker_id, int request_id); |
133 virtual void OnFetchEvent(int embedded_worker_id, | 140 virtual void OnFetchEvent(int embedded_worker_id, |
134 int response_id, | 141 int response_id, |
135 int event_finish_id, | 142 const ServiceWorkerFetchRequest& request, |
136 const ServiceWorkerFetchRequest& request); | 143 const FetchCallback& callback); |
137 virtual void OnPushEvent(int embedded_worker_id, | 144 virtual void OnPushEvent(int embedded_worker_id, |
138 int request_id, | 145 int request_id, |
139 const PushEventPayload& payload); | 146 const PushEventPayload& payload); |
140 | 147 |
141 // These functions simulate sending an EmbeddedHostMsg message to the | 148 // These functions simulate sending an EmbeddedHostMsg message to the |
142 // browser. | 149 // browser. |
143 void SimulateWorkerReadyForInspection(int embedded_worker_id); | 150 void SimulateWorkerReadyForInspection(int embedded_worker_id); |
144 void SimulateWorkerScriptCached(int embedded_worker_id); | 151 void SimulateWorkerScriptCached(int embedded_worker_id); |
145 void SimulateWorkerScriptLoaded(int embedded_worker_id); | 152 void SimulateWorkerScriptLoaded(int embedded_worker_id); |
146 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); | 153 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); |
147 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); | 154 void SimulateWorkerScriptEvaluated(int embedded_worker_id, bool success); |
148 void SimulateWorkerStarted(int embedded_worker_id); | 155 void SimulateWorkerStarted(int embedded_worker_id); |
149 void SimulateWorkerStopped(int embedded_worker_id); | 156 void SimulateWorkerStopped(int embedded_worker_id); |
150 void SimulateSend(IPC::Message* message); | 157 void SimulateSend(IPC::Message* message); |
151 | 158 |
152 EmbeddedWorkerRegistry* registry(); | 159 EmbeddedWorkerRegistry* registry(); |
153 | 160 |
154 private: | 161 private: |
155 using InterfaceRegistryAndProvider = | 162 using InterfaceRegistryAndProvider = |
156 std::pair<std::unique_ptr<shell::InterfaceRegistry>, | 163 std::pair<std::unique_ptr<shell::InterfaceRegistry>, |
157 std::unique_ptr<shell::InterfaceProvider>>; | 164 std::unique_ptr<shell::InterfaceProvider>>; |
158 | 165 |
159 class MockEmbeddedWorkerSetup; | 166 class MockEmbeddedWorkerSetup; |
167 class MockFetchEventDispatcher; | |
160 | 168 |
161 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params); | 169 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params); |
162 void OnResumeAfterDownloadStub(int embedded_worker_id); | 170 void OnResumeAfterDownloadStub(int embedded_worker_id); |
163 void OnStopWorkerStub(int embedded_worker_id); | 171 void OnStopWorkerStub(int embedded_worker_id); |
164 void OnMessageToWorkerStub(int thread_id, | 172 void OnMessageToWorkerStub(int thread_id, |
165 int embedded_worker_id, | 173 int embedded_worker_id, |
166 const IPC::Message& message); | 174 const IPC::Message& message); |
167 void OnActivateEventStub(int request_id); | 175 void OnActivateEventStub(int request_id); |
168 void OnExtendableMessageEventStub( | 176 void OnExtendableMessageEventStub( |
169 int request_id, | 177 int request_id, |
170 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); | 178 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); |
171 void OnInstallEventStub(int request_id); | 179 void OnInstallEventStub(int request_id); |
172 void OnFetchEventStub(int response_id, | 180 void OnFetchEventStub(int thread_id, |
173 int event_finish_id, | 181 int response_id, |
174 const ServiceWorkerFetchRequest& request); | 182 const ServiceWorkerFetchRequest& request, |
183 const FetchCallback& callback); | |
175 void OnPushEventStub(int request_id, const PushEventPayload& payload); | 184 void OnPushEventStub(int request_id, const PushEventPayload& payload); |
176 void OnSetupMojoStub(int thread_id, | 185 void OnSetupMojoStub(int thread_id, |
177 shell::mojom::InterfaceProviderRequest services, | 186 shell::mojom::InterfaceProviderRequest services, |
178 shell::mojom::InterfaceProviderPtr exposed_services); | 187 shell::mojom::InterfaceProviderPtr exposed_services); |
179 | 188 |
180 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 189 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
181 | 190 |
182 std::unique_ptr<TestBrowserContext> browser_context_; | 191 std::unique_ptr<TestBrowserContext> browser_context_; |
183 std::unique_ptr<MockRenderProcessHost> render_process_host_; | 192 std::unique_ptr<MockRenderProcessHost> render_process_host_; |
184 | 193 |
185 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 194 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
186 | 195 |
187 IPC::TestSink sink_; | 196 IPC::TestSink sink_; |
188 IPC::TestSink inner_sink_; | 197 IPC::TestSink inner_sink_; |
189 | 198 |
190 int next_thread_id_; | 199 int next_thread_id_; |
191 int mock_render_process_id_; | 200 int mock_render_process_id_; |
192 | 201 |
193 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; | 202 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; |
194 | 203 |
195 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 204 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
205 std::map<int, int> thread_id_embedded_worker_id_map_; | |
196 | 206 |
197 // Stores the InterfaceRegistry/InterfaceProviders that are associated with | 207 // Stores the InterfaceRegistry/InterfaceProviders that are associated with |
198 // each individual service worker. | 208 // each individual service worker. |
199 base::hash_map<int, InterfaceRegistryAndProvider> | 209 base::hash_map<int, InterfaceRegistryAndProvider> |
200 thread_id_service_registry_map_; | 210 thread_id_service_registry_map_; |
201 | 211 |
202 // Updated each time MessageToWorker message is received. | 212 // Updated each time MessageToWorker message is received. |
203 int current_embedded_worker_id_; | 213 int current_embedded_worker_id_; |
204 | 214 |
205 std::vector<scoped_refptr<MessagePortMessageFilter>> | 215 std::vector<scoped_refptr<MessagePortMessageFilter>> |
206 message_port_message_filters_; | 216 message_port_message_filters_; |
207 | 217 |
208 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 218 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
209 | 219 |
210 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 220 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
211 }; | 221 }; |
212 | 222 |
213 } // namespace content | 223 } // namespace content |
214 | 224 |
215 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 225 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
OLD | NEW |