File: power_event_observer_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (744 lines) | stat: -rw-r--r-- 29,639 bytes parent folder | download | duplicates (6)
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
// 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/system/power/power_event_observer.h"

#include <memory>

#include "ash/display/projecting_observer.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/power/power_event_observer_test_api.h"
#include "ash/test/ash_test_base.h"
#include "ash/wallpaper/views/wallpaper_widget_controller.h"
#include "ash/wm/lock_state_controller.h"
#include "ash/wm/lock_state_controller_test_api.h"
#include "ash/wm/test/test_session_state_animator.h"
#include "base/memory/raw_ptr.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/time/time.h"
#include "chromeos/ash/components/feature_usage/feature_usage_metrics.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/manager/test/fake_display_snapshot.h"

namespace ash {

class PowerEventObserverTest : public AshTestBase {
 public:
  PowerEventObserverTest() = default;

  PowerEventObserverTest(const PowerEventObserverTest&) = delete;
  PowerEventObserverTest& operator=(const PowerEventObserverTest&) = delete;

  ~PowerEventObserverTest() override = default;

  // AshTestBase:
  void SetUp() override {
    AshTestBase::SetUp();
    observer_ = Shell::Get()->power_event_observer();
  }

  void TearDown() override { AshTestBase::TearDown(); }

 protected:
  int GetNumVisibleCompositors() {
    int result = 0;
    for (aura::Window* window : Shell::GetAllRootWindows()) {
      if (window->GetHost()->compositor()->IsVisible())
        ++result;
    }

    return result;
  }

  bool GetLockedState() {
    // LockScreen is an async mojo call.
    GetSessionControllerClient()->FlushForTest();
    return Shell::Get()->session_controller()->IsScreenLocked();
  }

