Chromium Code Reviews| 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" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 22 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 23 | 27 |
| 24 class GURL; | 28 class GURL; |
| 25 struct EmbeddedWorkerMsg_StartWorker_Params; | 29 struct EmbeddedWorkerMsg_StartWorker_Params; |
| 26 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; | 30 struct ServiceWorkerMsg_ExtendableMessageEvent_Params; |
| 27 | 31 |
| 28 namespace shell { | 32 namespace shell { |
| 29 class InterfaceProvider; | 33 class InterfaceProvider; |
| 30 class InterfaceRegistry; | 34 class InterfaceRegistry; |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 53 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 57 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
| 54 // | 58 // |
| 55 // Alternatively consumers can subclass this helper and override On*() | 59 // Alternatively consumers can subclass this helper and override On*() |
| 56 // methods to add their own logic/verification code. | 60 // methods to add their own logic/verification code. |
| 57 // | 61 // |
| 58 // See embedded_worker_instance_unittest.cc for example usages. | 62 // See embedded_worker_instance_unittest.cc for example usages. |
| 59 // | 63 // |
| 60 class EmbeddedWorkerTestHelper : public IPC::Sender, | 64 class EmbeddedWorkerTestHelper : public IPC::Sender, |
| 61 public IPC::Listener { | 65 public IPC::Listener { |
| 62 public: | 66 public: |
| 67 class MockEmbeddedWorkerInstanceClient | |
| 68 : public mojom::EmbeddedWorkerInstanceClient { | |
| 69 public: | |
| 70 explicit MockEmbeddedWorkerInstanceClient( | |
| 71 base::WeakPtr<EmbeddedWorkerTestHelper> helper); | |
| 72 ~MockEmbeddedWorkerInstanceClient() override; | |
| 73 | |
| 74 static void Bind(const base::WeakPtr<EmbeddedWorkerTestHelper>& helper, | |
| 75 mojom::EmbeddedWorkerInstanceClientRequest request); | |
| 76 | |
| 77 // MOCK_METHOD cannot be used for StartWorker because the mock method cannot | |
| 78 // take move-only objects directly. | |
|
falken
2016/09/07 05:01:42
I'd rather we avoid gmock if possible.
shimazu
2016/09/12 06:28:19
Done.
| |
| 79 MOCK_METHOD1(MockStartWorker, | |
| 80 void(mojom::EmbeddedWorkerStartWorkerParams* params)); | |
| 81 | |
| 82 private: | |
| 83 // Implementation of mojo interface; they just calls a mock function. | |
| 84 void StartWorker( | |
| 85 mojom::EmbeddedWorkerStartWorkerParamsPtr params) override { | |
| 86 MockStartWorker(params.get()); | |
| 87 } | |
| 88 | |
| 89 // Default behavior which can be overridden by using mock interfaces. | |
| 90 void DefaultStartWorker(mojom::EmbeddedWorkerStartWorkerParams* params); | |
| 91 | |
| 92 base::WeakPtr<EmbeddedWorkerTestHelper> helper_; | |
| 93 mojo::Binding<mojom::EmbeddedWorkerInstanceClient> binding_; | |
| 94 | |
| 95 base::Optional<int> embedded_worker_id_; | |
| 96 | |
| 97 DISALLOW_COPY_AND_ASSIGN(MockEmbeddedWorkerInstanceClient); | |
| 98 }; | |
| 99 | |
| 63 // If |user_data_directory| is empty, the context makes storage stuff in | 100 // If |user_data_directory| is empty, the context makes storage stuff in |
| 64 // memory. | 101 // memory. |
| 65 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); | 102 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); |
| 66 ~EmbeddedWorkerTestHelper() override; | 103 ~EmbeddedWorkerTestHelper() override; |
| 67 | 104 |
| 68 // Call this to simulate add/associate a process to a pattern. | 105 // Call this to simulate add/associate a process to a pattern. |
| 69 // This also registers this sender for the process. | 106 // This also registers this sender for the process. |
| 70 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); | 107 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); |
| 71 | 108 |
| 72 // IPC::Sender implementation. | 109 // IPC::Sender implementation. |
| 73 bool Send(IPC::Message* message) override; | 110 bool Send(IPC::Message* message) override; |
| 74 | 111 |
| 75 // IPC::Listener implementation. | 112 // IPC::Listener implementation. |
| 76 bool OnMessageReceived(const IPC::Message& msg) override; | 113 bool OnMessageReceived(const IPC::Message& msg) override; |
| 77 | 114 |
| 78 // IPC sink for EmbeddedWorker messages. | 115 // IPC sink for EmbeddedWorker messages. |
| 79 IPC::TestSink* ipc_sink() { return &sink_; } | 116 IPC::TestSink* ipc_sink() { return &sink_; } |
| 80 // Inner IPC sink for script context messages sent via EmbeddedWorker. | 117 // Inner IPC sink for script context messages sent via EmbeddedWorker. |
| 81 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } | 118 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } |
| 82 | 119 |
| 120 // Creates mock instances before IPCs are fired to check IPCs are called as | |
| 121 // your expectation. | |
| 122 void PrepareMockInstanceClients(int number_of_clients); | |
| 123 | |
| 124 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>>* | |
| 125 mock_instance_clients() { | |
| 126 return &mock_instance_clients_; | |
| 127 } | |
| 128 | |
| 83 ServiceWorkerContextCore* context(); | 129 ServiceWorkerContextCore* context(); |
| 84 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } | 130 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } |
| 85 void ShutdownContext(); | 131 void ShutdownContext(); |
| 86 | 132 |
| 87 int GetNextThreadId() { return next_thread_id_++; } | 133 int GetNextThreadId() { return next_thread_id_++; } |
| 88 | 134 |
| 89 int mock_render_process_id() const { return mock_render_process_id_; } | 135 int mock_render_process_id() const { return mock_render_process_id_; } |
| 90 MockRenderProcessHost* mock_render_process_host() { | 136 MockRenderProcessHost* mock_render_process_host() { |
| 91 return render_process_host_.get(); | 137 return render_process_host_.get(); |
| 92 } | 138 } |
| 93 | 139 |
| 94 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() { | 140 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map() { |
| 95 return embedded_worker_id_service_worker_version_id_map_; | 141 return embedded_worker_id_service_worker_version_id_map_; |
| 96 } | 142 } |
| 97 | 143 |
| 98 // Only used for tests that force creating a new render process. There is no | 144 // Only used for tests that force creating a new render process. |
| 99 // corresponding MockRenderProcessHost. | 145 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 | 146 |
| 102 TestBrowserContext* browser_context() { return browser_context_.get(); } | 147 TestBrowserContext* browser_context() { return browser_context_.get(); } |
| 103 | 148 |
| 104 protected: | 149 protected: |
| 105 // Called when StartWorker, StopWorker and SendMessageToWorker message | 150 // Called when StartWorker, StopWorker and SendMessageToWorker message |
| 106 // is sent to the embedded worker. Override if necessary. By default | 151 // is sent to the embedded worker. Override if necessary. By default |
| 107 // they verify given parameters and: | 152 // they verify given parameters and: |
| 108 // - OnStartWorker calls SimulateWorkerStarted | 153 // - OnStartWorker calls SimulateWorkerStarted |
| 109 // - OnStopWorker calls SimulateWorkerStoped | 154 // - OnStopWorker calls SimulateWorkerStoped |
| 110 // - OnSendMessageToWorker calls the message's respective On*Event handler | 155 // - OnSendMessageToWorker calls the message's respective On*Event handler |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 const ServiceWorkerFetchRequest& request); | 219 const ServiceWorkerFetchRequest& request); |
| 175 void OnPushEventStub(int request_id, const PushEventPayload& payload); | 220 void OnPushEventStub(int request_id, const PushEventPayload& payload); |
| 176 void OnSetupMojoStub(int thread_id, | 221 void OnSetupMojoStub(int thread_id, |
| 177 shell::mojom::InterfaceProviderRequest services, | 222 shell::mojom::InterfaceProviderRequest services, |
| 178 shell::mojom::InterfaceProviderPtr exposed_services); | 223 shell::mojom::InterfaceProviderPtr exposed_services); |
| 179 | 224 |
| 180 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 225 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
| 181 | 226 |
| 182 std::unique_ptr<TestBrowserContext> browser_context_; | 227 std::unique_ptr<TestBrowserContext> browser_context_; |
| 183 std::unique_ptr<MockRenderProcessHost> render_process_host_; | 228 std::unique_ptr<MockRenderProcessHost> render_process_host_; |
| 229 std::unique_ptr<MockRenderProcessHost> new_render_process_host_; | |
| 184 | 230 |
| 185 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 231 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
| 186 | 232 |
| 187 IPC::TestSink sink_; | 233 IPC::TestSink sink_; |
| 188 IPC::TestSink inner_sink_; | 234 IPC::TestSink inner_sink_; |
| 189 | 235 |
| 236 std::vector<std::unique_ptr<MockEmbeddedWorkerInstanceClient>> | |
| 237 mock_instance_clients_; | |
| 238 size_t mock_instance_clients_next_; | |
| 239 | |
| 190 int next_thread_id_; | 240 int next_thread_id_; |
| 191 int mock_render_process_id_; | 241 int mock_render_process_id_; |
| 242 int new_mock_render_process_id_; | |
| 192 | 243 |
| 193 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; | 244 std::unique_ptr<shell::InterfaceRegistry> render_process_interface_registry_; |
| 245 std::unique_ptr<shell::InterfaceRegistry> | |
| 246 new_render_process_interface_registry_; | |
| 194 | 247 |
| 195 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 248 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
| 196 | 249 |
| 197 // Stores the InterfaceRegistry/InterfaceProviders that are associated with | 250 // Stores the InterfaceRegistry/InterfaceProviders that are associated with |
| 198 // each individual service worker. | 251 // each individual service worker. |
| 199 base::hash_map<int, InterfaceRegistryAndProvider> | 252 base::hash_map<int, InterfaceRegistryAndProvider> |
| 200 thread_id_service_registry_map_; | 253 thread_id_service_registry_map_; |
| 201 | 254 |
| 202 // Updated each time MessageToWorker message is received. | 255 // Updated each time MessageToWorker message is received. |
| 203 int current_embedded_worker_id_; | 256 int current_embedded_worker_id_; |
| 204 | 257 |
| 205 std::vector<scoped_refptr<MessagePortMessageFilter>> | 258 std::vector<scoped_refptr<MessagePortMessageFilter>> |
| 206 message_port_message_filters_; | 259 message_port_message_filters_; |
| 207 | 260 |
| 208 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 261 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
| 209 | 262 |
| 210 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 263 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
| 211 }; | 264 }; |
| 212 | 265 |
| 213 } // namespace content | 266 } // namespace content |
| 214 | 267 |
| 215 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 268 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| OLD | NEW |