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" |
| 19 #include "content/common/service_worker/embedded_worker.mojom.h" |
18 #include "ipc/ipc_listener.h" | 20 #include "ipc/ipc_listener.h" |
19 #include "ipc/ipc_test_sink.h" | 21 #include "ipc/ipc_test_sink.h" |
| 22 #include "mojo/public/cpp/bindings/binding.h" |
20 #include "services/shell/public/interfaces/interface_provider.mojom.h" | 23 #include "services/shell/public/interfaces/interface_provider.mojom.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "url/gurl.h" | 25 #include "url/gurl.h" |
23 | 26 |
24 class GURL; | 27 class GURL; |
25 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 } |
32 | 34 |
33 namespace content { | 35 namespace content { |
34 | 36 |
35 class EmbeddedWorkerRegistry; | 37 class EmbeddedWorkerRegistry; |
36 class EmbeddedWorkerTestHelper; | 38 class EmbeddedWorkerTestHelper; |
37 class MessagePortMessageFilter; | 39 class MessagePortMessageFilter; |
38 class MockRenderProcessHost; | 40 class MockRenderProcessHost; |
39 class ServiceWorkerContextCore; | 41 class ServiceWorkerContextCore; |
40 class ServiceWorkerContextWrapper; | 42 class ServiceWorkerContextWrapper; |
41 class TestBrowserContext; | 43 class TestBrowserContext; |
| 44 struct EmbeddedWorkerStartParams; |
42 struct PushEventPayload; | 45 struct PushEventPayload; |
43 struct ServiceWorkerFetchRequest; | 46 struct ServiceWorkerFetchRequest; |
44 | 47 |
45 // In-Process EmbeddedWorker test helper. | 48 // In-Process EmbeddedWorker test helper. |
46 // | 49 // |
47 // Usage: create an instance of this class to test browser-side embedded worker | 50 // Usage: create an instance of this class to test browser-side embedded worker |
48 // code without creating a child process. This class will create a | 51 // code without creating a child process. This class will create a |
49 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you. | 52 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you. |
50 // | 53 // |
51 // By default this class just notifies back WorkerStarted and WorkerStopped | 54 // By default this class just notifies back WorkerStarted and WorkerStopped |
52 // for StartWorker and StopWorker requests. The default implementation | 55 // for StartWorker and StopWorker requests. The default implementation |
53 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 56 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
54 // | 57 // |
55 // Alternatively consumers can subclass this helper and override On*() | 58 // Alternatively consumers can subclass this helper and override On*() |
56 // methods to add their own logic/verification code. | 59 // methods to add their own logic/verification code. |
57 // | 60 // |
58 // See embedded_worker_instance_unittest.cc for example usages. | 61 // See embedded_worker_instance_unittest.cc for example usages. |
59 // | 62 // |
60 class EmbeddedWorkerTestHelper : public IPC::Sender, | 63 class EmbeddedWorkerTestHelper : public IPC::Sender, |
61 public IPC::Listener { | 64 public IPC::Listener { |
62 public: | 65 public: |
| 66 class MockEmbeddedWorkerInstanceClient |
| 67 : public mojom::EmbeddedWorkerInstanceClient { |
| 68 public: |
| 69 explicit MockEmbeddedWorkerInstanceClient( |
| 70 base::WeakPtr<EmbeddedWorkerTestHelper> helper); |
| 71 ~MockEmbeddedWorkerInstanceClient() override; |
| 72 |
| 73 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, |
| 74 mojom::EmbeddedWorkerInstanceClientRequest request); |
| 75 |
| 76 private: |
| 77 // Implementation of mojo interfaces. |
| 78 void StartWorker(const EmbeddedWorkerStartParams& params) override; |
| 79 |
| 80 base::WeakPtr<EmbeddedWorkerTestHelper> helper_; |
| 81 mojo::Binding<mojom::EmbeddedWorkerInstanceClient> binding_; |
| 82 |
| 83 base::Optional<int> embedded_worker_id_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(MockEmbeddedWorkerInstanceClient); |
| 86 }; |
| 87 |
63 // If |user_data_directory| is empty, the context makes storage stuff in | 88 // If |user_data_directory| is empty, the context makes storage stuff in |
64 // memory. | 89 // memory. |
65 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); | 90 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); |
66 ~EmbeddedWorkerTestHelper() override; | 91 ~EmbeddedWorkerTestHelper() override; |
67 | 92 |
68 // Call this to simulate add/associate a process to a pattern. | 93 // Call this to simulate add/associate a process to a pattern. |
69 // This also registers this sender for the process. | 94 // This also registers this sender for the process. |
70 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); | 95 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); |
71 | 96 |
72 // IPC::Sender implementation. | 97 // IPC::Sender implementation. |
73 bool Send(IPC::Message* message) override; | 98 bool Send(IPC::Message* message) override; |
74 | 99 |
75 // IPC::Listener implementation. | 100 // IPC::Listener implementation. |
76 bool OnMessageReceived(const IPC::Message& msg) override; | 101 bool OnMessageReceived(const IPC::Message& msg) override; |
77 | 102 |
78 // IPC sink for EmbeddedWorker messages. | 103 // IPC sink for EmbeddedWorker messages. |
79 IPC::TestSink* ipc_sink() { return &sink_; } | 104 IPC::TestSink* ipc_sink() { return &sink_; } |
80 // Inner IPC sink for script context messages sent via EmbeddedWorker. | 105 // Inner IPC sink for script context messages sent via EmbeddedWorker. |
81 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } | 106 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } |
82 | 107 |
| 108 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>* |
| 109 mock_instance_clients() { |
| 110 return &mock_instance_clients_; |
| 111 } |
| 112 |
83 ServiceWorkerContextCore* context(); | 113 ServiceWorkerContextCore* context(); |
84 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } | 114 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } |
85 void ShutdownContext(); | 115 void ShutdownContext(); |
86 | 116 |
87 int GetNextThreadId() { return next_thread_id_++; } | 117 int GetNextThreadId() { return next_thread_id_++; } |
88 | 118 |
89 int mock_render_process_id() const { return mock_render_process_id_; } | 119 int mock_render_process_id() const { return mock_render_process_id_; } |
90 MockRenderProcessHost* mock_render_process_host() { | 120 MockRenderProcessHost* mock_render_process_host() { |
91 return render_process_host_.get(); | 121 return render_process_host_.get(); |
92 } | 122 } |
93 | 123 |
94 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() { | 124 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() { |
95 return embedded_worker_id_service_worker_version_id_map_; | 125 return embedded_worker_id_service_worker_version_id_map_; |
96 } | 126 } |
97 | 127 |
98 // Only used for tests that force creating a new render process. There is no | 128 // Only used for tests that force creating a new render process. |
99 // corresponding MockRenderProcessHost. | 129 int new_render_process_id() const { return new_mock_render_process_id_; } |
100 int new_render_process_id() const { return mock_render_process_id_ + 1; } | |
101 | 130 |
102 TestBrowserContext* browser_context() { return browser_context_.get(); } | 131 TestBrowserContext* browser_context() { return browser_context_.get(); } |
103 | 132 |
104 protected: | 133 protected: |
105 // Called when StartWorker, StopWorker and SendMessageToWorker message | 134 // Called when StartWorker, StopWorker and SendMessageToWorker message |
106 // is sent to the embedded worker. Override if necessary. By default | 135 // is sent to the embedded worker. Override if necessary. By default |
107 // they verify given parameters and: | 136 // they verify given parameters and: |
108 // - OnStartWorker calls SimulateWorkerStarted | 137 // - OnStartWorker calls SimulateWorkerStarted |
109 // - OnStopWorker calls SimulateWorkerStoped | 138 // - OnStopWorker calls SimulateWorkerStoped |
110 // - OnSendMessageToWorker calls the message's respective On*Event handler | 139 // - OnSendMessageToWorker calls the message's respective On*Event handler |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 | 180 |
152 EmbeddedWorkerRegistry* registry(); | 181 EmbeddedWorkerRegistry* registry(); |
153 | 182 |
154 private: | 183 private: |
155 using InterfaceRegistryAndProvider = | 184 using InterfaceRegistryAndProvider = |
156 std::pair<std::unique_ptr<shell::InterfaceRegistry>, | 185 std::pair<std::unique_ptr<shell::InterfaceRegistry>, |
157 std::unique_ptr<shell::InterfaceProvider>>; | 186 std::unique_ptr<shell::InterfaceProvider>>; |
158 | 187 |
159 class MockEmbeddedWorkerSetup; | 188 class MockEmbeddedWorkerSetup; |
160 | 189 |
161 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params); | 190 void OnStartWorkerStub(const EmbeddedWorkerStartParams& params); |
162 void OnResumeAfterDownloadStub(int embedded_worker_id); | 191 void OnResumeAfterDownloadStub(int embedded_worker_id); |
163 void OnStopWorkerStub(int embedded_worker_id); | 192 void OnStopWorkerStub(int embedded_worker_id); |
164 void OnMessageToWorkerStub(int thread_id, | 193 void OnMessageToWorkerStub(int thread_id, |
165 int embedded_worker_id, | 194 int embedded_worker_id, |
166 const IPC::Message& message); | 195 const IPC::Message& message); |
167 void OnActivateEventStub(int request_id); | 196 void OnActivateEventStub(int request_id); |
168 void OnExtendableMessageEventStub( | 197 void OnExtendableMessageEventStub( |
169 int request_id, | 198 int request_id, |
170 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); | 199 const ServiceWorkerMsg_ExtendableMessageEvent_Params& params); |
171 void OnInstallEventStub(int request_id); | 200 void OnInstallEventStub(int request_id); |
172 void OnFetchEventStub(int response_id, | 201 void OnFetchEventStub(int response_id, |
173 int event_finish_id, | 202 int event_finish_id, |
174 const ServiceWorkerFetchRequest& request); | 203 const ServiceWorkerFetchRequest& request); |
175 void OnPushEventStub(int request_id, const PushEventPayload& payload); | 204 void OnPushEventStub(int request_id, const PushEventPayload& payload); |
176 void OnSetupMojoStub(int thread_id, | 205 void OnSetupMojoStub(int thread_id, |
177 shell::mojom::InterfaceProviderRequest services, | 206 shell::mojom::InterfaceProviderRequest services, |
178 shell::mojom::InterfaceProviderPtr exposed_services); | 207 shell::mojom::InterfaceProviderPtr exposed_services); |
179 | 208 |
180 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 209 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
181 | 210 |
| 211 std::unique_ptr<shell::InterfaceRegistry> CreateInterfaceRegistry( |
| 212 MockRenderProcessHost* rph); |
| 213 |
182 std::unique_ptr<TestBrowserContext> browser_context_; | 214 std::unique_ptr<TestBrowserContext> browser_context_; |
183 std::unique_ptr<MockRenderProcessHost> render_process_host_; | 215 std::unique_ptr<MockRenderProcessHost> render_process_host_; |
| 216 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; |
184 | 217 |
185 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 218 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
186 | 219 |
187 IPC::TestSink sink_; | 220 IPC::TestSink sink_; |
188 IPC::TestSink inner_sink_; | 221 IPC::TestSink inner_sink_; |
189 | 222 |
| 223 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> |
| 224 mock_instance_clients_; |
| 225 size_t mock_instance_clients_next_index_; |
| 226 |
190 int next_thread_id_; | 227 int next_thread_id_; |
191 int mock_render_process_id_; | 228 int mock_render_process_id_; |
| 229 int new_mock_render_process_id_; |
192 | 230 |
193 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; | 231 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; |
| 232 std::unique_ptr<shell::InterfaceRegistry> |
| 233 new_render_process_interface_registry_; |
194 | 234 |
195 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 235 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
196 | 236 |
197 // Stores the InterfaceRegistry/InterfaceProviders that are associated with | 237 // Stores the InterfaceRegistry/InterfaceProviders that are associated with |
198 // each individual service worker. | 238 // each individual service worker. |
199 base::hash_map<int, InterfaceRegistryAndProvider> | 239 base::hash_map<int, InterfaceRegistryAndProvider> |
200 thread_id_service_registry_map_; | 240 thread_id_service_registry_map_; |
201 | 241 |
202 // Updated each time MessageToWorker message is received. | 242 // Updated each time MessageToWorker message is received. |
203 int current_embedded_worker_id_; | 243 int current_embedded_worker_id_; |
204 | 244 |
205 std::vector<scoped_refptr<MessagePortMessageFilter>> | 245 std::vector<scoped_refptr<MessagePortMessageFilter>> |
206 message_port_message_filters_; | 246 message_port_message_filters_; |
207 | 247 |
208 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 248 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
209 | 249 |
210 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 250 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
211 }; | 251 }; |
212 | 252 |
213 } // namespace content | 253 } // namespace content |
214 | 254 |
215 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 255 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
OLD | NEW |