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

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

Issue 2645973006: [Service Manager] Get rid of dynamic service discovery (Closed)
Patch Set: . Created 3 years, 11 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
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/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"
11 #include "base/i18n/icu_util.h" 13 #include "base/i18n/icu_util.h"
14 #include "base/json/json_reader.h"
12 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 16 #include "base/message_loop/message_loop.h"
17 #include "base/path_service.h"
14 #include "base/process/launch.h" 18 #include "base/process/launch.h"
15 #include "base/run_loop.h" 19 #include "base/run_loop.h"
16 #include "base/sys_info.h" 20 #include "base/sys_info.h"
17 #include "base/threading/thread_task_runner_handle.h" 21 #include "base/threading/thread_task_runner_handle.h"
22 #include "base/values.h"
18 #include "chrome/test/base/chrome_test_launcher.h" 23 #include "chrome/test/base/chrome_test_launcher.h"
19 #include "chrome/test/base/chrome_test_suite.h" 24 #include "chrome/test/base/chrome_test_suite.h"
20 #include "chrome/test/base/mojo_test_connector.h" 25 #include "chrome/test/base/mojo_test_connector.h"
21 #include "content/public/app/content_main.h" 26 #include "content/public/app/content_main.h"
22 #include "content/public/common/service_manager_connection.h" 27 #include "content/public/common/service_manager_connection.h"
23 #include "content/public/test/test_launcher.h" 28 #include "content/public/test/test_launcher.h"
24 #include "mash/package/mash_packaged_service.h" 29 #include "mash/package/mash_packaged_service.h"
25 #include "services/service_manager/public/cpp/connector.h" 30 #include "services/service_manager/public/cpp/connector.h"
26 #include "services/service_manager/public/cpp/service.h" 31 #include "services/service_manager/public/cpp/service.h"
27 #include "services/service_manager/public/cpp/service_context.h" 32 #include "services/service_manager/public/cpp/service_context.h"
28 #include "services/service_manager/public/cpp/service_runner.h" 33 #include "services/service_manager/public/cpp/service_runner.h"
29 #include "services/service_manager/public/cpp/standalone_service/standalone_serv ice.h" 34 #include "services/service_manager/public/cpp/standalone_service/standalone_serv ice.h"
30 #include "services/service_manager/runner/common/switches.h" 35 #include "services/service_manager/runner/common/switches.h"
31 #include "services/service_manager/runner/init.h" 36 #include "services/service_manager/runner/init.h"
32 #include "ui/aura/env.h" 37 #include "ui/aura/env.h"
33 38
34 namespace { 39 namespace {
35 40
41 const base::FilePath::CharType kCatalogFilename[] =
42 FILE_PATH_LITERAL("mash_browser_tests_catalog.json");
43
36 void ConnectToDefaultApps(service_manager::Connector* connector) { 44 void ConnectToDefaultApps(service_manager::Connector* connector) {
37 connector->Connect("mash_session"); 45 connector->Connect("mash_session");
38 } 46 }
39 47
40 class MashTestSuite : public ChromeTestSuite { 48 class MashTestSuite : public ChromeTestSuite {
41 public: 49 public:
42 MashTestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {} 50 MashTestSuite(int argc, char** argv) : ChromeTestSuite(argc, argv) {}
43 51
44 void SetMojoTestConnector(std::unique_ptr<MojoTestConnector> connector) { 52 void SetMojoTestConnector(std::unique_ptr<MojoTestConnector> connector) {
45 mojo_test_connector_ = std::move(connector); 53 mojo_test_connector_ = std::move(connector);
46 } 54 }
55
47 MojoTestConnector* mojo_test_connector() { 56 MojoTestConnector* mojo_test_connector() {
48 return mojo_test_connector_.get(); 57 return mojo_test_connector_.get();
49 } 58 }
50 59
51 private: 60 private:
52 // ChromeTestSuite: 61 // ChromeTestSuite:
53 void Shutdown() override { 62 void Shutdown() override {
54 mojo_test_connector_.reset(); 63 mojo_test_connector_.reset();
55 ChromeTestSuite::Shutdown(); 64 ChromeTestSuite::Shutdown();
56 } 65 }
57 66
58 std::unique_ptr<MojoTestConnector> mojo_test_connector_; 67 std::unique_ptr<MojoTestConnector> mojo_test_connector_;
59 68
60 DISALLOW_COPY_AND_ASSIGN(MashTestSuite); 69 DISALLOW_COPY_AND_ASSIGN(MashTestSuite);
61 }; 70 };
62 71
63 // Used to setup the command line for passing a mojo channel to tests. 72 // Used to setup the command line for passing a mojo channel to tests.
64 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate { 73 class MashTestLauncherDelegate : public ChromeTestLauncherDelegate {
65 public: 74 public:
66 MashTestLauncherDelegate() : ChromeTestLauncherDelegate(nullptr) {} 75 MashTestLauncherDelegate() : ChromeTestLauncherDelegate(nullptr) {}
67 ~MashTestLauncherDelegate() override {} 76 ~MashTestLauncherDelegate() override {}
68 77
69 MojoTestConnector* GetMojoTestConnectorForSingleProcess() { 78 MojoTestConnector* GetMojoTestConnectorForSingleProcess() {
70 // This is only called for single process tests, in which case we need 79 // This is only called for single process tests, in which case we need
71 // the TestSuite to own the MojoTestConnector. 80 // the TestSuite to own the MojoTestConnector.
72 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch( 81 DCHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
73 content::kSingleProcessTestsFlag)); 82 content::kSingleProcessTestsFlag));
74 DCHECK(test_suite_); 83 DCHECK(test_suite_);
75 test_suite_->SetMojoTestConnector(base::WrapUnique(new MojoTestConnector)); 84 test_suite_->SetMojoTestConnector(
85 base::MakeUnique<MojoTestConnector>(ReadCatalogManifest()));
76 return test_suite_->mojo_test_connector(); 86 return test_suite_->mojo_test_connector();
77 } 87 }
78 88
79 private: 89 private:
80 // ChromeTestLauncherDelegate: 90 // ChromeTestLauncherDelegate:
81 int RunTestSuite(int argc, char** argv) override { 91 int RunTestSuite(int argc, char** argv) override {
82 test_suite_.reset(new MashTestSuite(argc, argv)); 92 test_suite_.reset(new MashTestSuite(argc, argv));
83 content::GetContentMainParams()->env_mode = aura::Env::Mode::MUS; 93 content::GetContentMainParams()->env_mode = aura::Env::Mode::MUS;
84 const int result = test_suite_->Run(); 94 const int result = test_suite_->Run();
85 test_suite_.reset(); 95 test_suite_.reset();
86 return result; 96 return result;
87 } 97 }
88 98
89 std::unique_ptr<content::TestState> PreRunTest( 99 std::unique_ptr<content::TestState> PreRunTest(
90 base::CommandLine* command_line, 100 base::CommandLine* command_line,
91 base::TestLauncher::LaunchOptions* test_launch_options) override { 101 base::TestLauncher::LaunchOptions* test_launch_options) override {
92 if (!mojo_test_connector_) { 102 if (!mojo_test_connector_) {
93 mojo_test_connector_ = base::MakeUnique<MojoTestConnector>(); 103 mojo_test_connector_ =
104 base::MakeUnique<MojoTestConnector>(ReadCatalogManifest());
94 context_.reset(new service_manager::ServiceContext( 105 context_.reset(new service_manager::ServiceContext(
95 base::MakeUnique<mash::MashPackagedService>(), 106 base::MakeUnique<mash::MashPackagedService>(),
96 mojo_test_connector_->Init())); 107 mojo_test_connector_->Init()));
97 } 108 }
98 std::unique_ptr<content::TestState> test_state = 109 std::unique_ptr<content::TestState> test_state =
99 mojo_test_connector_->PrepareForTest(command_line, test_launch_options); 110 mojo_test_connector_->PrepareForTest(command_line, test_launch_options);
111
100 // Start default apps after chrome, as they may try to connect to chrome on 112 // Start default apps after chrome, as they may try to connect to chrome on
101 // startup. Attempt to connect once per test in case a previous test crashed 113 // startup. Attempt to connect once per test in case a previous test crashed
102 // mash_session. 114 // mash_session.
103 ConnectToDefaultApps(context_->connector()); 115 ConnectToDefaultApps(context_->connector());
104 return test_state; 116 return test_state;
105 } 117 }
106 118
107 void OnDoneRunningTests() override { 119 void OnDoneRunningTests() override {
108 // We have to shutdown this state here, while an AtExitManager is still 120 // We have to shutdown this state here, while an AtExitManager is still
109 // valid. 121 // valid.
110 context_.reset(); 122 context_.reset();
111 mojo_test_connector_.reset(); 123 mojo_test_connector_.reset();
112 } 124 }
113 125
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 DCHECK(base::ReadFileToString(catalog_path, &catalog_contents));
132 std::unique_ptr<base::Value> manifest_value =
133 base::JSONReader::Read(catalog_contents);
134 DCHECK(manifest_value);
135 return manifest_value;
136 }
137
114 std::unique_ptr<MashTestSuite> test_suite_; 138 std::unique_ptr<MashTestSuite> test_suite_;
115 std::unique_ptr<MojoTestConnector> mojo_test_connector_; 139 std::unique_ptr<MojoTestConnector> mojo_test_connector_;
116 std::unique_ptr<service_manager::ServiceContext> context_; 140 std::unique_ptr<service_manager::ServiceContext> context_;
117 141
118 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate); 142 DISALLOW_COPY_AND_ASSIGN(MashTestLauncherDelegate);
119 }; 143 };
120 144
121 std::unique_ptr<content::ServiceManagerConnection> 145 std::unique_ptr<content::ServiceManagerConnection>
122 CreateServiceManagerConnection(MashTestLauncherDelegate* delegate) { 146 CreateServiceManagerConnection(MashTestLauncherDelegate* delegate) {
123 std::unique_ptr<content::ServiceManagerConnection> connection( 147 std::unique_ptr<content::ServiceManagerConnection> connection(
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) && 197 if (command_line.HasSwitch(content::kSingleProcessTestsFlag) &&
174 !command_line.HasSwitch(switches::kPrimordialPipeToken)) { 198 !command_line.HasSwitch(switches::kPrimordialPipeToken)) {
175 service_manager_connection_factory = 199 service_manager_connection_factory =
176 base::Bind(&CreateServiceManagerConnection, &delegate); 200 base::Bind(&CreateServiceManagerConnection, &delegate);
177 content::ServiceManagerConnection::SetFactoryForTest( 201 content::ServiceManagerConnection::SetFactoryForTest(
178 &service_manager_connection_factory); 202 &service_manager_connection_factory);
179 } 203 }
180 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv); 204 *exit_code = LaunchChromeTests(default_jobs, &delegate, argc, argv);
181 return true; 205 return true;
182 } 206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698