| 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/i18n/icu_util.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" |
| 11 #include "base/process/launch.h" | 13 #include "base/process/launch.h" |
| 14 #include "base/run_loop.h" |
| 12 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "chrome/test/base/chrome_test_launcher.h" | 17 #include "chrome/test/base/chrome_test_launcher.h" |
| 15 #include "chrome/test/base/chrome_test_suite.h" | 18 #include "chrome/test/base/chrome_test_suite.h" |
| 16 #include "chrome/test/base/mojo_test_connector.h" | 19 #include "chrome/test/base/mojo_test_connector.h" |
| 17 #include "content/public/common/mojo_shell_connection.h" | 20 #include "content/public/common/mojo_shell_connection.h" |
| 18 #include "content/public/test/test_launcher.h" | 21 #include "content/public/test/test_launcher.h" |
| 22 #include "mash/package/mash_packaged_service.h" |
| 19 #include "services/shell/public/cpp/connector.h" | 23 #include "services/shell/public/cpp/connector.h" |
| 20 #include "services/shell/public/cpp/service.h" | 24 #include "services/shell/public/cpp/service.h" |
| 21 #include "services/shell/public/cpp/service_context.h" | 25 #include "services/shell/public/cpp/service_context.h" |
| 26 #include "services/shell/public/cpp/service_runner.h" |
| 22 #include "services/shell/runner/common/switches.h" | 27 #include "services/shell/runner/common/switches.h" |
| 23 #include "services/shell/runner/host/child_process.h" | 28 #include "services/shell/runner/host/child_process.h" |
| 29 #include "services/shell/runner/host/child_process_base.h" |
| 24 #include "services/shell/runner/init.h" | 30 #include "services/shell/runner/init.h" |
| 25 | 31 |
| 26 namespace { | 32 namespace { |
| 27 | 33 |
| 28 void ConnectToDefaultApps(shell::Connector* connector) { | 34 void ConnectToDefaultApps(shell::Connector* connector) { |
| 29 connector->Connect("mojo:mash_session"); | 35 connector->Connect("mojo:mash_session"); |
| 30 } | 36 } |
| 31 | 37 |
| 32 class MashTestSuite : public ChromeTestSuite { | 38 class MashTestSuite : public ChromeTestSuite { |
| 33 public: | 39 public: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 int RunTestSuite(int argc, char** argv) override { | 79 int RunTestSuite(int argc, char** argv) override { |
| 74 test_suite_.reset(new MashTestSuite(argc, argv)); | 80 test_suite_.reset(new MashTestSuite(argc, argv)); |
| 75 const int result = test_suite_->Run(); | 81 const int result = test_suite_->Run(); |
| 76 test_suite_.reset(); | 82 test_suite_.reset(); |
| 77 return result; | 83 return result; |
| 78 } | 84 } |
| 79 std::unique_ptr<content::TestState> PreRunTest( | 85 std::unique_ptr<content::TestState> PreRunTest( |
| 80 base::CommandLine* command_line, | 86 base::CommandLine* command_line, |
| 81 base::TestLauncher::LaunchOptions* test_launch_options) override { | 87 base::TestLauncher::LaunchOptions* test_launch_options) override { |
| 82 if (!mojo_test_connector_) { | 88 if (!mojo_test_connector_) { |
| 83 mojo_test_connector_.reset(new MojoTestConnector); | 89 mojo_test_connector_ = base::MakeUnique<MojoTestConnector>(); |
| 84 service_.reset(new shell::Service); | 90 service_ = base::MakeUnique<mash::MashPackagedService>(); |
| 85 shell_connection_.reset(new shell::ServiceContext( | 91 service_->set_context(base::MakeUnique<shell::ServiceContext>( |
| 86 service_.get(), mojo_test_connector_->Init())); | 92 service_.get(), mojo_test_connector_->Init())); |
| 87 ConnectToDefaultApps(shell_connection_->connector()); | 93 ConnectToDefaultApps(service_->connector()); |
| 88 } | 94 } |
| 89 return mojo_test_connector_->PrepareForTest(command_line, | 95 return mojo_test_connector_->PrepareForTest(command_line, |
| 90 test_launch_options); | 96 test_launch_options); |
| 91 } | 97 } |
| 92 void OnDoneRunningTests() override { | 98 void OnDoneRunningTests() override { |
| 93 // We have to shutdown this state here, while an AtExitManager is still | 99 // We have to shutdown this state here, while an AtExitManager is still |
| 94 // valid. | 100 // valid. |
| 95 shell_connection_.reset(); | |
| 96 service_.reset(); | 101 service_.reset(); |
| 97 mojo_test_connector_.reset(); | 102 mojo_test_connector_.reset(); |
| 98 } | 103 } |
| 99 | 104 |
| 100 std::unique_ptr<MashTestSuite> test_suite_; | 105 std::unique_ptr<MashTestSuite> test_suite_; |
| 101 std::unique_ptr<MojoTestConnector> mojo_test_connector_; | 106 std::unique_ptr<MojoTestConnector> mojo_test_connector_; |
| 102 std::unique_ptr<shell::Service> service_; | 107 std::unique_ptr<mash::MashPackagedService> service_; |
| 103 std::unique_ptr<shell::ServiceContext> shell_connection_; | |
| 104 | 108 |
| 105 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); | 109 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); |
| 106 }; | 110 }; |
| 107 | 111 |
| 108 std::unique_ptr<content::MojoShellConnection> CreateMojoShellConnection( | 112 std::unique_ptr<content::MojoShellConnection> CreateMojoShellConnection( |
| 109 MashTestLauncherDelegate* delegate) { | 113 MashTestLauncherDelegate* delegate) { |
| 110 std::unique_ptr<content::MojoShellConnection> connection( | 114 std::unique_ptr<content::MojoShellConnection> connection( |
| 111 content::MojoShellConnection::Create( | 115 content::MojoShellConnection::Create( |
| 112 delegate->GetMojoTestConnectorForSingleProcess()->Init(), | 116 delegate->GetMojoTestConnectorForSingleProcess()->Init(), |
| 113 base::ThreadTaskRunnerHandle::Get())); | 117 base::ThreadTaskRunnerHandle::Get())); |
| 114 connection->Start(); | 118 connection->Start(); |
| 115 ConnectToDefaultApps(connection->GetConnector()); | 119 ConnectToDefaultApps(connection->GetConnector()); |
| 116 return connection; | 120 return connection; |
| 117 } | 121 } |
| 118 | 122 |
| 123 void StartChildApp(shell::mojom::ServiceRequest service_request) { |
| 124 base::MessageLoop message_loop(base::MessageLoop::TYPE_UI); |
| 125 base::RunLoop run_loop; |
| 126 mash::MashPackagedService service; |
| 127 std::unique_ptr<shell::ServiceContext> context = |
| 128 base::MakeUnique<shell::ServiceContext>(&service, |
| 129 std::move(service_request)); |
| 130 context->SetConnectionLostClosure(run_loop.QuitClosure()); |
| 131 service.set_context(std::move(context)); |
| 132 run_loop.Run(); |
| 133 } |
| 134 |
| 119 } // namespace | 135 } // namespace |
| 120 | 136 |
| 121 bool RunMashBrowserTests(int argc, char** argv, int* exit_code) { | 137 bool RunMashBrowserTests(int argc, char** argv, int* exit_code) { |
| 122 base::CommandLine::Init(argc, argv); | 138 base::CommandLine::Init(argc, argv); |
| 123 const base::CommandLine& command_line = | 139 const base::CommandLine& command_line = |
| 124 *base::CommandLine::ForCurrentProcess(); | 140 *base::CommandLine::ForCurrentProcess(); |
| 125 if (!command_line.HasSwitch("run-in-mash")) | 141 if (!command_line.HasSwitch("run-in-mash")) |
| 126 return false; | 142 return false; |
| 127 | 143 |
| 144 if (command_line.HasSwitch(MojoTestConnector::kMashApp)) { |
| 145 #if defined(OS_LINUX) |
| 146 base::AtExitManager exit_manager; |
| 147 #endif |
| 148 base::i18n::InitializeICU(); |
| 149 shell::ChildProcessMainWithCallback(base::Bind(&StartChildApp)); |
| 150 *exit_code = 0; |
| 151 return true; |
| 152 } |
| 153 |
| 128 if (command_line.HasSwitch(switches::kChildProcess) && | 154 if (command_line.HasSwitch(switches::kChildProcess) && |
| 129 !command_line.HasSwitch(MojoTestConnector::kTestSwitch)) { | 155 !command_line.HasSwitch(MojoTestConnector::kTestSwitch)) { |
| 130 base::AtExitManager at_exit; | 156 base::AtExitManager at_exit; |
| 131 shell::InitializeLogging(); | 157 shell::InitializeLogging(); |
| 132 // TODO(sky): nuke once resolve why test isn't shutting down: 594600. | |
| 133 LOG(ERROR) << "starting app " << command_line.GetCommandLineString(); | |
| 134 shell::WaitForDebuggerIfNecessary(); | 158 shell::WaitForDebuggerIfNecessary(); |
| 135 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) | 159 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) |
| 136 base::RouteStdioToConsole(false); | 160 base::RouteStdioToConsole(false); |
| 137 #endif | 161 #endif |
| 138 *exit_code = shell::ChildProcessMain(); | 162 *exit_code = shell::ChildProcessMain(); |
| 139 // TODO(sky): nuke once resolve why test isn't shutting down: 594600. | |
| 140 LOG(ERROR) << "child exit_code=" << *exit_code; | |
| 141 return true; | 163 return true; |
| 142 } | 164 } |
| 143 | 165 |
| 144 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2); | 166 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2); |
| 145 MashTestLauncherDelegate delegate; | 167 MashTestLauncherDelegate delegate; |
| 146 // --single_process and no primoridal pipe token indicate we were run directly | 168 // --single_process and no primoridal pipe token indicate we were run directly |
| 147 // from the command line. In this case we have to start up MojoShellConnection | 169 // from the command line. In this case we have to start up MojoShellConnection |
| 148 // as though we were embedded. | 170 // as though we were embedded. |
| 149 content::MojoShellConnection::Factory shell_connection_factory; | 171 content::MojoShellConnection::Factory shell_connection_factory; |
| 150 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && | 172 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && |
| 151 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { | 173 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { |
| 152 shell_connection_factory = | 174 shell_connection_factory = |
| 153 base::Bind(&CreateMojoShellConnection, &delegate); | 175 base::Bind(&CreateMojoShellConnection, &delegate); |
| 154 content::MojoShellConnection::SetFactoryForTest(&shell_connection_factory); | 176 content::MojoShellConnection::SetFactoryForTest(&shell_connection_factory); |
| 155 } | 177 } |
| 156 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); | 178 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); |
| 157 // TODO(sky): nuke once resolve why test isn't shutting down: 594600. | |
| 158 LOG(ERROR) << "RunMashBrowserTests exit_code=" << *exit_code; | |
| 159 return true; | 179 return true; |
| 160 } | 180 } |
| OLD | NEW |