OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/test/base/mojo_test_connector.h" | 5 #include "chrome/test/base/mojo_test_connector.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 | 31 |
32 using service_manager::mojom::Service; | 32 using service_manager::mojom::Service; |
33 using service_manager::mojom::ServicePtr; | 33 using service_manager::mojom::ServicePtr; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kTestRunnerName[] = "exe:mash_browser_tests"; | 37 const char kTestRunnerName[] = "exe:mash_browser_tests"; |
38 const char kTestName[] = "service:content_browser"; | 38 const char kTestName[] = "service:content_browser"; |
39 | 39 |
40 // BackgroundTestState maintains all the state necessary to bind the test to | 40 // BackgroundTestState maintains all the state necessary to bind the test to |
41 // mojo. This class is only used on the thread created by BackgroundShell. | 41 // mojo. This class is only used on the thread created by |
| 42 // BackgroundServiceManager. |
42 class BackgroundTestState { | 43 class BackgroundTestState { |
43 public: | 44 public: |
44 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} | 45 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} |
45 ~BackgroundTestState() {} | 46 ~BackgroundTestState() {} |
46 | 47 |
47 // Prepares the command line and other setup for connecting the test to mojo. | 48 // Prepares the command line and other setup for connecting the test to mojo. |
48 // Must be paired with a call to ChildProcessLaunched(). | 49 // Must be paired with a call to ChildProcessLaunched(). |
49 void Connect(base::CommandLine* command_line, | 50 void Connect(base::CommandLine* command_line, |
50 service_manager::ServiceManager* service_manager, | 51 service_manager::ServiceManager* service_manager, |
51 base::TestLauncher::LaunchOptions* test_launch_options) { | 52 base::TestLauncher::LaunchOptions* test_launch_options) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 115 |
115 // Called used destroy BackgroundTestState on the background thread. | 116 // Called used destroy BackgroundTestState on the background thread. |
116 void DestroyBackgroundStateOnBackgroundThread( | 117 void DestroyBackgroundStateOnBackgroundThread( |
117 std::unique_ptr<BackgroundTestState> state, | 118 std::unique_ptr<BackgroundTestState> state, |
118 service_manager::ServiceManager* service_manager) {} | 119 service_manager::ServiceManager* service_manager) {} |
119 | 120 |
120 // State created per test. Manages creation of the corresponding | 121 // State created per test. Manages creation of the corresponding |
121 // BackgroundTestState and making sure processing runs on the right threads. | 122 // BackgroundTestState and making sure processing runs on the right threads. |
122 class MojoTestState : public content::TestState { | 123 class MojoTestState : public content::TestState { |
123 public: | 124 public: |
124 explicit MojoTestState(service_manager::BackgroundShell* background_shell) | 125 explicit MojoTestState( |
125 : background_shell_(background_shell) {} | 126 service_manager::BackgroundServiceManager* background_service_manager) |
| 127 : background_service_manager_(background_service_manager) {} |
126 | 128 |
127 ~MojoTestState() override { | 129 ~MojoTestState() override { |
128 DCHECK(background_state_); | 130 DCHECK(background_state_); |
129 // BackgroundState needs to be destroyed on the background thread. We're | 131 // BackgroundState needs to be destroyed on the background thread. We're |
130 // guaranteed |background_shell_| has been created by the time we | 132 // guaranteed |background_service_manager_| has been created by the time we |
131 // reach | 133 // reach |
132 // here as Init() blocks until |background_shell_| has been | 134 // here as Init() blocks until |background_service_manager_| has been |
133 // created. | 135 // created. |
134 background_shell_->ExecuteOnServiceManagerThread( | 136 background_service_manager_->ExecuteOnServiceManagerThread( |
135 base::Bind(&DestroyBackgroundStateOnBackgroundThread, | 137 base::Bind(&DestroyBackgroundStateOnBackgroundThread, |
136 base::Passed(&background_state_))); | 138 base::Passed(&background_state_))); |
137 } | 139 } |
138 | 140 |
139 void Init(base::CommandLine* command_line, | 141 void Init(base::CommandLine* command_line, |
140 base::TestLauncher::LaunchOptions* test_launch_options) { | 142 base::TestLauncher::LaunchOptions* test_launch_options) { |
141 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | 143 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, |
142 base::WaitableEvent::InitialState::NOT_SIGNALED); | 144 base::WaitableEvent::InitialState::NOT_SIGNALED); |
143 background_shell_->ExecuteOnServiceManagerThread(base::Bind( | 145 background_service_manager_->ExecuteOnServiceManagerThread(base::Bind( |
144 &MojoTestState::BindOnBackgroundThread, base::Unretained(this), &signal, | 146 &MojoTestState::BindOnBackgroundThread, base::Unretained(this), &signal, |
145 command_line, test_launch_options)); | 147 command_line, test_launch_options)); |
146 signal.Wait(); | 148 signal.Wait(); |
147 } | 149 } |
148 | 150 |
149 private: | 151 private: |
150 // content::TestState: | 152 // content::TestState: |
151 void ChildProcessLaunched(base::ProcessHandle handle, | 153 void ChildProcessLaunched(base::ProcessHandle handle, |
152 base::ProcessId pid) override { | 154 base::ProcessId pid) override { |
153 // This is called on a random thread. We need to ensure BackgroundTestState | 155 // This is called on a random thread. We need to ensure BackgroundTestState |
154 // is only called on the background thread, and we wait for | 156 // is only called on the background thread, and we wait for |
155 // ChildProcessLaunchedOnBackgroundThread() to be run before continuing so | 157 // ChildProcessLaunchedOnBackgroundThread() to be run before continuing so |
156 // that |handle| is still valid. | 158 // that |handle| is still valid. |
157 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, | 159 base::WaitableEvent signal(base::WaitableEvent::ResetPolicy::MANUAL, |
158 base::WaitableEvent::InitialState::NOT_SIGNALED); | 160 base::WaitableEvent::InitialState::NOT_SIGNALED); |
159 background_shell_->ExecuteOnServiceManagerThread( | 161 background_service_manager_->ExecuteOnServiceManagerThread( |
160 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread, | 162 base::Bind(&MojoTestState::ChildProcessLaunchedOnBackgroundThread, |
161 base::Unretained(this), handle, pid, &signal)); | 163 base::Unretained(this), handle, pid, &signal)); |
162 signal.Wait(); | 164 signal.Wait(); |
163 } | 165 } |
164 | 166 |
165 void ChildProcessLaunchedOnBackgroundThread( | 167 void ChildProcessLaunchedOnBackgroundThread( |
166 base::ProcessHandle handle, | 168 base::ProcessHandle handle, |
167 base::ProcessId pid, | 169 base::ProcessId pid, |
168 base::WaitableEvent* signal, | 170 base::WaitableEvent* signal, |
169 service_manager::ServiceManager* service_manager) { | 171 service_manager::ServiceManager* service_manager) { |
170 background_state_->ChildProcessLaunched(handle, pid); | 172 background_state_->ChildProcessLaunched(handle, pid); |
171 signal->Signal(); | 173 signal->Signal(); |
172 } | 174 } |
173 | 175 |
174 void BindOnBackgroundThread( | 176 void BindOnBackgroundThread( |
175 base::WaitableEvent* signal, | 177 base::WaitableEvent* signal, |
176 base::CommandLine* command_line, | 178 base::CommandLine* command_line, |
177 base::TestLauncher::LaunchOptions* test_launch_options, | 179 base::TestLauncher::LaunchOptions* test_launch_options, |
178 service_manager::ServiceManager* service_manager) { | 180 service_manager::ServiceManager* service_manager) { |
179 background_state_.reset(new BackgroundTestState); | 181 background_state_.reset(new BackgroundTestState); |
180 background_state_->Connect(command_line, service_manager, | 182 background_state_->Connect(command_line, service_manager, |
181 test_launch_options); | 183 test_launch_options); |
182 signal->Signal(); | 184 signal->Signal(); |
183 } | 185 } |
184 | 186 |
185 service_manager::BackgroundShell* background_shell_; | 187 service_manager::BackgroundServiceManager* background_service_manager_; |
186 std::unique_ptr<BackgroundTestState> background_state_; | 188 std::unique_ptr<BackgroundTestState> background_state_; |
187 | 189 |
188 DISALLOW_COPY_AND_ASSIGN(MojoTestState); | 190 DISALLOW_COPY_AND_ASSIGN(MojoTestState); |
189 }; | 191 }; |
190 | 192 |
191 // The name in the manifest results in getting exe:mash_browser_tests used, | 193 // The name in the manifest results in getting exe:mash_browser_tests used, |
192 // remap that to browser_tests. | 194 // remap that to browser_tests. |
193 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { | 195 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { |
194 base::FilePath exe_path(command_line->GetProgram()); | 196 base::FilePath exe_path(command_line->GetProgram()); |
195 #if defined(OS_WIN) | 197 #if defined(OS_WIN) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 // static | 238 // static |
237 const char MojoTestConnector::kTestSwitch[] = "is_test"; | 239 const char MojoTestConnector::kTestSwitch[] = "is_test"; |
238 // static | 240 // static |
239 const char MojoTestConnector::kMashApp[] = "mash-app"; | 241 const char MojoTestConnector::kMashApp[] = "mash-app"; |
240 | 242 |
241 MojoTestConnector::MojoTestConnector() {} | 243 MojoTestConnector::MojoTestConnector() {} |
242 | 244 |
243 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { | 245 service_manager::mojom::ServiceRequest MojoTestConnector::Init() { |
244 native_runner_delegate_ = base::MakeUnique<NativeRunnerDelegateImpl>(); | 246 native_runner_delegate_ = base::MakeUnique<NativeRunnerDelegateImpl>(); |
245 | 247 |
246 std::unique_ptr<service_manager::BackgroundShell::InitParams> init_params( | 248 std::unique_ptr<service_manager::BackgroundServiceManager::InitParams> |
247 new service_manager::BackgroundShell::InitParams); | 249 init_params(new service_manager::BackgroundServiceManager::InitParams); |
248 // When running in single_process mode chrome initializes the edk. | 250 // When running in single_process mode chrome initializes the edk. |
249 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( | 251 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( |
250 content::kSingleProcessTestsFlag); | 252 content::kSingleProcessTestsFlag); |
251 init_params->native_runner_delegate = native_runner_delegate_.get(); | 253 init_params->native_runner_delegate = native_runner_delegate_.get(); |
252 background_shell_.Init(std::move(init_params)); | 254 background_service_manager_.Init(std::move(init_params)); |
253 return background_shell_.CreateServiceRequest(kTestRunnerName); | 255 return background_service_manager_.CreateServiceRequest(kTestRunnerName); |
254 } | 256 } |
255 | 257 |
256 MojoTestConnector::~MojoTestConnector() {} | 258 MojoTestConnector::~MojoTestConnector() {} |
257 | 259 |
258 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 260 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
259 base::CommandLine* command_line, | 261 base::CommandLine* command_line, |
260 base::TestLauncher::LaunchOptions* test_launch_options) { | 262 base::TestLauncher::LaunchOptions* test_launch_options) { |
261 std::unique_ptr<MojoTestState> test_state( | 263 std::unique_ptr<MojoTestState> test_state( |
262 new MojoTestState(&background_shell_)); | 264 new MojoTestState(&background_service_manager_)); |
263 test_state->Init(command_line, test_launch_options); | 265 test_state->Init(command_line, test_launch_options); |
264 return std::move(test_state); | 266 return std::move(test_state); |
265 } | 267 } |
OLD | NEW |