1 // Copyright 2019 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 
5 #include "ash/wm/overview/overview_test_util.h"
6 
7 #include "ash/public/cpp/overview_test_api.h"
8 #include "ash/public/cpp/shelf_config.h"
9 #include "ash/shell.h"
10 #include "ash/wm/overview/overview_controller.h"
11 #include "ash/wm/overview/overview_grid.h"
12 #include "ash/wm/overview/overview_highlight_controller.h"
13 #include "ash/wm/overview/overview_item.h"
14 #include "base/run_loop.h"
15 #include "base/test/bind.h"
16 #include "ui/compositor/scoped_animation_duration_scale_mode.h"
17 #include "ui/events/test/event_generator.h"
18 
19 namespace ash {
20 
21 namespace {
22 
WaitForOverviewAnimationState(OverviewAnimationState state)23 void WaitForOverviewAnimationState(OverviewAnimationState state) {
24   // Early out if animations are disabled.
25   if (ui::ScopedAnimationDurationScaleMode::duration_multiplier() ==
26       ui::ScopedAnimationDurationScaleMode::ZERO_DURATION) {
27     return;
28   }
29 
30   base::RunLoop run_loop;
31   OverviewTestApi().WaitForOverviewState(
32       state, base::BindLambdaForTesting([&](bool) { run_loop.Quit(); }));
33   run_loop.Run();
34 }
35 
36 }  // namespace
37 
38 // TODO(sammiequon): Consider adding an overload for this function to trigger
39 // the key event |count| times.
SendKey(ui::KeyboardCode key,int flags)40 void SendKey(ui::KeyboardCode key, int flags) {
41   ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
42   generator.PressKey(key, flags);
43   generator.ReleaseKey(key, flags);
44 }
45 
HighlightOverviewWindow(const aura::Window * window)46 bool HighlightOverviewWindow(const aura::Window* window) {
47   if (GetOverviewHighlightedWindow() == nullptr)
48     SendKey(ui::VKEY_TAB);
49   const aura::Window* start_window = GetOverviewHighlightedWindow();
50   if (start_window == window)
51     return true;
52   aura::Window* window_it = nullptr;
53   do {
54     SendKey(ui::VKEY_TAB);
55     window_it = const_cast<aura::Window*>(GetOverviewHighlightedWindow());
56   } while (window_it != window && window_it != start_window);
57   return window_it == window;
58 }
59 
GetOverviewHighlightedWindow()60 const aura::Window* GetOverviewHighlightedWindow() {
61   OverviewItem* item =
62       GetOverviewSession()->highlight_controller()->GetHighlightedItem();
63   if (!item)
64     return nullptr;
65   return item->GetWindow();
66 }
67 
ToggleOverview(OverviewEnterExitType type)68 void ToggleOverview(OverviewEnterExitType type) {
69   auto* overview_controller = Shell::Get()->overview_controller();
70   if (overview_controller->InOverviewSession())
71     overview_controller->EndOverview(type);
72   else
73     overview_controller->StartOverview(type);
74 }
75 
WaitForOverviewEnterAnimation()76 void WaitForOverviewEnterAnimation() {
77   WaitForOverviewAnimationState(
78       OverviewAnimationState::kEnterAnimationComplete);
79 }
80 
WaitForOverviewExitAnimation()81 void WaitForOverviewExitAnimation() {
82   WaitForOverviewAnimationState(OverviewAnimationState::kExitAnimationComplete);
83 }
84 
GetOverviewSession()85 OverviewSession* GetOverviewSession() {
86   auto* session = Shell::Get()->overview_controller()->overview_session();
87   DCHECK(session);
88   return session;
89 }
90 
GetOverviewItemsForRoot(int index)91 const std::vector<std::unique_ptr<OverviewItem>>& GetOverviewItemsForRoot(
92     int index) {
93   return GetOverviewSession()->grid_list()[index]->window_list();
94 }
95 
GetOverviewItemForWindow(aura::Window * window)96 OverviewItem* GetOverviewItemForWindow(aura::Window* window) {
97   return GetOverviewSession()->GetOverviewItemForWindow(window);
98 }
99 
ShrinkBoundsByHotseatInset(const gfx::Rect & rect)100 gfx::Rect ShrinkBoundsByHotseatInset(const gfx::Rect& rect) {
101   gfx::Rect new_rect = rect;
102   const int hotseat_bottom_inset = ShelfConfig::Get()->GetHotseatSize(
103                                        /*density=*/HotseatDensity::kNormal) +
104                                    ShelfConfig::Get()->hotseat_bottom_padding();
105   new_rect.Inset(0, 0, 0, hotseat_bottom_inset);
106   return new_rect;
107 }
108 
109 }  // namespace ash
110