1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/power/power_event_observer.h"
8 #include "ash/test/ash_test_base.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/time/time.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/power_manager_client.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_tree_host.h"
15 #include "ui/compositor/compositor.h"
19 class PowerEventObserverTest
: public test::AshTestBase
{
21 PowerEventObserverTest() {}
22 ~PowerEventObserverTest() override
{}
24 // test::AshTestBase::SetUp() overrides:
25 void SetUp() override
{
26 test::AshTestBase::SetUp();
27 observer_
.reset(new PowerEventObserver());
30 void TearDown() override
{
32 test::AshTestBase::TearDown();
36 int GetNumVisibleCompositors() {
38 for (const auto& window
: Shell::GetAllRootWindows()) {
39 if (window
->GetHost()->compositor()->IsVisible())
46 scoped_ptr
<PowerEventObserver
> observer_
;
49 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest
);
52 TEST_F(PowerEventObserverTest
, LockBeforeSuspend
) {
53 chromeos::PowerManagerClient
* client
=
54 chromeos::DBusThreadManager::Get()->GetPowerManagerClient();
55 ASSERT_EQ(0, client
->GetNumPendingSuspendReadinessCallbacks());
57 // Check that the observer requests a suspend-readiness callback when it hears
58 // that the system is about to suspend.
59 SetCanLockScreen(true);
60 SetShouldLockScreenBeforeSuspending(true);
61 observer_
->SuspendImminent();
62 EXPECT_EQ(1, client
->GetNumPendingSuspendReadinessCallbacks());
64 // It should run the callback when it hears that the screen is locked and the
65 // lock screen animations have completed.
66 observer_
->ScreenIsLocked();
67 observer_
->OnLockAnimationsComplete();
68 EXPECT_EQ(0, client
->GetNumPendingSuspendReadinessCallbacks());
70 // If the system is already locked, no callback should be requested.
71 observer_
->SuspendDone(base::TimeDelta());
72 observer_
->ScreenIsUnlocked();
73 observer_
->ScreenIsLocked();
74 observer_
->OnLockAnimationsComplete();
75 observer_
->SuspendImminent();
76 EXPECT_EQ(0, client
->GetNumPendingSuspendReadinessCallbacks());
78 // It also shouldn't request a callback if it isn't instructed to lock the
80 observer_
->SuspendDone(base::TimeDelta());
81 SetShouldLockScreenBeforeSuspending(false);
82 observer_
->SuspendImminent();
83 EXPECT_EQ(0, client
->GetNumPendingSuspendReadinessCallbacks());
86 TEST_F(PowerEventObserverTest
, SetInvisibleBeforeSuspend
) {
87 // Tests that all the Compositors are marked invisible before a suspend
88 // request when the screen is not supposed to be locked before a suspend.
89 EXPECT_EQ(1, GetNumVisibleCompositors());
91 observer_
->SuspendImminent();
92 EXPECT_EQ(0, GetNumVisibleCompositors());
93 observer_
->SuspendDone(base::TimeDelta());
95 // Tests that all the Compositors are marked invisible _after_ the screen lock
96 // animations have completed.
97 SetCanLockScreen(true);
98 SetShouldLockScreenBeforeSuspending(true);
100 observer_
->SuspendImminent();
101 EXPECT_EQ(1, GetNumVisibleCompositors());
103 observer_
->ScreenIsLocked();
104 EXPECT_EQ(1, GetNumVisibleCompositors());
106 observer_
->OnLockAnimationsComplete();
107 EXPECT_EQ(0, GetNumVisibleCompositors());
109 observer_
->SuspendDone(base::TimeDelta());
110 EXPECT_EQ(1, GetNumVisibleCompositors());
113 TEST_F(PowerEventObserverTest
, CanceledSuspend
) {
114 // Tests that the Compositors are not marked invisible if a suspend is
115 // canceled or the system resumes before the lock screen is ready.
116 SetCanLockScreen(true);
117 SetShouldLockScreenBeforeSuspending(true);
118 observer_
->SuspendImminent();
119 EXPECT_EQ(1, GetNumVisibleCompositors());
121 observer_
->SuspendDone(base::TimeDelta());
122 observer_
->ScreenIsLocked();
123 observer_
->OnLockAnimationsComplete();
124 EXPECT_EQ(1, GetNumVisibleCompositors());
127 TEST_F(PowerEventObserverTest
, DelayResuspendForLockAnimations
) {
128 // Tests that the following order of events is handled correctly:
130 // - A suspend request is started.
131 // - The screen is locked.
132 // - The suspend request is canceled.
133 // - Another suspend request is started.
134 // - The screen lock animations complete.
136 // In this case, the observer should block the second suspend request until
137 // the animations have completed.
138 SetCanLockScreen(true);
139 SetShouldLockScreenBeforeSuspending(true);
141 chromeos::PowerManagerClient
* client
=
142 chromeos::DBusThreadManager::Get()->GetPowerManagerClient();
143 observer_
->SuspendImminent();
144 EXPECT_EQ(1, client
->GetNumPendingSuspendReadinessCallbacks());
146 observer_
->ScreenIsLocked();
147 observer_
->SuspendDone(base::TimeDelta());
148 observer_
->SuspendImminent();
150 // The expected number of suspend readiness callbacks is 2 because the
151 // observer has not run the callback that it got from the first suspend
152 // request. The real PowerManagerClient would reset its internal counter in
153 // this situation but the stub client is not that smart.
154 EXPECT_EQ(2, client
->GetNumPendingSuspendReadinessCallbacks());
156 observer_
->OnLockAnimationsComplete();
157 EXPECT_EQ(1, client
->GetNumPendingSuspendReadinessCallbacks());
158 EXPECT_EQ(0, GetNumVisibleCompositors());