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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
ash / shelf / home_to_overview_nudge_controller_unittest.cc [blame]
// Copyright 2020 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/shelf/home_to_overview_nudge_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/controls/contextual_nudge.h"
#include "ash/controls/contextual_tooltip.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shelf/hotseat_widget.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/swipe_home_to_overview_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "ash/wm/window_state.h"
#include "base/functional/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/window_util.h"
namespace ash {
class WidgetCloseObserver {
public:
explicit WidgetCloseObserver(views::Widget* widget)
: widget_(widget->GetWeakPtr()) {}
~WidgetCloseObserver() = default;
bool WidgetClosed() const { return !widget_ || widget_->IsClosed(); }
private:
base::WeakPtr<views::Widget> widget_;
};
class HomeToOverviewNudgeControllerWithNudgesDisabledTest
: public NoSessionAshTestBase {
public:
HomeToOverviewNudgeControllerWithNudgesDisabledTest() {
scoped_feature_list_.InitAndDisableFeature(
features::kHideShelfControlsInTabletMode);
}
~HomeToOverviewNudgeControllerWithNudgesDisabledTest() override = default;
HomeToOverviewNudgeControllerWithNudgesDisabledTest(
const HomeToOverviewNudgeControllerWithNudgesDisabledTest& other) =
delete;
HomeToOverviewNudgeControllerWithNudgesDisabledTest& operator=(
const HomeToOverviewNudgeControllerWithNudgesDisabledTest& other) =
delete;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class HomeToOverviewNudgeControllerTest : public NoSessionAshTestBase {
public:
HomeToOverviewNudgeControllerTest() {
scoped_feature_list_.InitAndEnableFeature(
features::kHideShelfControlsInTabletMode);
}
~HomeToOverviewNudgeControllerTest() override = default;
HomeToOverviewNudgeControllerTest(
const HomeToOverviewNudgeControllerTest& other) = delete;
HomeToOverviewNudgeControllerTest& operator=(
const HomeToOverviewNudgeControllerTest& other) = delete;
// AshTestBase:
void SetUp() override {
NoSessionAshTestBase::SetUp();
GetSessionControllerClient()->SetSessionState(
session_manager::SessionState::LOGIN_PRIMARY);
test_clock_.Advance(base::Hours(2));
contextual_tooltip::OverrideClockForTesting(&test_clock_);
}
void TearDown() override {
contextual_tooltip::ClearClockOverrideForTesting();
NoSessionAshTestBase::TearDown();
}
HomeToOverviewNudgeController* GetNudgeController() {
return GetPrimaryShelf()
->shelf_layout_manager()
->home_to_overview_nudge_controller_for_testing();
}
views::Widget* GetNudgeWidget() {
if (!GetNudgeController()->nudge_for_testing())
return nullptr;
return GetNudgeController()->nudge_for_testing()->GetWidget();
}
HotseatWidget* GetHotseatWidget() {
return GetPrimaryShelf()->shelf_widget()->hotseat_widget();
}
// Helper that creates and minimzes |count| number of windows.
using ScopedWindowList = std::vector<std::unique_ptr<aura::Window>>;
ScopedWindowList CreateAndMinimizeWindows(int count) {
ScopedWindowList windows;
for (int i = 0; i < count; ++i) {
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
WindowState::Get(window.get())->Minimize();
windows.push_back(std::move(window));
}
return windows;
}
void SanityCheckNudgeBounds() {
views::Widget* const nudge_widget = GetNudgeWidget();
ASSERT_TRUE(nudge_widget);
EXPECT_TRUE(nudge_widget->IsVisible());
const gfx::Rect nudge_bounds =
nudge_widget->GetLayer()->transform().MapRect(
nudge_widget->GetNativeWindow()->GetTargetBounds());
HotseatWidget* const hotseat = GetHotseatWidget();
const gfx::Rect hotseat_bounds =
hotseat->GetLayerForNudgeAnimation()->transform().MapRect(
hotseat->GetNativeWindow()->GetTargetBounds());
// Nudge and hotseat should have the same transform.
EXPECT_EQ(hotseat->GetLayerForNudgeAnimation()->transform(),
nudge_widget->GetLayer()->transform());
// Nudge should be under the hotseat.
EXPECT_LE(hotseat_bounds.bottom(), nudge_bounds.y());
const gfx::Rect display_bounds = GetPrimaryDisplay().bounds();
EXPECT_TRUE(display_bounds.Contains(nudge_bounds))
<< display_bounds.ToString() << " contains " << nudge_bounds.ToString();
// Verify that the nudge is centered within the display bounds.
EXPECT_LE((nudge_bounds.x() - display_bounds.x()) -
(display_bounds.right() - nudge_bounds.right()),
1)
<< nudge_bounds.ToString() << " within " << display_bounds.ToString();
// Verify that the nudge label is visible
EXPECT_EQ(
1.0f,
GetNudgeController()->nudge_for_testing()->label()->layer()->opacity());
}
base::SimpleTestClock test_clock_;
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class HomeToOverviewNudgeControllerTestWithA11yPrefs
: public HomeToOverviewNudgeControllerTest,
public ::testing::WithParamInterface<std::string> {};
INSTANTIATE_TEST_SUITE_P(
all,
HomeToOverviewNudgeControllerTestWithA11yPrefs,
testing::Values(prefs::kAccessibilityAutoclickEnabled,
prefs::kAccessibilitySpokenFeedbackEnabled,
prefs::kAccessibilitySwitchAccessEnabled));
// Tests that home to overview gesture nudge is not shown if contextual nudges
// are disabled.
TEST_F(HomeToOverviewNudgeControllerWithNudgesDisabledTest,
NoNudgeOnHomeScreen) {
EXPECT_FALSE(GetPrimaryShelf()
->shelf_layout_manager()
->home_to_overview_nudge_controller_for_testing());
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
std::unique_ptr<aura::Window> window_1 =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
WindowState::Get(window_1.get())->Minimize();
std::unique_ptr<aura::Window> window_2 =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
WindowState::Get(window_2.get())->Minimize();
EXPECT_FALSE(GetPrimaryShelf()
->shelf_layout_manager()
->home_to_overview_nudge_controller_for_testing());
}
// Tests that home to overview nudge is not shown before user logs in.
TEST_F(HomeToOverviewNudgeControllerTest, NoNudgeBeforeLogin) {
TabletModeControllerTestApi().EnterTabletMode();
EXPECT_FALSE(GetNudgeController());
SimulateUserLogin(kDefaultUserEmail);
EXPECT_TRUE(GetNudgeController());
}
// Test the flow for showing the home to overview gesture nudge - when shown the
// first time, nudge should remain visible until the hotseat state changes. On
// subsequent shows, the nudge should be hidden after a timeout.
TEST_F(HomeToOverviewNudgeControllerTest, ShownOnHomeScreen) {
base::HistogramTester histogram_tester;
SimulateUserLogin(kDefaultUserEmail);
// The nudge should not be shown in clamshell.
EXPECT_FALSE(GetNudgeController());
// In tablet mode, the nudge should be shown after at least 2 windows are
// minimized.
TabletModeControllerTestApi().EnterTabletMode();
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
ScopedWindowList window_1 = CreateAndMinimizeWindows(1);
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
ScopedWindowList window_2 = CreateAndMinimizeWindows(1);
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeController()->nudge_for_testing());
{
SCOPED_TRACE("First nudge");
SanityCheckNudgeBounds();
}
EXPECT_FALSE(GetNudgeController()->HasHideTimerForTesting());
// Transitioning to overview should hide the nudge.
EnterOverview();
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
// Ending overview, and transitioning to the home screen again should not show
// the nudge.
ExitOverview();
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->transform());
// Advance time for more than a day (which should enable the nudge again).
test_clock_.Advance(base::Hours(25));
// The nudge should not show up unless the user actually transitions to home.
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
// Create and minimize another test window to force a transition to home.
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
wm::ActivateWindow(window.get());
WindowState::Get(window.get())->Minimize();
// Nudge should be shown again.
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeController()->nudge_for_testing());
{
SCOPED_TRACE("Second nudge");
SanityCheckNudgeBounds();
}
// The second time, the nudge should be hidden after a timeout.
ASSERT_TRUE(GetNudgeController()->HasHideTimerForTesting());
GetNudgeController()->FireHideTimerForTesting();
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->transform());
}
// Tests that the nudge eventually stops showing.
TEST_F(HomeToOverviewNudgeControllerTest, ShownLimitedNumberOfTimes) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
// Show the nudge kNotificationLimit amount of time.
for (int i = 0; i < contextual_tooltip::kNotificationLimit; ++i) {
SCOPED_TRACE(testing::Message() << "Attempt " << i);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeController()->nudge_for_testing());
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
wm::ActivateWindow(window.get());
test_clock_.Advance(base::Hours(25));
WindowState::Get(window.get())->Minimize();
}
// At this point, given the nudge was shown the intended number of times
// already, the nudge should not show up, even though the device is on home
// screen.
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
}
// Tests that the nudge is hidden when tablet mode exits.
TEST_F(HomeToOverviewNudgeControllerTest, HiddenOnTabletModeExit) {
base::HistogramTester histogram_tester;
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
TabletModeControllerTestApi().LeaveTabletMode();
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
// Tests that the nudge show is canceled when tablet mode exits.
TEST_F(HomeToOverviewNudgeControllerTest, ShowCanceledOnTabletModeExit) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
TabletModeControllerTestApi().LeaveTabletMode();
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
// Tests that the nudge show animation is canceled when tablet mode exits.
TEST_F(HomeToOverviewNudgeControllerTest,
ShowAnimationCanceledOnTabletModeExit) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeWidget()->GetLayer()->GetAnimator()->is_animating());
TabletModeControllerTestApi().LeaveTabletMode();
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
// Tests that the nudge is hidden when the screen is locked.
TEST_F(HomeToOverviewNudgeControllerTest, HiddenOnScreenLock) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
GetSessionControllerClient()->SetSessionState(
session_manager::SessionState::LOCKED);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
// Nudge should not be shown if a window is shown and hidden behind a lock
// screen.
test_clock_.Advance(base::Hours(25));
ScopedWindowList locked_session_window = CreateAndMinimizeWindows(1);
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
}
// Tests that the nudge show is canceled if the in-app shelf is shown before the
// show timer runs.
TEST_F(HomeToOverviewNudgeControllerTest, InAppShelfShownBeforeShowTimer) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_TRUE(GetNudgeController()->HasShowTimerForTesting());
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
wm::ActivateWindow(window.get());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
// When the home screen is shown the next time, the nudge should be shown
// again, without timeout to hide it.
WindowState::Get(window.get())->Minimize();
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
EXPECT_TRUE(GetNudgeController()->nudge_for_testing());
EXPECT_FALSE(GetNudgeController()->HasHideTimerForTesting());
}
// Tests that in-app shelf will hide the nudge if it happens during the
// animation to show the nudge.
TEST_F(HomeToOverviewNudgeControllerTest, NudgeHiddenDuringShowAnimation) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeWidget()->GetLayer()->GetAnimator()->is_animating());
// Cache the widget, as GetNudgeWidget() will start returning nullptr when the
// nudge starts hiding.
ContextualNudge* nudge = GetNudgeController()->nudge_for_testing();
views::Widget* nudge_widget = nudge->GetWidget();
WidgetCloseObserver widget_close_observer(nudge_widget);
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
wm::ActivateWindow(window.get());
EXPECT_FALSE(GetNudgeWidget());
EXPECT_FALSE(nudge_widget->IsVisible());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
EXPECT_TRUE(widget_close_observer.WidgetClosed());
EXPECT_TRUE(GetHotseatWidget()
->GetLayerForNudgeAnimation()
->GetAnimator()
->is_animating());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
// When the nudge is shown again, it should be hidden after a timeout.
test_clock_.Advance(base::Hours(25));
WindowState::Get(window.get())->Minimize();
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
EXPECT_TRUE(GetNudgeController()->nudge_for_testing());
EXPECT_TRUE(GetNudgeController()->HasHideTimerForTesting());
}
// Tests that there is no crash if the nudge widget gets closed unexpectedly.
TEST_F(HomeToOverviewNudgeControllerTest, NoCrashIfNudgeWidgetGetsClosed) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
GetNudgeWidget()->CloseNow();
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
std::unique_ptr<aura::Window> window =
CreateTestWindow(gfx::Rect(0, 0, 400, 400));
wm::ActivateWindow(window.get());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
}
// Tests that tapping on the nudge hides the nudge.
TEST_F(HomeToOverviewNudgeControllerTest, TapOnTheNudgeClosesTheNudge) {
base::HistogramTester histogram_tester;
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeController()->nudge_for_testing());
views::Widget* nudge_widget = GetNudgeWidget();
WidgetCloseObserver widget_close_observer(nudge_widget);
GetEventGenerator()->GestureTapAt(
nudge_widget->GetWindowBoundsInScreen().CenterPoint());
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_TRUE(widget_close_observer.WidgetClosed());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
TEST_F(HomeToOverviewNudgeControllerTest, TapOnTheNudgeDuringShowAnimation) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList extra_windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
ui::ScopedAnimationDurationScaleMode test_duration_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
GetNudgeController()->FireShowTimerForTesting();
ASSERT_TRUE(GetNudgeWidget()->GetLayer()->GetAnimator()->is_animating());
// Cache the widget, as GetNudgeWidget() will start returning nullptr when the
// nudge starts hiding.
ContextualNudge* nudge = GetNudgeController()->nudge_for_testing();
views::Widget* nudge_widget = nudge->GetWidget();
WidgetCloseObserver widget_close_observer(nudge_widget);
GetEventGenerator()->GestureTapAt(
nudge_widget->GetWindowBoundsInScreen().CenterPoint());
ASSERT_TRUE(nudge_widget->GetLayer()->GetAnimator()->is_animating());
EXPECT_TRUE(nudge_widget->IsVisible());
EXPECT_EQ(gfx::Transform(), nudge_widget->GetLayer()->GetTargetTransform());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
EXPECT_FALSE(widget_close_observer.WidgetClosed());
ASSERT_TRUE(nudge->label()->layer()->GetAnimator()->is_animating());
EXPECT_EQ(0.0f, nudge->label()->layer()->GetTargetOpacity());
nudge->label()->layer()->GetAnimator()->StopAnimating();
EXPECT_FALSE(GetNudgeWidget());
EXPECT_FALSE(nudge_widget->IsVisible());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
EXPECT_TRUE(widget_close_observer.WidgetClosed());
EXPECT_TRUE(GetHotseatWidget()
->GetLayerForNudgeAnimation()
->GetAnimator()
->is_animating());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
// Tests that the nudge stops showing up if the user performs the gesture few
// times.
TEST_F(HomeToOverviewNudgeControllerTest, NoNudgeAfterSuccessfulGestures) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
// Perform home to overview gesture kSuccessLimitHomeToOverview times.
for (int i = 0; i < contextual_tooltip::kSuccessLimitHomeToOverview; ++i) {
SCOPED_TRACE(testing::Message() << "Attempt " << i);
// Perform home to overview gesture.
wm::ActivateWindow(windows[0].get());
WindowState::Get(windows[0].get())->Minimize();
// Simluate swipe up and hold gesture on the home screen (which should
// transition to overview).
const gfx::Point start = GetPrimaryShelf()
->hotseat_widget()
->GetWindowBoundsInScreen()
.CenterPoint();
GetEventGenerator()->GestureScrollSequenceWithCallback(
start, start + gfx::Vector2d(0, -100), base::Milliseconds(50),
/*num_steps = */ 12,
base::BindRepeating(
[](ui::EventType type, const gfx::Vector2dF& offset) {
if (type != ui::EventType::kGestureScrollUpdate) {
return;
}
// If the swipe home to overview controller started the timer to
// transition to overview (which happens after swipe moves far
// enough), run it to trigger transition to overview.
SwipeHomeToOverviewController* swipe_controller =
GetPrimaryShelf()
->shelf_layout_manager()
->swipe_home_to_overview_controller_for_testing();
ASSERT_TRUE(swipe_controller);
base::OneShotTimer* transition_timer =
swipe_controller->overview_transition_timer_for_testing();
if (transition_timer->IsRunning())
transition_timer->FireNow();
}));
// No point in continuing the test if transition to overview failed.
ASSERT_TRUE(Shell::Get()->overview_controller()->InOverviewSession());
}
// The nudge should not be shown next time the user transitions to home.
test_clock_.Advance(base::Hours(25));
ScopedWindowList extra_window = CreateAndMinimizeWindows(1);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
EXPECT_FALSE(GetNudgeController()->HasShowTimerForTesting());
EXPECT_EQ(
gfx::Transform(),
GetHotseatWidget()->GetLayerForNudgeAnimation()->GetTargetTransform());
}
// Tests that swipe up and hold gesture that starts on top of contextual nudge
// widget works - i.e. that home still transitions to overview.
TEST_F(HomeToOverviewNudgeControllerTest, HomeToOverviewGestureFromNudge) {
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
// Simluate swipe up and hold gesture on home screen from the nudge widget.
const gfx::Point start =
GetNudgeWidget()->GetWindowBoundsInScreen().CenterPoint();
GetEventGenerator()->GestureScrollSequenceWithCallback(
start, start + gfx::Vector2d(0, -100), base::Milliseconds(50),
/*num_steps = */ 12,
base::BindRepeating([](ui::EventType type, const gfx::Vector2dF& offset) {
if (type != ui::EventType::kGestureScrollUpdate) {
return;
}
// If the swipe home to overview controller started the timer to
// transition to overview (which happens after swipe moves far
// enough), run it to trigger transition to overview.
SwipeHomeToOverviewController* swipe_controller =
GetPrimaryShelf()
->shelf_layout_manager()
->swipe_home_to_overview_controller_for_testing();
ASSERT_TRUE(swipe_controller);
base::OneShotTimer* transition_timer =
swipe_controller->overview_transition_timer_for_testing();
if (transition_timer->IsRunning())
transition_timer->FireNow();
}));
EXPECT_TRUE(Shell::Get()->overview_controller()->InOverviewSession());
}
// Tests that nudge and hotseat get repositioned appropriatelly if the display
// bounds change.
TEST_F(HomeToOverviewNudgeControllerTest,
NudgeBoundsUpdatedOnDisplayBoundsChange) {
UpdateDisplay("768x1200");
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
ASSERT_TRUE(GetNudgeController()->HasShowTimerForTesting());
GetNudgeController()->FireShowTimerForTesting();
{
SCOPED_TRACE("Initial bounds");
SanityCheckNudgeBounds();
}
UpdateDisplay("1200x768");
{
SCOPED_TRACE("Updated bounds");
SanityCheckNudgeBounds();
}
}
// Home to Overview Nudge should be hidden when shelf controls are enabled.
TEST_P(HomeToOverviewNudgeControllerTestWithA11yPrefs,
HideNudgesForShelfControls) {
SCOPED_TRACE(testing::Message() << "Pref=" << GetParam());
// Enters tablet mode and sets up two minimized windows. This will create the
// show nudge timer.
TabletModeControllerTestApi().EnterTabletMode();
SimulateUserLogin(kDefaultUserEmail);
ScopedWindowList windows = CreateAndMinimizeWindows(2);
ASSERT_TRUE(GetNudgeController());
EXPECT_TRUE(GetNudgeController()->HasShowTimerForTesting());
// Fires the show nudge timer to make the nudge visible.
GetNudgeController()->FireShowTimerForTesting();
EXPECT_TRUE(GetNudgeController()->nudge_for_testing());
// Enabling accessibility shelf controls should hide the nudge.
Shell::Get()
->session_controller()
->GetLastActiveUserPrefService()
->SetBoolean(GetParam(), true);
EXPECT_FALSE(GetNudgeController()->nudge_for_testing());
}
// Home to Overview Nudge should not be shown when shelf controls are enabled.
TEST_P(HomeToOverviewNudgeControllerTestWithA11yPrefs,
DisableNudgesForShelfControls) {
SCOPED_TRACE(testing::Message() << "Pref=" << GetParam());
// Enabling accessibility shelf controls should disable the nudge.
Shell::Get()->session_controller()->GetActivePrefService()->SetBoolean(
GetParam(), true);
// Enters tablet mode and sets up two minimized windows. This should not
// trigger the nudge show timer because shelf controls are on.
TabletModeControllerTestApi().EnterTabletMode();
SimulateNewUserFirstLogin("test@gmail.com");
ScopedWindowList windows = CreateAndMinimizeWindows(2);
EXPECT_FALSE(GetNudgeController());
}
} // namespace ash