Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "content/browser/service_worker/embedded_worker_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/command_line.h" | |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 14 #include "content/browser/service_worker/embedded_worker_registry.h" | 15 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 15 #include "content/browser/service_worker/embedded_worker_status.h" | 16 #include "content/browser/service_worker/embedded_worker_status.h" |
| 16 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 17 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 17 #include "content/browser/service_worker/service_worker_context_core.h" | 18 #include "content/browser/service_worker/service_worker_context_core.h" |
| 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 19 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 20 #include "content/common/service_worker/embedded_worker.mojom.h" | |
| 19 #include "content/common/service_worker/embedded_worker_messages.h" | 21 #include "content/common/service_worker/embedded_worker_messages.h" |
| 20 #include "content/public/common/child_process_host.h" | 22 #include "content/public/common/child_process_host.h" |
| 23 #include "content/public/common/content_switches.h" | |
| 21 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 25 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 28 |
| 29 using ::testing::Eq; | |
| 30 using ::testing::Field; | |
| 31 using ::testing::Pointee; | |
| 32 | |
| 25 namespace content { | 33 namespace content { |
| 26 | 34 |
| 27 namespace { | 35 namespace { |
| 28 | 36 |
| 29 void SaveStatusAndCall(ServiceWorkerStatusCode* out, | 37 void SaveStatusAndCall(ServiceWorkerStatusCode* out, |
| 30 const base::Closure& callback, | 38 const base::Closure& callback, |
| 31 ServiceWorkerStatusCode status) { | 39 ServiceWorkerStatusCode status) { |
| 32 *out = status; | 40 *out = status; |
| 33 callback.Run(); | 41 callback.Run(); |
| 34 } | 42 } |
| 35 | 43 |
| 36 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> | 44 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> |
| 37 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { | 45 CreateStartParams(int version_id, const GURL& scope, const GURL& script_url) { |
| 38 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 46 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 39 new EmbeddedWorkerMsg_StartWorker_Params); | 47 new EmbeddedWorkerMsg_StartWorker_Params); |
| 40 params->service_worker_version_id = version_id; | 48 params->service_worker_version_id = version_id; |
| 41 params->scope = scope; | 49 params->scope = scope; |
| 42 params->script_url = script_url; | 50 params->script_url = script_url; |
| 43 params->pause_after_download = false; | 51 params->pause_after_download = false; |
| 44 return params; | 52 return params; |
| 45 } | 53 } |
| 46 | 54 |
| 55 bool IsMojoForServiceWorkerEnabled() { | |
| 56 return base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 57 switches::kMojoServiceWorker); | |
| 58 } | |
| 59 | |
| 47 } // namespace | 60 } // namespace |
| 48 | 61 |
| 49 class EmbeddedWorkerInstanceTest : public testing::Test, | 62 class EmbeddedWorkerInstanceTest : public testing::Test, |
| 50 public EmbeddedWorkerInstance::Listener { | 63 public EmbeddedWorkerInstance::Listener { |
| 51 protected: | 64 protected: |
| 52 EmbeddedWorkerInstanceTest() | 65 EmbeddedWorkerInstanceTest() |
| 53 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 66 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 54 | 67 |
| 55 enum EventType { | 68 enum EventType { |
| 56 PROCESS_ALLOCATED, | 69 PROCESS_ALLOCATED, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 128 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 116 | 129 |
| 117 TestBrowserThreadBundle thread_bundle_; | 130 TestBrowserThreadBundle thread_bundle_; |
| 118 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 131 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 119 std::vector<EventLog> events_; | 132 std::vector<EventLog> events_; |
| 120 | 133 |
| 121 private: | 134 private: |
| 122 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 135 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 123 }; | 136 }; |
| 124 | 137 |
| 138 class MojoEmbeddedWorkerInstanceTest : public EmbeddedWorkerInstanceTest { | |
| 139 protected: | |
| 140 void SetUp() override { | |
| 141 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 142 switches::kMojoServiceWorker); | |
| 143 EmbeddedWorkerInstanceTest::SetUp(); | |
| 144 } | |
| 145 | |
| 146 std::vector<std::unique_ptr< | |
| 147 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient>>* | |
| 148 mock_instance_clients() { | |
| 149 return helper_->mock_instance_clients(); | |
| 150 } | |
| 151 }; | |
| 152 | |
| 125 // A helper to simulate the start worker sequence is stalled in a worker | 153 // A helper to simulate the start worker sequence is stalled in a worker |
| 126 // process. | 154 // process. |
| 127 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { | 155 class StalledInStartWorkerHelper : public EmbeddedWorkerTestHelper { |
| 128 public: | 156 public: |
| 129 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 157 StalledInStartWorkerHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 130 ~StalledInStartWorkerHelper() override{}; | 158 ~StalledInStartWorkerHelper() override{}; |
| 131 | 159 |
| 132 void OnStartWorker(int embedded_worker_id, | 160 void OnStartWorker(int embedded_worker_id, |
| 133 int64_t service_worker_version_id, | 161 int64_t service_worker_version_id, |
| 134 const GURL& scope, | 162 const GURL& scope, |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 // EmbeddedWorkerTestHelper. | 228 // EmbeddedWorkerTestHelper. |
| 201 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 229 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 202 | 230 |
| 203 // Verify that we've sent two messages to start and terminate the worker. | 231 // Verify that we've sent two messages to start and terminate the worker. |
| 204 ASSERT_TRUE( | 232 ASSERT_TRUE( |
| 205 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); | 233 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
| 206 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 234 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
| 207 EmbeddedWorkerMsg_StopWorker::ID)); | 235 EmbeddedWorkerMsg_StopWorker::ID)); |
| 208 } | 236 } |
| 209 | 237 |
| 238 TEST_F(MojoEmbeddedWorkerInstanceTest, StartAndStop) { | |
| 239 using mojom::EmbeddedWorkerStartWorkerParams; | |
| 240 | |
| 241 ASSERT_TRUE(IsMojoForServiceWorkerEnabled()); | |
| 242 | |
| 243 std::unique_ptr<EmbeddedWorkerInstance> worker = | |
| 244 embedded_worker_registry()->CreateWorker(); | |
| 245 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | |
| 246 | |
| 247 const int64_t service_worker_version_id = 55L; | |
| 248 const GURL pattern("http://example.com/"); | |
| 249 const GURL url("http://example.com/worker.js"); | |
| 250 | |
| 251 // Simulate adding one process to the pattern. | |
| 252 helper_->SimulateAddProcessToPattern(pattern, | |
| 253 helper_->mock_render_process_id()); | |
| 254 | |
| 255 // Check if StartWorker will be called via mojo IPC. | |
| 256 helper_->PrepareMockInstanceClients(1); | |
| 257 ASSERT_EQ(mock_instance_clients()->size(), 1UL); | |
| 258 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient* instance_client = | |
| 259 mock_instance_clients()->at(0).get(); | |
| 260 EXPECT_CALL( | |
| 261 *instance_client, | |
| 262 MockStartWorker(AllOf( | |
| 263 Pointee( | |
| 264 Field(&EmbeddedWorkerStartWorkerParams::service_worker_version_id, | |
| 265 Eq(service_worker_version_id))), | |
| 266 Pointee(Field(&EmbeddedWorkerStartWorkerParams::scope, Eq(pattern))), | |
| 267 Pointee( | |
| 268 Field(&EmbeddedWorkerStartWorkerParams::script_url, Eq(url)))))) | |
| 269 .Times(1); | |
|
falken
2016/09/07 05:01:42
Sorry I got to this late. gmock is fairly discoura
shimazu
2016/09/12 06:28:19
Oops, I didn't know that...
The only use case of g
| |
| 270 | |
| 271 // Start should succeed. | |
| 272 ServiceWorkerStatusCode status; | |
| 273 base::RunLoop run_loop; | |
| 274 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params = | |
| 275 CreateStartParams(service_worker_version_id, pattern, url); | |
| 276 worker->Start(std::move(params), base::Bind(&SaveStatusAndCall, &status, | |
| 277 run_loop.QuitClosure())); | |
| 278 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, worker->status()); | |
| 279 run_loop.Run(); | |
| 280 EXPECT_EQ(SERVICE_WORKER_OK, status); | |
| 281 | |
| 282 // The 'WorkerStarted' message should have been sent by | |
| 283 // EmbeddedWorkerTestHelper. | |
| 284 EXPECT_EQ(EmbeddedWorkerStatus::RUNNING, worker->status()); | |
| 285 EXPECT_EQ(helper_->mock_render_process_id(), worker->process_id()); | |
| 286 | |
| 287 // Stop the worker. | |
| 288 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | |
| 289 EXPECT_EQ(EmbeddedWorkerStatus::STOPPING, worker->status()); | |
| 290 base::RunLoop().RunUntilIdle(); | |
| 291 | |
| 292 // The 'WorkerStopped' message should have been sent by | |
| 293 // EmbeddedWorkerTestHelper. | |
| 294 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | |
| 295 | |
| 296 // Ckeck if StopWorker has been sent via chromium IPC. | |
| 297 ASSERT_TRUE( | |
| 298 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StopWorker::ID)); | |
| 299 } | |
| 300 | |
| 210 // Test that a worker that failed twice will use a new render process | 301 // Test that a worker that failed twice will use a new render process |
| 211 // on the next attempt. | 302 // on the next attempt. |
| 212 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { | 303 TEST_F(EmbeddedWorkerInstanceTest, ForceNewProcess) { |
| 213 std::unique_ptr<EmbeddedWorkerInstance> worker = | 304 std::unique_ptr<EmbeddedWorkerInstance> worker = |
| 214 embedded_worker_registry()->CreateWorker(); | 305 embedded_worker_registry()->CreateWorker(); |
| 215 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); | 306 EXPECT_EQ(EmbeddedWorkerStatus::STOPPED, worker->status()); |
| 216 | 307 |
| 217 const int64_t service_worker_version_id = 55L; | 308 const int64_t service_worker_version_id = 55L; |
| 218 const GURL pattern("http://example.com/"); | 309 const GURL pattern("http://example.com/"); |
| 219 const GURL url("http://example.com/worker.js"); | 310 const GURL url("http://example.com/worker.js"); |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 643 | 734 |
| 644 // The callback should have run, and we should have got an OnStopped message. | 735 // The callback should have run, and we should have got an OnStopped message. |
| 645 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); | 736 EXPECT_EQ(SERVICE_WORKER_ERROR_IPC_FAILED, status); |
| 646 ASSERT_EQ(2u, events_.size()); | 737 ASSERT_EQ(2u, events_.size()); |
| 647 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); | 738 EXPECT_EQ(PROCESS_ALLOCATED, events_[0].type); |
| 648 EXPECT_EQ(STOPPED, events_[1].type); | 739 EXPECT_EQ(STOPPED, events_[1].type); |
| 649 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); | 740 EXPECT_EQ(EmbeddedWorkerStatus::STARTING, events_[1].status); |
| 650 } | 741 } |
| 651 | 742 |
| 652 } // namespace content | 743 } // namespace content |
| OLD | NEW |