| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/ui/public/cpp/tests/window_server_test_base.h" | 5 #include "services/ui/public/cpp/tests/window_server_test_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 11 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "services/shell/public/cpp/connector.h" | 14 #include "services/shell/public/cpp/connector.h" |
| 14 #include "services/ui/public/cpp/window.h" | 15 #include "services/ui/public/cpp/window.h" |
| 15 #include "services/ui/public/cpp/window_tree_client.h" | 16 #include "services/ui/public/cpp/window_tree_client.h" |
| 16 #include "services/ui/public/cpp/window_tree_host_factory.h" | 17 #include "services/ui/public/cpp/window_tree_host_factory.h" |
| 17 | 18 |
| 18 namespace ui { | 19 namespace ui { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 base::RunLoop* current_run_loop = nullptr; | 22 base::RunLoop* current_run_loop = nullptr; |
| 22 | 23 |
| 23 void TimeoutRunLoop(const base::Closure& timeout_task, bool* timeout) { | 24 void TimeoutRunLoop(const base::Closure& timeout_task, bool* timeout) { |
| 24 CHECK(current_run_loop); | 25 CHECK(current_run_loop); |
| 25 *timeout = true; | 26 *timeout = true; |
| 26 timeout_task.Run(); | 27 timeout_task.Run(); |
| 27 } | 28 } |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 WindowServerTestBase::WindowServerTestBase() | 32 WindowServerTestBase::WindowServerTestBase() |
| 32 : most_recent_client_(nullptr), | 33 : most_recent_client_(nullptr), |
| 33 window_manager_(nullptr), | 34 window_manager_(nullptr), |
| 34 window_manager_delegate_(nullptr), | 35 window_manager_delegate_(nullptr), |
| 35 window_manager_client_(nullptr), | 36 window_manager_client_(nullptr) {} |
| 36 window_tree_client_destroyed_(false) {} | |
| 37 | 37 |
| 38 WindowServerTestBase::~WindowServerTestBase() {} | 38 WindowServerTestBase::~WindowServerTestBase() { |
| 39 window_tree_clients_.clear(); |
| 40 } |
| 39 | 41 |
| 40 // static | 42 // static |
| 41 bool WindowServerTestBase::DoRunLoopWithTimeout() { | 43 bool WindowServerTestBase::DoRunLoopWithTimeout() { |
| 42 if (current_run_loop != nullptr) | 44 if (current_run_loop != nullptr) |
| 43 return false; | 45 return false; |
| 44 | 46 |
| 45 bool timeout = false; | 47 bool timeout = false; |
| 46 base::RunLoop run_loop; | 48 base::RunLoop run_loop; |
| 47 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 49 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 48 FROM_HERE, base::Bind(&TimeoutRunLoop, run_loop.QuitClosure(), &timeout), | 50 FROM_HERE, base::Bind(&TimeoutRunLoop, run_loop.QuitClosure(), &timeout), |
| 49 TestTimeouts::action_timeout()); | 51 TestTimeouts::action_timeout()); |
| 50 | 52 |
| 51 current_run_loop = &run_loop; | 53 current_run_loop = &run_loop; |
| 52 current_run_loop->Run(); | 54 current_run_loop->Run(); |
| 53 current_run_loop = nullptr; | 55 current_run_loop = nullptr; |
| 54 return !timeout; | 56 return !timeout; |
| 55 } | 57 } |
| 56 | 58 |
| 57 // static | 59 // static |
| 58 bool WindowServerTestBase::QuitRunLoop() { | 60 bool WindowServerTestBase::QuitRunLoop() { |
| 59 if (!current_run_loop) | 61 if (!current_run_loop) |
| 60 return false; | 62 return false; |
| 61 | 63 |
| 62 current_run_loop->Quit(); | 64 current_run_loop->Quit(); |
| 63 current_run_loop = nullptr; | 65 current_run_loop = nullptr; |
| 64 return true; | 66 return true; |
| 65 } | 67 } |
| 66 | 68 |
| 69 void WindowServerTestBase::DeleteWindowTreeClient( |
| 70 ui::WindowTreeClient* client) { |
| 71 for (auto iter = window_tree_clients_.begin(); |
| 72 iter != window_tree_clients_.end(); ++iter) { |
| 73 if (iter->get() == client) { |
| 74 window_tree_clients_.erase(iter); |
| 75 return; |
| 76 } |
| 77 } |
| 78 NOTREACHED(); |
| 79 } |
| 80 |
| 67 void WindowServerTestBase::SetUp() { | 81 void WindowServerTestBase::SetUp() { |
| 68 WindowServerServiceTestBase::SetUp(); | 82 WindowServerServiceTestBase::SetUp(); |
| 69 | 83 |
| 70 CreateWindowTreeHost(connector(), this, &host_, this); | 84 std::unique_ptr<WindowTreeClient> window_manager_window_tree_client = |
| 85 CreateWindowTreeHost(connector(), this, &host_, this); |
| 86 window_manager_ = window_manager_window_tree_client.get(); |
| 87 window_tree_clients_.insert(std::move(window_manager_window_tree_client)); |
| 71 | 88 |
| 72 ASSERT_TRUE(DoRunLoopWithTimeout()); // RunLoop should be quit by OnEmbed(). | 89 ASSERT_TRUE(DoRunLoopWithTimeout()); // RunLoop should be quit by OnEmbed(). |
| 73 std::swap(window_manager_, most_recent_client_); | |
| 74 } | 90 } |
| 75 | 91 |
| 76 bool WindowServerTestBase::OnConnect(const shell::Identity& remote_identity, | 92 bool WindowServerTestBase::OnConnect(const shell::Identity& remote_identity, |
| 77 shell::InterfaceRegistry* registry) { | 93 shell::InterfaceRegistry* registry) { |
| 78 registry->AddInterface<mojom::WindowTreeClient>(this); | 94 registry->AddInterface<mojom::WindowTreeClient>(this); |
| 79 return true; | 95 return true; |
| 80 } | 96 } |
| 81 | 97 |
| 82 void WindowServerTestBase::OnEmbed(Window* root) { | 98 void WindowServerTestBase::OnEmbed(Window* root) { |
| 83 most_recent_client_ = root->window_tree(); | 99 most_recent_client_ = root->window_tree(); |
| 84 EXPECT_TRUE(QuitRunLoop()); | 100 EXPECT_TRUE(QuitRunLoop()); |
| 85 ASSERT_TRUE(window_manager_client_); | 101 ASSERT_TRUE(window_manager_client_); |
| 86 window_manager_client_->AddActivationParent(root); | 102 window_manager_client_->AddActivationParent(root); |
| 87 } | 103 } |
| 88 | 104 |
| 89 void WindowServerTestBase::OnDidDestroyClient(WindowTreeClient* client) { | 105 void WindowServerTestBase::OnLostConnection(WindowTreeClient* client) { |
| 90 window_tree_client_destroyed_ = true; | 106 window_tree_client_lost_connection_ = true; |
| 107 DeleteWindowTreeClient(client); |
| 108 } |
| 109 |
| 110 void WindowServerTestBase::OnEmbedRootDestroyed(Window* root) { |
| 111 DeleteWindowTreeClient(root->window_tree()); |
| 91 } | 112 } |
| 92 | 113 |
| 93 void WindowServerTestBase::OnPointerEventObserved(const ui::PointerEvent& event, | 114 void WindowServerTestBase::OnPointerEventObserved(const ui::PointerEvent& event, |
| 94 Window* target) {} | 115 Window* target) {} |
| 95 | 116 |
| 96 void WindowServerTestBase::SetWindowManagerClient(WindowManagerClient* client) { | 117 void WindowServerTestBase::SetWindowManagerClient(WindowManagerClient* client) { |
| 97 window_manager_client_ = client; | 118 window_manager_client_ = client; |
| 98 } | 119 } |
| 99 | 120 |
| 100 bool WindowServerTestBase::OnWmSetBounds(Window* window, gfx::Rect* bounds) { | 121 bool WindowServerTestBase::OnWmSetBounds(Window* window, gfx::Rect* bounds) { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 171 |
| 151 mojom::EventResult WindowServerTestBase::OnAccelerator(uint32_t accelerator_id, | 172 mojom::EventResult WindowServerTestBase::OnAccelerator(uint32_t accelerator_id, |
| 152 const ui::Event& event) { | 173 const ui::Event& event) { |
| 153 return window_manager_delegate_ | 174 return window_manager_delegate_ |
| 154 ? window_manager_delegate_->OnAccelerator(accelerator_id, event) | 175 ? window_manager_delegate_->OnAccelerator(accelerator_id, event) |
| 155 : mojom::EventResult::UNHANDLED; | 176 : mojom::EventResult::UNHANDLED; |
| 156 } | 177 } |
| 157 | 178 |
| 158 void WindowServerTestBase::Create(const shell::Identity& remote_identity, | 179 void WindowServerTestBase::Create(const shell::Identity& remote_identity, |
| 159 mojom::WindowTreeClientRequest request) { | 180 mojom::WindowTreeClientRequest request) { |
| 160 new WindowTreeClient(this, nullptr, std::move(request)); | 181 window_tree_clients_.insert( |
| 182 base::MakeUnique<WindowTreeClient>(this, nullptr, std::move(request))); |
| 161 } | 183 } |
| 162 | 184 |
| 163 } // namespace ui | 185 } // namespace ui |
| OLD | NEW |