| 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/scoped_ptr_hash_map.h" |
| 15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 16 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "content/common/mojo/service_registry_impl.h" |
| 17 #include "ipc/ipc_listener.h" | 19 #include "ipc/ipc_listener.h" |
| 18 #include "ipc/ipc_test_sink.h" | 20 #include "ipc/ipc_test_sink.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 21 | 23 |
| 22 struct EmbeddedWorkerMsg_StartWorker_Params; | 24 struct EmbeddedWorkerMsg_StartWorker_Params; |
| 23 class GURL; | 25 class GURL; |
| 24 | 26 |
| 25 namespace content { | 27 namespace content { |
| 26 | 28 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 // - OnSendMessageToWorker calls the message's respective On*Event handler | 94 // - OnSendMessageToWorker calls the message's respective On*Event handler |
| 93 virtual void OnStartWorker(int embedded_worker_id, | 95 virtual void OnStartWorker(int embedded_worker_id, |
| 94 int64_t service_worker_version_id, | 96 int64_t service_worker_version_id, |
| 95 const GURL& scope, | 97 const GURL& scope, |
| 96 const GURL& script_url); | 98 const GURL& script_url); |
| 97 virtual void OnStopWorker(int embedded_worker_id); | 99 virtual void OnStopWorker(int embedded_worker_id); |
| 98 virtual bool OnMessageToWorker(int thread_id, | 100 virtual bool OnMessageToWorker(int thread_id, |
| 99 int embedded_worker_id, | 101 int embedded_worker_id, |
| 100 const IPC::Message& message); | 102 const IPC::Message& message); |
| 101 | 103 |
| 104 // Called to setup mojo for a new embedded worker. Override to register |
| 105 // services the worker should expose to the browser. |
| 106 virtual void OnSetupMojo(ServiceRegistry* service_registry); |
| 107 |
| 102 // On*Event handlers. Called by the default implementation of | 108 // On*Event handlers. Called by the default implementation of |
| 103 // OnMessageToWorker when events are sent to the embedded | 109 // OnMessageToWorker when events are sent to the embedded |
| 104 // worker. By default they just return success via | 110 // worker. By default they just return success via |
| 105 // SimulateSendReplyToBrowser. | 111 // SimulateSendReplyToBrowser. |
| 106 virtual void OnActivateEvent(int embedded_worker_id, int request_id); | 112 virtual void OnActivateEvent(int embedded_worker_id, int request_id); |
| 107 virtual void OnInstallEvent(int embedded_worker_id, int request_id); | 113 virtual void OnInstallEvent(int embedded_worker_id, int request_id); |
| 108 virtual void OnFetchEvent(int embedded_worker_id, | 114 virtual void OnFetchEvent(int embedded_worker_id, |
| 109 int request_id, | 115 int request_id, |
| 110 const ServiceWorkerFetchRequest& request); | 116 const ServiceWorkerFetchRequest& request); |
| 111 virtual void OnPushEvent(int embedded_worker_id, | 117 virtual void OnPushEvent(int embedded_worker_id, |
| 112 int request_id, | 118 int request_id, |
| 113 const std::string& data); | 119 const std::string& data); |
| 114 | 120 |
| 115 // These functions simulate sending an EmbeddedHostMsg message to the | 121 // These functions simulate sending an EmbeddedHostMsg message to the |
| 116 // browser. | 122 // browser. |
| 117 void SimulateWorkerReadyForInspection(int embedded_worker_id); | 123 void SimulateWorkerReadyForInspection(int embedded_worker_id); |
| 118 void SimulateWorkerScriptCached(int embedded_worker_id); | 124 void SimulateWorkerScriptCached(int embedded_worker_id); |
| 119 void SimulateWorkerScriptLoaded(int embedded_worker_id); | 125 void SimulateWorkerScriptLoaded(int embedded_worker_id); |
| 120 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); | 126 void SimulateWorkerThreadStarted(int thread_id, int embedded_worker_id); |
| 121 void SimulateWorkerScriptEvaluated(int embedded_worker_id); | 127 void SimulateWorkerScriptEvaluated(int embedded_worker_id); |
| 122 void SimulateWorkerStarted(int embedded_worker_id); | 128 void SimulateWorkerStarted(int embedded_worker_id); |
| 123 void SimulateWorkerStopped(int embedded_worker_id); | 129 void SimulateWorkerStopped(int embedded_worker_id); |
| 124 void SimulateSend(IPC::Message* message); | 130 void SimulateSend(IPC::Message* message); |
| 125 | 131 |
| 126 EmbeddedWorkerRegistry* registry(); | 132 EmbeddedWorkerRegistry* registry(); |
| 127 | 133 |
| 128 private: | 134 private: |
| 135 class MockEmbeddedWorkerSetup; |
| 136 |
| 129 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params); | 137 void OnStartWorkerStub(const EmbeddedWorkerMsg_StartWorker_Params& params); |
| 130 void OnStopWorkerStub(int embedded_worker_id); | 138 void OnStopWorkerStub(int embedded_worker_id); |
| 131 void OnMessageToWorkerStub(int thread_id, | 139 void OnMessageToWorkerStub(int thread_id, |
| 132 int embedded_worker_id, | 140 int embedded_worker_id, |
| 133 const IPC::Message& message); | 141 const IPC::Message& message); |
| 134 void OnActivateEventStub(int request_id); | 142 void OnActivateEventStub(int request_id); |
| 135 void OnInstallEventStub(int request_id); | 143 void OnInstallEventStub(int request_id); |
| 136 void OnFetchEventStub(int request_id, | 144 void OnFetchEventStub(int request_id, |
| 137 const ServiceWorkerFetchRequest& request); | 145 const ServiceWorkerFetchRequest& request); |
| 138 void OnPushEventStub(int request_id, const std::string& data); | 146 void OnPushEventStub(int request_id, const std::string& data); |
| 147 void OnSetupMojoStub(int thread_id, |
| 148 mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 149 mojo::ServiceProviderPtr exposed_services); |
| 139 | 150 |
| 140 MessagePortMessageFilter* NewMessagePortMessageFilter(); | 151 MessagePortMessageFilter* NewMessagePortMessageFilter(); |
| 141 | 152 |
| 142 scoped_ptr<TestBrowserContext> browser_context_; | 153 scoped_ptr<TestBrowserContext> browser_context_; |
| 143 scoped_ptr<MockRenderProcessHost> render_process_host_; | 154 scoped_ptr<MockRenderProcessHost> render_process_host_; |
| 144 | 155 |
| 145 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; | 156 scoped_refptr<ServiceWorkerContextWrapper> wrapper_; |
| 146 | 157 |
| 147 IPC::TestSink sink_; | 158 IPC::TestSink sink_; |
| 148 IPC::TestSink inner_sink_; | 159 IPC::TestSink inner_sink_; |
| 149 | 160 |
| 150 int next_thread_id_; | 161 int next_thread_id_; |
| 151 int mock_render_process_id_; | 162 int mock_render_process_id_; |
| 152 | 163 |
| 164 ServiceRegistryImpl render_process_service_registry_; |
| 165 |
| 153 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; | 166 std::map<int, int64_t> embedded_worker_id_service_worker_version_id_map_; |
| 154 | 167 |
| 168 // Stores the ServiceRegistries that are associated with each individual |
| 169 // service worker. |
| 170 base::ScopedPtrHashMap<int, scoped_ptr<ServiceRegistryImpl>> |
| 171 thread_id_service_registry_map_; |
| 172 |
| 155 // Updated each time MessageToWorker message is received. | 173 // Updated each time MessageToWorker message is received. |
| 156 int current_embedded_worker_id_; | 174 int current_embedded_worker_id_; |
| 157 | 175 |
| 158 std::vector<scoped_refptr<MessagePortMessageFilter>> | 176 std::vector<scoped_refptr<MessagePortMessageFilter>> |
| 159 message_port_message_filters_; | 177 message_port_message_filters_; |
| 160 | 178 |
| 161 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; | 179 base::WeakPtrFactory<EmbeddedWorkerTestHelper> weak_factory_; |
| 162 | 180 |
| 163 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); | 181 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerTestHelper); |
| 164 }; | 182 }; |
| 165 | 183 |
| 166 } // namespace content | 184 } // namespace content |
| 167 | 185 |
| 168 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ | 186 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_TEST_HELPER_H_ |
| OLD | NEW |