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()); |
81 base::RunLoop().RunUntilIdle(); | 90 base::RunLoop().RunUntilIdle(); |
82 | 91 |
83 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper). | 92 // Worker stopped message should be notified (by EmbeddedWorkerTestHelper). |
84 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 93 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
85 | 94 |
86 // Verify that we've sent two messages to start and terminate the worker. | 95 // Verify that we've sent two messages to start and terminate the worker. |
87 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 96 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
88 EmbeddedWorkerMsg_StartWorker::ID)); | 97 EmbeddedWorkerMsg_StartWorker::ID)); |
89 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( | 98 ASSERT_TRUE(ipc_sink()->GetUniqueMessageMatching( |
90 EmbeddedWorkerMsg_StopWorker::ID)); | 99 EmbeddedWorkerMsg_StopWorker::ID)); |
91 } | 100 } |
92 | 101 |
| 102 TEST_F(EmbeddedWorkerInstanceTest, InstanceDestroyedBeforeStartFinishes) { |
| 103 scoped_ptr<EmbeddedWorkerInstance> worker = |
| 104 embedded_worker_registry()->CreateWorker(); |
| 105 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
| 106 |
| 107 const int64 service_worker_version_id = 55L; |
| 108 const GURL scope("http://example.com/*"); |
| 109 const GURL url("http://example.com/worker.js"); |
| 110 |
| 111 ServiceWorkerStatusCode status; |
| 112 base::RunLoop run_loop; |
| 113 // Begin starting the worker. |
| 114 std::vector<int> available_process; |
| 115 available_process.push_back(kRenderProcessId); |
| 116 worker->Start( |
| 117 service_worker_version_id, |
| 118 scope, |
| 119 url, |
| 120 available_process, |
| 121 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 122 // But destroy it before it gets a chance to complete. |
| 123 worker.reset(); |
| 124 run_loop.Run(); |
| 125 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
| 126 |
| 127 // Verify that we didn't send the message to start the worker. |
| 128 ASSERT_FALSE( |
| 129 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
| 130 } |
| 131 |
93 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { | 132 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { |
94 scoped_ptr<EmbeddedWorkerInstance> worker = | 133 scoped_ptr<EmbeddedWorkerInstance> worker = |
95 embedded_worker_registry()->CreateWorker(); | 134 embedded_worker_registry()->CreateWorker(); |
96 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 135 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); |
97 | 136 |
98 // Simulate adding processes to the worker. | 137 // Simulate adding processes to the worker. |
99 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | 138 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. |
100 const int embedded_worker_id = worker->embedded_worker_id(); | 139 const int embedded_worker_id = worker->embedded_worker_id(); |
101 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); | 140 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); |
102 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 141 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
103 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 142 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); |
104 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 143 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
105 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 144 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
106 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | 145 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); |
107 | 146 |
108 // Process 3 has the biggest # of references and it should be chosen. | 147 // Process 3 has the biggest # of references and it should be chosen. |
109 EXPECT_EQ(SERVICE_WORKER_OK, | 148 ServiceWorkerStatusCode status; |
110 worker->Start(1L, | 149 base::RunLoop run_loop; |
111 GURL("http://example.com/*"), | 150 worker->Start( |
112 GURL("http://example.com/worker.js"))); | 151 1L, |
| 152 GURL("http://example.com/*"), |
| 153 GURL("http://example.com/worker.js"), |
| 154 std::vector<int>(), |
| 155 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); |
| 156 run_loop.Run(); |
| 157 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); |
113 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 158 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); |
114 EXPECT_EQ(3, worker->process_id()); | 159 EXPECT_EQ(3, worker->process_id()); |
115 | 160 |
116 // Wait until started message is sent back. | 161 // Wait until started message is sent back. |
117 base::RunLoop().RunUntilIdle(); | 162 base::RunLoop().RunUntilIdle(); |
118 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | 163 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); |
119 } | 164 } |
120 | 165 |
121 } // namespace content | 166 } // namespace content |
OLD | NEW |