| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
| 8 #include "content/browser/service_worker/embedded_worker_instance.h" | 8 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 9 #include "content/browser/service_worker/embedded_worker_registry.h" | 9 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 10 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| 11 #include "content/browser/service_worker/service_worker_context_core.h" | 11 #include "content/browser/service_worker/service_worker_context_core.h" |
| 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 12 #include "content/common/service_worker/embedded_worker_messages.h" | 13 #include "content/common/service_worker/embedded_worker_messages.h" |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 | 18 |
| 18 static const int kRenderProcessId = 11; | 19 static const int kRenderProcessId = 11; |
| 19 | 20 |
| 20 class EmbeddedWorkerInstanceTest : public testing::Test { | 21 class EmbeddedWorkerInstanceTest : public testing::Test { |
| 21 protected: | 22 protected: |
| 22 EmbeddedWorkerInstanceTest() | 23 EmbeddedWorkerInstanceTest() |
| 23 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 24 | 25 |
| 25 virtual void SetUp() OVERRIDE { | 26 virtual void SetUp() OVERRIDE { |
| 26 context_.reset(new ServiceWorkerContextCore(base::FilePath(), NULL, NULL)); | 27 helper_.reset(new EmbeddedWorkerTestHelper(kRenderProcessId)); |
| 27 helper_.reset(new EmbeddedWorkerTestHelper( | |
| 28 context_.get(), kRenderProcessId)); | |
| 29 } | 28 } |
| 30 | 29 |
| 31 virtual void TearDown() OVERRIDE { | 30 virtual void TearDown() OVERRIDE { |
| 32 helper_.reset(); | 31 helper_.reset(); |
| 33 context_.reset(); | |
| 34 } | 32 } |
| 35 | 33 |
| 34 ServiceWorkerContextCore* context() { return helper_->context(); } |
| 35 |
| 36 EmbeddedWorkerRegistry* embedded_worker_registry() { | 36 EmbeddedWorkerRegistry* embedded_worker_registry() { |
| 37 DCHECK(context_); | 37 DCHECK(context()); |
| 38 return context_->embedded_worker_registry(); | 38 return context()->embedded_worker_registry(); |
| 39 } | 39 } |
| 40 | 40 |
| 41 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } | 41 IPC::TestSink* ipc_sink() { return helper_->ipc_sink(); } |
| 42 | 42 |
| 43 TestBrowserThreadBundle thread_bundle_; | 43 TestBrowserThreadBundle thread_bundle_; |
| 44 scoped_ptr<ServiceWorkerContextCore> context_; | |
| 45 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 44 scoped_ptr<EmbeddedWorkerTestHelper> helper_; |
| 46 | 45 |
| 47 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); | 46 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceTest); |
| 48 }; | 47 }; |
| 49 | 48 |
| 49 static void SaveStatusAndCall(ServiceWorkerStatusCode* out, |
| 50 const base::Closure& callback, |
| 51 ServiceWorkerStatusCode status) { |
| 52 *out = status; |
| 53 callback.Run(); |
| 54 } |
| 55 |
| 50 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { | 56 TEST_F(EmbeddedWorkerInstanceTest, StartAndStop) { |
| 51 scoped_ptr<EmbeddedWorkerInstance> worker = | 57 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 52 embedded_worker_registry()->CreateWorker(); | 58 embedded_worker_registry()->CreateWorker(); |
| 53 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 59 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 54 | 60 |
| 55 const int embedded_worker_id = worker->embedded_worker_id(); | 61 const int embedded_worker_id = worker->embedded_worker_id(); |
| 56 const int64 service_worker_version_id = 55L; | 62 const int64 service_worker_version_id = 55L; |
| 57 const GURL scope("http://example.com/*"); | 63 const GURL scope("http://example.com/*"); |
| 58 const GURL url("http://example.com/worker.js"); | 64 const GURL url("http://example.com/worker.js"); |
| 59 | 65 |
| 60 // This fails as we have no available process yet. | |
| 61 EXPECT_EQ(SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND, | |
| 62 worker->Start(service_worker_version_id, scope, url)); | |
| 63 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | |
| 64 | |
| 65 // Simulate adding one process to the worker. | 66 // Simulate adding one process to the worker. |
| 66 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); | 67 helper_->SimulateAddProcessToWorker(embedded_worker_id, kRenderProcessId); |
| 67 | 68 |
| 68 // Start should succeed. | 69 // Start should succeed. |
| 69 EXPECT_EQ(SERVICE_WORKER_OK, | 70 ServiceWorkerStatusCode status; |
| 70 worker->Start(service_worker_version_id, scope, url)); | 71 base::RunLoop run_loop; |
| 72 worker->Start( |
| 73 service_worker_version_id, |
| 74 scope, |
| 75 url, |
| 76 std::vector<int>(), |
| 77 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 78 run_loop.Run(); |
| 79 EXPECT_EQ(SERVICE_WORKER_OK, status); |
| 71 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 80 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 72 base::RunLoop().RunUntilIdle(); | 81 base::RunLoop().RunUntilIdle(); |
| 73 | 82 |
| 74 // Worker started message should be notified (by EmbeddedWorkerTestHelper). | 83 // Worker started message should be notified (by EmbeddedWorkerTestHelper). |
| 75 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 84 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 76 EXPECT_EQ(kRenderProcessId, worker->process_id()); | 85 EXPECT_EQ(kRenderProcessId, worker->process_id()); |
| 77 | 86 |
| 78 // Stop the worker. | 87 // Stop the worker. |
| 79 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); | 88 EXPECT_EQ(SERVICE_WORKER_OK, worker->Stop()); |
| 80 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); | 89 EXPECT_EQ(EmbeddedWorkerInstance::STOPPING, worker->status()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 99 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | 108 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. |
| 100 const int embedded_worker_id = worker->embedded_worker_id(); | 109 const int embedded_worker_id = worker->embedded_worker_id(); |
| 101 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); | 110 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); |
| 102 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 111 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
| 103 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 112 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
| 104 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 113 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 105 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 114 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 106 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 115 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
| 107 | 116 |
| 108 // Process 3 has the biggest # of references and it should be chosen. | 117 // Process 3 has the biggest # of references and it should be chosen. |
| 109 EXPECT_EQ(SERVICE_WORKER_OK, | 118 ServiceWorkerStatusCode status; |
| 110 worker->Start(1L, | 119 base::RunLoop run_loop; |
| 111 GURL("http://example.com/*"), | 120 worker->Start( |
| 112 GURL("http://example.com/worker.js"))); | 121 1L, |
| 122 GURL("http://example.com/*"), |
| 123 GURL("http://example.com/worker.js"), |
| 124 std::vector<int>(), |
| 125 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 126 run_loop.Run(); |
| 127 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
| 113 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 128 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
| 114 EXPECT_EQ(3, worker->process_id()); | 129 EXPECT_EQ(3, worker->process_id()); |
| 115 | 130 |
| 116 // Wait until started message is sent back. | 131 // Wait until started message is sent back. |
| 117 base::RunLoop().RunUntilIdle(); | 132 base::RunLoop().RunUntilIdle(); |
| 118 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 133 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
| 119 } | 134 } |
| 120 | 135 |
| 121 } // namespace content | 136 } // namespace content |
| OLD | NEW |