| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/views/mus/views_aura_mus_test_suite.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/synchronization/waitable_event.h" | |
| 15 #include "base/threading/simple_thread.h" | |
| 16 #include "base/threading/thread.h" | |
| 17 #include "services/service_manager/background/background_service_manager.h" | |
| 18 #include "services/service_manager/public/cpp/connector.h" | |
| 19 #include "services/service_manager/public/cpp/service.h" | |
| 20 #include "services/service_manager/public/cpp/service_context.h" | |
| 21 #include "services/ui/common/switches.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 #include "ui/aura/mus/window_tree_host_mus.h" | |
| 24 #include "ui/aura/test/env_test_helper.h" | |
| 25 #include "ui/aura/window.h" | |
| 26 #include "ui/gl/gl_switches.h" | |
| 27 #include "ui/views/mus/desktop_window_tree_host_mus.h" | |
| 28 #include "ui/views/mus/mus_client.h" | |
| 29 #include "ui/views/mus/test_utils.h" | |
| 30 #include "ui/views/test/platform_test_helper.h" | |
| 31 #include "ui/views/test/views_test_helper_aura.h" | |
| 32 #include "ui/views/views_delegate.h" | |
| 33 #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" | |
| 34 | |
| 35 namespace views { | |
| 36 namespace { | |
| 37 | |
| 38 void EnsureCommandLineSwitch(const std::string& name) { | |
| 39 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
| 40 if (!cmd_line->HasSwitch(name)) | |
| 41 cmd_line->AppendSwitch(name); | |
| 42 } | |
| 43 | |
| 44 class DefaultService : public service_manager::Service { | |
| 45 public: | |
| 46 DefaultService() {} | |
| 47 ~DefaultService() override {} | |
| 48 | |
| 49 // service_manager::Service: | |
| 50 bool OnConnect(const service_manager::ServiceInfo& remote_info, | |
| 51 service_manager::InterfaceRegistry* registry) override { | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 private: | |
| 56 DISALLOW_COPY_AND_ASSIGN(DefaultService); | |
| 57 }; | |
| 58 | |
| 59 class PlatformTestHelperMus : public PlatformTestHelper { | |
| 60 public: | |
| 61 PlatformTestHelperMus(service_manager::Connector* connector, | |
| 62 const service_manager::Identity& identity) { | |
| 63 aura::test::EnvTestHelper().SetWindowTreeClient(nullptr); | |
| 64 // It is necessary to recreate the MusClient for each test, | |
| 65 // since a new MessageLoop is created for each test. | |
| 66 mus_client_ = test::MusClientTestApi::Create(connector, identity); | |
| 67 } | |
| 68 ~PlatformTestHelperMus() override { | |
| 69 aura::test::EnvTestHelper().SetWindowTreeClient(nullptr); | |
| 70 } | |
| 71 | |
| 72 // PlatformTestHelper: | |
| 73 void OnTestHelperCreated(ViewsTestHelper* helper) override { | |
| 74 static_cast<ViewsTestHelperAura*>(helper)->EnableMusWithWindowTreeClient( | |
| 75 mus_client_->window_tree_client()); | |
| 76 } | |
| 77 void SimulateNativeDestroy(Widget* widget) override { | |
| 78 aura::WindowTreeHostMus* window_tree_host = | |
| 79 static_cast<aura::WindowTreeHostMus*>( | |
| 80 widget->GetNativeView()->GetHost()); | |
| 81 static_cast<aura::WindowTreeClientDelegate*>(mus_client_.get()) | |
| 82 ->OnEmbedRootDestroyed(window_tree_host); | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 std::unique_ptr<MusClient> mus_client_; | |
| 87 | |
| 88 DISALLOW_COPY_AND_ASSIGN(PlatformTestHelperMus); | |
| 89 }; | |
| 90 | |
| 91 std::unique_ptr<PlatformTestHelper> CreatePlatformTestHelper( | |
| 92 const service_manager::Identity& identity, | |
| 93 const base::Callback<service_manager::Connector*(void)>& callback) { | |
| 94 return base::MakeUnique<PlatformTestHelperMus>(callback.Run(), identity); | |
| 95 } | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 class ServiceManagerConnection { | |
| 100 public: | |
| 101 ServiceManagerConnection() | |
| 102 : thread_("Persistent service_manager connections") { | |
| 103 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 104 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 105 base::Thread::Options options; | |
| 106 thread_.StartWithOptions(options); | |
| 107 thread_.task_runner()->PostTask( | |
| 108 FROM_HERE, base::Bind(&ServiceManagerConnection::SetUpConnections, | |
| 109 base::Unretained(this), &wait)); | |
| 110 wait.Wait(); | |
| 111 | |
| 112 // WindowManagerConnection cannot be created from here yet, although the | |
| 113 // connector and identity are available at this point. This is because | |
| 114 // WindowManagerConnection needs a ViewsDelegate and a MessageLoop to have | |
| 115 // been installed first. So delay the creation until the necessary | |
| 116 // dependencies have been met. | |
| 117 PlatformTestHelper::set_factory( | |
| 118 base::Bind(&CreatePlatformTestHelper, service_manager_identity_, | |
| 119 base::Bind(&ServiceManagerConnection::GetConnector, | |
| 120 base::Unretained(this)))); | |
| 121 } | |
| 122 | |
| 123 ~ServiceManagerConnection() { | |
| 124 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 125 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 126 thread_.task_runner()->PostTask( | |
| 127 FROM_HERE, base::Bind(&ServiceManagerConnection::TearDownConnections, | |
| 128 base::Unretained(this), &wait)); | |
| 129 wait.Wait(); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 service_manager::Connector* GetConnector() { | |
| 134 service_manager_connector_.reset(); | |
| 135 base::WaitableEvent wait(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 136 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
| 137 thread_.task_runner()->PostTask( | |
| 138 FROM_HERE, base::Bind(&ServiceManagerConnection::CloneConnector, | |
| 139 base::Unretained(this), &wait)); | |
| 140 wait.Wait(); | |
| 141 DCHECK(service_manager_connector_); | |
| 142 return service_manager_connector_.get(); | |
| 143 } | |
| 144 | |
| 145 void CloneConnector(base::WaitableEvent* wait) { | |
| 146 service_manager_connector_ = context_->connector()->Clone(); | |
| 147 wait->Signal(); | |
| 148 } | |
| 149 | |
| 150 void SetUpConnections(base::WaitableEvent* wait) { | |
| 151 background_service_manager_ = | |
| 152 base::MakeUnique<service_manager::BackgroundServiceManager>(); | |
| 153 background_service_manager_->Init(nullptr); | |
| 154 context_ = | |
| 155 base::MakeUnique<service_manager::ServiceContext>( | |
| 156 base::MakeUnique<DefaultService>(), | |
| 157 background_service_manager_->CreateServiceRequest(GetTestName())); | |
| 158 | |
| 159 // ui/views/mus requires a WindowManager running, so launch test_wm. | |
| 160 service_manager::Connector* connector = context_->connector(); | |
| 161 connector->Connect("test_wm"); | |
| 162 service_manager_connector_ = connector->Clone(); | |
| 163 service_manager_identity_ = context_->identity(); | |
| 164 wait->Signal(); | |
| 165 } | |
| 166 | |
| 167 void TearDownConnections(base::WaitableEvent* wait) { | |
| 168 context_.reset(); | |
| 169 wait->Signal(); | |
| 170 } | |
| 171 | |
| 172 // Returns the name of the test executable, e.g. | |
| 173 // "views_mus_unittests". | |
| 174 std::string GetTestName() { | |
| 175 base::FilePath executable = base::CommandLine::ForCurrentProcess() | |
| 176 ->GetProgram() | |
| 177 .BaseName() | |
| 178 .RemoveExtension(); | |
| 179 return std::string("") + executable.MaybeAsASCII(); | |
| 180 } | |
| 181 | |
| 182 base::Thread thread_; | |
| 183 std::unique_ptr<service_manager::BackgroundServiceManager> | |
| 184 background_service_manager_; | |
| 185 std::unique_ptr<service_manager::ServiceContext> context_; | |
| 186 std::unique_ptr<service_manager::Connector> service_manager_connector_; | |
| 187 service_manager::Identity service_manager_identity_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(ServiceManagerConnection); | |
| 190 }; | |
| 191 | |
| 192 ViewsAuraMusTestSuite::ViewsAuraMusTestSuite(int argc, char** argv) | |
| 193 : ViewsTestSuite(argc, argv) {} | |
| 194 | |
| 195 ViewsAuraMusTestSuite::~ViewsAuraMusTestSuite() {} | |
| 196 | |
| 197 void ViewsAuraMusTestSuite::Initialize() { | |
| 198 PlatformTestHelper::SetIsMus(); | |
| 199 // Let other services know that we're running in tests. Do this with a | |
| 200 // command line flag to avoid making blocking calls to other processes for | |
| 201 // setup for tests (e.g. to unlock the screen in the window manager). | |
| 202 EnsureCommandLineSwitch(ui::switches::kUseTestConfig); | |
| 203 | |
| 204 EnsureCommandLineSwitch(switches::kOverrideUseGLWithOSMesaForTests); | |
| 205 | |
| 206 ViewsTestSuite::Initialize(); | |
| 207 service_manager_connections_ = base::MakeUnique<ServiceManagerConnection>(); | |
| 208 } | |
| 209 | |
| 210 void ViewsAuraMusTestSuite::Shutdown() { | |
| 211 service_manager_connections_.reset(); | |
| 212 ViewsTestSuite::Shutdown(); | |
| 213 } | |
| 214 | |
| 215 } // namespace views | |
| OLD | NEW |