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
ash / wm / gestures / back_gesture / back_gesture_contextual_nudge_controller_impl_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/wm/gestures/back_gesture/back_gesture_contextual_nudge_controller_impl.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/controls/contextual_tooltip.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/test_shell_delegate.h"
#include "ash/wm/gestures/back_gesture/back_gesture_contextual_nudge.h"
#include "ash/wm/gestures/back_gesture/back_gesture_event_handler.h"
#include "ash/wm/gestures/back_gesture/test_back_gesture_contextual_nudge_delegate.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_clock.h"
#include "build/build_config.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
constexpr char kUser1Email[] = "user1@test.com";
constexpr char kUser2Email[] = "user2@test.com";
// Distance that swiping from left edge to let the affordance achieve
// activated state.
static constexpr int kSwipingDistanceForGoingBack = 80;
} // namespace
class BackGestureContextualNudgeControllerTest : public NoSessionAshTestBase {
public:
explicit BackGestureContextualNudgeControllerTest(bool can_go_back = true)
: can_go_back_(can_go_back) {
scoped_feature_list_.InitAndEnableFeature(
features::kHideShelfControlsInTabletMode);
}
BackGestureContextualNudgeControllerTest(
base::test::TaskEnvironment::TimeSource time,
bool can_go_back = true)
: NoSessionAshTestBase(time), can_go_back_(can_go_back) {
scoped_feature_list_.InitAndEnableFeature(
features::kHideShelfControlsInTabletMode);
}
~BackGestureContextualNudgeControllerTest() override = default;
// NoSessionAshTestBase:
void SetUp() override {
std::unique_ptr<TestShellDelegate> delegate;
if (!can_go_back_) {
delegate = std::make_unique<TestShellDelegate>();
delegate->SetCanGoBack(false);
}
NoSessionAshTestBase::SetUp(std::move(delegate));
GetSessionControllerClient()->AddUserSession(kUser1Email);
GetSessionControllerClient()->AddUserSession(kUser2Email);
// Simulate login of user 1.
SwitchActiveUser(kUser1Email);
GetSessionControllerClient()->SetSessionState(
session_manager::SessionState::ACTIVE);
// Is only allowed after the drag handle nudge has been shown - simulate
// drag handle so back gesture gets enabled.
contextual_tooltip::OverrideClockForTesting(&test_clock_);
test_clock_.Advance(base::Seconds(360));
contextual_tooltip::HandleNudgeShown(
user1_pref_service(), contextual_tooltip::TooltipType::kInAppToHome);
contextual_tooltip::HandleNudgeShown(
user2_pref_service(), contextual_tooltip::TooltipType::kInAppToHome);
test_clock_.Advance(
contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge * 2);
// Enter tablet mode.
TabletModeControllerTestApi().EnterTabletMode();
}
void TearDown() override {
contextual_tooltip::ClearClockOverrideForTesting();
NoSessionAshTestBase::TearDown();
}
void SwitchActiveUser(const std::string& email) {
GetSessionControllerClient()->SwitchActiveUser(
AccountId::FromUserEmail(email));
}
void WaitNudgeAnimationDone() {
while (nudge()) {
base::RunLoop run_loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, run_loop.QuitClosure(), base::Milliseconds(100));
run_loop.Run();
}
}
// Set nudge as shown for testing. Only after nudge is counted as shown,
// the nudge dismiss metrics can be correctly logged. This is to simulate
// something happens in the middle of nudge animation to dismiss the nudge.
void SetNudgeShownForTesting() {
if (nudge())
nudge()->SetNudgeShownForTesting();
}
PrefService* user1_pref_service() {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(kUser1Email));
}
PrefService* user2_pref_service() {
return Shell::Get()->session_controller()->GetUserPrefServiceForUser(
AccountId::FromUserEmail(kUser2Email));
}
BackGestureContextualNudgeControllerImpl* nudge_controller() {
return Shell::Get()
->back_gesture_event_handler()
->nudge_controller_for_testing();
}
BackGestureContextualNudge* nudge() { return nudge_controller()->nudge(); }
base::SimpleTestClock* clock() { return &test_clock_; }
// Generates a scroll sequence that will create a back gesture.
void GenerateBackSequence() {
GetEventGenerator()->GestureScrollSequence(
gfx::Point(0, 100), gfx::Point(kSwipingDistanceForGoingBack + 10, 100),
base::Milliseconds(100), 3);
}
private:
bool can_go_back_;
base::SimpleTestClock test_clock_;
base::test::ScopedFeatureList scoped_feature_list_;
};
class BackGestureContextualNudgeControllerTestCantGoBack
: public BackGestureContextualNudgeControllerTest {
public:
BackGestureContextualNudgeControllerTestCantGoBack()
: BackGestureContextualNudgeControllerTest(false) {}
};
class BackGestureContextualNudgeControllerTestA11yPrefs
: public BackGestureContextualNudgeControllerTest,
public ::testing::WithParamInterface<std::string> {
public:
BackGestureContextualNudgeControllerTestA11yPrefs()
: BackGestureContextualNudgeControllerTest(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
BackGestureContextualNudgeControllerTestA11yPrefs(
const BackGestureContextualNudgeControllerTestA11yPrefs&) = delete;
BackGestureContextualNudgeControllerTestA11yPrefs& operator=(
const BackGestureContextualNudgeControllerTestA11yPrefs&) = delete;
~BackGestureContextualNudgeControllerTestA11yPrefs() override = default;
};
INSTANTIATE_TEST_SUITE_P(
All,
BackGestureContextualNudgeControllerTestA11yPrefs,
testing::Values(prefs::kAccessibilityAutoclickEnabled,
prefs::kAccessibilitySpokenFeedbackEnabled,
prefs::kAccessibilitySwitchAccessEnabled));
// Tests the timing when BackGestureContextualNudgeControllerImpl should monitor
// window activation changes.
TEST_F(BackGestureContextualNudgeControllerTest, MonitorWindowsTest) {
// Only monitor windows in tablet mode.
EXPECT_TRUE(nudge_controller()->is_monitoring_windows());
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
EXPECT_FALSE(nudge_controller()->is_monitoring_windows());
tablet_mode_api.EnterTabletMode();
EXPECT_TRUE(nudge_controller()->is_monitoring_windows());
// Only monitor windows in active user session.
GetSessionControllerClient()->LockScreen();
EXPECT_FALSE(nudge_controller()->is_monitoring_windows());
GetSessionControllerClient()->UnlockScreen();
EXPECT_TRUE(nudge_controller()->is_monitoring_windows());
// Exit tablet mode for kUser1Email.
tablet_mode_api.LeaveTabletMode();
EXPECT_FALSE(nudge_controller()->is_monitoring_windows());
// Then enter tablet mode for kUserEmail2.
SwitchActiveUser(kUser2Email);
tablet_mode_api.EnterTabletMode();
EXPECT_TRUE(nudge_controller()->is_monitoring_windows());
}
// Tests the activation of another window will cancel the in-waiting or
// in-progress nudge animation.
TEST_F(BackGestureContextualNudgeControllerTest,
ActivationCancelAnimationTest) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
EXPECT_FALSE(nudge());
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
std::unique_ptr<aura::Window> window1 = CreateTestWindow();
// If nudge() is true, it indicates that it's currently in animation.
EXPECT_TRUE(nudge());
// At this moment, change window activation should cancel the previous nudge
// showup animation on |window1|, and start show nudge on |window2|.
std::unique_ptr<aura::Window> window2 = CreateTestWindow();
EXPECT_FALSE(nudge()->ShouldNudgeCountAsShown());
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
// Wait until nudge animation is finished.
WaitNudgeAnimationDone();
EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
}
// Test that ending tablet mode will cancel in-waiting or in-progress nudge
// animation.
TEST_F(BackGestureContextualNudgeControllerTest,
EndTabletModeCancelAnimationTest) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
EXPECT_FALSE(nudge());
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
TabletModeControllerTestApi().LeaveTabletMode();
WaitNudgeAnimationDone();
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
}
// Do not show nudge ui on window that can't perform "go back" operation.
TEST_F(BackGestureContextualNudgeControllerTestCantGoBack, WindowTest) {
EXPECT_FALSE(nudge());
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_FALSE(nudge());
EXPECT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
}
TEST_F(BackGestureContextualNudgeControllerTest, ShowNudgeOnExistingWindow) {
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
EXPECT_FALSE(nudge());
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_FALSE(nudge());
tablet_mode_api.EnterTabletMode();
EXPECT_TRUE(nudge());
}
// Do not show nudge ui on window if shelf drag handle nudge should be shown at
// the same time.
TEST_F(BackGestureContextualNudgeControllerTest, NotShownWithDragHandleNudge) {
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
// Advance the contextual tooltip manager's clock so drag handle nudge can be
// shown again (note that the drag handle nudge is first shown during test
// setup to enable the back gesture nudge).
clock()->Advance(contextual_tooltip::kMinInterval);
tablet_mode_api.EnterTabletMode();
ASSERT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kInAppToHome,
nullptr));
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_FALSE(nudge());
EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
}
// Verifies that back gesture nudge will be shown after drag handle nudge if
// enough time passes, even if the user does not leave tablet mode.
TEST_F(BackGestureContextualNudgeControllerTest,
CanBeShownAfterDragHandleNudgeWithoutLeavingTabletMode) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
// Advance the contextual tooltip manager's clock so drag handle nudge can be
// shown again (note that the drag handle nudge is first shown during test
// setup to enable the back gesture nudge).
clock()->Advance(contextual_tooltip::kMinInterval);
tablet_mode_api.EnterTabletMode();
ASSERT_TRUE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kInAppToHome,
nullptr));
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_FALSE(nudge());
EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
contextual_tooltip::HandleNudgeShown(
user1_pref_service(), contextual_tooltip::TooltipType::kInAppToHome);
clock()->Advance(
contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge);
ASSERT_TRUE(nudge_controller()->auto_show_timer_for_testing()->IsRunning());
nudge_controller()->auto_show_timer_for_testing()->FireNow();
std::unique_ptr<aura::Window> window_2 = CreateTestWindow();
EXPECT_TRUE(nudge());
}
// Verifies that back gesture nudge will be shown again if enough time passes,
// even it the user does not leave tablet mode.
TEST_F(BackGestureContextualNudgeControllerTest,
CanBeShownAfterRenteringTabletMode) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Verify the nudge is created and wait until nudge animation is shown.
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
SetNudgeShownForTesting();
EXPECT_FALSE(contextual_tooltip::ShouldShowNudge(
user1_pref_service(), contextual_tooltip::TooltipType::kBackGesture,
nullptr));
// Reenter tablet mode, and verify the nudge can be shown again after the
// nudge interval passes.
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
tablet_mode_api.EnterTabletMode();
clock()->Advance(contextual_tooltip::kMinInterval);
contextual_tooltip::HandleNudgeShown(
user1_pref_service(), contextual_tooltip::TooltipType::kInAppToHome);
clock()->Advance(
contextual_tooltip::kMinIntervalBetweenBackAndDragHandleNudge);
ASSERT_TRUE(nudge_controller()->auto_show_timer_for_testing()->IsRunning());
nudge_controller()->auto_show_timer_for_testing()->FireNow();
std::unique_ptr<aura::Window> window_2 = CreateTestWindow();
EXPECT_TRUE(nudge());
}
// Back gesture metrics should be recorded after performing gesture.
TEST_F(BackGestureContextualNudgeControllerTest, GesturePerformedMetricTest) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Verify the nudge is created and wait until nudge animation is shown.
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
SetNudgeShownForTesting();
GenerateBackSequence();
}
TEST_P(BackGestureContextualNudgeControllerTestA11yPrefs, TimeoutMetricsTest) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
WaitNudgeAnimationDone();
EXPECT_FALSE(nudge());
}
TEST_P(BackGestureContextualNudgeControllerTestA11yPrefs,
LogDismissMetricsAfterNudgeShown) {
ui::ScopedAnimationDurationScaleMode non_zero(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
// Before nudge is still waiting to be shown, exit tablet mode. The nudge will
// be dismissed immediately.
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
EXPECT_FALSE(nudge());
tablet_mode_api.EnterTabletMode();
std::unique_ptr<aura::Window> window2 = CreateTestWindow();
EXPECT_TRUE(nudge());
SetNudgeShownForTesting();
// Exit tablet mode in the middle of the animation, test the dismissmal
// metrics should be correctly logged.
tablet_mode_api.LeaveTabletMode();
WaitNudgeAnimationDone();
}
// Back Gesture Nudge should be hidden when shelf controls are enabled.
TEST_P(BackGestureContextualNudgeControllerTestA11yPrefs,
HideNudgesForShelfControls) {
SCOPED_TRACE(testing::Message() << "Pref=" << GetParam());
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_TRUE(nudge());
SetNudgeShownForTesting();
// Turn on accessibility settings to enable shelf controls.
Shell::Get()
->session_controller()
->GetLastActiveUserPrefService()
->SetBoolean(GetParam(), true);
EXPECT_FALSE(nudge());
TabletModeControllerTestApi tablet_mode_api;
tablet_mode_api.LeaveTabletMode();
}
// Back Gesture Nudge should be disabled when shelf controls are enabled.
TEST_P(BackGestureContextualNudgeControllerTestA11yPrefs,
DisableNudgesForShelfControls) {
SCOPED_TRACE(testing::Message() << "Pref=" << GetParam());
// Turn on accessibility settings to enable shelf controls.
Shell::Get()
->session_controller()
->GetLastActiveUserPrefService()
->SetBoolean(GetParam(), true);
std::unique_ptr<aura::Window> window = CreateTestWindow();
EXPECT_FALSE(nudge());
}
} // namespace ash