[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 5 | #include "ash/public/cpp/test/shell_test_api.h" |
sky | 5ad143a | 2017-03-22 04:31:23 | [diff] [blame] | 6 | |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 7 | #include <memory> |
xiyuan | 317fdbe | 2017-05-31 15:02:02 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
Evan Stade | 7768d47 | 2018-10-25 22:40:17 | [diff] [blame] | 10 | #include "ash/accelerators/accelerator_commands.h" |
Mitsuru Oshima | 8abd8b1d0 | 2019-05-09 17:55:23 | [diff] [blame] | 11 | #include "ash/accelerometer/accelerometer_reader.h" |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 12 | #include "ash/app_list/app_list_controller_impl.h" |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 13 | #include "ash/app_list/views/app_list_view.h" |
Darren Shen | 7daf3e1 | 2019-07-02 07:25:49 | [diff] [blame] | 14 | #include "ash/keyboard/keyboard_controller_impl.h" |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 15 | #include "ash/public/cpp/autotest_private_api_utils.h" |
Mitsuru Oshima | a6adca4f | 2019-07-10 23:30:46 | [diff] [blame] | 16 | #include "ash/public/cpp/tablet_mode_observer.h" |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 17 | #include "ash/root_window_controller.h" |
| 18 | #include "ash/shell.h" |
Toni Barzic | 9d0c82f4 | 2017-12-06 00:53:52 | [diff] [blame] | 19 | #include "ash/system/power/backlights_forced_off_setter.h" |
Qiang Xu | 0a86020f | 2017-10-12 08:34:54 | [diff] [blame] | 20 | #include "ash/system/power/power_button_controller.h" |
Jun Mukai | 2b91e70 | 2019-09-05 18:24:38 | [diff] [blame] | 21 | #include "ash/wm/overview/overview_animation_state_waiter.h" |
Sammie Quon | 80e82a1 | 2019-01-23 19:55:22 | [diff] [blame] | 22 | #include "ash/wm/overview/overview_controller.h" |
Evan Stade | 426a298 | 2018-10-19 22:42:07 | [diff] [blame] | 23 | #include "ash/wm/splitview/split_view_controller.h" |
Evan Stade | d689b33 | 2018-08-02 01:17:22 | [diff] [blame] | 24 | #include "ash/wm/tablet_mode/tablet_mode_controller.h" |
Ahmed Fakhry | fee7a0a | 2019-04-05 22:49:48 | [diff] [blame] | 25 | #include "ash/wm/workspace_controller.h" |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 26 | #include "base/run_loop.h" |
James Cook | bbe5cb1 | 2017-08-08 18:32:21 | [diff] [blame] | 27 | #include "components/prefs/testing_pref_service.h" |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 28 | #include "ui/aura/window_tree_host.h" |
| 29 | #include "ui/compositor/compositor.h" |
| 30 | #include "ui/compositor/compositor_observer.h" |
Sammie Quon | 358e957 | 2019-11-21 04:38:02 | [diff] [blame] | 31 | #include "ui/compositor/layer_animation_observer.h" |
Sammie Quon | 4986fdf | 2020-05-15 19:33:58 | [diff] [blame] | 32 | #include "ui/display/manager/display_manager.h" |
Ahmed Fakhry | 607ade0 | 2019-10-31 15:59:39 | [diff] [blame] | 33 | #include "ui/events/devices/device_data_manager_test_api.h" |
Jun Mukai | 2af6db5 | 2019-01-09 00:22:42 | [diff] [blame] | 34 | #include "ui/events/gesture_detection/gesture_configuration.h" |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 35 | |
| 36 | namespace ash { |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | // Wait for a WindowTreeHost to no longer be holding pointer events. |
| 40 | class PointerMoveLoopWaiter : public ui::CompositorObserver { |
| 41 | public: |
| 42 | explicit PointerMoveLoopWaiter(aura::WindowTreeHost* window_tree_host) |
| 43 | : window_tree_host_(window_tree_host) { |
| 44 | window_tree_host_->compositor()->AddObserver(this); |
| 45 | } |
| 46 | |
| 47 | ~PointerMoveLoopWaiter() override { |
| 48 | window_tree_host_->compositor()->RemoveObserver(this); |
| 49 | } |
| 50 | |
| 51 | void Wait() { |
| 52 | // Use a while loop as it's possible for releasing the lock to trigger |
| 53 | // processing events, which again grabs the lock. |
| 54 | while (window_tree_host_->holding_pointer_moves()) { |
| 55 | run_loop_ = std::make_unique<base::RunLoop>( |
| 56 | base::RunLoop::Type::kNestableTasksAllowed); |
| 57 | run_loop_->Run(); |
| 58 | run_loop_.reset(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // ui::CompositorObserver: |
| 63 | void OnCompositingEnded(ui::Compositor* compositor) override { |
| 64 | if (run_loop_) |
| 65 | run_loop_->Quit(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | aura::WindowTreeHost* window_tree_host_; |
| 70 | std::unique_ptr<base::RunLoop> run_loop_; |
| 71 | |
| 72 | DISALLOW_COPY_AND_ASSIGN(PointerMoveLoopWaiter); |
| 73 | }; |
| 74 | |
Sammie Quon | 358e957 | 2019-11-21 04:38:02 | [diff] [blame] | 75 | class WindowAnimationWaiter : public ui::LayerAnimationObserver { |
| 76 | public: |
| 77 | explicit WindowAnimationWaiter(aura::Window* window) |
| 78 | : animator_(window->layer()->GetAnimator()) { |
| 79 | animator_->AddObserver(this); |
| 80 | } |
| 81 | ~WindowAnimationWaiter() override = default; |
| 82 | |
| 83 | WindowAnimationWaiter(const WindowAnimationWaiter& other) = delete; |
| 84 | WindowAnimationWaiter& operator=(const WindowAnimationWaiter& rhs) = delete; |
| 85 | |
| 86 | // ui::LayerAnimationObserver: |
| 87 | void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override { |
| 88 | if (!animator_->is_animating()) { |
| 89 | animator_->RemoveObserver(this); |
| 90 | run_loop_.Quit(); |
| 91 | } |
| 92 | } |
| 93 | void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {} |
| 94 | void OnLayerAnimationScheduled( |
| 95 | ui::LayerAnimationSequence* sequence) override {} |
| 96 | |
| 97 | void Wait() { |
Sammie Quon | 358e957 | 2019-11-21 04:38:02 | [diff] [blame] | 98 | run_loop_.Run(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | ui::LayerAnimator* animator_; |
| 103 | base::RunLoop run_loop_; |
| 104 | }; |
| 105 | |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 106 | } // namespace |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 107 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 108 | ShellTestApi::ShellTestApi() : shell_(Shell::Get()) {} |
| 109 | ShellTestApi::~ShellTestApi() = default; |
James Cook | 00848f0 | 2018-01-05 18:02:57 | [diff] [blame] | 110 | |
Sammie Quon | 408e9da | 2019-06-20 19:36:21 | [diff] [blame] | 111 | // static |
| 112 | void ShellTestApi::SetTabletControllerUseScreenshotForTest( |
| 113 | bool use_screenshot) { |
| 114 | TabletModeController::SetUseScreenshotForTest(use_screenshot); |
| 115 | } |
| 116 | |
James Cook | 459354b3 | 2017-09-27 23:18:01 | [diff] [blame] | 117 | MessageCenterController* ShellTestApi::message_center_controller() { |
| 118 | return shell_->message_center_controller_.get(); |
| 119 | } |
| 120 | |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 121 | WorkspaceController* ShellTestApi::workspace_controller() { |
Ahmed Fakhry | fee7a0a | 2019-04-05 22:49:48 | [diff] [blame] | 122 | // TODO(afakhry): Split this into two, one for root, and one for context. |
| 123 | return GetActiveWorkspaceController(shell_->GetPrimaryRootWindow()); |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 126 | ScreenPositionController* ShellTestApi::screen_position_controller() { |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 127 | return shell_->screen_position_controller_.get(); |
| 128 | } |
| 129 | |
erg | 36cfb95 | 2017-06-21 19:41:45 | [diff] [blame] | 130 | NativeCursorManagerAsh* ShellTestApi::native_cursor_manager_ash() { |
[email protected] | c0ff034 | 2013-02-27 00:41:29 | [diff] [blame] | 131 | return shell_->native_cursor_manager_; |
| 132 | } |
| 133 | |
[email protected] | 093b8d64 | 2014-04-03 20:59:28 | [diff] [blame] | 134 | DragDropController* ShellTestApi::drag_drop_controller() { |
[email protected] | c086bab | 2013-06-06 21:46:44 | [diff] [blame] | 135 | return shell_->drag_drop_controller_.get(); |
| 136 | } |
| 137 | |
Qiang Xu | 36234e53 | 2018-04-11 04:54:02 | [diff] [blame] | 138 | PowerPrefs* ShellTestApi::power_prefs() { |
| 139 | return shell_->power_prefs_.get(); |
| 140 | } |
| 141 | |
Sammie Quon | 4986fdf | 2020-05-15 19:33:58 | [diff] [blame] | 142 | display::DisplayManager* ShellTestApi::display_manager() { |
| 143 | return shell_->display_manager(); |
| 144 | } |
| 145 | |
Qiang Xu | 50c6f8c | 2017-08-31 20:55:24 | [diff] [blame] | 146 | void ShellTestApi::ResetPowerButtonControllerForTest() { |
Toni Barzic | 9d0c82f4 | 2017-12-06 00:53:52 | [diff] [blame] | 147 | shell_->backlights_forced_off_setter_->ResetForTest(); |
| 148 | shell_->power_button_controller_ = std::make_unique<PowerButtonController>( |
| 149 | shell_->backlights_forced_off_setter_.get()); |
Qiang Xu | 50c6f8c | 2017-08-31 20:55:24 | [diff] [blame] | 150 | } |
| 151 | |
James Cook | 7b7c6a5 | 2018-01-04 23:40:03 | [diff] [blame] | 152 | void ShellTestApi::SimulateModalWindowOpenForTest(bool modal_window_open) { |
| 153 | shell_->simulate_modal_window_open_for_test_ = modal_window_open; |
| 154 | } |
| 155 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 156 | bool ShellTestApi::IsSystemModalWindowOpen() { |
| 157 | return Shell::IsSystemModalWindowOpen(); |
James Cook | 00848f0 | 2018-01-05 18:02:57 | [diff] [blame] | 158 | } |
| 159 | |
James Cook | bbca645 | 2021-01-13 17:26:03 | [diff] [blame^] | 160 | void ShellTestApi::SetTabletModeEnabledForTest(bool enable) { |
Ahmed Fakhry | 607ade0 | 2019-10-31 15:59:39 | [diff] [blame] | 161 | // Detach mouse devices, so we can enter tablet mode. |
| 162 | // Calling RunUntilIdle() here is necessary before setting the mouse devices |
| 163 | // to prevent the callback from evdev thread from overwriting whatever we set |
| 164 | // here below. See `InputDeviceFactoryEvdevProxy::OnStartupScanComplete()`. |
| 165 | base::RunLoop().RunUntilIdle(); |
Xiaoqian Dai | 7e18245 | 2020-05-05 23:29:19 | [diff] [blame] | 166 | ui::DeviceDataManagerTestApi().OnDeviceListsComplete(); |
Ahmed Fakhry | 607ade0 | 2019-10-31 15:59:39 | [diff] [blame] | 167 | ui::DeviceDataManagerTestApi().SetMouseDevices({}); |
| 168 | |
Mitsuru Oshima | a6adca4f | 2019-07-10 23:30:46 | [diff] [blame] | 169 | TabletMode::Waiter waiter(enable); |
Mitsuru Oshima | 9e05edd | 2019-06-17 19:35:39 | [diff] [blame] | 170 | shell_->tablet_mode_controller()->SetEnabledForTest(enable); |
Mitsuru Oshima | a6adca4f | 2019-07-10 23:30:46 | [diff] [blame] | 171 | waiter.Wait(); |
Evan Stade | d689b33 | 2018-08-02 01:17:22 | [diff] [blame] | 172 | } |
| 173 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 174 | void ShellTestApi::EnableVirtualKeyboard() { |
Darren Shen | 7daf3e1 | 2019-07-02 07:25:49 | [diff] [blame] | 175 | shell_->keyboard_controller()->SetEnableFlag( |
Darren Shen | 8604a21 | 2019-06-07 00:41:38 | [diff] [blame] | 176 | keyboard::KeyboardEnableFlag::kCommandLineEnabled); |
Noel Gordon | fe3ac34 | 2018-09-10 05:12:57 | [diff] [blame] | 177 | } |
| 178 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 179 | void ShellTestApi::ToggleFullscreen() { |
Manu Cornet | c3511d90 | 2020-01-10 22:44:43 | [diff] [blame] | 180 | accelerators::ToggleFullscreen(); |
Evan Stade | 7768d47 | 2018-10-25 22:40:17 | [diff] [blame] | 181 | } |
| 182 | |
Jun Mukai | aa8479c8 | 2019-01-08 02:30:39 | [diff] [blame] | 183 | void ShellTestApi::AddRemoveDisplay() { |
| 184 | shell_->display_manager()->AddRemoveDisplay(); |
| 185 | } |
| 186 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 187 | void ShellTestApi::WaitForNoPointerHoldLock() { |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 188 | aura::WindowTreeHost* primary_host = |
| 189 | Shell::GetPrimaryRootWindowController()->GetHost(); |
| 190 | if (primary_host->holding_pointer_moves()) |
| 191 | PointerMoveLoopWaiter(primary_host).Wait(); |
Scott Violet | d8ceadb | 2019-03-21 01:31:43 | [diff] [blame] | 192 | } |
| 193 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 194 | void ShellTestApi::WaitForNextFrame(base::OnceClosure closure) { |
Xiyuan Xia | 65d4bbf | 2019-04-08 15:13:38 | [diff] [blame] | 195 | Shell::GetPrimaryRootWindowController() |
| 196 | ->GetHost() |
| 197 | ->compositor() |
| 198 | ->RequestPresentationTimeForNextFrame(base::BindOnce( |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 199 | [](base::OnceClosure closure, |
Xiyuan Xia | 65d4bbf | 2019-04-08 15:13:38 | [diff] [blame] | 200 | const gfx::PresentationFeedback& feedback) { |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 201 | std::move(closure).Run(); |
Xiyuan Xia | 65d4bbf | 2019-04-08 15:13:38 | [diff] [blame] | 202 | }, |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 203 | std::move(closure))); |
Xiyuan Xia | 65d4bbf | 2019-04-08 15:13:38 | [diff] [blame] | 204 | } |
| 205 | |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 206 | void ShellTestApi::WaitForOverviewAnimationState(OverviewAnimationState state) { |
| 207 | auto* overview_controller = shell_->overview_controller(); |
| 208 | if (state == OverviewAnimationState::kEnterAnimationComplete && |
Sammie Quon | 2c85f73d | 2019-05-08 23:10:27 | [diff] [blame] | 209 | overview_controller->InOverviewSession() && |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 210 | !overview_controller->IsInStartAnimation()) { |
| 211 | // If there is no animation applied, call the callback immediately. |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 212 | return; |
| 213 | } |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 214 | if (state == OverviewAnimationState::kExitAnimationComplete && |
Sammie Quon | 2c85f73d | 2019-05-08 23:10:27 | [diff] [blame] | 215 | !overview_controller->InOverviewSession() && |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 216 | !overview_controller->IsCompletingShutdownAnimations()) { |
| 217 | // If there is no animation applied, call the callback immediately. |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 218 | return; |
| 219 | } |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 220 | base::RunLoop run_loop; |
Jun Mukai | 2b91e70 | 2019-09-05 18:24:38 | [diff] [blame] | 221 | new OverviewAnimationStateWaiter( |
Reilly Grant | b670223 | 2019-11-26 22:46:04 | [diff] [blame] | 222 | state, base::BindOnce([](base::RunLoop* run_loop, |
| 223 | bool finished) { run_loop->QuitWhenIdle(); }, |
| 224 | base::Unretained(&run_loop))); |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 225 | run_loop.Run(); |
Mitsuru Oshima | 57cc785 | 2019-04-10 00:15:27 | [diff] [blame] | 226 | } |
| 227 | |
Mitsuru Oshima | 77bfc27 | 2019-04-10 23:40:18 | [diff] [blame] | 228 | void ShellTestApi::WaitForLauncherAnimationState( |
Manu Cornet | c3511d90 | 2020-01-10 22:44:43 | [diff] [blame] | 229 | AppListViewState target_state) { |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 230 | base::RunLoop run_loop; |
Mitsuru Oshima | 9a6378d9 | 2019-11-01 00:48:15 | [diff] [blame] | 231 | WaitForLauncherState(target_state, run_loop.QuitWhenIdleClosure()); |
Jun Mukai | 4498638 | 2019-05-14 18:19:20 | [diff] [blame] | 232 | run_loop.Run(); |
Mitsuru Oshima | 77bfc27 | 2019-04-10 23:40:18 | [diff] [blame] | 233 | } |
| 234 | |
Sammie Quon | 358e957 | 2019-11-21 04:38:02 | [diff] [blame] | 235 | void ShellTestApi::WaitForWindowFinishAnimating(aura::Window* window) { |
| 236 | WindowAnimationWaiter waiter(window); |
| 237 | waiter.Wait(); |
| 238 | } |
| 239 | |
Toni Barzic | 8caccc8 | 2020-01-30 23:27:19 | [diff] [blame] | 240 | base::OnceClosure ShellTestApi::CreateWaiterForFinishingWindowAnimation( |
| 241 | aura::Window* window) { |
| 242 | auto waiter = std::make_unique<WindowAnimationWaiter>(window); |
| 243 | return base::BindOnce(&WindowAnimationWaiter::Wait, std::move(waiter)); |
| 244 | } |
| 245 | |
Jun Mukai | c9ba6b51 | 2019-06-05 19:21:42 | [diff] [blame] | 246 | PaginationModel* ShellTestApi::GetAppListPaginationModel() { |
Manu Cornet | 191c314 | 2019-10-09 06:31:32 | [diff] [blame] | 247 | AppListView* view = |
Jun Mukai | c9ba6b51 | 2019-06-05 19:21:42 | [diff] [blame] | 248 | Shell::Get()->app_list_controller()->presenter()->GetView(); |
| 249 | if (!view) |
| 250 | return nullptr; |
| 251 | return view->GetAppsPaginationModel(); |
| 252 | } |
| 253 | |
Sammie Quon | d322167b | 2021-01-11 22:10:40 | [diff] [blame] | 254 | bool ShellTestApi::IsContextMenuShown() const { |
| 255 | return Shell::GetPrimaryRootWindowController()->IsContextMenuShown(); |
| 256 | } |
| 257 | |
[email protected] | 0c68e89 | 2012-12-08 16:54:53 | [diff] [blame] | 258 | } // namespace ash |