| 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 <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "ipc/ipc_listener.h" | 14 #include "ipc/ipc_listener.h" |
| 15 #include "ipc/ipc_test_sink.h" | 15 #include "ipc/ipc_test_sink.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 struct EmbeddedWorkerMsg_StartWorker_Params; | 19 struct EmbeddedWorkerMsg_StartWorker_Params; |
| 20 class GURL; | 20 class GURL; |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 class EmbeddedWorkerRegistry; | 24 class EmbeddedWorkerRegistry; |
| 25 class EmbeddedWorkerTestHelper; | 25 class EmbeddedWorkerTestHelper; |
| 26 class MessagePortMessageFilter; | 26 class MessagePortMessageFilter; |
| 27 class MockRenderProcessHost; |
| 27 class ServiceWorkerContextCore; | 28 class ServiceWorkerContextCore; |
| 28 class ServiceWorkerContextWrapper; | 29 class ServiceWorkerContextWrapper; |
| 29 struct ServiceWorkerFetchRequest; | 30 struct ServiceWorkerFetchRequest; |
| 31 class TestBrowserContext; |
| 30 | 32 |
| 31 // In-Process EmbeddedWorker test helper. | 33 // In-Process EmbeddedWorker test helper. |
| 32 // | 34 // |
| 33 // Usage: create an instance of this class to test browser-side embedded worker | 35 // Usage: create an instance of this class to test browser-side embedded worker |
| 34 // code without creating a child process. This class will create a | 36 // code without creating a child process. This class will create a |
| 35 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you. | 37 // ServiceWorkerContextWrapper and ServiceWorkerContextCore for you. |
| 36 // | 38 // |
| 37 // By default this class just notifies back WorkerStarted and WorkerStopped | 39 // By default this class just notifies back WorkerStarted and WorkerStopped |
| 38 // for StartWorker and StopWorker requests. The default implementation | 40 // for StartWorker and StopWorker requests. The default implementation |
| 39 // also returns success for event messages (e.g. InstallEvent, FetchEvent). | 41 // also returns success for event messages (e.g. InstallEvent, FetchEvent). |
| 40 // | 42 // |
| 41 // Alternatively consumers can subclass this helper and override On*() | 43 // Alternatively consumers can subclass this helper and override On*() |
| 42 // methods to add their own logic/verification code. | 44 // methods to add their own logic/verification code. |
| 43 // | 45 // |
| 44 // See embedded_worker_instance_unittest.cc for example usages. | 46 // See embedded_worker_instance_unittest.cc for example usages. |
| 45 // | 47 // |
| 46 class EmbeddedWorkerTestHelper : public IPC::Sender, | 48 class EmbeddedWorkerTestHelper : public IPC::Sender, |
| 47 public IPC::Listener { | 49 public IPC::Listener { |
| 48 public: | 50 public: |
| 49 // Initialize this helper for |context|, and enable this as an IPC | 51 // Initialize this helper for |context|, and enable this as an IPC |
| 50 // sender for |mock_render_process_id|. If |user_data_directory| is empty, | 52 // sender for |mock_render_process_id|. If |user_data_directory| is empty, |
| 51 // the context makes storage stuff in memory. | 53 // the context makes storage stuff in memory. |
| 52 EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory, | 54 EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory, |
| 53 int mock_render_process_id); | 55 int mock_render_process_id); |
| 56 // Use this constructor to have |EmbeddedWorkerTestHelper| create a |
| 57 // |MockRenderProcessHost| for its render process, instead of just using |
| 58 // a hardcoded (invalid) process id. |
| 59 explicit EmbeddedWorkerTestHelper(const base::FilePath& user_data_directory); |
| 54 ~EmbeddedWorkerTestHelper() override; | 60 ~EmbeddedWorkerTestHelper() override; |
| 55 | 61 |
| 56 // Call this to simulate add/associate a process to a pattern. | 62 // Call this to simulate add/associate a process to a pattern. |
| 57 // This also registers this sender for the process. | 63 // This also registers this sender for the process. |
| 58 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); | 64 void SimulateAddProcessToPattern(const GURL& pattern, int process_id); |
| 59 | 65 |
| 60 // IPC::Sender implementation. | 66 // IPC::Sender implementation. |
| 61 bool Send(IPC::Message* message) override; | 67 bool Send(IPC::Message* message) override; |
| 62 | 68 |
| 63 // IPC::Listener implementation. | 69 // IPC::Listener implementation. |
| 64 bool OnMessageReceived(const IPC::Message& msg) override; | 70 bool OnMessageReceived(const IPC::Message& msg) override; |
| 65 | 71 |
| 66 // IPC sink for EmbeddedWorker messages. | 72 // IPC sink for EmbeddedWorker messages. |
| 67 IPC::TestSink* ipc_sink() { return &sink_; } | 73 IPC::TestSink* ipc_sink() { return &sink_; } |
| 68 // Inner IPC sink for script context messages sent via EmbeddedWorker. | 74 // Inner IPC sink for script context messages sent via EmbeddedWorker. |
| 69 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } | 75 IPC::TestSink* inner_ipc_sink() { return &inner_sink_; } |
| 70 | 76 |
| 71 ServiceWorkerContextCore* context(); | 77 ServiceWorkerContextCore* context(); |
| 72 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } | 78 ServiceWorkerContextWrapper* context_wrapper() { return wrapper_.get(); } |
| 73 void ShutdownContext(); | 79 void ShutdownContext(); |
| 74 | 80 |
| 75 int mock_render_process_id() const { return mock_render_process_id_;} | 81 int mock_render_process_id() const { return mock_render_process_id_;} |
| 82 // Mock render process. Only set if the one-parameter constructor was used. |
| 83 MockRenderProcessHost* mock_render_process_host() { |
| 84 return render_process_host_.get(); |
| 85 } |
| 76 | 86 |
| 77 protected: | 87 protected: |
| 78 // Called when StartWorker, StopWorker and SendMessageToWorker message | 88 // Called when StartWorker, StopWorker and SendMessageToWorker message |
| 79 // is sent to the embedded worker. Override if necessary. By default | 89 // is sent to the embedded worker. Override if necessary. By default |
| 80 // they verify given parameters and: | 90 // they verify given parameters and: |
| 81 // - OnStartWorker calls SimulateWorkerStarted | 91 // - OnStartWorker calls SimulateWorkerStarted |
| 82 // - OnStopWorker calls SimulateWorkerStoped | 92 // - OnStopWorker calls SimulateWorkerStoped |
| 83 // - OnSendMessageToWorker calls the message's respective On*Event handler | 93 // - OnSendMessageToWorker calls the message's respective On*Event handler |
| 84 virtual void OnStartWorker(int embedded_worker_id, | 94 virtual void OnStartWorker(int embedded_worker_id, |
| 85 int64 service_worker_version_id, | 95 int64 service_worker_version_id, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 int embedded_worker_id, | 133 int embedded_worker_id, |
| 124 const IPC::Message& message); | 134 const IPC::Message& message); |
| 125 void OnActivateEventStub(int request_id); | 135 void OnActivateEventStub(int request_id); |
| 126 void OnInstallEventStub(int request_id); | 136 void OnInstallEventStub(int request_id); |
| 127 void OnFetchEventStub(int request_id, | 137 void OnFetchEventStub(int request_id, |
| 128 const ServiceWorkerFetchRequest& request); | 138 const ServiceWorkerFetchRequest& request); |
| 129 void OnPushEventStub(int request_id, const std::string& data); | 139 void OnPushEventStub(int request_id, const std::string& data); |
| 130 | 140 |
| 131 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 141 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
| 132 | 142 |
| 143 scoped_ptr<TestBrowserContext> browser_context_; |
| 144 scoped_ptr<MockRenderProcessHost> render_process_host_; |
| 145 |
| 133 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 146 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
| 134 | 147 |
| 135 IPC::TestSink sink_; | 148 IPC::TestSink sink_; |
| 136 IPC::TestSink inner_sink_; | 149 IPC::TestSink inner_sink_; |
| 137 | 150 |
| 138 int next_thread_id_; | 151 int next_thread_id_; |
| 139 int mock_render_process_id_; | 152 int mock_render_process_id_; |
| 140 | 153 |
| 141 std::map<int, int64> embedded_worker_id_service_worker_version_id_map_; | 154 std::map<int, int64> embedded_worker_id_service_worker_version_id_map_; |
| 142 | 155 |
| 143 // Updated each time MessageToWorker message is received. | 156 // Updated each time MessageToWorker message is received. |
| 144 int current_embedded_worker_id_; | 157 int current_embedded_worker_id_; |
| 145 | 158 |
| 146 std::vector<scoped_refptr<MessagePortMessageFilter>> | 159 std::vector<scoped_refptr<MessagePortMessageFilter>> |
| 147 message_port_message_filters_; | 160 message_port_message_filters_; |
| 148 | 161 |
| 149 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 162 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
| 150 | 163 |
| 151 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 164 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
| 152 }; | 165 }; |
| 153 | 166 |
| 154 } // namespace content | 167 } // namespace content |
| 155 | 168 |
| 156 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 169 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| OLD | NEW |