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 "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/browser/service_worker/service_worker_context_wrapper.h" |
| 13 #include "content/browser/site_instance_impl.h" | |
| 13 #include "content/common/service_worker/embedded_worker_messages.h" | 14 #include "content/common/service_worker/embedded_worker_messages.h" |
| 15 #include "content/public/browser/storage_partition.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/test/test_browser_context.h" | |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "content/public/test/test_renderer_host.h" | |
| 20 #include "content/public/test/web_contents_tester.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 22 |
| 17 namespace content { | 23 namespace content { |
| 18 | 24 |
| 19 static const int kRenderProcessId = 11; | 25 static const int kRenderProcessId = 11; |
| 20 | 26 |
| 21 class EmbeddedWorkerInstanceTest : public testing::Test { | 27 class EmbeddedWorkerInstanceTest : public testing::Test { |
| 22 protected: | 28 protected: |
| 23 EmbeddedWorkerInstanceTest() | 29 EmbeddedWorkerInstanceTest() |
| 24 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 30 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 // But destroy it before it gets a chance to complete. | 128 // But destroy it before it gets a chance to complete. |
| 123 worker.reset(); | 129 worker.reset(); |
| 124 run_loop.Run(); | 130 run_loop.Run(); |
| 125 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); | 131 EXPECT_EQ(SERVICE_WORKER_ERROR_ABORT, status); |
| 126 | 132 |
| 127 // Verify that we didn't send the message to start the worker. | 133 // Verify that we didn't send the message to start the worker. |
| 128 ASSERT_FALSE( | 134 ASSERT_FALSE( |
| 129 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); | 135 ipc_sink()->GetUniqueMessageMatching(EmbeddedWorkerMsg_StartWorker::ID)); |
| 130 } | 136 } |
| 131 | 137 |
| 132 TEST_F(EmbeddedWorkerInstanceTest, ChooseProcess) { | 138 static scoped_ptr<WebContents> CreateNavigatedWebContents( |
| 133 scoped_ptr<EmbeddedWorkerInstance> worker = | 139 BrowserContext* browser_context, |
| 134 embedded_worker_registry()->CreateWorker(); | 140 const GURL& url) { |
| 135 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | 141 scoped_ptr<WebContents> result(WebContentsTester::CreateTestWebContents( |
| 142 browser_context, SiteInstance::Create(browser_context))); | |
| 143 WebContentsTester::For(result.get())->NavigateAndCommit(url); | |
| 144 return result.Pass(); | |
| 145 } | |
| 136 | 146 |
| 137 // Simulate adding processes to the worker. | 147 TEST(EmbeddedWorkerInstanceTestWithMockProcesses, ChooseProcess) { |
| 138 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | 148 TestBrowserThreadBundle thread_bundle(TestBrowserThreadBundle::IO_MAINLOOP); |
| 139 const int embedded_worker_id = worker->embedded_worker_id(); | 149 IPC::TestSink test_sink; |
| 140 helper_->SimulateAddProcessToWorker(embedded_worker_id, 1); | 150 TestBrowserContext browser_context; |
| 141 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | 151 RenderViewHostTestEnabler rvh_test_enabler; |
| 142 helper_->SimulateAddProcessToWorker(embedded_worker_id, 2); | |
| 143 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
| 144 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
| 145 helper_->SimulateAddProcessToWorker(embedded_worker_id, 3); | |
| 146 | 152 |
| 147 // Process 3 has the biggest # of references and it should be chosen. | 153 { |
| 148 ServiceWorkerStatusCode status; | 154 scoped_ptr<WebContents> web_contents1(CreateNavigatedWebContents( |
|
Jeffrey Yasskin
2014/05/21 02:01:11
Hard-coding the process ID for tests in the Proces
| |
| 149 base::RunLoop run_loop; | 155 &browser_context, GURL("https://example1.com/"))); |
| 150 worker->Start( | 156 scoped_ptr<WebContents> web_contents2(CreateNavigatedWebContents( |
| 151 1L, | 157 &browser_context, GURL("https://example2.com/"))); |
| 152 GURL("http://example.com/*"), | 158 scoped_ptr<WebContents> web_contents3(CreateNavigatedWebContents( |
| 153 GURL("http://example.com/worker.js"), | 159 &browser_context, GURL("https://example3.com/"))); |
| 154 std::vector<int>(), | 160 const int pid1 = web_contents1->GetRenderProcessHost()->GetID(); |
| 155 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | 161 const int pid2 = web_contents2->GetRenderProcessHost()->GetID(); |
| 156 run_loop.Run(); | 162 const int pid3 = web_contents3->GetRenderProcessHost()->GetID(); |
| 157 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | 163 ASSERT_NE(pid1, pid2); |
| 158 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | 164 ASSERT_NE(pid1, pid3); |
| 159 EXPECT_EQ(3, worker->process_id()); | 165 ASSERT_NE(pid2, pid3); |
| 160 | 166 |
| 161 // Wait until started message is sent back. | 167 scoped_refptr<ServiceWorkerContextWrapper> sw_context_wrapper = |
| 168 static_cast<ServiceWorkerContextWrapper*>( | |
| 169 BrowserContext::GetStoragePartition( | |
| 170 &browser_context, web_contents1->GetSiteInstance()) | |
| 171 ->GetServiceWorkerContext()); | |
| 172 | |
| 173 scoped_ptr<EmbeddedWorkerInstance> worker = sw_context_wrapper->context() | |
| 174 ->embedded_worker_registry() | |
| 175 ->CreateWorker(); | |
| 176 EXPECT_EQ(EmbeddedWorkerInstance::STOPPED, worker->status()); | |
| 177 | |
| 178 // Simulate adding processes to the worker. | |
| 179 // Process 1 has 1 ref, 2 has 2 refs and 3 has 3 refs. | |
| 180 worker->AddProcessReference(pid1); | |
| 181 worker->AddProcessReference(pid2); | |
| 182 worker->AddProcessReference(pid2); | |
| 183 worker->AddProcessReference(pid3); | |
| 184 worker->AddProcessReference(pid3); | |
| 185 worker->AddProcessReference(pid3); | |
| 186 sw_context_wrapper->context() | |
| 187 ->embedded_worker_registry() | |
| 188 ->AddChildProcessSender(pid3, &test_sink); | |
| 189 | |
| 190 // Process 3 has the biggest # of references and it should be chosen. | |
| 191 ServiceWorkerStatusCode status; | |
| 192 base::RunLoop run_loop; | |
| 193 worker->Start( | |
| 194 1L, | |
| 195 GURL("http://example.com/*"), | |
| 196 GURL("http://example.com/worker.js"), | |
| 197 std::vector<int>(), | |
| 198 base::Bind(&SaveStatusAndCall, &status, run_loop.QuitClosure())); | |
| 199 run_loop.Run(); | |
| 200 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status); | |
| 201 EXPECT_EQ(EmbeddedWorkerInstance::STARTING, worker->status()); | |
| 202 EXPECT_EQ(pid3, worker->process_id()); | |
| 203 } | |
| 204 | |
| 162 base::RunLoop().RunUntilIdle(); | 205 base::RunLoop().RunUntilIdle(); |
| 163 EXPECT_EQ(EmbeddedWorkerInstance::RUNNING, worker->status()); | |
| 164 } | 206 } |
| 165 | 207 |
| 166 } // namespace content | 208 } // namespace content |
| OLD | NEW |