blob: 90c910f43f14bcf63b1f07b6cfb2c6a3126b43d6 [file] [log] [blame]
[email protected]0c68e892012-12-08 16:54:531// 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 Mukai44986382019-05-14 18:19:205#include "ash/public/cpp/test/shell_test_api.h"
sky5ad143a2017-03-22 04:31:236
Scott Violetd8ceadb2019-03-21 01:31:437#include <memory>
xiyuan317fdbe2017-05-31 15:02:028#include <utility>
9
Evan Stade7768d472018-10-25 22:40:1710#include "ash/accelerators/accelerator_commands.h"
Alexander Alekseev2b4c9592021-05-04 22:26:4411#include "ash/accelerators/accelerator_controller_impl.h"
Mitsuru Oshima8abd8b1d02019-05-09 17:55:2312#include "ash/accelerometer/accelerometer_reader.h"
Mitsuru Oshima57cc7852019-04-10 00:15:2713#include "ash/app_list/app_list_controller_impl.h"
James Cook4f4c3612021-05-27 21:45:2214#include "ash/app_list/app_list_presenter_impl.h"
Mitsuru Oshima57cc7852019-04-10 00:15:2715#include "ash/app_list/views/app_list_view.h"
Alexander Alekseev2b4c9592021-05-04 22:26:4416#include "ash/hud_display/hud_display.h"
Darren Shen7daf3e12019-07-02 07:25:4917#include "ash/keyboard/keyboard_controller_impl.h"
Mitsuru Oshima9a6378d92019-11-01 00:48:1518#include "ash/public/cpp/autotest_private_api_utils.h"
Mitsuru Oshimaa6adca4f2019-07-10 23:30:4619#include "ash/public/cpp/tablet_mode_observer.h"
[email protected]0c68e892012-12-08 16:54:5320#include "ash/root_window_controller.h"
21#include "ash/shell.h"
Ahmed Mehfooz812c5422021-03-29 22:42:1622#include "ash/system/message_center/session_state_notification_blocker.h"
Toni Barzic9d0c82f42017-12-06 00:53:5223#include "ash/system/power/backlights_forced_off_setter.h"
Qiang Xu0a86020f2017-10-12 08:34:5424#include "ash/system/power/power_button_controller.h"
Jun Mukai2b91e702019-09-05 18:24:3825#include "ash/wm/overview/overview_animation_state_waiter.h"
Sammie Quon80e82a12019-01-23 19:55:2226#include "ash/wm/overview/overview_controller.h"
Evan Stade426a2982018-10-19 22:42:0727#include "ash/wm/splitview/split_view_controller.h"
Evan Staded689b332018-08-02 01:17:2228#include "ash/wm/tablet_mode/tablet_mode_controller.h"
Ahmed Fakhryfee7a0a2019-04-05 22:49:4829#include "ash/wm/workspace_controller.h"
Peter Boströmbceb9af2021-05-27 18:18:5830#include "base/bind.h"
Scott Violetd8ceadb2019-03-21 01:31:4331#include "base/run_loop.h"
James Cookbbe5cb12017-08-08 18:32:2132#include "components/prefs/testing_pref_service.h"
Scott Violetd8ceadb2019-03-21 01:31:4333#include "ui/aura/window_tree_host.h"
34#include "ui/compositor/compositor.h"
35#include "ui/compositor/compositor_observer.h"
Lei Zhang0c043322021-04-27 16:43:0536#include "ui/compositor/layer.h"
Sammie Quon358e9572019-11-21 04:38:0237#include "ui/compositor/layer_animation_observer.h"
Sammie Quon4986fdf2020-05-15 19:33:5838#include "ui/display/manager/display_manager.h"
Ahmed Fakhry607ade02019-10-31 15:59:3939#include "ui/events/devices/device_data_manager_test_api.h"
Jun Mukai2af6db52019-01-09 00:22:4240#include "ui/events/gesture_detection/gesture_configuration.h"
[email protected]0c68e892012-12-08 16:54:5341
42namespace ash {
Scott Violetd8ceadb2019-03-21 01:31:4343namespace {
44
45// Wait for a WindowTreeHost to no longer be holding pointer events.
46class PointerMoveLoopWaiter : public ui::CompositorObserver {
47 public:
48 explicit PointerMoveLoopWaiter(aura::WindowTreeHost* window_tree_host)
49 : window_tree_host_(window_tree_host) {
50 window_tree_host_->compositor()->AddObserver(this);
51 }
52
53 ~PointerMoveLoopWaiter() override {
54 window_tree_host_->compositor()->RemoveObserver(this);
55 }
56
57 void Wait() {
58 // Use a while loop as it's possible for releasing the lock to trigger
59 // processing events, which again grabs the lock.
60 while (window_tree_host_->holding_pointer_moves()) {
61 run_loop_ = std::make_unique<base::RunLoop>(
62 base::RunLoop::Type::kNestableTasksAllowed);
63 run_loop_->Run();
64 run_loop_.reset();
65 }
66 }
67
68 // ui::CompositorObserver:
69 void OnCompositingEnded(ui::Compositor* compositor) override {
70 if (run_loop_)
71 run_loop_->Quit();
72 }
73
74 private:
75 aura::WindowTreeHost* window_tree_host_;
76 std::unique_ptr<base::RunLoop> run_loop_;
77
78 DISALLOW_COPY_AND_ASSIGN(PointerMoveLoopWaiter);
79};
80
Sammie Quon358e9572019-11-21 04:38:0281class WindowAnimationWaiter : public ui::LayerAnimationObserver {
82 public:
83 explicit WindowAnimationWaiter(aura::Window* window)
84 : animator_(window->layer()->GetAnimator()) {
85 animator_->AddObserver(this);
86 }
87 ~WindowAnimationWaiter() override = default;
88
89 WindowAnimationWaiter(const WindowAnimationWaiter& other) = delete;
90 WindowAnimationWaiter& operator=(const WindowAnimationWaiter& rhs) = delete;
91
92 // ui::LayerAnimationObserver:
93 void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
94 if (!animator_->is_animating()) {
95 animator_->RemoveObserver(this);
96 run_loop_.Quit();
97 }
98 }
99 void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
100 void OnLayerAnimationScheduled(
101 ui::LayerAnimationSequence* sequence) override {}
102
103 void Wait() {
Sammie Quon358e9572019-11-21 04:38:02104 run_loop_.Run();
105 }
106
107 private:
108 ui::LayerAnimator* animator_;
109 base::RunLoop run_loop_;
110};
111
Scott Violetd8ceadb2019-03-21 01:31:43112} // namespace
[email protected]0c68e892012-12-08 16:54:53113
Jun Mukai44986382019-05-14 18:19:20114ShellTestApi::ShellTestApi() : shell_(Shell::Get()) {}
115ShellTestApi::~ShellTestApi() = default;
James Cook00848f02018-01-05 18:02:57116
Sammie Quon408e9da2019-06-20 19:36:21117// static
118void ShellTestApi::SetTabletControllerUseScreenshotForTest(
119 bool use_screenshot) {
120 TabletModeController::SetUseScreenshotForTest(use_screenshot);
121}
122
Ahmed Mehfooz812c5422021-03-29 22:42:16123// static
124void ShellTestApi::SetUseLoginNotificationDelayForTest(bool use_delay) {
125 SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(
126 use_delay);
127}
128
Zentaro Kavanagh1f436492021-05-20 23:29:36129// static
130void ShellTestApi::SetShouldShowShortcutNotificationForTest(
131 bool show_notification) {
132 AcceleratorControllerImpl::SetShouldShowShortcutNotificationForTest(
133 show_notification);
134}
135
James Cook459354b32017-09-27 23:18:01136MessageCenterController* ShellTestApi::message_center_controller() {
137 return shell_->message_center_controller_.get();
138}
139
[email protected]093b8d642014-04-03 20:59:28140WorkspaceController* ShellTestApi::workspace_controller() {
Ahmed Fakhryfee7a0a2019-04-05 22:49:48141 // TODO(afakhry): Split this into two, one for root, and one for context.
142 return GetActiveWorkspaceController(shell_->GetPrimaryRootWindow());
[email protected]0c68e892012-12-08 16:54:53143}
144
[email protected]093b8d642014-04-03 20:59:28145ScreenPositionController* ShellTestApi::screen_position_controller() {
[email protected]0c68e892012-12-08 16:54:53146 return shell_->screen_position_controller_.get();
147}
148
erg36cfb952017-06-21 19:41:45149NativeCursorManagerAsh* ShellTestApi::native_cursor_manager_ash() {
[email protected]c0ff0342013-02-27 00:41:29150 return shell_->native_cursor_manager_;
151}
152
[email protected]093b8d642014-04-03 20:59:28153DragDropController* ShellTestApi::drag_drop_controller() {
[email protected]c086bab2013-06-06 21:46:44154 return shell_->drag_drop_controller_.get();
155}
156
Qiang Xu36234e532018-04-11 04:54:02157PowerPrefs* ShellTestApi::power_prefs() {
158 return shell_->power_prefs_.get();
159}
160
Sammie Quon4986fdf2020-05-15 19:33:58161display::DisplayManager* ShellTestApi::display_manager() {
162 return shell_->display_manager();
163}
164
Qiang Xu50c6f8c2017-08-31 20:55:24165void ShellTestApi::ResetPowerButtonControllerForTest() {
Toni Barzic9d0c82f42017-12-06 00:53:52166 shell_->backlights_forced_off_setter_->ResetForTest();
167 shell_->power_button_controller_ = std::make_unique<PowerButtonController>(
168 shell_->backlights_forced_off_setter_.get());
Qiang Xu50c6f8c2017-08-31 20:55:24169}
170
James Cook7b7c6a52018-01-04 23:40:03171void ShellTestApi::SimulateModalWindowOpenForTest(bool modal_window_open) {
172 shell_->simulate_modal_window_open_for_test_ = modal_window_open;
173}
174
Jun Mukai44986382019-05-14 18:19:20175bool ShellTestApi::IsSystemModalWindowOpen() {
176 return Shell::IsSystemModalWindowOpen();
James Cook00848f02018-01-05 18:02:57177}
178
James Cookbbca6452021-01-13 17:26:03179void ShellTestApi::SetTabletModeEnabledForTest(bool enable) {
Ahmed Fakhry607ade02019-10-31 15:59:39180 // Detach mouse devices, so we can enter tablet mode.
181 // Calling RunUntilIdle() here is necessary before setting the mouse devices
182 // to prevent the callback from evdev thread from overwriting whatever we set
183 // here below. See `InputDeviceFactoryEvdevProxy::OnStartupScanComplete()`.
184 base::RunLoop().RunUntilIdle();
Xiaoqian Dai7e182452020-05-05 23:29:19185 ui::DeviceDataManagerTestApi().OnDeviceListsComplete();
Ahmed Fakhry607ade02019-10-31 15:59:39186 ui::DeviceDataManagerTestApi().SetMouseDevices({});
187
Mitsuru Oshimaa6adca4f2019-07-10 23:30:46188 TabletMode::Waiter waiter(enable);
Mitsuru Oshima9e05edd2019-06-17 19:35:39189 shell_->tablet_mode_controller()->SetEnabledForTest(enable);
Mitsuru Oshimaa6adca4f2019-07-10 23:30:46190 waiter.Wait();
Evan Staded689b332018-08-02 01:17:22191}
192
Jun Mukai44986382019-05-14 18:19:20193void ShellTestApi::EnableVirtualKeyboard() {
Darren Shen7daf3e12019-07-02 07:25:49194 shell_->keyboard_controller()->SetEnableFlag(
Darren Shen8604a212019-06-07 00:41:38195 keyboard::KeyboardEnableFlag::kCommandLineEnabled);
Noel Gordonfe3ac342018-09-10 05:12:57196}
197
Jun Mukai44986382019-05-14 18:19:20198void ShellTestApi::ToggleFullscreen() {
Manu Cornetc3511d902020-01-10 22:44:43199 accelerators::ToggleFullscreen();
Evan Stade7768d472018-10-25 22:40:17200}
201
Jun Mukaiaa8479c82019-01-08 02:30:39202void ShellTestApi::AddRemoveDisplay() {
203 shell_->display_manager()->AddRemoveDisplay();
204}
205
Jun Mukai44986382019-05-14 18:19:20206void ShellTestApi::WaitForNoPointerHoldLock() {
Scott Violetd8ceadb2019-03-21 01:31:43207 aura::WindowTreeHost* primary_host =
208 Shell::GetPrimaryRootWindowController()->GetHost();
209 if (primary_host->holding_pointer_moves())
210 PointerMoveLoopWaiter(primary_host).Wait();
Scott Violetd8ceadb2019-03-21 01:31:43211}
212
Jun Mukai44986382019-05-14 18:19:20213void ShellTestApi::WaitForNextFrame(base::OnceClosure closure) {
Xiyuan Xia65d4bbf2019-04-08 15:13:38214 Shell::GetPrimaryRootWindowController()
215 ->GetHost()
216 ->compositor()
217 ->RequestPresentationTimeForNextFrame(base::BindOnce(
Jun Mukai44986382019-05-14 18:19:20218 [](base::OnceClosure closure,
Xiyuan Xia65d4bbf2019-04-08 15:13:38219 const gfx::PresentationFeedback& feedback) {
Jun Mukai44986382019-05-14 18:19:20220 std::move(closure).Run();
Xiyuan Xia65d4bbf2019-04-08 15:13:38221 },
Jun Mukai44986382019-05-14 18:19:20222 std::move(closure)));
Xiyuan Xia65d4bbf2019-04-08 15:13:38223}
224
Jun Mukai44986382019-05-14 18:19:20225void ShellTestApi::WaitForOverviewAnimationState(OverviewAnimationState state) {
226 auto* overview_controller = shell_->overview_controller();
227 if (state == OverviewAnimationState::kEnterAnimationComplete &&
Sammie Quon2c85f73d2019-05-08 23:10:27228 overview_controller->InOverviewSession() &&
Mitsuru Oshima57cc7852019-04-10 00:15:27229 !overview_controller->IsInStartAnimation()) {
230 // If there is no animation applied, call the callback immediately.
Mitsuru Oshima57cc7852019-04-10 00:15:27231 return;
232 }
Jun Mukai44986382019-05-14 18:19:20233 if (state == OverviewAnimationState::kExitAnimationComplete &&
Sammie Quon2c85f73d2019-05-08 23:10:27234 !overview_controller->InOverviewSession() &&
Mitsuru Oshima57cc7852019-04-10 00:15:27235 !overview_controller->IsCompletingShutdownAnimations()) {
236 // If there is no animation applied, call the callback immediately.
Mitsuru Oshima57cc7852019-04-10 00:15:27237 return;
238 }
Jun Mukai44986382019-05-14 18:19:20239 base::RunLoop run_loop;
Jun Mukai2b91e702019-09-05 18:24:38240 new OverviewAnimationStateWaiter(
Reilly Grantb6702232019-11-26 22:46:04241 state, base::BindOnce([](base::RunLoop* run_loop,
242 bool finished) { run_loop->QuitWhenIdle(); },
243 base::Unretained(&run_loop)));
Jun Mukai44986382019-05-14 18:19:20244 run_loop.Run();
Mitsuru Oshima57cc7852019-04-10 00:15:27245}
246
Mitsuru Oshima77bfc272019-04-10 23:40:18247void ShellTestApi::WaitForLauncherAnimationState(
Manu Cornetc3511d902020-01-10 22:44:43248 AppListViewState target_state) {
Jun Mukai44986382019-05-14 18:19:20249 base::RunLoop run_loop;
Mitsuru Oshima9a6378d92019-11-01 00:48:15250 WaitForLauncherState(target_state, run_loop.QuitWhenIdleClosure());
Jun Mukai44986382019-05-14 18:19:20251 run_loop.Run();
Mitsuru Oshima77bfc272019-04-10 23:40:18252}
253
Sammie Quon358e9572019-11-21 04:38:02254void ShellTestApi::WaitForWindowFinishAnimating(aura::Window* window) {
255 WindowAnimationWaiter waiter(window);
256 waiter.Wait();
257}
258
Toni Barzic8caccc82020-01-30 23:27:19259base::OnceClosure ShellTestApi::CreateWaiterForFinishingWindowAnimation(
260 aura::Window* window) {
261 auto waiter = std::make_unique<WindowAnimationWaiter>(window);
262 return base::BindOnce(&WindowAnimationWaiter::Wait, std::move(waiter));
263}
264
Jun Mukaic9ba6b512019-06-05 19:21:42265PaginationModel* ShellTestApi::GetAppListPaginationModel() {
Manu Cornet191c3142019-10-09 06:31:32266 AppListView* view =
Jun Mukaic9ba6b512019-06-05 19:21:42267 Shell::Get()->app_list_controller()->presenter()->GetView();
268 if (!view)
269 return nullptr;
270 return view->GetAppsPaginationModel();
271}
272
Sammie Quond322167b2021-01-11 22:10:40273bool ShellTestApi::IsContextMenuShown() const {
274 return Shell::GetPrimaryRootWindowController()->IsContextMenuShown();
275}
276
Alexander Alekseev2b4c9592021-05-04 22:26:44277bool ShellTestApi::IsActionForAcceleratorEnabled(
278 const ui::Accelerator& accelerator) const {
279 return Shell::Get()->accelerator_controller()->IsActionForAcceleratorEnabled(
280 accelerator);
281}
282
283bool ShellTestApi::PressAccelerator(const ui::Accelerator& accelerator) {
284 return Shell::Get()->accelerator_controller()->AcceleratorPressed(
285 accelerator);
286}
287
288bool ShellTestApi::IsHUDShown() {
289 return hud_display::HUDDisplayView::IsShown();
290}
291
[email protected]0c68e892012-12-08 16:54:53292} // namespace ash