shell_test_api.cc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. #include "ash/public/cpp/test/shell_test_api.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/accelerators/accelerator_commands.h"
  8. #include "ash/accelerators/accelerator_controller_impl.h"
  9. #include "ash/accelerometer/accelerometer_reader.h"
  10. #include "ash/app_list/app_list_controller_impl.h"
  11. #include "ash/app_list/app_list_presenter_impl.h"
  12. #include "ash/app_list/views/app_list_view.h"
  13. #include "ash/hud_display/hud_display.h"
  14. #include "ash/keyboard/keyboard_controller_impl.h"
  15. #include "ash/public/cpp/autotest_private_api_utils.h"
  16. #include "ash/public/cpp/tablet_mode_observer.h"
  17. #include "ash/root_window_controller.h"
  18. #include "ash/shell.h"
  19. #include "ash/system/message_center/session_state_notification_blocker.h"
  20. #include "ash/system/power/backlights_forced_off_setter.h"
  21. #include "ash/system/power/power_button_controller.h"
  22. #include "ash/wm/overview/overview_animation_state_waiter.h"
  23. #include "ash/wm/overview/overview_controller.h"
  24. #include "ash/wm/splitview/split_view_controller.h"
  25. #include "ash/wm/tablet_mode/tablet_mode_controller.h"
  26. #include "ash/wm/workspace_controller.h"
  27. #include "base/bind.h"
  28. #include "base/run_loop.h"
  29. #include "components/prefs/testing_pref_service.h"
  30. #include "ui/aura/window_tree_host.h"
  31. #include "ui/compositor/compositor.h"
  32. #include "ui/compositor/compositor_observer.h"
  33. #include "ui/compositor/layer.h"
  34. #include "ui/compositor/layer_animation_observer.h"
  35. #include "ui/compositor/layer_animator.h"
  36. #include "ui/display/manager/display_manager.h"
  37. #include "ui/events/devices/device_data_manager_test_api.h"
  38. #include "ui/events/gesture_detection/gesture_configuration.h"
  39. namespace ash {
  40. namespace {
  41. // Wait for a WindowTreeHost to no longer be holding pointer events.
  42. class PointerMoveLoopWaiter : public ui::CompositorObserver {
  43. public:
  44. explicit PointerMoveLoopWaiter(aura::WindowTreeHost* window_tree_host)
  45. : window_tree_host_(window_tree_host) {
  46. window_tree_host_->compositor()->AddObserver(this);
  47. }
  48. PointerMoveLoopWaiter(const PointerMoveLoopWaiter&) = delete;
  49. PointerMoveLoopWaiter& operator=(const PointerMoveLoopWaiter&) = delete;
  50. ~PointerMoveLoopWaiter() override {
  51. window_tree_host_->compositor()->RemoveObserver(this);
  52. }
  53. void Wait() {
  54. // Use a while loop as it's possible for releasing the lock to trigger
  55. // processing events, which again grabs the lock.
  56. while (window_tree_host_->holding_pointer_moves()) {
  57. run_loop_ = std::make_unique<base::RunLoop>(
  58. base::RunLoop::Type::kNestableTasksAllowed);
  59. run_loop_->Run();
  60. run_loop_.reset();
  61. }
  62. }
  63. // ui::CompositorObserver:
  64. void OnCompositingEnded(ui::Compositor* compositor) override {
  65. if (run_loop_)
  66. run_loop_->Quit();
  67. }
  68. private:
  69. aura::WindowTreeHost* window_tree_host_;
  70. std::unique_ptr<base::RunLoop> run_loop_;
  71. };
  72. class WindowAnimationWaiter : public ui::LayerAnimationObserver {
  73. public:
  74. explicit WindowAnimationWaiter(aura::Window* window)
  75. : animator_(window->layer()->GetAnimator()) {
  76. animator_->AddObserver(this);
  77. }
  78. ~WindowAnimationWaiter() override = default;
  79. WindowAnimationWaiter(const WindowAnimationWaiter& other) = delete;
  80. WindowAnimationWaiter& operator=(const WindowAnimationWaiter& rhs) = delete;
  81. // ui::LayerAnimationObserver:
  82. void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
  83. if (!animator_->is_animating()) {
  84. animator_->RemoveObserver(this);
  85. run_loop_.Quit();
  86. }
  87. }
  88. void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
  89. void OnLayerAnimationScheduled(
  90. ui::LayerAnimationSequence* sequence) override {}
  91. void Wait() { run_loop_.Run(); }
  92. private:
  93. ui::LayerAnimator* animator_;
  94. base::RunLoop run_loop_;
  95. };
  96. } // namespace
  97. ShellTestApi::ShellTestApi() : shell_(Shell::Get()) {}
  98. ShellTestApi::~ShellTestApi() = default;
  99. // static
  100. void ShellTestApi::SetTabletControllerUseScreenshotForTest(
  101. bool use_screenshot) {
  102. TabletModeController::SetUseScreenshotForTest(use_screenshot);
  103. }
  104. // static
  105. void ShellTestApi::SetUseLoginNotificationDelayForTest(bool use_delay) {
  106. SessionStateNotificationBlocker::SetUseLoginNotificationDelayForTest(
  107. use_delay);
  108. }
  109. MessageCenterController* ShellTestApi::message_center_controller() {
  110. return shell_->message_center_controller_.get();
  111. }
  112. WorkspaceController* ShellTestApi::workspace_controller() {
  113. // TODO(afakhry): Split this into two, one for root, and one for context.
  114. return GetActiveWorkspaceController(shell_->GetPrimaryRootWindow());
  115. }
  116. ScreenPositionController* ShellTestApi::screen_position_controller() {
  117. return shell_->screen_position_controller_.get();
  118. }
  119. NativeCursorManagerAsh* ShellTestApi::native_cursor_manager_ash() {
  120. return shell_->native_cursor_manager_;
  121. }
  122. DragDropController* ShellTestApi::drag_drop_controller() {
  123. return shell_->drag_drop_controller_.get();
  124. }
  125. PowerPrefs* ShellTestApi::power_prefs() {
  126. return shell_->power_prefs_.get();
  127. }
  128. display::DisplayManager* ShellTestApi::display_manager() {
  129. return shell_->display_manager();
  130. }
  131. void ShellTestApi::ResetPowerButtonControllerForTest() {
  132. shell_->backlights_forced_off_setter_->ResetForTest();
  133. shell_->power_button_controller_.reset();
  134. shell_->power_button_controller_ = std::make_unique<PowerButtonController>(
  135. shell_->backlights_forced_off_setter_.get());
  136. }
  137. void ShellTestApi::SimulateModalWindowOpenForTest(bool modal_window_open) {
  138. shell_->simulate_modal_window_open_for_test_ = modal_window_open;
  139. }
  140. bool ShellTestApi::IsSystemModalWindowOpen() {
  141. return Shell::IsSystemModalWindowOpen();
  142. }
  143. void ShellTestApi::SetTabletModeEnabledForTest(bool enable) {
  144. // Detach mouse devices, so we can enter tablet mode.
  145. // Calling RunUntilIdle() here is necessary before setting the mouse devices
  146. // to prevent the callback from evdev thread from overwriting whatever we set
  147. // here below. See `InputDeviceFactoryEvdevProxy::OnStartupScanComplete()`.
  148. base::RunLoop().RunUntilIdle();
  149. ui::DeviceDataManagerTestApi().OnDeviceListsComplete();
  150. ui::DeviceDataManagerTestApi().SetMouseDevices({});
  151. TabletMode::Waiter waiter(enable);
  152. shell_->tablet_mode_controller()->SetEnabledForTest(enable);
  153. waiter.Wait();
  154. }
  155. void ShellTestApi::EnableVirtualKeyboard() {
  156. shell_->keyboard_controller()->SetEnableFlag(
  157. keyboard::KeyboardEnableFlag::kCommandLineEnabled);
  158. }
  159. void ShellTestApi::ToggleFullscreen() {
  160. accelerators::ToggleFullscreen();
  161. }
  162. void ShellTestApi::AddRemoveDisplay() {
  163. shell_->display_manager()->AddRemoveDisplay();
  164. }
  165. void ShellTestApi::WaitForNoPointerHoldLock() {
  166. aura::WindowTreeHost* primary_host =
  167. Shell::GetPrimaryRootWindowController()->GetHost();
  168. if (primary_host->holding_pointer_moves())
  169. PointerMoveLoopWaiter(primary_host).Wait();
  170. }
  171. void ShellTestApi::WaitForNextFrame(base::OnceClosure closure) {
  172. Shell::GetPrimaryRootWindowController()
  173. ->GetHost()
  174. ->compositor()
  175. ->RequestPresentationTimeForNextFrame(base::BindOnce(
  176. [](base::OnceClosure closure,
  177. const gfx::PresentationFeedback& feedback) {
  178. std::move(closure).Run();
  179. },
  180. std::move(closure)));
  181. }
  182. void ShellTestApi::WaitForOverviewAnimationState(OverviewAnimationState state) {
  183. auto* overview_controller = shell_->overview_controller();
  184. if (state == OverviewAnimationState::kEnterAnimationComplete &&
  185. overview_controller->InOverviewSession() &&
  186. !overview_controller->IsInStartAnimation()) {
  187. // If there is no animation applied, call the callback immediately.
  188. return;
  189. }
  190. if (state == OverviewAnimationState::kExitAnimationComplete &&
  191. !overview_controller->InOverviewSession() &&
  192. !overview_controller->IsCompletingShutdownAnimations()) {
  193. // If there is no animation applied, call the callback immediately.
  194. return;
  195. }
  196. base::RunLoop run_loop;
  197. new OverviewAnimationStateWaiter(
  198. state, base::BindOnce([](base::RunLoop* run_loop,
  199. bool finished) { run_loop->QuitWhenIdle(); },
  200. base::Unretained(&run_loop)));
  201. run_loop.Run();
  202. }
  203. void ShellTestApi::WaitForLauncherAnimationState(
  204. AppListViewState target_state) {
  205. base::RunLoop run_loop;
  206. WaitForLauncherState(target_state, run_loop.QuitWhenIdleClosure());
  207. run_loop.Run();
  208. }
  209. void ShellTestApi::WaitForWindowFinishAnimating(aura::Window* window) {
  210. WindowAnimationWaiter waiter(window);
  211. waiter.Wait();
  212. }
  213. base::OnceClosure ShellTestApi::CreateWaiterForFinishingWindowAnimation(
  214. aura::Window* window) {
  215. auto waiter = std::make_unique<WindowAnimationWaiter>(window);
  216. return base::BindOnce(&WindowAnimationWaiter::Wait, std::move(waiter));
  217. }
  218. PaginationModel* ShellTestApi::GetAppListPaginationModel() {
  219. AppListView* view =
  220. Shell::Get()->app_list_controller()->fullscreen_presenter()->GetView();
  221. if (!view)
  222. return nullptr;
  223. return view->GetAppsPaginationModel();
  224. }
  225. bool ShellTestApi::IsContextMenuShown() const {
  226. return Shell::GetPrimaryRootWindowController()->IsContextMenuShown();
  227. }
  228. bool ShellTestApi::IsActionForAcceleratorEnabled(
  229. const ui::Accelerator& accelerator) const {
  230. auto* controller = Shell::Get()->accelerator_controller();
  231. return AcceleratorControllerImpl::TestApi(controller)
  232. .IsActionForAcceleratorEnabled(accelerator);
  233. }
  234. bool ShellTestApi::PressAccelerator(const ui::Accelerator& accelerator) {
  235. return Shell::Get()->accelerator_controller()->AcceleratorPressed(
  236. accelerator);
  237. }
  238. bool ShellTestApi::IsHUDShown() {
  239. return hud_display::HUDDisplayView::IsShown();
  240. }
  241. } // namespace ash