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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/debugger.h" | 9 #include "base/debug/debugger.h" |
10 #include "base/debug/stack_trace.h" | 10 #include "base/debug/stack_trace.h" |
11 #include "base/files/file_path.h" | |
12 #include "base/files/file_util.h" | |
13 #include "base/i18n/icu_util.h" | 11 #include "base/i18n/icu_util.h" |
14 #include "base/json/json_reader.h" | |
15 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
16 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
17 #include "base/path_service.h" | |
18 #include "base/process/launch.h" | 14 #include "base/process/launch.h" |
19 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
20 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
22 #include "base/values.h" | |
23 #include "chrome/test/base/chrome_test_launcher.h" | 18 #include "chrome/test/base/chrome_test_launcher.h" |
24 #include "chrome/test/base/chrome_test_suite.h" | 19 #include "chrome/test/base/chrome_test_suite.h" |
25 #include "chrome/test/base/mojo_test_connector.h" | 20 #include "chrome/test/base/mojo_test_connector.h" |
26 #include "content/public/app/content_main.h" | 21 #include "content/public/app/content_main.h" |
27 #include "content/public/common/service_manager_connection.h" | 22 #include "content/public/common/service_manager_connection.h" |
28 #include "content/public/test/test_launcher.h" | 23 #include "content/public/test/test_launcher.h" |
29 #include "mash/package/mash_packaged_service.h" | 24 #include "mash/package/mash_packaged_service.h" |
30 #include "services/service_manager/public/cpp/connector.h" | 25 #include "services/service_manager/public/cpp/connector.h" |
31 #include "services/service_manager/public/cpp/service.h" | 26 #include "services/service_manager/public/cpp/service.h" |
32 #include "services/service_manager/public/cpp/service_context.h" | 27 #include "services/service_manager/public/cpp/service_context.h" |
33 #include "services/service_manager/public/cpp/service_runner.h" | 28 #include "services/service_manager/public/cpp/service_runner.h" |
34 #include "services/service_manager/public/cpp/standalone_service/standalone_serv
ice.h" | 29 #include "services/service_manager/public/cpp/standalone_service/standalone_serv
ice.h" |
35 #include "services/service_manager/runner/common/switches.h" | 30 #include "services/service_manager/runner/common/switches.h" |
36 #include "services/service_manager/runner/init.h" | 31 #include "services/service_manager/runner/init.h" |
37 #include "ui/aura/env.h" | 32 #include "ui/aura/env.h" |
38 | 33 |
39 namespace { | 34 namespace { |
40 | 35 |
41 const base::FilePath::CharType kCatalogFilename[] = | |
42 FILE_PATH_LITERAL("mash_browser_tests_catalog.json"); | |
43 | |
44 void ConnectToDefaultApps(service_manager::Connector* connector) { | 36 void ConnectToDefaultApps(service_manager::Connector* connector) { |
45 connector->Connect("mash_session"); | 37 connector->Connect("mash_session"); |
46 } | 38 } |
47 | 39 |
48 class MashTestSuite : public ChromeTestSuite { | 40 class MashTestSuite : public ChromeTestSuite { |
49 public: | 41 public: |
50 MashTestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {} | 42 MashTestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {} |
51 | 43 |
52 void SetMojoTestConnector(std::unique_ptr<MojoTestConnector> connector) { | 44 void SetMojoTestConnector(std::unique_ptr<MojoTestConnector> connector) { |
53 mojo_test_connector_ = std::move(connector); | 45 mojo_test_connector_ = std::move(connector); |
54 } | 46 } |
55 | |
56 MojoTestConnector* mojo_test_connector() { | 47 MojoTestConnector* mojo_test_connector() { |
57 return mojo_test_connector_.get(); | 48 return mojo_test_connector_.get(); |
58 } | 49 } |
59 | 50 |
60 private: | 51 private: |
61 // ChromeTestSuite: | 52 // ChromeTestSuite: |
62 void Shutdown() override { | 53 void Shutdown() override { |
63 mojo_test_connector_.reset(); | 54 mojo_test_connector_.reset(); |
64 ChromeTestSuite::Shutdown(); | 55 ChromeTestSuite::Shutdown(); |
65 } | 56 } |
66 | 57 |
67 std::unique_ptr<MojoTestConnector> mojo_test_connector_; | 58 std::unique_ptr<MojoTestConnector> mojo_test_connector_; |
68 | 59 |
69 DISALLOW_COPY_AND_ASSIGN(MashTestSuite); | 60 DISALLOW_COPY_AND_ASSIGN(MashTestSuite); |
70 }; | 61 }; |
71 | 62 |
72 // Used to setup the command line for passing a mojo channel to tests. | 63 // Used to setup the command line for passing a mojo channel to tests. |
73 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate { | 64 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate { |
74 public: | 65 public: |
75 MashTestLauncherDelegate() : ChromeTestLauncherDelegate(nullptr) {} | 66 MashTestLauncherDelegate() : ChromeTestLauncherDelegate(nullptr) {} |
76 ~MashTestLauncherDelegate() override {} | 67 ~MashTestLauncherDelegate() override {} |
77 | 68 |
78 MojoTestConnector* GetMojoTestConnectorForSingleProcess() { | 69 MojoTestConnector* GetMojoTestConnectorForSingleProcess() { |
79 // This is only called for single process tests, in which case we need | 70 // This is only called for single process tests, in which case we need |
80 // the TestSuite to own the MojoTestConnector. | 71 // the TestSuite to own the MojoTestConnector. |
81 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( | 72 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( |
82 content::kSingleProcessTestsFlag)); | 73 content::kSingleProcessTestsFlag)); |
83 DCHECK(test_suite_); | 74 DCHECK(test_suite_); |
84 test_suite_->SetMojoTestConnector( | 75 test_suite_->SetMojoTestConnector(base::WrapUnique(new MojoTestConnector)); |
85 base::MakeUnique<MojoTestConnector>(ReadCatalogManifest())); | |
86 return test_suite_->mojo_test_connector(); | 76 return test_suite_->mojo_test_connector(); |
87 } | 77 } |
88 | 78 |
89 private: | 79 private: |
90 // ChromeTestLauncherDelegate: | 80 // ChromeTestLauncherDelegate: |
91 int RunTestSuite(int argc, char** argv) override { | 81 int RunTestSuite(int argc, char** argv) override { |
92 test_suite_.reset(new MashTestSuite(argc, argv)); | 82 test_suite_.reset(new MashTestSuite(argc, argv)); |
93 content::GetContentMainParams()->env_mode = aura::Env::Mode::MUS; | 83 content::GetContentMainParams()->env_mode = aura::Env::Mode::MUS; |
94 const int result = test_suite_->Run(); | 84 const int result = test_suite_->Run(); |
95 test_suite_.reset(); | 85 test_suite_.reset(); |
96 return result; | 86 return result; |
97 } | 87 } |
98 | 88 |
99 std::unique_ptr<content::TestState> PreRunTest( | 89 std::unique_ptr<content::TestState> PreRunTest( |
100 base::CommandLine* command_line, | 90 base::CommandLine* command_line, |
101 base::TestLauncher::LaunchOptions* test_launch_options) override { | 91 base::TestLauncher::LaunchOptions* test_launch_options) override { |
102 if (!mojo_test_connector_) { | 92 if (!mojo_test_connector_) { |
103 mojo_test_connector_ = | 93 mojo_test_connector_ = base::MakeUnique<MojoTestConnector>(); |
104 base::MakeUnique<MojoTestConnector>(ReadCatalogManifest()); | |
105 context_.reset(new service_manager::ServiceContext( | 94 context_.reset(new service_manager::ServiceContext( |
106 base::MakeUnique<mash::MashPackagedService>(), | 95 base::MakeUnique<mash::MashPackagedService>(), |
107 mojo_test_connector_->Init())); | 96 mojo_test_connector_->Init())); |
108 } | 97 } |
109 std::unique_ptr<content::TestState> test_state = | 98 std::unique_ptr<content::TestState> test_state = |
110 mojo_test_connector_->PrepareForTest(command_line, test_launch_options); | 99 mojo_test_connector_->PrepareForTest(command_line, test_launch_options); |
111 | |
112 // Start default apps after chrome, as they may try to connect to chrome on | 100 // Start default apps after chrome, as they may try to connect to chrome on |
113 // startup. Attempt to connect once per test in case a previous test crashed | 101 // startup. Attempt to connect once per test in case a previous test crashed |
114 // mash_session. | 102 // mash_session. |
115 ConnectToDefaultApps(context_->connector()); | 103 ConnectToDefaultApps(context_->connector()); |
116 return test_state; | 104 return test_state; |
117 } | 105 } |
118 | 106 |
119 void OnDoneRunningTests() override { | 107 void OnDoneRunningTests() override { |
120 // We have to shutdown this state here, while an AtExitManager is still | 108 // We have to shutdown this state here, while an AtExitManager is still |
121 // valid. | 109 // valid. |
122 context_.reset(); | 110 context_.reset(); |
123 mojo_test_connector_.reset(); | 111 mojo_test_connector_.reset(); |
124 } | 112 } |
125 | 113 |
126 std::unique_ptr<base::Value> ReadCatalogManifest() { | |
127 std::string catalog_contents; | |
128 base::FilePath exe_path; | |
129 base::PathService::Get(base::DIR_EXE, &exe_path); | |
130 base::FilePath catalog_path = exe_path.Append(kCatalogFilename); | |
131 bool result = base::ReadFileToString(catalog_path, &catalog_contents); | |
132 DCHECK(result); | |
133 std::unique_ptr<base::Value> manifest_value = | |
134 base::JSONReader::Read(catalog_contents); | |
135 DCHECK(manifest_value); | |
136 return manifest_value; | |
137 } | |
138 | |
139 std::unique_ptr<MashTestSuite> test_suite_; | 114 std::unique_ptr<MashTestSuite> test_suite_; |
140 std::unique_ptr<MojoTestConnector> mojo_test_connector_; | 115 std::unique_ptr<MojoTestConnector> mojo_test_connector_; |
141 std::unique_ptr<service_manager::ServiceContext> context_; | 116 std::unique_ptr<service_manager::ServiceContext> context_; |
142 | 117 |
143 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); | 118 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); |
144 }; | 119 }; |
145 | 120 |
146 std::unique_ptr<content::ServiceManagerConnection> | 121 std::unique_ptr<content::ServiceManagerConnection> |
147 CreateServiceManagerConnection(MashTestLauncherDelegate* delegate) { | 122 CreateServiceManagerConnection(MashTestLauncherDelegate* delegate) { |
148 std::unique_ptr<content::ServiceManagerConnection> connection( | 123 std::unique_ptr<content::ServiceManagerConnection> connection( |
149 content::ServiceManagerConnection::Create( | 124 content::ServiceManagerConnection::Create( |
150 delegate->GetMojoTestConnectorForSingleProcess()->Init(), | 125 delegate->GetMojoTestConnectorForSingleProcess()->Init(), |
151 base::ThreadTaskRunnerHandle::Get())); | 126 base::ThreadTaskRunnerHandle::Get())); |
152 connection->Start(); | 127 connection->Start(); |
153 ConnectToDefaultApps(connection->GetConnector()); | 128 ConnectToDefaultApps(connection->GetConnector()); |
154 return connection; | 129 return connection; |
155 } | 130 } |
156 | 131 |
157 void StartChildApp(service_manager::mojom::ServiceRequest service_request) { | 132 void StartChildApp(service_manager::mojom::ServiceRequest service_request) { |
158 // The UI service requires this to be TYPE_UI. We don't know which service | 133 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT); |
159 // we're going to run yet, so we just always use TYPE_UI for now. | |
160 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); | |
161 base::RunLoop run_loop; | 134 base::RunLoop run_loop; |
162 service_manager::ServiceContext context( | 135 service_manager::ServiceContext context( |
163 base::MakeUnique<mash::MashPackagedService>(), | 136 base::MakeUnique<mash::MashPackagedService>(), |
164 std::move(service_request)); | 137 std::move(service_request)); |
165 context.SetConnectionLostClosure(run_loop.QuitClosure()); | 138 context.SetConnectionLostClosure(run_loop.QuitClosure()); |
166 run_loop.Run(); | 139 run_loop.Run(); |
167 } | 140 } |
168 | 141 |
169 } // namespace | 142 } // namespace |
170 | 143 |
(...skipping 29 matching lines...) Expand all Loading... |
200 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && | 173 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && |
201 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { | 174 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { |
202 service_manager_connection_factory = | 175 service_manager_connection_factory = |
203 base::Bind(&CreateServiceManagerConnection, &delegate); | 176 base::Bind(&CreateServiceManagerConnection, &delegate); |
204 content::ServiceManagerConnection::SetFactoryForTest( | 177 content::ServiceManagerConnection::SetFactoryForTest( |
205 &service_manager_connection_factory); | 178 &service_manager_connection_factory); |
206 } | 179 } |
207 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); | 180 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); |
208 return true; | 181 return true; |
209 } | 182 } |
OLD | NEW |