OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/system/chromeos/power/power_event_observer.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "ash/common/test/test_session_state_delegate.h" | |
10 #include "ash/shell.h" | |
11 #include "ash/test/ash_test_base.h" | |
12 #include "base/time/time.h" | |
13 #include "chromeos/dbus/dbus_thread_manager.h" | |
14 #include "chromeos/dbus/power_manager_client.h" | |
15 #include "ui/aura/window.h" | |
16 #include "ui/aura/window_tree_host.h" | |
17 #include "ui/compositor/compositor.h" | |
18 | |
19 namespace ash { | |
20 | |
21 class PowerEventObserverTest : public test::AshTestBase { | |
22 public: | |
23 PowerEventObserverTest() {} | |
24 ~PowerEventObserverTest() override {} | |
25 | |
26 // test::AshTestBase::SetUp() overrides: | |
27 void SetUp() override { | |
28 test::AshTestBase::SetUp(); | |
29 observer_.reset(new PowerEventObserver()); | |
30 } | |
31 | |
32 void TearDown() override { | |
33 observer_.reset(); | |
34 test::AshTestBase::TearDown(); | |
35 } | |
36 | |
37 protected: | |
38 int GetNumVisibleCompositors() { | |
39 int result = 0; | |
40 for (auto* window : Shell::GetAllRootWindows()) { | |
41 if (window->GetHost()->compositor()->IsVisible()) | |
42 ++result; | |
43 } | |
44 | |
45 return result; | |
46 } | |
47 | |
48 std::unique_ptr<PowerEventObserver> observer_; | |
49 | |
50 private: | |
51 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest); | |
52 }; | |
53 | |
54 TEST_F(PowerEventObserverTest, LockBeforeSuspend) { | |
55 chromeos::PowerManagerClient* client = | |
56 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
57 ASSERT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | |
58 | |
59 // Check that the observer requests a suspend-readiness callback when it hears | |
60 // that the system is about to suspend. | |
61 test::TestSessionStateDelegate::SetCanLockScreen(true); | |
62 SetShouldLockScreenAutomatically(true); | |
63 observer_->SuspendImminent(); | |
64 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | |
65 | |
66 // It should run the callback when it hears that the screen is locked and the | |
67 // lock screen animations have completed. | |
68 observer_->ScreenIsLocked(); | |
69 observer_->OnLockAnimationsComplete(); | |
70 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | |
71 | |
72 // If the system is already locked, no callback should be requested. | |
73 observer_->SuspendDone(base::TimeDelta()); | |
74 observer_->ScreenIsUnlocked(); | |
75 observer_->ScreenIsLocked(); | |
76 observer_->OnLockAnimationsComplete(); | |
77 observer_->SuspendImminent(); | |
78 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | |
79 | |
80 // It also shouldn't request a callback if it isn't instructed to lock the | |
81 // screen. | |
82 observer_->SuspendDone(base::TimeDelta()); | |
83 SetShouldLockScreenAutomatically(false); | |
84 observer_->SuspendImminent(); | |
85 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | |
86 } | |
87 | |
88 TEST_F(PowerEventObserverTest, SetInvisibleBeforeSuspend) { | |
89 // Tests that all the Compositors are marked invisible before a suspend | |
90 // request when the screen is not supposed to be locked before a suspend. | |
91 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
92 | |
93 observer_->SuspendImminent(); | |
94 EXPECT_EQ(0, GetNumVisibleCompositors()); | |
95 observer_->SuspendDone(base::TimeDelta()); | |
96 | |
97 // Tests that all the Compositors are marked invisible _after_ the screen lock | |
98 // animations have completed. | |
99 test::TestSessionStateDelegate::SetCanLockScreen(true); | |
100 SetShouldLockScreenAutomatically(true); | |
101 | |
102 observer_->SuspendImminent(); | |
103 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
104 | |
105 observer_->ScreenIsLocked(); | |
106 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
107 | |
108 observer_->OnLockAnimationsComplete(); | |
109 EXPECT_EQ(0, GetNumVisibleCompositors()); | |
110 | |
111 observer_->SuspendDone(base::TimeDelta()); | |
112 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
113 } | |
114 | |
115 TEST_F(PowerEventObserverTest, CanceledSuspend) { | |
116 // Tests that the Compositors are not marked invisible if a suspend is | |
117 // canceled or the system resumes before the lock screen is ready. | |
118 test::TestSessionStateDelegate::SetCanLockScreen(true); | |
119 SetShouldLockScreenAutomatically(true); | |
120 observer_->SuspendImminent(); | |
121 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
122 | |
123 observer_->SuspendDone(base::TimeDelta()); | |
124 observer_->ScreenIsLocked(); | |
125 observer_->OnLockAnimationsComplete(); | |
126 EXPECT_EQ(1, GetNumVisibleCompositors()); | |
127 } | |
128 | |
129 TEST_F(PowerEventObserverTest, DelayResuspendForLockAnimations) { | |
130 // Tests that the following order of events is handled correctly: | |
131 // | |
132 // - A suspend request is started. | |
133 // - The screen is locked. | |
134 // - The suspend request is canceled. | |
135 // - Another suspend request is started. | |
136 // - The screen lock animations complete. | |
137 // | |
138 // In this case, the observer should block the second suspend request until | |
139 // the animations have completed. | |
140 test::TestSessionStateDelegate::SetCanLockScreen(true); | |
141 SetShouldLockScreenAutomatically(true); | |
142 | |
143 chromeos::PowerManagerClient* client = | |
144 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
145 observer_->SuspendImminent(); | |
146 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | |
147 | |
148 observer_->ScreenIsLocked(); | |
149 observer_->SuspendDone(base::TimeDelta()); | |
150 observer_->SuspendImminent(); | |
151 | |
152 // The expected number of suspend readiness callbacks is 2 because the | |
153 // observer has not run the callback that it got from the first suspend | |
154 // request. The real PowerManagerClient would reset its internal counter in | |
155 // this situation but the stub client is not that smart. | |
156 EXPECT_EQ(2, client->GetNumPendingSuspendReadinessCallbacks()); | |
157 | |
158 observer_->OnLockAnimationsComplete(); | |
159 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | |
160 EXPECT_EQ(0, GetNumVisibleCompositors()); | |
161 } | |
162 | |
163 } // namespace ash | |
OLD | NEW |