| 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/run_all_unittests.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/run_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/threading/simple_thread.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "services/shell/background/background_shell.h" |
| 14 #include "services/shell/public/cpp/connector.h" |
| 15 #include "services/shell/public/cpp/shell_client.h" |
| 16 #include "services/shell/public/cpp/shell_connection.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "ui/views/mus/window_manager_connection.h" |
| 19 #include "ui/views/test/platform_test_helper.h" |
| 20 #include "ui/views/views_delegate.h" |
| 21 |
| 22 namespace views { |
| 23 namespace { |
| 24 |
| 25 const char kTestName[] = "exe:views_mus_unittests"; |
| 26 |
| 27 class DefaultShellClient : public shell::ShellClient { |
| 28 public: |
| 29 DefaultShellClient() {} |
| 30 ~DefaultShellClient() override {} |
| 31 |
| 32 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(DefaultShellClient); |
| 34 }; |
| 35 |
| 36 class PlatformTestHelperMus : public PlatformTestHelper { |
| 37 public: |
| 38 PlatformTestHelperMus() {} |
| 39 ~PlatformTestHelperMus() override {} |
| 40 |
| 41 // PlatformTestHelper: |
| 42 bool IsMus() const override { return true; } |
| 43 |
| 44 private: |
| 45 DISALLOW_COPY_AND_ASSIGN(PlatformTestHelperMus); |
| 46 }; |
| 47 |
| 48 std::unique_ptr<PlatformTestHelper> CreatePlatformTestHelper( |
| 49 shell::Connector* connector, |
| 50 const shell::Identity& identity) { |
| 51 if (!WindowManagerConnection::Exists()) |
| 52 WindowManagerConnection::Create(connector, identity); |
| 53 return base::WrapUnique(new PlatformTestHelperMus); |
| 54 } |
| 55 |
| 56 class ShellConnection { |
| 57 public: |
| 58 ShellConnection() : thread_("Persistent shell connections") { |
| 59 base::WaitableEvent wait(false, false); |
| 60 base::Thread::Options options; |
| 61 thread_.StartWithOptions(options); |
| 62 thread_.task_runner()->PostTask( |
| 63 FROM_HERE, base::Bind(&ShellConnection::SetUpConnections, |
| 64 base::Unretained(this), &wait)); |
| 65 wait.Wait(); |
| 66 |
| 67 // WindowManagerConnection cannot be created from here yet, although the |
| 68 // connector and identity are available at this point. This is because |
| 69 // WindowManagerConnection needs a ViewsDelegate and a MessageLoop to have |
| 70 // been installed first. So delay the creation until the necessary |
| 71 // dependencies have been met. |
| 72 PlatformTestHelper::set_factory(base::Bind( |
| 73 &CreatePlatformTestHelper, shell_connector_.get(), shell_identity_)); |
| 74 } |
| 75 |
| 76 ~ShellConnection() { |
| 77 views::WindowManagerConnection::Reset(); |
| 78 base::WaitableEvent wait(false, false); |
| 79 thread_.task_runner()->PostTask( |
| 80 FROM_HERE, base::Bind(&ShellConnection::TearDownConnections, |
| 81 base::Unretained(this), &wait)); |
| 82 wait.Wait(); |
| 83 } |
| 84 |
| 85 private: |
| 86 void SetUpConnections(base::WaitableEvent* wait) { |
| 87 background_shell_.reset(new shell::BackgroundShell); |
| 88 background_shell_->Init(nullptr); |
| 89 shell_client_.reset(new DefaultShellClient); |
| 90 shell_connection_.reset(new shell::ShellConnection( |
| 91 shell_client_.get(), |
| 92 background_shell_->CreateShellClientRequest(kTestName))); |
| 93 |
| 94 // ui/views/mus requires a WindowManager running, for now use the desktop |
| 95 // one. |
| 96 shell::Connector* connector = shell_connection_->connector(); |
| 97 connector->Connect("mojo:desktop_wm"); |
| 98 shell_connector_ = connector->Clone(); |
| 99 shell_identity_ = shell_connection_->identity(); |
| 100 wait->Signal(); |
| 101 } |
| 102 |
| 103 void TearDownConnections(base::WaitableEvent* wait) { |
| 104 shell_connection_.reset(); |
| 105 wait->Signal(); |
| 106 } |
| 107 |
| 108 base::Thread thread_; |
| 109 std::unique_ptr<shell::BackgroundShell> background_shell_; |
| 110 std::unique_ptr<shell::ShellConnection> shell_connection_; |
| 111 std::unique_ptr<DefaultShellClient> shell_client_; |
| 112 std::unique_ptr<shell::Connector> shell_connector_; |
| 113 shell::Identity shell_identity_; |
| 114 |
| 115 DISALLOW_COPY_AND_ASSIGN(ShellConnection); |
| 116 }; |
| 117 |
| 118 class ViewMusTestSuite : public ViewTestSuite { |
| 119 public: |
| 120 ViewMusTestSuite(int argc, char** argv) : ViewTestSuite(argc, argv) {} |
| 121 |
| 122 protected: |
| 123 void Initialize() override { |
| 124 ViewTestSuite::Initialize(); |
| 125 shell_connections_.reset(new ShellConnection); |
| 126 } |
| 127 |
| 128 void Shutdown() override { |
| 129 shell_connections_.reset(); |
| 130 ViewTestSuite::Shutdown(); |
| 131 } |
| 132 |
| 133 private: |
| 134 std::unique_ptr<ShellConnection> shell_connections_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(ViewMusTestSuite); |
| 137 }; |
| 138 |
| 139 } // namespace |
| 140 } // namespace views |
| 6 | 141 |
| 7 int MasterProcessMain(int argc, char** argv) { | 142 int MasterProcessMain(int argc, char** argv) { |
| 8 return views::ViewsMusTestSuite(argc, argv).RunTests(); | 143 return views::ViewMusTestSuite(argc, argv).RunTests(); |
| 9 } | 144 } |
| OLD | NEW |