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 16 matching lines...) Expand all Loading... |
27 #include "services/shell/runner/common/client_util.h" | 27 #include "services/shell/runner/common/client_util.h" |
28 #include "services/shell/runner/common/switches.h" | 28 #include "services/shell/runner/common/switches.h" |
29 #include "services/shell/service_manager.h" | 29 #include "services/shell/service_manager.h" |
30 #include "services/shell/switches.h" | 30 #include "services/shell/switches.h" |
31 | 31 |
32 using shell::mojom::Service; | 32 using shell::mojom::Service; |
33 using shell::mojom::ServicePtr; | 33 using shell::mojom::ServicePtr; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 const char kTestRunnerName[] = "mojo:test-runner"; | 37 const char kTestRunnerName[] = "exe:mash_browser_tests"; |
38 const char kTestName[] = "exe:chrome"; | 38 const char kTestName[] = "exe:chrome"; |
39 | 39 |
40 // Returns the Dictionary value of |parent| under the specified key, creating | |
41 // and adding as necessary. | |
42 base::DictionaryValue* EnsureDictionary(base::DictionaryValue* parent, | |
43 const char* key) { | |
44 base::DictionaryValue* dictionary = nullptr; | |
45 if (parent->GetDictionary(key, &dictionary)) | |
46 return dictionary; | |
47 | |
48 std::unique_ptr<base::DictionaryValue> owned_dictionary( | |
49 new base::DictionaryValue); | |
50 dictionary = owned_dictionary.get(); | |
51 parent->Set(key, std::move(owned_dictionary)); | |
52 return dictionary; | |
53 } | |
54 | |
55 // This builds a permissive catalog with the addition of the 'instance_name' | |
56 // permission. | |
57 std::unique_ptr<shell::TestCatalogStore> BuildTestCatalogStore() { | |
58 std::unique_ptr<base::ListValue> apps(new base::ListValue); | |
59 std::unique_ptr<base::DictionaryValue> test_app_config = | |
60 shell::BuildPermissiveSerializedAppInfo(kTestRunnerName, "test"); | |
61 base::DictionaryValue* capabilities = | |
62 EnsureDictionary(test_app_config.get(), catalog::Store::kCapabilitiesKey); | |
63 base::DictionaryValue* required_capabilities = | |
64 EnsureDictionary(capabilities, catalog::Store::kCapabilities_RequiredKey); | |
65 std::unique_ptr<base::ListValue> required_shell_classes(new base::ListValue); | |
66 required_shell_classes->AppendString("instance_name"); | |
67 required_shell_classes->AppendString("client_process"); | |
68 std::unique_ptr<base::DictionaryValue> shell_caps(new base::DictionaryValue); | |
69 shell_caps->Set(catalog::Store::kCapabilities_ClassesKey, | |
70 std::move(required_shell_classes)); | |
71 required_capabilities->Set("mojo:shell", std::move(shell_caps)); | |
72 apps->Append(std::move(test_app_config)); | |
73 return base::WrapUnique(new shell::TestCatalogStore(std::move(apps))); | |
74 } | |
75 | |
76 // BackgroundTestState maintains all the state necessary to bind the test to | 40 // BackgroundTestState maintains all the state necessary to bind the test to |
77 // mojo. This class is only used on the thread created by BackgroundShell. | 41 // mojo. This class is only used on the thread created by BackgroundShell. |
78 class BackgroundTestState { | 42 class BackgroundTestState { |
79 public: | 43 public: |
80 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} | 44 BackgroundTestState() : child_token_(mojo::edk::GenerateRandomToken()) {} |
81 ~BackgroundTestState() {} | 45 ~BackgroundTestState() {} |
82 | 46 |
83 // Prepares the command line and other setup for connecting the test to mojo. | 47 // Prepares the command line and other setup for connecting the test to mojo. |
84 // Must be paired with a call to ChildProcessLaunched(). | 48 // Must be paired with a call to ChildProcessLaunched(). |
85 void Connect(base::CommandLine* command_line, | 49 void Connect(base::CommandLine* command_line, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 test_launch_options); | 177 test_launch_options); |
214 signal->Signal(); | 178 signal->Signal(); |
215 } | 179 } |
216 | 180 |
217 shell::BackgroundShell* background_shell_; | 181 shell::BackgroundShell* background_shell_; |
218 std::unique_ptr<BackgroundTestState> background_state_; | 182 std::unique_ptr<BackgroundTestState> background_state_; |
219 | 183 |
220 DISALLOW_COPY_AND_ASSIGN(MojoTestState); | 184 DISALLOW_COPY_AND_ASSIGN(MojoTestState); |
221 }; | 185 }; |
222 | 186 |
| 187 // The name in the manifest results in getting exe:mash_browser_tests used, |
| 188 // remap that to browser_tests. |
| 189 void RemoveMashFromBrowserTests(base::CommandLine* command_line) { |
| 190 base::FilePath exe_path(command_line->GetProgram()); |
| 191 #if defined(OS_WIN) |
| 192 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests.exe")); |
| 193 #else |
| 194 exe_path = exe_path.DirName().Append(FILE_PATH_LITERAL("browser_tests")); |
| 195 #endif |
| 196 command_line->SetProgram(exe_path); |
| 197 } |
| 198 |
223 } // namespace | 199 } // namespace |
224 | 200 |
| 201 // NativeRunnerDelegate that makes exe:mash_browser_tests to exe:browser_tests, |
| 202 // and removes '--run-in-mash'. |
| 203 class MojoTestConnector::NativeRunnerDelegateImpl |
| 204 : public shell::NativeRunnerDelegate { |
| 205 public: |
| 206 NativeRunnerDelegateImpl() {} |
| 207 ~NativeRunnerDelegateImpl() override {} |
| 208 |
| 209 private: |
| 210 // shell::NativeRunnerDelegate: |
| 211 void AdjustCommandLineArgumentsForTarget( |
| 212 const shell::Identity& target, |
| 213 base::CommandLine* command_line) override { |
| 214 if (target.name() != "exe:chrome") { |
| 215 if (target.name() == "exe:mash_browser_tests") |
| 216 RemoveMashFromBrowserTests(command_line); |
| 217 command_line->AppendSwitch(MojoTestConnector::kMashApp); |
| 218 return; |
| 219 } |
| 220 |
| 221 base::CommandLine::StringVector argv(command_line->argv()); |
| 222 auto iter = |
| 223 std::find(argv.begin(), argv.end(), FILE_PATH_LITERAL("--run-in-mash")); |
| 224 if (iter != argv.end()) |
| 225 argv.erase(iter); |
| 226 *command_line = base::CommandLine(argv); |
| 227 } |
| 228 |
| 229 DISALLOW_COPY_AND_ASSIGN(NativeRunnerDelegateImpl); |
| 230 }; |
| 231 |
225 // static | 232 // static |
226 const char MojoTestConnector::kTestSwitch[] = "is_test"; | 233 const char MojoTestConnector::kTestSwitch[] = "is_test"; |
| 234 // static |
| 235 const char MojoTestConnector::kMashApp[] = "mash-app"; |
227 | 236 |
228 MojoTestConnector::MojoTestConnector() {} | 237 MojoTestConnector::MojoTestConnector() {} |
229 | 238 |
230 shell::mojom::ServiceRequest MojoTestConnector::Init() { | 239 shell::mojom::ServiceRequest MojoTestConnector::Init() { |
| 240 native_runner_delegate_ = base::MakeUnique<NativeRunnerDelegateImpl>(); |
| 241 |
231 std::unique_ptr<shell::BackgroundShell::InitParams> init_params( | 242 std::unique_ptr<shell::BackgroundShell::InitParams> init_params( |
232 new shell::BackgroundShell::InitParams); | 243 new shell::BackgroundShell::InitParams); |
233 init_params->catalog_store = BuildTestCatalogStore(); | |
234 // When running in single_process mode chrome initializes the edk. | 244 // When running in single_process mode chrome initializes the edk. |
235 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( | 245 init_params->init_edk = !base::CommandLine::ForCurrentProcess()->HasSwitch( |
236 content::kSingleProcessTestsFlag); | 246 content::kSingleProcessTestsFlag); |
| 247 init_params->native_runner_delegate = native_runner_delegate_.get(); |
237 background_shell_.Init(std::move(init_params)); | 248 background_shell_.Init(std::move(init_params)); |
238 return background_shell_.CreateServiceRequest(kTestRunnerName); | 249 return background_shell_.CreateServiceRequest(kTestRunnerName); |
239 } | 250 } |
240 | 251 |
241 MojoTestConnector::~MojoTestConnector() {} | 252 MojoTestConnector::~MojoTestConnector() {} |
242 | 253 |
243 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( | 254 std::unique_ptr<content::TestState> MojoTestConnector::PrepareForTest( |
244 base::CommandLine* command_line, | 255 base::CommandLine* command_line, |
245 base::TestLauncher::LaunchOptions* test_launch_options) { | 256 base::TestLauncher::LaunchOptions* test_launch_options) { |
246 std::unique_ptr<MojoTestState> test_state( | 257 std::unique_ptr<MojoTestState> test_state( |
247 new MojoTestState(&background_shell_)); | 258 new MojoTestState(&background_shell_)); |
248 test_state->Init(command_line, test_launch_options); | 259 test_state->Init(command_line, test_launch_options); |
249 return std::move(test_state); | 260 return std::move(test_state); |
250 } | 261 } |
OLD | NEW |