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 "ui/views/mus/views_mus_test_suite.h" | 5 #include "ui/views/mus/views_mus_test_suite.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 namespace views { | 26 namespace views { |
27 namespace { | 27 namespace { |
28 | 28 |
29 void EnsureCommandLineSwitch(const std::string& name) { | 29 void EnsureCommandLineSwitch(const std::string& name) { |
30 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | 30 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); |
31 if (!cmd_line->HasSwitch(name)) | 31 if (!cmd_line->HasSwitch(name)) |
32 cmd_line->AppendSwitch(name); | 32 cmd_line->AppendSwitch(name); |
33 } | 33 } |
34 | 34 |
35 class DefaultService : public shell::Service { | 35 class DefaultService : public service_manager::Service { |
36 public: | 36 public: |
37 DefaultService() {} | 37 DefaultService() {} |
38 ~DefaultService() override {} | 38 ~DefaultService() override {} |
39 | 39 |
40 private: | 40 private: |
41 DISALLOW_COPY_AND_ASSIGN(DefaultService); | 41 DISALLOW_COPY_AND_ASSIGN(DefaultService); |
42 }; | 42 }; |
43 | 43 |
44 class PlatformTestHelperMus : public PlatformTestHelper { | 44 class PlatformTestHelperMus : public PlatformTestHelper { |
45 public: | 45 public: |
46 PlatformTestHelperMus(shell::Connector* connector, | 46 PlatformTestHelperMus(service_manager::Connector* connector, |
47 const shell::Identity& identity) { | 47 const service_manager::Identity& identity) { |
48 // It is necessary to recreate the WindowManagerConnection for each test, | 48 // It is necessary to recreate the WindowManagerConnection for each test, |
49 // since a new MessageLoop is created for each test. | 49 // since a new MessageLoop is created for each test. |
50 connection_ = WindowManagerConnection::Create(connector, identity); | 50 connection_ = WindowManagerConnection::Create(connector, identity); |
51 } | 51 } |
52 ~PlatformTestHelperMus() override {} | 52 ~PlatformTestHelperMus() override {} |
53 | 53 |
54 private: | 54 private: |
55 std::unique_ptr<WindowManagerConnection> connection_; | 55 std::unique_ptr<WindowManagerConnection> connection_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(PlatformTestHelperMus); | 57 DISALLOW_COPY_AND_ASSIGN(PlatformTestHelperMus); |
58 }; | 58 }; |
59 | 59 |
60 std::unique_ptr<PlatformTestHelper> CreatePlatformTestHelper( | 60 std::unique_ptr<PlatformTestHelper> CreatePlatformTestHelper( |
61 const shell::Identity& identity, | 61 const service_manager::Identity& identity, |
62 const base::Callback<shell::Connector*(void)>& callback) { | 62 const base::Callback<service_manager::Connector*(void)>& callback) { |
63 return base::MakeUnique<PlatformTestHelperMus>(callback.Run(), identity); | 63 return base::MakeUnique<PlatformTestHelperMus>(callback.Run(), identity); |
64 } | 64 } |
65 | 65 |
66 } // namespace | 66 } // namespace |
67 | 67 |
68 class ShellConnection { | 68 class ShellConnection { |
69 public: | 69 public: |
70 ShellConnection() : thread_("Persistent shell connections") { | 70 ShellConnection() : thread_("Persistent service_manager connections") { |
71 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 71 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
72 base::WaitableEvent::InitialState::NOT_SIGNALED); | 72 base::WaitableEvent::InitialState::NOT_SIGNALED); |
73 base::Thread::Options options; | 73 base::Thread::Options options; |
74 thread_.StartWithOptions(options); | 74 thread_.StartWithOptions(options); |
75 thread_.task_runner()->PostTask( | 75 thread_.task_runner()->PostTask( |
76 FROM_HERE, base::Bind(&ShellConnection::SetUpConnections, | 76 FROM_HERE, base::Bind(&ShellConnection::SetUpConnections, |
77 base::Unretained(this), &wait)); | 77 base::Unretained(this), &wait)); |
78 wait.Wait(); | 78 wait.Wait(); |
79 | 79 |
80 // WindowManagerConnection cannot be created from here yet, although the | 80 // WindowManagerConnection cannot be created from here yet, although the |
81 // connector and identity are available at this point. This is because | 81 // connector and identity are available at this point. This is because |
82 // WindowManagerConnection needs a ViewsDelegate and a MessageLoop to have | 82 // WindowManagerConnection needs a ViewsDelegate and a MessageLoop to have |
83 // been installed first. So delay the creation until the necessary | 83 // been installed first. So delay the creation until the necessary |
84 // dependencies have been met. | 84 // dependencies have been met. |
85 PlatformTestHelper::set_factory(base::Bind( | 85 PlatformTestHelper::set_factory(base::Bind( |
86 &CreatePlatformTestHelper, shell_identity_, | 86 &CreatePlatformTestHelper, service_manager_identity_, |
87 base::Bind(&ShellConnection::GetConnector, base::Unretained(this)))); | 87 base::Bind(&ShellConnection::GetConnector, base::Unretained(this)))); |
88 } | 88 } |
89 | 89 |
90 ~ShellConnection() { | 90 ~ShellConnection() { |
91 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 91 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
92 base::WaitableEvent::InitialState::NOT_SIGNALED); | 92 base::WaitableEvent::InitialState::NOT_SIGNALED); |
93 thread_.task_runner()->PostTask( | 93 thread_.task_runner()->PostTask( |
94 FROM_HERE, base::Bind(&ShellConnection::TearDownConnections, | 94 FROM_HERE, base::Bind(&ShellConnection::TearDownConnections, |
95 base::Unretained(this), &wait)); | 95 base::Unretained(this), &wait)); |
96 wait.Wait(); | 96 wait.Wait(); |
97 } | 97 } |
98 | 98 |
99 private: | 99 private: |
100 shell::Connector* GetConnector() { | 100 service_manager::Connector* GetConnector() { |
101 shell_connector_.reset(); | 101 service_manager_connector_.reset(); |
102 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 102 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
103 base::WaitableEvent::InitialState::NOT_SIGNALED); | 103 base::WaitableEvent::InitialState::NOT_SIGNALED); |
104 thread_.task_runner()->PostTask(FROM_HERE, | 104 thread_.task_runner()->PostTask(FROM_HERE, |
105 base::Bind(&ShellConnection::CloneConnector, | 105 base::Bind(&ShellConnection::CloneConnector, |
106 base::Unretained(this), &wait)); | 106 base::Unretained(this), &wait)); |
107 wait.Wait(); | 107 wait.Wait(); |
108 DCHECK(shell_connector_); | 108 DCHECK(service_manager_connector_); |
109 return shell_connector_.get(); | 109 return service_manager_connector_.get(); |
110 } | 110 } |
111 | 111 |
112 void CloneConnector(base::WaitableEvent* wait) { | 112 void CloneConnector(base::WaitableEvent* wait) { |
113 shell_connector_ = shell_connection_->connector()->Clone(); | 113 service_manager_connector_ = |
| 114 service_manager_connection_->connector()->Clone(); |
114 wait->Signal(); | 115 wait->Signal(); |
115 } | 116 } |
116 | 117 |
117 void SetUpConnections(base::WaitableEvent* wait) { | 118 void SetUpConnections(base::WaitableEvent* wait) { |
118 background_shell_ = base::MakeUnique<shell::BackgroundShell>(); | 119 background_shell_ = base::MakeUnique<service_manager::BackgroundShell>(); |
119 background_shell_->Init(nullptr); | 120 background_shell_->Init(nullptr); |
120 service_ = base::MakeUnique<DefaultService>(); | 121 service_ = base::MakeUnique<DefaultService>(); |
121 shell_connection_ = base::MakeUnique<shell::ServiceContext>( | 122 service_manager_connection_ = |
122 service_.get(), background_shell_->CreateServiceRequest(GetTestName())); | 123 base::MakeUnique<service_manager::ServiceContext>( |
| 124 service_.get(), |
| 125 background_shell_->CreateServiceRequest(GetTestName())); |
123 | 126 |
124 // ui/views/mus requires a WindowManager running, so launch test_wm. | 127 // ui/views/mus requires a WindowManager running, so launch test_wm. |
125 shell::Connector* connector = shell_connection_->connector(); | 128 service_manager::Connector* connector = |
| 129 service_manager_connection_->connector(); |
126 connector->Connect("service:test_wm"); | 130 connector->Connect("service:test_wm"); |
127 shell_connector_ = connector->Clone(); | 131 service_manager_connector_ = connector->Clone(); |
128 shell_identity_ = shell_connection_->identity(); | 132 service_manager_identity_ = service_manager_connection_->identity(); |
129 wait->Signal(); | 133 wait->Signal(); |
130 } | 134 } |
131 | 135 |
132 void TearDownConnections(base::WaitableEvent* wait) { | 136 void TearDownConnections(base::WaitableEvent* wait) { |
133 shell_connection_.reset(); | 137 service_manager_connection_.reset(); |
134 wait->Signal(); | 138 wait->Signal(); |
135 } | 139 } |
136 | 140 |
137 // Returns the name of the test executable, e.g. "exe:views_mus_unittests". | 141 // Returns the name of the test executable, e.g. "exe:views_mus_unittests". |
138 std::string GetTestName() { | 142 std::string GetTestName() { |
139 base::FilePath executable = base::CommandLine::ForCurrentProcess() | 143 base::FilePath executable = base::CommandLine::ForCurrentProcess() |
140 ->GetProgram() | 144 ->GetProgram() |
141 .BaseName() | 145 .BaseName() |
142 .RemoveExtension(); | 146 .RemoveExtension(); |
143 return std::string("exe:") + executable.MaybeAsASCII(); | 147 return std::string("exe:") + executable.MaybeAsASCII(); |
144 } | 148 } |
145 | 149 |
146 base::Thread thread_; | 150 base::Thread thread_; |
147 std::unique_ptr<shell::BackgroundShell> background_shell_; | 151 std::unique_ptr<service_manager::BackgroundShell> background_shell_; |
148 std::unique_ptr<shell::ServiceContext> shell_connection_; | 152 std::unique_ptr<service_manager::ServiceContext> service_manager_connection_; |
149 std::unique_ptr<DefaultService> service_; | 153 std::unique_ptr<DefaultService> service_; |
150 std::unique_ptr<shell::Connector> shell_connector_; | 154 std::unique_ptr<service_manager::Connector> service_manager_connector_; |
151 shell::Identity shell_identity_; | 155 service_manager::Identity service_manager_identity_; |
152 | 156 |
153 DISALLOW_COPY_AND_ASSIGN(ShellConnection); | 157 DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
154 }; | 158 }; |
155 | 159 |
156 ViewsMusTestSuite::ViewsMusTestSuite(int argc, char** argv) | 160 ViewsMusTestSuite::ViewsMusTestSuite(int argc, char** argv) |
157 : ViewsTestSuite(argc, argv) {} | 161 : ViewsTestSuite(argc, argv) {} |
158 | 162 |
159 ViewsMusTestSuite::~ViewsMusTestSuite() {} | 163 ViewsMusTestSuite::~ViewsMusTestSuite() {} |
160 | 164 |
161 void ViewsMusTestSuite::Initialize() { | 165 void ViewsMusTestSuite::Initialize() { |
162 PlatformTestHelper::SetIsMus(); | 166 PlatformTestHelper::SetIsMus(); |
163 // Let other services know that we're running in tests. Do this with a | 167 // Let other services know that we're running in tests. Do this with a |
164 // command line flag to avoid making blocking calls to other processes for | 168 // command line flag to avoid making blocking calls to other processes for |
165 // setup for tests (e.g. to unlock the screen in the window manager). | 169 // setup for tests (e.g. to unlock the screen in the window manager). |
166 EnsureCommandLineSwitch(ui::switches::kUseTestConfig); | 170 EnsureCommandLineSwitch(ui::switches::kUseTestConfig); |
167 | 171 |
168 ViewsTestSuite::Initialize(); | 172 ViewsTestSuite::Initialize(); |
169 shell_connections_ = base::MakeUnique<ShellConnection>(); | 173 service_manager_connections_ = base::MakeUnique<ShellConnection>(); |
170 } | 174 } |
171 | 175 |
172 void ViewsMusTestSuite::Shutdown() { | 176 void ViewsMusTestSuite::Shutdown() { |
173 shell_connections_.reset(); | 177 service_manager_connections_.reset(); |
174 ViewsTestSuite::Shutdown(); | 178 ViewsTestSuite::Shutdown(); |
175 } | 179 } |
176 | 180 |
177 } // namespace views | 181 } // namespace views |
OLD | NEW |