  raw_ptr<PowerEventObserver, DanglingUntriaged> observer_ = nullptr;
};

TEST_F(PowerEventObserverTest, LockBeforeSuspend) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  // Check that the observer requests a suspend-readiness callback when it hears
  // that the system is about to suspend.
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  // It should run the callback when it hears that the screen is locked and the
  // lock screen animations have completed.
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);

  PowerEventObserverTestApi test_api(observer_);

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();

  test_api.CompositingDidCommit(compositor);
  observer_->OnLockAnimationsComplete();

  // Verify that CompositingStarted and CompositingAckDeprecated observed before
  // CompositingDidCommit are ignored.
  test_api.CompositingStarted(compositor);
  test_api.CompositingAckDeprecated(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  // Suspend should remain delayed after first compositing cycle ends.
  test_api.CompositeFrame(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositingDidCommit(compositor);
  test_api.CompositingStarted(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositingAckDeprecated(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());

  // If the system is already locked, no callback should be requested.
  observer_->SuspendDoneEx(power_manager::SuspendDone());
  EXPECT_EQ(1, GetNumVisibleCompositors());
  UnblockUserSession();
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);

  // Notify that lock animation is complete.
  observer_->OnLockAnimationsComplete();

  // Wait for a compositing after lock animation completes before suspending.
  // In this case compositors should be made invisible immediately
  test_api.CompositeFrame(compositor);
  test_api.CompositeFrame(compositor);

  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());

  // It also shouldn't request a callback if it isn't instructed to lock the
  // screen.
  observer_->SuspendDoneEx(power_manager::SuspendDone());
  UnblockUserSession();
  SetShouldLockScreenAutomatically(false);
  EXPECT_EQ(1, GetNumVisibleCompositors());
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest, SetInvisibleBeforeSuspend) {
  // Tests that all the Compositors are marked invisible before a suspend
  // request when the screen is not supposed to be locked before a suspend.
  EXPECT_EQ(1, GetNumVisibleCompositors());

  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(0, GetNumVisibleCompositors());
  observer_->SuspendDoneEx(power_manager::SuspendDone());

  // Tests that all the Compositors are marked invisible _after_ the screen lock
  // animations have completed.
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, GetNumVisibleCompositors());

  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  EXPECT_EQ(1, GetNumVisibleCompositors());

  observer_->OnLockAnimationsComplete();

  EXPECT_EQ(1, GetNumVisibleCompositors());
  ASSERT_TRUE(PowerEventObserverTestApi(observer_)
                  .SimulateCompositorsReadyForSuspend());
  EXPECT_EQ(0, GetNumVisibleCompositors());

  observer_->SuspendDoneEx(power_manager::SuspendDone());
  EXPECT_EQ(1, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest, CanceledSuspend) {
  // Tests that the Compositors are not marked invisible if a suspend is
  // canceled or the system resumes before the lock screen is ready.
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, GetNumVisibleCompositors());

  observer_->SuspendDoneEx(power_manager::SuspendDone());
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  observer_->OnLockAnimationsComplete();
  EXPECT_EQ(1, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest, DelayResuspendForLockAnimations) {
  // Tests that the following order of events is handled correctly:
  //
  // - A suspend request is started.
  // - The screen is locked.
  // - The suspend request is canceled.
  // - Another suspend request is started.
  // - The screen lock animations complete.
  // - The screen lock UI changes get composited
  //
  // In this case, the observer should block the second suspend request until
  // the screen lock compositing is done.
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  observer_->SuspendDoneEx(power_manager::SuspendDone());
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);

  // The expected number of suspend readiness callbacks is 2 because the
  // observer has not run the callback that it got from the first suspend
  // request.  The real PowerManagerClient would reset its internal counter in
  // this situation but the stub client is not that smart.
  EXPECT_EQ(2, client->num_pending_suspend_readiness_callbacks());

  observer_->OnLockAnimationsComplete();
  EXPECT_EQ(2, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  ASSERT_TRUE(PowerEventObserverTestApi(observer_)
                  .SimulateCompositorsReadyForSuspend());
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that device suspend is delayed for screen lock until the screen lock
// changes are composited for all root windows.
TEST_F(PowerEventObserverTest, DelaySuspendForCompositing_MultiDisplay) {
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  UpdateDisplay("200x100,300x200");

  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  aura::Window::Windows windows = Shell::GetAllRootWindows();
  ASSERT_EQ(2u, windows.size());

  ui::Compositor* primary_compositor = windows[0]->GetHost()->compositor();
  ui::Compositor* secondary_compositor = windows[1]->GetHost()->compositor();
  ASSERT_EQ(2, GetNumVisibleCompositors());
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  PowerEventObserverTestApi test_api(observer_);

  // Simulate a commit before lock animations complete, and verify associated
  // compositing ends are ignored.
  test_api.CompositingDidCommit(secondary_compositor);
  observer_->OnLockAnimationsComplete();

  test_api.CompositingStarted(secondary_compositor);
  test_api.CompositingAckDeprecated(secondary_compositor);

  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(2, GetNumVisibleCompositors());

  test_api.CompositeFrame(primary_compositor);
  test_api.CompositeFrame(primary_compositor);

  test_api.CompositeFrame(secondary_compositor);

  // Even though compositing for one display is done, changes to compositor
  // visibility, and suspend readiness state should be delayed until compositing
  // for the other display finishes.
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(2, GetNumVisibleCompositors());

  test_api.CompositeFrame(secondary_compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest,
       DISABLED_DelaySuspendForCompositing_PendingDisplayRemoved) {
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  UpdateDisplay("200x100,300x200");

  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  aura::Window::Windows windows = Shell::GetAllRootWindows();
  ASSERT_EQ(2u, windows.size());

  ui::Compositor* primary_compositor = windows[0]->GetHost()->compositor();
  ASSERT_EQ(2, GetNumVisibleCompositors());
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  observer_->OnLockAnimationsComplete();

  PowerEventObserverTestApi test_api(observer_);

  test_api.CompositeFrame(primary_compositor);
  test_api.CompositeFrame(primary_compositor);

  // Even though compositing for one display is done, changes to compositor
  // visibility, and suspend readiness state should be delayed until compositing
  // for the other display finishes.
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(2, GetNumVisibleCompositors());

  // Remove the second display, and verify the remaining compositor is hidden
  // at this point.
  UpdateDisplay("200x100");
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest, CompositorNotVisibleAtLockAnimationsComplete) {
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());

  Shell::GetPrimaryRootWindow()->GetHost()->compositor()->SetVisible(false);

  observer_->OnLockAnimationsComplete();
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  base::RunLoop().RunUntilIdle();

  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that for suspend imminent induced locking screen, locking animations
// are immediate.
TEST_F(PowerEventObserverTest, ImmediateLockAnimations) {
  TestSessionStateAnimator* test_animator = new TestSessionStateAnimator;
  LockStateController* lock_state_controller =
      Shell::Get()->lock_state_controller();
  lock_state_controller->set_animator_for_test(test_animator);
  LockStateControllerTestApi lock_state_test_api(lock_state_controller);
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);
  ASSERT_FALSE(GetLockedState());

  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  // Tests that locking animation starts.
  EXPECT_TRUE(lock_state_test_api.is_animating_lock());

  // Tests that we have two active animation containers for pre-lock animation,
  // which are non lock screen containers and shelf container.
  EXPECT_EQ(2u, test_animator->GetAnimationCount());
  test_animator->AreContainersAnimated(
      LockStateController::kPreLockContainersMask,
      SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY);
  // Tests that after finishing immediate animation, we have no active
  // animations left.
  test_animator->Advance(test_animator->GetDuration(
      SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE));
  EXPECT_EQ(0u, test_animator->GetAnimationCount());

  // Flushes locking screen async request to start post-lock animation.
  EXPECT_TRUE(GetLockedState());
  EXPECT_TRUE(lock_state_test_api.is_animating_lock());
  // Tests that we have two active animation container for post-lock animation,
  // which are lock screen containers and shelf container.
  EXPECT_EQ(2u, test_animator->GetAnimationCount());
  test_animator->AreContainersAnimated(
      SessionStateAnimator::LOCK_SCREEN_CONTAINERS,
      SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN);
  test_animator->AreContainersAnimated(SessionStateAnimator::SHELF,
                                       SessionStateAnimator::ANIMATION_FADE_IN);
  // Tests that after finishing immediate animation, we have no active
  // animations left. Also checks that animation ends.
  test_animator->Advance(test_animator->GetDuration(
      SessionStateAnimator::ANIMATION_SPEED_IMMEDIATE));
  EXPECT_EQ(0u, test_animator->GetAnimationCount());
  EXPECT_FALSE(lock_state_test_api.is_animating_lock());
}

// Tests that displays will not be considered ready to suspend until the
// animated wallpaper change finishes (if the wallpaper is being animated to
// another wallpaper after the screen is locked).
// Flaky: https://crbug.com/1293178
TEST_F(PowerEventObserverTest,
       DISABLED_DisplaysNotReadyForSuspendUntilWallpaperAnimationEnds) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  // Set up animation state so wallpaper widget animations are not ended on
  // their creation.
  ui::ScopedAnimationDurationScaleMode test_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  // Lock screen - this is expected to start wallpaper change (e.g. to a
  // widget with a blurred wallpaper).
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  observer_->OnLockAnimationsComplete();

  WallpaperWidgetController* wallpaper_widget_controller =
      Shell::GetPrimaryRootWindowController()->wallpaper_widget_controller();
  // Assert that the wallpaper is being animated here - otherwise the test will
  // not work.
  ASSERT_TRUE(wallpaper_widget_controller->IsAnimating());

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  PowerEventObserverTestApi test_api(observer_);

  // Simulate a single frame getting composited before the wallpaper animation
  // is done - this frame is expected to be ignored by power event observer's
  // compositing state observer.
  test_api.CompositeFrame(compositor);

  // Simulate wallpaper animation finishing - for the purpose of this test,
  // before suspend begins.
  wallpaper_widget_controller->StopAnimating();

  // Expect that two compositing cycles are completed before suspend continues,
  // and displays get suspended.
  test_api.CompositeFrame(compositor);
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositeFrame(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that animated wallpaper changes will be finished immediately when
// suspend starts (if the screen was locked when suspend started).
TEST_F(PowerEventObserverTest, EndWallpaperAnimationOnSuspendWhileLocked) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  // Set up animation state so wallpaper widget animations are not ended on
  // their creation.
  ui::ScopedAnimationDurationScaleMode test_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  // Lock screen - this is expected to start wallpaper change (e.g. to a
  // widget with a blurred wallpaper).
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  observer_->OnLockAnimationsComplete();

  // Wallpaper animation should be stopped immediately on suspend.
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);

  WallpaperWidgetController* wallpaper_widget_controller =
      Shell::GetPrimaryRootWindowController()->wallpaper_widget_controller();
  EXPECT_FALSE(wallpaper_widget_controller->IsAnimating());

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  PowerEventObserverTestApi test_api(observer_);

  // Expect that two compositing cycles are completed before suspend continues,
  // and displays get suspended.
  test_api.CompositeFrame(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositeFrame(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that animated wallpaper changes will be finished immediately when
// suspend starts (if the screen lock started before suspend).
TEST_F(PowerEventObserverTest, EndWallpaperAnimationOnSuspendWhileLocking) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  // Set up animation state so wallpaper widget animations are not ended on
  // their creation.
  ui::ScopedAnimationDurationScaleMode test_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  // Lock screen - this is expected to start wallpaper change (e.g. to a
  // widget with a blurred wallpaper).
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);

  // If suspend starts, wallpaper animation should be stopped after screen lock
  // completes.
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  observer_->OnLockAnimationsComplete();

  WallpaperWidgetController* wallpaper_widget_controller =
      Shell::GetPrimaryRootWindowController()->wallpaper_widget_controller();
  EXPECT_FALSE(wallpaper_widget_controller->IsAnimating());

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  PowerEventObserverTestApi test_api(observer_);

  // Expect that two compositing cycles are completed before suspend continues,
  // and displays get suspended.
  test_api.CompositeFrame(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositeFrame(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that animated wallpaper changes will be finished immediately when
// suspend starts and causes a screen lock.
TEST_F(PowerEventObserverTest, EndWallpaperAnimationAfterLockDueToSuspend) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  // Set up animation state so wallpaper widget animations are not ended on
  // their creation.
  ui::ScopedAnimationDurationScaleMode test_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  // Start suspend (which should start screen lock) - verify that wallpaper is
  // not animating after the screen lock animations are reported as complete.
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  observer_->OnLockAnimationsComplete();

  WallpaperWidgetController* wallpaper_widget_controller =
      Shell::GetPrimaryRootWindowController()->wallpaper_widget_controller();
  EXPECT_FALSE(wallpaper_widget_controller->IsAnimating());

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  PowerEventObserverTestApi test_api(observer_);

  // Expect that two compositing cycles are completed before suspend continues,
  // and displays get suspended.
  test_api.CompositeFrame(compositor);
  EXPECT_EQ(1, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(1, GetNumVisibleCompositors());

  test_api.CompositeFrame(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

// Tests that removing a display while power event observer is waiting for the
// wallpaper animation does not cause suspend to hang.
TEST_F(PowerEventObserverTest, DisplayRemovedDuringWallpaperAnimation) {
  chromeos::FakePowerManagerClient* client =
      chromeos::FakePowerManagerClient::Get();
  ASSERT_EQ(0, client->num_pending_suspend_readiness_callbacks());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  UpdateDisplay("200x100,300x200");

  // Set up animation state so wallpaper widget animations are not ended on
  // their creation.
  ui::ScopedAnimationDurationScaleMode test_duration_mode(
      ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);

  // Lock screen - this is expected to start wallpaper change (e.g. to a
  // widget with a blurred wallpaper).
  BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
  observer_->OnLockAnimationsComplete();

  // Remove a display before wallpaper animation ends.
  UpdateDisplay("200x100");
  base::RunLoop().RunUntilIdle();

  // Start suspend and verify the suspend proceeds when the primary window's
  // compositors go through two compositing cycles.
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);

  ui::Compositor* compositor =
      Shell::GetPrimaryRootWindow()->GetHost()->compositor();
  PowerEventObserverTestApi test_api(observer_);

  // Expect that two compositing cycles are completed before suspend continues,
  // and displays get suspended.
  test_api.CompositeFrame(compositor);
  test_api.CompositeFrame(compositor);
  EXPECT_EQ(0, client->num_pending_suspend_readiness_callbacks());
  EXPECT_EQ(0, GetNumVisibleCompositors());
}

TEST_F(PowerEventObserverTest, LockOnLidClose) {
  // Screen should not lock if values not set.
  SetCanLockScreen(false);
  SetShouldLockScreenAutomatically(false);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());

  SetCanLockScreen(false);
  SetShouldLockScreenAutomatically(true);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(false);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());

  // Screen should only lock on CLOSED event.
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::OPEN,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_TRUE(GetLockedState());
}

TEST_F(PowerEventObserverTest, LockOnLidCloseWhenDocked) {
  std::unique_ptr<display::DisplaySnapshot> internal_display =
      display::FakeDisplaySnapshot::Builder()
          .SetId(123)
          .SetNativeMode(gfx::Size(1024, 768))
          .SetType(display::DISPLAY_CONNECTION_TYPE_INTERNAL)
          .Build();

  std::unique_ptr<display::DisplaySnapshot> external_display =
      display::FakeDisplaySnapshot::Builder()
          .SetId(456)
          .SetNativeMode(gfx::Size(1024, 768))
          .SetType(display::DISPLAY_CONNECTION_TYPE_VGA)
          .Build();

  auto set_docked = [&](bool docked) {
    std::vector<raw_ptr<display::DisplaySnapshot, VectorExperimental>> displays(
        {internal_display.get()});
    if (docked) {
      displays.push_back(external_display.get());
    }
    Shell::Get()->projecting_observer()->OnDisplayConfigurationChanged(
        displays);
  };

  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  // Closing lid should not lock when projecting.
  set_docked(true);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());

  // Opening lid, then disconnect display should not lock.
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::OPEN,
                              base::TimeTicks::Now());
  set_docked(false);
  EXPECT_FALSE(GetLockedState());

  // Closing lid while projecting, then removing display should lock.
  set_docked(true);
  observer_->LidEventReceived(chromeos::PowerManagerClient::LidState::CLOSED,
                              base::TimeTicks::Now());
  EXPECT_FALSE(GetLockedState());
  set_docked(false);
  EXPECT_TRUE(GetLockedState());
}

class LockOnSuspendUsageTest : public PowerEventObserverTest {
 public:
  LockOnSuspendUsageTest() { set_start_session(false); }
};

TEST_F(LockOnSuspendUsageTest, LockOnSuspendUsage) {
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(true);

  SimulateNewUserFirstLogin("user@gmail.com");
  PowerEventObserverTestApi test_api(observer_);
  ASSERT_TRUE(test_api.TrackingLockOnSuspendUsage());

  base::HistogramTester histogram_tester;
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  EXPECT_THAT(
      histogram_tester.GetAllSamples("ChromeOS.FeatureUsage.LockOnSuspend"),
      ::testing::ElementsAre(
          base::Bucket(
              static_cast<int>(
                  feature_usage::FeatureUsageMetrics::Event::kEligible),
              1),
          base::Bucket(static_cast<int>(
                           feature_usage::FeatureUsageMetrics::Event::kEnabled),
                       1),
          base::Bucket(
              static_cast<int>(
                  feature_usage::FeatureUsageMetrics::Event::kUsedWithSuccess),
              1)));
}

// TODO(crbug.com/40898491): Test is failing on "Linux ChromiumOS MSan Tests".
#if defined(MEMORY_SANITIZER)
#define MAYBE_No_ShouldLockScreenAutomatically \
  DISABLED_No_ShouldLockScreenAutomatically
#else
#define MAYBE_No_ShouldLockScreenAutomatically No_ShouldLockScreenAutomatically
#endif
TEST_F(LockOnSuspendUsageTest, MAYBE_No_ShouldLockScreenAutomatically) {
  SetCanLockScreen(true);
  SetShouldLockScreenAutomatically(false);

  SimulateNewUserFirstLogin("user@gmail.com");
  PowerEventObserverTestApi test_api(observer_);
  ASSERT_TRUE(test_api.TrackingLockOnSuspendUsage());

  base::HistogramTester histogram_tester;
  observer_->SuspendImminent(power_manager::SuspendImminent_Reason_OTHER);
  histogram_tester.ExpectTotalCount("ChromeOS.FeatureUsage.LockOnSuspend", 0);
}

TEST_F(LockOnSuspendUsageTest, No_CanLockScreen) {
  SetCanLockScreen(false);
  SimulateNewUserFirstLogin("user@gmail.com");
  PowerEventObserverTestApi test_api(observer_);
  ASSERT_FALSE(test_api.TrackingLockOnSuspendUsage());
}

}  // namespace ash