1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
ash / wm / desks / desks_test_api.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/desks/desks_test_api.h"
#include "ash/shell.h"
#include "ash/system/toast/toast_manager_impl.h"
#include "ash/wm/desks/desk.h"
#include "ash/wm/desks/desk_action_button.h"
#include "ash/wm/desks/desk_action_view.h"
#include "ash/wm/desks/desk_bar_controller.h"
#include "ash/wm/desks/desk_bar_view_base.h"
#include "ash/wm/desks/desk_icon_button.h"
#include "ash/wm/desks/desk_mini_view.h"
#include "ash/wm/desks/desk_preview_view.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/desks_restore_util.h"
#include "ash/wm/desks/desks_test_util.h"
#include "ash/wm/desks/overview_desk_bar_view.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_grid.h"
#include "ash/wm/overview/overview_test_util.h"
#include "ash/wm/overview/overview_utils.h"
#include "base/run_loop.h"
#include "ui/base/mojom/menu_source_type.mojom.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/background.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_model_adapter.h"
#include "ui/views/controls/menu/menu_runner.h"
#include "ui/views/view_utils.h"
namespace ash {
namespace {
const DeskBarViewBase* GetDeskBarView(DeskBarViewBase::Type type,
aura::Window* root_window) {
if (type == DeskBarViewBase::Type::kOverview) {
auto* overview_controller = Shell::Get()->overview_controller();
CHECK(overview_controller->InOverviewSession());
return overview_controller->overview_session()
->GetGridWithRootWindow(root_window)
->desks_bar_view();
} else {
auto* desk_bar_controller = DesksController::Get()->desk_bar_controller();
CHECK(desk_bar_controller);
return desk_bar_controller->GetDeskBarView(root_window);
}
}
const DeskBarViewBase* GetDeskBarView(DeskBarViewBase::Type type) {
return GetDeskBarView(type, Shell::GetPrimaryRootWindow());
}
} // namespace
// static
ScrollArrowButton* DesksTestApi::GetDeskBarLeftScrollButton(
DeskBarViewBase::Type type) {
return GetDeskBarView(type)->left_scroll_button_;
}
// static
ScrollArrowButton* DesksTestApi::GetDeskBarRightScrollButton(
DeskBarViewBase::Type type) {
return GetDeskBarView(type)->right_scroll_button_;
}
// static
views::ScrollView* DesksTestApi::GetDeskBarScrollView(
DeskBarViewBase::Type type) {
return GetDeskBarView(type)->scroll_view_;
}
// static
const DeskMiniView* DesksTestApi::GetDeskBarDragView(
DeskBarViewBase::Type type) {
return GetDeskBarView(type)->drag_view_;
}
// static
views::LabelButton* DesksTestApi::GetCloseAllUndoToastDismissButton() {
ToastManagerImpl* toast_manager = Shell::Get()->toast_manager();
return views::AsViewClass<views::LabelButton>(
toast_manager->GetCurrentOverlayForTesting()->button_for_testing());
}
// static
views::View* DesksTestApi::GetHighlightOverlayForDeskPreview(
DeskBarViewBase::Type type,
int index) {
return GetDeskBarView(type)
->mini_views()[index]
->desk_preview()
->highlight_overlay_;
}
// static
ui::LayerTreeOwner* DesksTestApi::GetMirroredContentsLayerTreeForRootAndDesk(
aura::Window* root,
Desk* desk) {
auto& mini_views =
GetDeskBarView(DeskBarViewBase::Type::kOverview, root)->mini_views();
for (DeskMiniView* mini_view : mini_views) {
if (mini_view->desk() == desk) {
return mini_view->desk_preview()
->desk_mirrored_contents_layer_tree_owner_.get();
}
}
return nullptr;
}
// static
views::Label* DesksTestApi::GetDeskShortcutLabel(DeskMiniView* mini_view) {
return mini_view->desk_shortcut_label_;
}
// static
bool DesksTestApi::IsDeskShortcutViewVisible(DeskMiniView* mini_view) {
// If the mini_view is in the overview desk bar desk_shortcut_view_ will be
// nullptr.
return mini_view->desk_shortcut_view_ &&
mini_view->desk_shortcut_view_->GetVisible();
}
// static
DeskProfilesButton* DesksTestApi::GetDeskProfileButton(
DeskMiniView* mini_view) {
return mini_view->desk_profile_button_;
}
// static
bool DesksTestApi::DesksControllerHasDesk(Desk* desk) {
return DesksController::Get()->HasDesk(desk);
}
// static
bool DesksTestApi::DesksControllerCanUndoDeskRemoval() {
return DesksController::Get()->temporary_removed_desk_ != nullptr;
}
// static
bool DesksTestApi::IsDeskBarLeftGradientVisible(DeskBarViewBase::Type type) {
views::View* const scroll_view = GetDeskBarView(type)->scroll_view_;
if (!scroll_view) {
return false;
}
const auto& gradient_mask =
GetDeskBarView(type)->scroll_view_->layer()->gradient_mask();
return !gradient_mask.IsEmpty() &&
cc::MathUtil::IsWithinEpsilon(gradient_mask.steps()[0].fraction, 0.f);
}
// static
bool DesksTestApi::IsDeskBarRightGradientVisible(DeskBarViewBase::Type type) {
views::View* const scroll_view = GetDeskBarView(type)->scroll_view_;
if (!scroll_view) {
return false;
}
const auto& gradient_mask =
GetDeskBarView(type)->scroll_view_->layer()->gradient_mask();
return !gradient_mask.IsEmpty() &&
cc::MathUtil::IsWithinEpsilon(
gradient_mask.steps()[gradient_mask.step_count() - 1].fraction,
1.f);
}
// static
void DesksTestApi::ResetDeskVisitedMetrics(Desk* desk) {
const int current_date = desks_restore_util::GetDaysFromLocalEpoch();
desk->first_day_visited_ = current_date;
desk->last_day_visited_ = current_date;
}
// static
void DesksTestApi::WaitForDeskBarUiUpdate(DeskBarViewBase* desk_bar_view) {
base::RunLoop run_loop;
desk_bar_view->on_update_ui_closure_for_testing_ = run_loop.QuitClosure();
run_loop.Run();
}
// static
void DesksTestApi::SetDeskBarUiUpdateCallback(DeskBarViewBase* desk_bar_view,
base::OnceClosure done) {
desk_bar_view->on_update_ui_closure_for_testing_ = std::move(done);
}
// static
DeskActionContextMenu* DesksTestApi::GetContextMenuForDesk(
DeskBarViewBase::Type type,
int index) {
DeskMiniView* mini_view = GetDeskBarView(type)->mini_views()[index];
// The context menu is not created until it is opened, so open it first.
mini_view->OpenContextMenu(ui::mojom::MenuSourceType::kMouse);
return mini_view->context_menu();
}
// static
const ui::SimpleMenuModel& DesksTestApi::GetContextMenuModelForDesk(
DeskBarViewBase::Type type,
int index) {
return GetContextMenuForDesk(type, index)->context_menu_model_;
}
// static
bool DesksTestApi::IsContextMenuRunningForDesk(DeskBarViewBase::Type type,
int index) {
DeskMiniView* mini_view = GetDeskBarView(type)->mini_views()[index];
DeskActionContextMenu* menu = mini_view->context_menu();
if (!menu) {
return false;
}
return menu->context_menu_runner_->IsRunning();
}
// static
views::MenuItemView* DesksTestApi::GetDeskActionContextMenuItem(
DeskActionContextMenu* menu,
int command_id) {
// Verify that the menu is active.
CHECK(menu->context_menu_runner_);
return menu->root_menu_item_view_->GetMenuItemByID(command_id);
}
// static
views::MenuItemView* DesksTestApi::OpenDeskContextMenuAndGetMenuItem(
aura::Window* root,
DeskBarViewBase::Type bar_type,
size_t index,
DeskActionContextMenu::CommandId command_id) {
ui::test::EventGenerator event_generator(root);
auto click_on_view = [&event_generator](const views::View* view) {
event_generator.MoveMouseToInHost(view->GetBoundsInScreen().CenterPoint());
event_generator.ClickLeftButton();
};
auto* bar_view = const_cast<DeskBarViewBase*>(GetDeskBarView(bar_type));
// Expand the desk bar if necessary.
if (bar_view->IsZeroState()) {
// Click the default desk button so it will expand to a `DeskMiniView`.
auto* default_button = bar_view->default_desk_button();
CHECK(default_button);
click_on_view(default_button);
// Wait for the desk bar to finish animating to the expanded state.
if (!ui::ScopedAnimationDurationScaleMode::is_zero()) {
DesksTestApi::WaitForDeskBarUiUpdate(bar_view);
}
CHECK(!bar_view->IsZeroState());
}
CHECK_LE(index, bar_view->mini_views().size());
DeskMiniView* mini_view = bar_view->mini_views()[index];
// If we have previously been using touch controls, open the menu using a long
// press on the mini view. Otherwise, hover over the mini view to get the
// action buttons to show, then click the menu button.
if (!aura::client::GetCursorClient(root)->IsCursorVisible()) {
// Long press until the context menu opens.
LongGestureTap(
mini_view->desk_action_view()->GetBoundsInScreen().CenterPoint(),
&event_generator);
} else {
// Hover over the mini view to show the context menu button.
event_generator.MoveMouseToInHost(
mini_view->GetBoundsInScreen().CenterPoint());
// The menu button container should be visible, along with the button
// itself.
CHECK(mini_view->desk_action_view()->GetVisible());
DeskActionButton* menu_button =
mini_view->desk_action_view()->context_menu_button();
CHECK(menu_button);
CHECK(menu_button->GetVisible());
// Click the button to open the context menu.
click_on_view(menu_button);
}
DeskActionContextMenu* menu = mini_view->context_menu();
CHECK(menu);
return GetDeskActionContextMenuItem(menu, command_id);
}
// static
void DesksTestApi::MaybeCloseContextMenuForGrid(OverviewGrid* overview_grid) {
for (DeskMiniView* mini_view :
overview_grid->desks_bar_view()->mini_views()) {
mini_view->MaybeCloseContextMenu();
// Closing the menu is asynchronous, so we want to wait until it has
// actually closed.
base::RunLoop().RunUntilIdle();
}
}
// static
base::TimeDelta DesksTestApi::GetCloseAllWindowCloseTimeout() {
return DesksController::GetCloseAllWindowCloseTimeoutForTest();
}
// static
base::AutoReset<base::TimeDelta> DesksTestApi::SetCloseAllWindowCloseTimeout(
base::TimeDelta interval) {
return DesksController::SetCloseAllWindowCloseTimeoutForTest(interval);
}
// static
base::AutoReset<base::TimeDelta> DesksTestApi::SetScrollTimeInterval(
base::TimeDelta interval) {
return ScrollArrowButton::SetScrollTimeIntervalForTest(interval);
}
} // namespace ash