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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
ash / wm / lock_state_controller_unittest.cc [blame]
// Copyright 2013 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/lock_state_controller.h"
#include <memory>
#include <optional>
#include <utility>
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/shutdown_controller.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/shutdown_reason.h"
#include "ash/system/power/power_button_controller.h"
#include "ash/system/power/power_button_controller_test_api.h"
#include "ash/system/power/power_button_test_base.h"
#include "ash/touch/touch_devices_controller.h"
#include "ash/utility/layer_copy_animator.h"
#include "ash/wallpaper/views/wallpaper_view.h"
#include "ash/wallpaper/views/wallpaper_widget_controller.h"
#include "ash/wm/lock_state_controller_test_api.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/session_state_animator.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "ash/wm/test/test_session_state_animator.h"
#include "ash/wm/window_restore/window_restore_metrics.h"
#include "ash/wm/window_restore/window_restore_util.h"
#include "ash/wm/window_util.h"
#include "base/barrier_closure.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "ui/aura/test/test_windows.h"
#include "ui/display/manager/display_configurator.h"
#include "ui/display/manager/test/fake_display_snapshot.h"
#include "ui/display/tablet_state.h"
#include "ui/display/types/display_constants.h"
#include "ui/gfx/geometry/size.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
constexpr char kShelfShutdownConfirmationHistogramName[] =
"Ash.Shelf.ShutdownConfirmationBubble.TimeToNextBoot."
"LoginShutdownToPowerUpDuration";
// Shorthand for some long constants.
constexpr power_manager::BacklightBrightnessChange_Cause kUserCause =
power_manager::BacklightBrightnessChange_Cause_USER_REQUEST;
constexpr power_manager::BacklightBrightnessChange_Cause kOtherCause =
power_manager::BacklightBrightnessChange_Cause_OTHER;
bool cursor_visible() {
return Shell::Get()->cursor_manager()->IsCursorVisible();
}
void CheckCalledCallback(bool* flag, bool aborted) {
if (flag)
(*flag) = true;
}
// ShutdownController that tracks how many shutdown requests have been made.
class TestShutdownController : public ShutdownController {
public:
TestShutdownController() = default;
TestShutdownController(const TestShutdownController&) = delete;
TestShutdownController& operator=(const TestShutdownController&) = delete;
~TestShutdownController() override = default;
int num_shutdown_requests() const { return num_shutdown_requests_; }
private:
// ShutdownController:
void SetRebootOnShutdown(bool reboot_on_shutdown) override {}
void ShutDownOrReboot(ShutdownReason reason) override {
num_shutdown_requests_++;
}
int num_shutdown_requests_ = 0;
};
} // namespace
class LockStateControllerTest : public PowerButtonTestBase {
public:
LockStateControllerTest() = default;
LockStateControllerTest(const LockStateControllerTest&) = delete;
LockStateControllerTest& operator=(const LockStateControllerTest&) = delete;
~LockStateControllerTest() override = default;
// PowerButtonTestBase:
void SetUp() override {
PowerButtonTestBase::SetUp();
InitPowerButtonControllerMembers(
chromeos::PowerManagerClient::TabletMode::UNSUPPORTED);
test_animator_ = new TestSessionStateAnimator;
lock_state_controller_->set_animator_for_test(test_animator_);
shutdown_controller_resetter_ =
std::make_unique<ShutdownController::ScopedResetterForTest>();
test_shutdown_controller_ = std::make_unique<TestShutdownController>();
lock_state_test_api_->set_shutdown_controller(
test_shutdown_controller_.get());
local_state_ = Shell::Get()->local_state();
}
void TearDown() override {
test_shutdown_controller_.reset();
shutdown_controller_resetter_.reset();
PowerButtonTestBase::TearDown();
}
protected:
int NumShutdownRequests() {
return test_shutdown_controller_->num_shutdown_requests();
}
void Advance(SessionStateAnimator::AnimationSpeed speed) {
test_animator_->Advance(test_animator_->GetDuration(speed));
}
void AdvancePartially(SessionStateAnimator::AnimationSpeed speed,
float factor) {
test_animator_->Advance(test_animator_->GetDuration(speed) * factor);
}
void SendBrightnessChange(
double percent,
power_manager::BacklightBrightnessChange_Cause cause) {
power_manager::BacklightBrightnessChange change;
change.set_percent(percent);
change.set_cause(cause);
power_manager_client()->SendScreenBrightnessChanged(change);
}
void ExpectPreLockAnimationStarted(const std::string& scope) {
SCOPED_TRACE("ExpectPreLockAnimationStarted:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_LIFT));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_OUT));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
EXPECT_TRUE(lock_state_test_api_->is_animating_lock());
}
void ExpectPreLockAnimationRunning(const std::string& scope) {
SCOPED_TRACE("ExpectPreLockAnimationRunning:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_LIFT));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_OUT));
EXPECT_TRUE(lock_state_test_api_->is_animating_lock());
}
void ExpectPreLockAnimationCancel(const std::string& scope) {
SCOPED_TRACE("ExpectPreLockAnimationCancel:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_UNDO_LIFT));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectPreLockAnimationFinished(const std::string& scope) {
SCOPED_TRACE("ExpectPreLockAnimationFinished:" + scope);
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_LIFT));
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_OUT));
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
}
void ExpectPostLockAnimationStarted(const std::string& scope) {
SCOPED_TRACE("ExpectPostLockAnimationStarted:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectPostLockAnimationFinished(const std::string& scope) {
SCOPED_TRACE("ExpectPostLockAnimationFinished:" + scope);
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN));
}
void ExpectUnlockBeforeUIDestroyedAnimationStarted(const std::string& scope) {
SCOPED_TRACE("ExpectUnlockBeforeUIDestroyedAnimationStarted:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_LIFT));
}
void ExpectUnlockBeforeUIDestroyedAnimationFinished(
const std::string& scope) {
SCOPED_TRACE("ExpectUnlockBeforeUIDestroyedAnimationFinished:" + scope);
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_LIFT));
}
void ExpectUnlockAfterUIDestroyedAnimationStarted(const std::string& scope) {
SCOPED_TRACE("ExpectUnlockAfterUIDestroyedAnimationStarted:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_DROP));
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectUnlockAfterUIDestroyedAnimationFinished(const std::string& scope) {
SCOPED_TRACE("ExpectUnlockAfterUIDestroyedAnimationFinished:" + scope);
EXPECT_EQ(0u, test_animator_->GetAnimationCount());
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS,
SessionStateAnimator::ANIMATION_DROP));
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::SHELF, SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectShutdownAnimationStarted(const std::string& scope) {
SCOPED_TRACE("ExpectShutdownAnimationStarted:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::ROOT_CONTAINER,
SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
}
void ExpectShutdownAnimationFinished(const std::string& scope) {
SCOPED_TRACE(" ExpectShutdownAnimationFinished:" + scope);
EXPECT_EQ(0u, test_animator_->GetAnimationCount());
EXPECT_FALSE(test_animator_->AreContainersAnimated(
SessionStateAnimator::ROOT_CONTAINER,
SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS));
}
void ExpectShutdownAnimationCancel(const std::string& scope) {
SCOPED_TRACE("ExpectShutdownAnimationCancel:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::ROOT_CONTAINER,
SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS));
}
void ExpectWallpaperIsShowing(const std::string& scope) {
SCOPED_TRACE("ExpectWallpaperIsShowing:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::WALLPAPER,
SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectWallpaperIsHiding(const std::string& scope) {
SCOPED_TRACE("ExpectWallpaperIsHiding:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::WALLPAPER,
SessionStateAnimator::ANIMATION_FADE_OUT));
}
void ExpectRestoringWallpaperVisibility(const std::string& scope) {
SCOPED_TRACE(" ExpectRestoringWallpaperVisibility:" + scope);
EXPECT_LT(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::WALLPAPER,
SessionStateAnimator::ANIMATION_FADE_IN));
}
void ExpectUnlockedState(const std::string& scope) {
SCOPED_TRACE("ExpectUnlockedState:" + scope);
EXPECT_EQ(0u, test_animator_->GetAnimationCount());
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
}
void ExpectLockedState(const std::string& scope) {
SCOPED_TRACE("ExpectLockedState:" + scope);
EXPECT_EQ(0u, test_animator_->GetAnimationCount());
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
}
void HideWallpaper() { test_animator_->HideWallpaper(); }
void PressLockButton() {
power_button_controller_->OnLockButtonEvent(true, base::TimeTicks::Now());
}
void ReleaseLockButton() {
power_button_controller_->OnLockButtonEvent(false, base::TimeTicks::Now());
}
void SuccessfulAuthentication(bool* call_flag) {
auto callback = base::BindOnce(&CheckCalledCallback, call_flag);
lock_state_controller_->OnLockScreenHide(std::move(callback));
}
bool IsDefaultValueLoginShutdownTimestamp() {
auto* login_shutdown_timestamp_pref =
local_state_->FindPreference(prefs::kLoginShutdownTimestampPrefName);
return login_shutdown_timestamp_pref->IsDefaultValue();
}
// To check if histogram of LockStateController is recorded correctly, we need
// to simulate the restart of a device as the metrics measures the time delta
// between a shutdown from login/lock screen and a following restart. By
// calling the constructor of LockStateController, the restart of a device is
// simulated and the call of UmaHistogramLongTimes is triggered if a previous
// shutdown was initiated with ShutdownReason::LOGIN_SHUT_DOWN_BUTTON.
void RestartDevice() {
LockStateController(test_shutdown_controller_.get(), local_state_);
}
base::HistogramTester& histograms() { return histograms_; }
std::unique_ptr<ShutdownController::ScopedResetterForTest>
shutdown_controller_resetter_;
std::unique_ptr<TestShutdownController> test_shutdown_controller_;
raw_ptr<TestSessionStateAnimator, DanglingUntriaged> test_animator_ =
nullptr; // not owned
private:
// Histogram value verifier.
base::HistogramTester histograms_;
// To access the pref kLoginShutdownTimestampPrefName
raw_ptr<PrefService> local_state_ = nullptr;
};
// Test the show menu and shutdown flow for non-Chrome-OS hardware that doesn't
// correctly report power button releases. We should show menu the first
// time the button is pressed and shut down when it's pressed from the locked
// state.
TEST_F(LockStateControllerTest, LegacyShowMenuAndShutDown) {
Initialize(ButtonType::LEGACY, LoginStatus::USER);
ExpectUnlockedState("1");
// We should request that the screen be locked immediately after seeing the
// power button get pressed.
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->IsMenuOpened());
// We shouldn't progress towards the shutdown state, however.
EXPECT_FALSE(lock_state_test_api_->shutdown_timer_is_running());
ReleasePowerButton();
// Hold the button again and check that we start shutting down.
PressPowerButton();
ExpectShutdownAnimationStarted("2");
EXPECT_EQ(0, NumShutdownRequests());
// Make sure a mouse move event won't show the cursor.
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
// Shutdown was not initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
}
// Test that we ignore power button presses when the screen is turned off on an
// unofficial system.
TEST_F(LockStateControllerTest, LegacyIgnorePowerButtonIfScreenIsOff) {
Initialize(ButtonType::LEGACY, LoginStatus::USER);
// When the screen brightness is at 0%, we shouldn't do anything in response
// to power button presses.
SendBrightnessChange(0, kUserCause);
PressPowerButton();
EXPECT_FALSE(power_button_test_api_->IsMenuOpened());
ReleasePowerButton();
// After increasing the brightness to 10%, we should show the menu as usual.
SendBrightnessChange(10, kUserCause);
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->IsMenuOpened());
ReleasePowerButton();
}
TEST_F(LockStateControllerTest, LegacyHonorPowerButtonInDockedMode) {
Initialize(ButtonType::LEGACY, LoginStatus::USER);
// Create two outputs, the first internal and the second external.
display::DisplayConfigurator::DisplayStateList outputs;
std::unique_ptr<display::DisplaySnapshot> internal_display =
display::FakeDisplaySnapshot::Builder()
.SetId(123)
.SetNativeMode(gfx::Size(1, 1))
.SetType(display::DISPLAY_CONNECTION_TYPE_INTERNAL)
.Build();
outputs.push_back(internal_display.get());
std::unique_ptr<display::DisplaySnapshot> external_display =
display::FakeDisplaySnapshot::Builder()
.SetId(456)
.SetNativeMode(gfx::Size(1, 1))
.SetType(display::DISPLAY_CONNECTION_TYPE_HDMI)
.Build();
outputs.push_back(external_display.get());
// When all of the displays are turned off (e.g. due to user inactivity), the
// power button should be ignored.
SendBrightnessChange(0, kUserCause);
internal_display->set_current_mode(nullptr);
external_display->set_current_mode(nullptr);
power_button_controller_->OnDisplayConfigurationChanged(outputs);
PressPowerButton();
EXPECT_FALSE(power_button_test_api_->IsMenuOpened());
ReleasePowerButton();
// When the screen brightness is 0% but the external display is still turned
// on (indicating either docked mode or the user having manually decreased the
// brightness to 0%), the power button should still be handled.
external_display->set_current_mode(external_display->modes().back().get());
power_button_controller_->OnDisplayConfigurationChanged(outputs);
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->IsMenuOpened());
ReleasePowerButton();
}
// Test the basic operation of the lock button (not logged in).
TEST_F(LockStateControllerTest, LockButtonBasicNotLoggedIn) {
// The lock button shouldn't do anything if we aren't logged in.
Initialize(ButtonType::NORMAL, LoginStatus::NOT_LOGGED_IN);
PressLockButton();
EXPECT_FALSE(lock_state_test_api_->is_animating_lock());
ReleaseLockButton();
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
}
// Test the basic operation of the lock button (guest).
TEST_F(LockStateControllerTest, LockButtonBasicGuest) {
// The lock button shouldn't do anything when we're logged in as a guest.
Initialize(ButtonType::NORMAL, LoginStatus::GUEST);
PressLockButton();
EXPECT_FALSE(lock_state_test_api_->is_animating_lock());
ReleaseLockButton();
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
}
class LockStateControllerAnimationTest
: public LockStateControllerTest,
public ::testing::WithParamInterface<bool> {
public:
void AdvanceOrAbort(SessionStateAnimator::AnimationSpeed speed) {
if (GetParam()) {
test_animator_->Advance(test_animator_->GetDuration(speed));
} else {
test_animator_->AbortAnimations(
SessionStateAnimator::kAllNonRootContainersMask);
}
}
// Switches to tablet mode for tests that want table mode power button
// behavior, also sets other session related info to simulate being on a lock
// screen with some other relevant user prefs.
void PrepareSessionForUnlockAnimationInTabletModeTest() {
power_button_controller_->OnDisplayTabletStateChanged(
display::TabletState::kInTabletMode);
// Advance mock clock to now. If we don't do this, PowerButtonController
// will wrongly assume that we have accidental button presses due to all
// timestamps zeroed.
tick_clock_.SetNowTicks(base::TimeTicks::Now());
Shell::Get()->session_controller()->SetSessionInfo(
SessionInfo{.can_lock_screen = true,
.should_lock_screen_automatically = true,
.state = session_manager::SessionState::LOCKED});
}
};
// Test the basic operation of the lock button.
TEST_P(LockStateControllerAnimationTest, LockButtonBasic) {
// If we're logged in as a regular user, we should start the lock timer and
// the pre-lock animation.
Initialize(ButtonType::NORMAL, LoginStatus::USER);
PressLockButton();
ExpectPreLockAnimationStarted("1");
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
ExpectPreLockAnimationRunning("2");
// If the button is released immediately, we shouldn't lock the screen.
ReleaseLockButton();
ExpectPreLockAnimationCancel("3");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectUnlockedState("4");
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
// Press the button again and let the lock timeout fire. We should request
// that the screen be locked.
PressLockButton();
ExpectPreLockAnimationStarted("4");
Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
GetSessionControllerClient()->FlushForTest();
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
// Pressing the lock button while we have a pending lock request shouldn't do
// anything.
ReleaseLockButton();
PressLockButton();
ExpectPreLockAnimationFinished("5");
ReleaseLockButton();
// Pressing the button also shouldn't do anything after the screen is locked.
ExpectPostLockAnimationStarted("6");
PressLockButton();
ReleaseLockButton();
ExpectPostLockAnimationStarted("7");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectPostLockAnimationFinished("8");
PressLockButton();
ReleaseLockButton();
ExpectPostLockAnimationFinished("9");
}
TEST_P(LockStateControllerAnimationTest,
PowerButtonCancelsUnlockBeforeLockUIDestroyedInTabletMode) {
PrepareSessionForUnlockAnimationInTabletModeTest();
Shell::Get()->session_controller()->RunUnlockAnimation(
base::BindLambdaForTesting([](bool aborted) { EXPECT_TRUE(aborted); }));
ExpectUnlockBeforeUIDestroyedAnimationStarted("0");
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
PressPowerButton();
ReleasePowerButton();
Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectUnlockBeforeUIDestroyedAnimationFinished("1");
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
}
TEST_P(LockStateControllerAnimationTest,
PowerButtonCancelsUnlockAfterLockUIDestroyedInTabletMode) {
PrepareSessionForUnlockAnimationInTabletModeTest();
Shell::Get()->session_controller()->RunUnlockAnimation(
base::BindLambdaForTesting([](bool aborted) {
EXPECT_TRUE(true);
Shell::Get()->session_controller()->SetSessionInfo(
SessionInfo{.can_lock_screen = true,
.should_lock_screen_automatically = true,
.state = session_manager::SessionState::ACTIVE});
}));
ExpectUnlockBeforeUIDestroyedAnimationStarted("0");
Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectUnlockBeforeUIDestroyedAnimationFinished("1");
ExpectUnlockAfterUIDestroyedAnimationStarted("2");
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS, 0.5f);
PressPowerButton();
ReleasePowerButton();
Advance(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
GetSessionControllerClient()->FlushForTest();
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
}
#if 0
// When the screen is locked without going through the usual power-button
// slow-close path (e.g. via the wrench menu), test that we still show the
// fast-close animation.
TEST_F(LockStateControllerTest, LockWithoutButton) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
lock_state_controller_->OnStartingLock();
ExpectPreLockAnimationStarted();
EXPECT_FALSE(lock_state_test_api_->is_lock_cancellable());
EXPECT_LT(0u, test_animator_->GetAnimationCount());
test_animator_->CompleteAllAnimations(true);
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
}
#endif
// When we hear that the process is exiting but we haven't had a chance to
// display an animation, we should just blank the screen.
TEST_F(LockStateControllerTest, ShutdownWithoutButton) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
lock_state_controller_->OnChromeTerminating();
EXPECT_TRUE(test_animator_->AreContainersAnimated(
SessionStateAnimator::kAllNonRootContainersMask,
SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY));
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
// Shutdown was not initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
}
// Test that we display the fast-close animation and shut down when we get an
// outside request to shut down (e.g. from the login or lock screen).
TEST_P(LockStateControllerAnimationTest, RequestShutdownFromLoginScreen) {
Initialize(ButtonType::NORMAL, LoginStatus::NOT_LOGGED_IN);
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
lock_state_controller_->RequestShutdown(
ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
// Shutdown was initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_FALSE(IsDefaultValueLoginShutdownTimestamp());
ExpectShutdownAnimationStarted("1");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_EQ(0, NumShutdownRequests());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
}
TEST_P(LockStateControllerAnimationTest, RequestShutdownFromLockScreen) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
LockScreen();
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
ExpectPostLockAnimationFinished("1");
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
lock_state_controller_->RequestShutdown(
ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
// Shutdown was initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_FALSE(IsDefaultValueLoginShutdownTimestamp());
ExpectShutdownAnimationStarted("2");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_EQ(0, NumShutdownRequests());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
}
// Test that histogram of time delta was recorded if a previous shutdown was
// initiated from login/lock screen.
TEST_F(LockStateControllerTest, RequestShutdownFromLoginScreenThenRestart) {
Initialize(ButtonType::NORMAL, LoginStatus::NOT_LOGGED_IN);
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
lock_state_controller_->RequestShutdown(
ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
// Shutdown was initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_FALSE(IsDefaultValueLoginShutdownTimestamp());
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_EQ(0, NumShutdownRequests());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
// Simulate restarting device
RestartDevice();
histograms().ExpectTotalCount(kShelfShutdownConfirmationHistogramName, 1);
}
TEST_F(LockStateControllerTest, RequestShutdownFromLockScreenThenRestart) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
LockScreen();
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
lock_state_controller_->RequestShutdown(
ShutdownReason::LOGIN_SHUT_DOWN_BUTTON);
// Shutdown was initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_FALSE(IsDefaultValueLoginShutdownTimestamp());
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_EQ(0, NumShutdownRequests());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
// Simulate restarting device
RestartDevice();
histograms().ExpectTotalCount(kShelfShutdownConfirmationHistogramName, 1);
}
// Test that histogram of time delta was not recorded if a previous shutdown
// was not initiated from login/lock screen.
TEST_F(LockStateControllerTest, LegacyShowMenuAndShutDownThenRestart) {
Initialize(ButtonType::LEGACY, LoginStatus::USER);
ExpectUnlockedState("1");
// We should request that the screen be locked immediately after seeing the
// power button get pressed.
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->IsMenuOpened());
// We shouldn't progress towards the shutdown state, however.
EXPECT_FALSE(lock_state_test_api_->shutdown_timer_is_running());
ReleasePowerButton();
// Hold the button again and check that we start shutting down.
PressPowerButton();
ExpectShutdownAnimationStarted("2");
EXPECT_EQ(0, NumShutdownRequests());
// Make sure a mouse move event won't show the cursor.
GenerateMouseMoveEvent();
EXPECT_FALSE(cursor_visible());
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
// Shutdown was not initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
// Simulate restarting device
RestartDevice();
histograms().ExpectTotalCount(kShelfShutdownConfirmationHistogramName, 0);
}
// Test that hidden wallpaper appears and reverts correctly on lock/cancel.
TEST_P(LockStateControllerAnimationTest, TestHiddenWallpaperLockCancel) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
HideWallpaper();
ExpectUnlockedState("1");
PressLockButton();
ExpectPreLockAnimationStarted("2");
ExpectWallpaperIsShowing("3");
// Forward only half way through.
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
// Release the button before the lock timer fires.
ReleaseLockButton();
ExpectPreLockAnimationCancel("4");
ExpectWallpaperIsHiding("5");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_UNDO_MOVE_WINDOWS);
// When the CancelPrelockAnimation sequence finishes it queues up a
// restore wallpaper visibility sequence when the wallpaper is hidden.
ExpectRestoringWallpaperVisibility("6");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
ExpectUnlockedState("6");
}
// Test that hidden wallpaper appears and revers correctly on lock/unlock.
TEST_P(LockStateControllerAnimationTest, TestHiddenWallpaperLockUnlock) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
HideWallpaper();
ExpectUnlockedState("1");
// Press the lock button and check that the lock timer is started and that we
// start lifting the non-screen-locker containers.
PressLockButton();
ExpectPreLockAnimationStarted("2");
ExpectWallpaperIsShowing("3");
Advance(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE);
ExpectPreLockAnimationFinished("4");
LockScreen();
ReleaseLockButton();
ExpectPostLockAnimationStarted("5");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectPostLockAnimationFinished("6");
ExpectLockedState("7");
SuccessfulAuthentication(nullptr);
ExpectUnlockBeforeUIDestroyedAnimationStarted("8");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectUnlockBeforeUIDestroyedAnimationFinished("9");
UnlockScreen();
ExpectUnlockAfterUIDestroyedAnimationStarted("10");
ExpectWallpaperIsHiding("11");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
// When the StartUnlockAnimationAfterUIDestroyed sequence finishes it queues
// up a restore wallpaper visibility sequence when the wallpaper is hidden.
ExpectRestoringWallpaperVisibility("12");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE);
ExpectUnlockAfterUIDestroyedAnimationFinished("13");
ExpectUnlockedState("14");
}
// Tests the default behavior of disabling the touchscreen when the screen is
// turned off due to user inactivity.
TEST_F(LockStateControllerTest, DisableTouchscreenForScreenOff) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
// Run the event loop so PowerButtonDisplayController will get the initial
// backlights-forced-off state from chromeos::PowerManagerClient.
base::RunLoop().RunUntilIdle();
// Manually turn the screen off and check that the touchscreen is enabled.
SendBrightnessChange(0, kUserCause);
EXPECT_TRUE(Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
TouchDeviceEnabledSource::GLOBAL));
// It should be disabled if the screen is turned off due to user inactivity.
SendBrightnessChange(100, kUserCause);
SendBrightnessChange(0, kOtherCause);
EXPECT_FALSE(Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
TouchDeviceEnabledSource::GLOBAL));
}
// Tests that the kTouchscreenUsableWhileScreenOff switch keeps the touchscreen
// enabled when the screen is turned off due to user inactivity.
TEST_F(LockStateControllerTest, TouchscreenUnableWhileScreenOff) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kTouchscreenUsableWhileScreenOff);
ResetPowerButtonController();
Initialize(ButtonType::NORMAL, LoginStatus::USER);
// Run the event loop so PowerButtonDisplayController will get the initial
// backlights-forced-off state from chromeos::PowerManagerClient.
base::RunLoop().RunUntilIdle();
// The touchscreen should remain enabled.
SendBrightnessChange(0, kOtherCause);
EXPECT_TRUE(Shell::Get()->touch_devices_controller()->GetTouchscreenEnabled(
TouchDeviceEnabledSource::GLOBAL));
}
// Tests that continue pressing the power button for a while after power menu is
// shown should trigger the cancellable pre-shutdown animation.
TEST_F(LockStateControllerTest, ShutDownAfterShowPowerMenu) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
PressPowerButton();
EXPECT_TRUE(power_button_test_api_->IsMenuOpened());
ASSERT_TRUE(power_button_test_api_->TriggerPreShutdownTimeout());
EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
ExpectShutdownAnimationStarted("1");
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN, 0.5f);
// Release the power button before the shutdown timer fires.
ReleasePowerButton();
EXPECT_FALSE(lock_state_test_api_->shutdown_timer_is_running());
ExpectShutdownAnimationCancel("2");
power_button_controller_->DismissMenu();
EXPECT_FALSE(power_button_test_api_->IsMenuOpened());
// Press the button again and make the shutdown timeout fire this time.
// Check that we start the timer for actually requesting the shutdown.
PressPowerButton();
ASSERT_TRUE(power_button_test_api_->TriggerPreShutdownTimeout());
EXPECT_TRUE(lock_state_test_api_->shutdown_timer_is_running());
Advance(SessionStateAnimator::ANIMATION_SPEED_SHUTDOWN);
ExpectShutdownAnimationFinished("3");
lock_state_test_api_->trigger_shutdown_timeout();
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
EXPECT_EQ(0, NumShutdownRequests());
// When the timeout fires, we should request a shutdown.
lock_state_test_api_->trigger_real_shutdown_timeout();
EXPECT_EQ(1, NumShutdownRequests());
// Shutdown was not initiated with reason LOGIN_SHUT_DOWN_BUTTON
EXPECT_TRUE(IsDefaultValueLoginShutdownTimestamp());
}
TEST_P(LockStateControllerAnimationTest, CancelShouldResetWallpaperBlur) {
Initialize(ButtonType::NORMAL, LoginStatus::USER);
ExpectUnlockedState("1");
auto* wallpaper_view = Shell::Get()
->GetPrimaryRootWindowController()
->wallpaper_widget_controller()
->wallpaper_view();
// Enter Overview and verify wallpaper properties.
EnterOverview();
EXPECT_EQ(wallpaper_constants::kClear, wallpaper_view->blur_sigma());
// Start lock animation and verify wallpaper properties.
PressLockButton();
ExpectPreLockAnimationStarted("2");
EXPECT_EQ(wallpaper_constants::kLockLoginBlur, wallpaper_view->blur_sigma());
// Cancel lock animation.
AdvancePartially(SessionStateAnimator::ANIMATION_SPEED_UNDOABLE, 0.5f);
ReleaseLockButton();
ExpectPreLockAnimationCancel("3");
AdvanceOrAbort(SessionStateAnimator::ANIMATION_SPEED_MOVE_WINDOWS);
ExpectUnlockedState("4");
// Verify wallpaper blur are restored to overview's.
EXPECT_EQ(wallpaper_constants::kClear, wallpaper_view->blur_sigma());
}
INSTANTIATE_TEST_SUITE_P(LockStateControllerAnimation,
LockStateControllerAnimationTest,
/*complete or abort=*/::testing::Bool());
class LockStateControllerMockTimeTest : public PowerButtonTestBase {
public:
LockStateControllerMockTimeTest()
: PowerButtonTestBase(
base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
LockStateControllerMockTimeTest(const LockStateControllerMockTimeTest&) =
delete;
LockStateControllerMockTimeTest& operator=(
const LockStateControllerMockTimeTest&) = delete;
~LockStateControllerMockTimeTest() override = default;
void Advance(const base::TimeDelta& delta) {
task_environment()->FastForwardBy(delta);
}
void SetUp() override {
PowerButtonTestBase::SetUp();
InitPowerButtonControllerMembers(
chromeos::PowerManagerClient::TabletMode::UNSUPPORTED);
}
};
class TestLayerCopyAnimator final : public LayerCopyAnimator {
public:
TestLayerCopyAnimator(aura::Window* window, base::OnceClosure callback)
: LayerCopyAnimator(window), callback_(std::move(callback)) {}
TestLayerCopyAnimator(const TestLayerCopyAnimator&) = delete;
TestLayerCopyAnimator& operator=(const TestLayerCopyAnimator&) = delete;
~TestLayerCopyAnimator() override = default;
// LayerCopyAnimator:
void OnLayerCopied(std::unique_ptr<ui::Layer> new_layer) override {
// Move the callback first because the object may be deleted.
auto callback = std::move(callback_);
LayerCopyAnimator::OnLayerCopied(std::move(new_layer));
std::move(callback).Run();
}
private:
base::OnceClosure callback_;
};
TEST_F(LockStateControllerMockTimeTest, LockWithoutAnimation) {
Initialize(ButtonType::LEGACY, LoginStatus::USER);
EXPECT_FALSE(Shell::Get()->session_controller()->IsScreenLocked());
auto* shelf_container = Shell::GetContainer(Shell::GetPrimaryRootWindow(),
kShellWindowId_ShelfContainer);
auto* lock_container =
Shell::GetContainer(Shell::GetPrimaryRootWindow(),
kShellWindowId_LockScreenContainersContainer);
base::RunLoop loop;
base::RepeatingClosure done_closure =
base::BarrierClosure(2, loop.QuitClosure());
auto callback = [&]() { done_closure.Run(); };
new TestLayerCopyAnimator(shelf_container,
base::BindLambdaForTesting(callback));
new TestLayerCopyAnimator(lock_container,
base::BindLambdaForTesting(callback));
lock_state_controller_->LockWithoutAnimation();
EXPECT_TRUE(lock_state_controller_->animating_lock_for_test());
loop.Run();
EXPECT_FALSE(lock_state_controller_->animating_lock_for_test());
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
}
class LockStateControllerInformedRestoreTest : public LockStateControllerTest {
public:
LockStateControllerInformedRestoreTest() = default;
LockStateControllerInformedRestoreTest(
const LockStateControllerInformedRestoreTest&) = delete;
LockStateControllerInformedRestoreTest& operator=(
const LockStateControllerInformedRestoreTest&) = delete;
~LockStateControllerInformedRestoreTest() override = default;
// LockStateControllerTest:
void SetUp() override {
LockStateControllerTest::SetUp();
CHECK(temp_dir_.CreateUniqueTempDir());
file_path_ = temp_dir_.GetPath().AppendASCII("test_informed_restore.png");
SetInformedRestoreImagePathForTest(file_path_);
Initialize(ButtonType::NORMAL, LoginStatus::USER);
}
void TearDown() override {
SetInformedRestoreImagePathForTest(base::FilePath());
LockStateControllerTest::TearDown();
}
void RequestShutdownWithoutFailTimer() {
base::RunLoop run_loop;
lock_state_test_api_->set_informed_restore_image_callback(
run_loop.QuitClosure());
lock_state_test_api_->disable_screenshot_timeout_for_test(true);
lock_state_controller_->RequestShutdown(
ShutdownReason::TRAY_SHUT_DOWN_BUTTON);
run_loop.Run();
}
// Checks that the informed restore image was taken and saved at `file_path`
// on disk successfully.
void VerifyInformedRestoreImageOnDisk() {
EXPECT_TRUE(base::PathExists(file_path()));
std::optional<int64_t> file_size = base::GetFileSize(file_path());
ASSERT_TRUE(file_size.has_value());
EXPECT_GT(file_size.value(), 0);
}
const base::FilePath& file_path() const { return file_path_; }
private:
base::ScopedAllowBlockingForTesting allow_blocking_;
base::ScopedTempDir temp_dir_;
base::FilePath file_path_;
base::test::ScopedFeatureList scoped_feature_list_{features::kForestFeature};
};
// Tests that a informed restore image is taken when there are windows open.
TEST_F(LockStateControllerInformedRestoreTest, ShutdownWithWindows) {
std::unique_ptr<aura::Window> window = CreateTestWindow();
base::HistogramTester histogram_tester;
RequestShutdownWithoutFailTimer();
// The informed restore image was taken and not empty.
VerifyInformedRestoreImageOnDisk();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(
base::Bucket(ScreenshotOnShutdownStatus::kSucceeded, 1)));
auto* local_state = Shell::Get()->local_state();
// Informed restore screenshot related durations were recorded.
const base::TimeDelta screenshot_taken_duration =
local_state->GetTimeDelta(prefs::kInformedRestoreScreenshotTakenDuration);
EXPECT_FALSE(screenshot_taken_duration.is_zero());
const base::TimeDelta screenshot_encode_and_save_duration =
local_state->GetTimeDelta(
prefs::kInformedRestoreScreenshotEncodeAndSaveDuration);
EXPECT_FALSE(screenshot_encode_and_save_duration.is_zero());
}
// Tests that no informed restore image is taken when there are no windows
// opened and the existing informed restore image should be deleted.
TEST_F(LockStateControllerInformedRestoreTest, ShutdownWithoutWindows) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedWithNoWindows, 1)));
// Existing informed restore image was deleted.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, ShutdownInOverview) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
// Create a window and enter the overview before requesting shutdown.
CreateTestWindow();
EnterOverview();
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedInOverview, 1)));
// The informed restore image should not be taken if it is in overview when
// shutting down. The existing informed restore image should be deleted as
// well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, ShutdownInGuest) {
SimulateUserLogin("foo@example.com", user_manager::UserType::kGuest);
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
CreateTestWindow();
ASSERT_TRUE(Shell::Get()->session_controller()->IsUserGuest());
// Request shutdown while in guest mode.
RequestShutdownWithoutFailTimer();
EXPECT_THAT(
histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedInGuestOrPublicUserSession, 1)));
// The informed restore image should not be taken if it is in the guest
// session. The existing informed restore image should be deleted as well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, ShutdownInLockScreen) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
// Create a window and go the lock screen before requesting shutdown.
CreateTestWindowInShellWithId(0);
GetSessionControllerClient()->LockScreen();
EXPECT_TRUE(Shell::Get()->session_controller()->IsScreenLocked());
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedInLockScreen, 1)));
// The informed restore image should not be taken if it is in the lock screen.
// The existing informed restore image should be deleted as well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, ShutdownInHomeLauncher) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
// Create a window and go to the home launcher page before requesting
// shutdown.
std::unique_ptr<aura::Window> window(CreateTestWindow());
TabletModeControllerTestApi().EnterTabletMode();
auto* app_list_controller = Shell::Get()->app_list_controller();
app_list_controller->GoHome(GetPrimaryDisplay().id());
ASSERT_TRUE(app_list_controller->IsHomeScreenVisible());
EXPECT_TRUE(WindowState::Get(window.get())->IsMinimized());
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedInHomeLauncher, 1)));
// The informed restore image should not be taken if it is in the home
// launcher page when shutting down. The existing image should be deleted as
// well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, PinnedState) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
// Create and pin a window before requesting shutdown.
std::unique_ptr<aura::Window> pinned_window = CreateAppWindow();
wm::ActivateWindow(pinned_window.get());
window_util::PinWindow(pinned_window.get(), /*trusted=*/false);
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedInPinnedMode, 1)));
// The informed restore image should not be taken when it is in pinned state.
// The existing image should be deleted as well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
TEST_F(LockStateControllerInformedRestoreTest, AllWindowsMinimized) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
std::unique_ptr<aura::Window> window1(CreateTestWindow());
std::unique_ptr<aura::Window> window2(CreateTestWindow());
WindowState::Get(window1.get())->Minimize();
WindowState::Get(window2.get())->Minimize();
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedWithNoWindows, 1)));
// The informed restore image should not be taken if all the windows inside
// the active desk are minimized. The existing image should be deleted as
// well.
EXPECT_FALSE(lock_state_test_api_->mirror_wallpaper_layer());
EXPECT_FALSE(base::PathExists(file_path()));
}
// Tests that the informed restore image should be taken with only the floated
// window.
TEST_F(LockStateControllerInformedRestoreTest, ShutdownWithFloatWindow) {
base::HistogramTester histogram_tester;
std::unique_ptr<aura::Window> floated_window = CreateAppWindow();
PressAndReleaseKey(ui::VKEY_F, ui::EF_ALT_DOWN | ui::EF_COMMAND_DOWN);
ASSERT_TRUE(WindowState::Get(floated_window.get())->IsFloated());
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(
base::Bucket(ScreenshotOnShutdownStatus::kSucceeded, 1)));
// The informed restore image was taken and not empty with the float window
// only.
VerifyInformedRestoreImageOnDisk();
}
// Tests that the informed restore image should be taken with only the always on
// top window.
TEST_F(LockStateControllerInformedRestoreTest, ShutdownWithAlwaysOnTopWindow) {
base::HistogramTester histogram_tester;
aura::Window* top_container = Shell::GetContainer(
Shell::GetPrimaryRootWindow(), kShellWindowId_AlwaysOnTopContainer);
std::unique_ptr<aura::Window> window_always_on_top(
aura::test::CreateTestWindowWithId(1, top_container));
RequestShutdownWithoutFailTimer();
EXPECT_THAT(histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(
base::Bucket(ScreenshotOnShutdownStatus::kSucceeded, 1)));
// The informed restore image was taken and not empty with the always on top
// window only.
VerifyInformedRestoreImageOnDisk();
}
TEST_F(LockStateControllerInformedRestoreTest, TakeScreenshotTimeout) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
base::HistogramTester histogram_tester;
std::unique_ptr<aura::Window> window(CreateTestWindow());
base::RunLoop run_loop;
lock_state_test_api_->set_informed_restore_image_callback(
run_loop.QuitClosure());
lock_state_controller_->RequestShutdown(
ShutdownReason::TRAY_SHUT_DOWN_BUTTON);
// Fire the screenshot timeout before taking the screenshot completes. Then no
// screenshot should be saved, the existing one should be deleted as well and
// the shutdown process should be triggered directly.
lock_state_test_api_->trigger_take_screenshot_timeout();
run_loop.Run();
EXPECT_FALSE(base::PathExists(file_path()));
EXPECT_TRUE(lock_state_test_api_->real_shutdown_timer_is_running());
EXPECT_THAT(
histogram_tester.GetAllSamples(kScreenshotOnShutdownStatus),
testing::ElementsAre(base::Bucket(
ScreenshotOnShutdownStatus::kFailedOnTakingScreenshotTimeout, 1)));
}
TEST_F(LockStateControllerInformedRestoreTest, CancelShutdown) {
// Create an empty file to simulate an old informed restore image.
ASSERT_TRUE(base::WriteFile(file_path(), ""));
std::unique_ptr<aura::Window> window(CreateTestWindow());
base::RunLoop run_loop;
lock_state_test_api_->set_informed_restore_image_callback(
run_loop.QuitClosure());
lock_state_controller_->RequestCancelableShutdown(
ShutdownReason::TRAY_SHUT_DOWN_BUTTON);
// The shutdown should be cancelable and the existing informed restore image
// should be deleted as the shutdown was canceled.
EXPECT_TRUE(lock_state_controller_->MaybeCancelShutdownAnimation());
run_loop.Run();
EXPECT_FALSE(base::PathExists(file_path()));
}
} // namespace ash