Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: chrome/test/base/mash_browser_tests_main.cc

Issue 1822213002: Changes to get mash browser_tests shutdown working correctly (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: order Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chrome_browser_main.cc ('k') | chrome/test/base/mojo_test_connector.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/process/launch.h" 10 #include "base/process/launch.h"
11 #include "base/sys_info.h" 11 #include "base/sys_info.h"
12 #include "chrome/test/base/chrome_test_launcher.h" 12 #include "chrome/test/base/chrome_test_launcher.h"
13 #include "chrome/test/base/chrome_test_suite.h" 13 #include "chrome/test/base/chrome_test_suite.h"
14 #include "chrome/test/base/mojo_test_connector.h" 14 #include "chrome/test/base/mojo_test_connector.h"
15 #include "content/public/common/mojo_shell_connection.h" 15 #include "content/public/common/mojo_shell_connection.h"
16 #include "content/public/test/test_launcher.h" 16 #include "content/public/test/test_launcher.h"
17 #include "mojo/shell/public/cpp/connector.h" 17 #include "mojo/shell/public/cpp/connector.h"
18 #include "mojo/shell/public/cpp/shell_client.h" 18 #include "mojo/shell/public/cpp/shell_client.h"
19 #include "mojo/shell/public/cpp/shell_connection.h" 19 #include "mojo/shell/public/cpp/shell_connection.h"
20 #include "mojo/shell/runner/common/switches.h" 20 #include "mojo/shell/runner/common/switches.h"
21 #include "mojo/shell/runner/host/child_process.h" 21 #include "mojo/shell/runner/host/child_process.h"
22 #include "mojo/shell/runner/init.h" 22 #include "mojo/shell/runner/init.h"
23 23
24 namespace { 24 namespace {
25 25
26 void ConnectToDefaultApps(mojo::Connector* connector) { 26 void ConnectToDefaultApps(mojo::Connector* connector) {
27 connector->Connect("mojo:mash_session"); 27 connector->Connect("mojo:mash_session");
28 } 28 }
29 29
30 class MashTestSuite : public ChromeTestSuite {
31 public:
32 MashTestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {}
33
34 void SetMojoTestConnector(scoped_ptr<MojoTestConnector> connector) {
35 mojo_test_connector_ = std::move(connector);
36 }
37 MojoTestConnector* mojo_test_connector() {
38 return mojo_test_connector_.get();
39 }
40
41 private:
42 // ChromeTestSuite:
43 void Shutdown() override {
44 mojo_test_connector_.reset();
45 ChromeTestSuite::Shutdown();
46 }
47
48 scoped_ptr<MojoTestConnector> mojo_test_connector_;
49
50 DISALLOW_COPY_AND_ASSIGN(MashTestSuite);
51 };
52
30 // Used to setup the command line for passing a mojo channel to tests. 53 // Used to setup the command line for passing a mojo channel to tests.
31 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate { 54 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate {
32 public: 55 public:
33 explicit MashTestLauncherDelegate(ChromeTestSuiteRunner* runner) 56 MashTestLauncherDelegate() : ChromeTestLauncherDelegate(nullptr) {}
34 : ChromeTestLauncherDelegate(runner) {}
35 ~MashTestLauncherDelegate() override {} 57 ~MashTestLauncherDelegate() override {}
36 58
37 MojoTestConnector* GetMojoTestConnector() { 59 MojoTestConnector* GetMojoTestConnectorForSingleProcess() {
38 if (!mojo_test_connector_) 60 // This is only called for single process tests, in which case we need
39 mojo_test_connector_.reset(new MojoTestConnector); 61 // the TestSuite to own the MojoTestConnector.
40 return mojo_test_connector_.get(); 62 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
63 content::kSingleProcessTestsFlag));
64 DCHECK(test_suite_);
65 test_suite_->SetMojoTestConnector(make_scoped_ptr(new MojoTestConnector));
66 return test_suite_->mojo_test_connector();
41 } 67 }
42 68
43 private: 69 private:
44 // ChromeTestLauncherDelegate: 70 // ChromeTestLauncherDelegate:
71 int RunTestSuite(int argc, char** argv) override {
72 test_suite_.reset(new MashTestSuite(argc, argv));
73 const int result = test_suite_->Run();
74 test_suite_.reset();
75 return result;
76 }
45 scoped_ptr<content::TestState> PreRunTest( 77 scoped_ptr<content::TestState> PreRunTest(
46 base::CommandLine* command_line, 78 base::CommandLine* command_line,
47 base::TestLauncher::LaunchOptions* test_launch_options) override { 79 base::TestLauncher::LaunchOptions* test_launch_options) override {
48 if (!mojo_test_connector_) { 80 if (!mojo_test_connector_) {
49 mojo_test_connector_.reset(new MojoTestConnector); 81 mojo_test_connector_.reset(new MojoTestConnector);
50 shell_client_.reset(new mojo::ShellClient); 82 shell_client_.reset(new mojo::ShellClient);
51 shell_connection_.reset(new mojo::ShellConnection( 83 shell_connection_.reset(new mojo::ShellConnection(
52 shell_client_.get(), mojo_test_connector_->Init())); 84 shell_client_.get(), mojo_test_connector_->Init()));
53 ConnectToDefaultApps(shell_connection_->connector()); 85 ConnectToDefaultApps(shell_connection_->connector());
54 } 86 }
55 return GetMojoTestConnector()->PrepareForTest(command_line, 87 return mojo_test_connector_->PrepareForTest(command_line,
56 test_launch_options); 88 test_launch_options);
89 }
90 void OnDoneRunningTests() override {
91 // We have to shutdown this state here, while an AtExitManager is still
92 // valid.
93 shell_connection_.reset();
94 shell_client_.reset();
95 mojo_test_connector_.reset();
57 } 96 }
58 97
98 scoped_ptr<MashTestSuite> test_suite_;
59 scoped_ptr<MojoTestConnector> mojo_test_connector_; 99 scoped_ptr<MojoTestConnector> mojo_test_connector_;
60 scoped_ptr<mojo::ShellClient> shell_client_; 100 scoped_ptr<mojo::ShellClient> shell_client_;
61 scoped_ptr<mojo::ShellConnection> shell_connection_; 101 scoped_ptr<mojo::ShellConnection> shell_connection_;
62 102
63 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); 103 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate);
64 }; 104 };
65 105
66 void CreateMojoShellConnection(MashTestLauncherDelegate* delegate) { 106 void CreateMojoShellConnection(MashTestLauncherDelegate* delegate) {
67 const bool is_external_shell = true; 107 const bool is_external_shell = true;
68 content::MojoShellConnection::Create(delegate->GetMojoTestConnector()->Init(), 108 content::MojoShellConnection::Create(
69 is_external_shell); 109 delegate->GetMojoTestConnectorForSingleProcess()->Init(),
110 is_external_shell);
70 ConnectToDefaultApps(content::MojoShellConnection::Get()->GetConnector()); 111 ConnectToDefaultApps(content::MojoShellConnection::Get()->GetConnector());
71 } 112 }
72 113
73 } // namespace 114 } // namespace
74 115
75 bool RunMashBrowserTests(int argc, char** argv, int* exit_code) { 116 bool RunMashBrowserTests(int argc, char** argv, int* exit_code) {
76 base::CommandLine::Init(argc, argv); 117 base::CommandLine::Init(argc, argv);
77 const base::CommandLine& command_line = 118 const base::CommandLine& command_line =
78 *base::CommandLine::ForCurrentProcess(); 119 *base::CommandLine::ForCurrentProcess();
79 if (!command_line.HasSwitch("run-in-mash")) 120 if (!command_line.HasSwitch("run-in-mash"))
80 return false; 121 return false;
81 122
82 if (command_line.HasSwitch(switches::kChildProcess) && 123 if (command_line.HasSwitch(switches::kChildProcess) &&
83 !command_line.HasSwitch(MojoTestConnector::kTestSwitch)) { 124 !command_line.HasSwitch(MojoTestConnector::kTestSwitch)) {
84 base::AtExitManager at_exit; 125 base::AtExitManager at_exit;
85 mojo::shell::InitializeLogging(); 126 mojo::shell::InitializeLogging();
86 mojo::shell::WaitForDebuggerIfNecessary(); 127 mojo::shell::WaitForDebuggerIfNecessary();
87 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) 128 #if !defined(OFFICIAL_BUILD) && defined(OS_WIN)
88 base::RouteStdioToConsole(false); 129 base::RouteStdioToConsole(false);
89 #endif 130 #endif
90 *exit_code = mojo::shell::ChildProcessMain(); 131 *exit_code = mojo::shell::ChildProcessMain();
91 return true; 132 return true;
92 } 133 }
93 134
94 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2); 135 int default_jobs = std::max(1, base::SysInfo::NumberOfProcessors() / 2);
95 ChromeTestSuiteRunner runner; 136 MashTestLauncherDelegate delegate;
96 MashTestLauncherDelegate delegate(&runner);
97 // --single_process and no primoridal pipe token indicate we were run directly 137 // --single_process and no primoridal pipe token indicate we were run directly
98 // from the command line. In this case we have to start up MojoShellConnection 138 // from the command line. In this case we have to start up MojoShellConnection
99 // as though we were embedded. 139 // as though we were embedded.
100 content::MojoShellConnection::Factory shell_connection_factory; 140 content::MojoShellConnection::Factory shell_connection_factory;
101 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && 141 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) &&
102 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { 142 !command_line.HasSwitch(switches::kPrimordialPipeToken)) {
103 shell_connection_factory = 143 shell_connection_factory =
104 base::Bind(&CreateMojoShellConnection, &delegate); 144 base::Bind(&CreateMojoShellConnection, &delegate);
105 content::MojoShellConnection::SetFactoryForTest(&shell_connection_factory); 145 content::MojoShellConnection::SetFactoryForTest(&shell_connection_factory);
106 } 146 }
107 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); 147 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv);
108 return true; 148 return true;
109 } 149 }
OLDNEW
« no previous file with comments | « chrome/browser/chrome_browser_main.cc ('k') | chrome/test/base/mojo_test_connector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698