| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/chromeos/power/power_event_observer.h" | 5 #include "ash/system/chromeos/power/power_event_observer.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" |
| 7 #include "ash/test/ash_test_base.h" | 8 #include "ash/test/ash_test_base.h" |
| 8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 10 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 11 #include "chromeos/dbus/power_manager_client.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" |
| 12 | 16 |
| 13 namespace ash { | 17 namespace ash { |
| 14 | 18 |
| 15 class PowerEventObserverTest : public test::AshTestBase { | 19 class PowerEventObserverTest : public test::AshTestBase { |
| 16 public: | 20 public: |
| 17 PowerEventObserverTest() {} | 21 PowerEventObserverTest() {} |
| 18 ~PowerEventObserverTest() override {} | 22 ~PowerEventObserverTest() override {} |
| 19 | 23 |
| 20 // test::AshTestBase::SetUp() overrides: | 24 // test::AshTestBase::SetUp() overrides: |
| 21 void SetUp() override { | 25 void SetUp() override { |
| 22 test::AshTestBase::SetUp(); | 26 test::AshTestBase::SetUp(); |
| 23 observer_.reset(new PowerEventObserver()); | 27 observer_.reset(new PowerEventObserver()); |
| 24 } | 28 } |
| 25 | 29 |
| 26 void TearDown() override { | 30 void TearDown() override { |
| 27 observer_.reset(); | 31 observer_.reset(); |
| 28 test::AshTestBase::TearDown(); | 32 test::AshTestBase::TearDown(); |
| 29 } | 33 } |
| 30 | 34 |
| 31 protected: | 35 protected: |
| 36 int GetNumVisibleCompositors() { |
| 37 int result = 0; |
| 38 for (const auto& window : Shell::GetAllRootWindows()) { |
| 39 if (window->GetHost()->compositor()->IsVisible()) |
| 40 ++result; |
| 41 } |
| 42 |
| 43 return result; |
| 44 } |
| 45 |
| 32 scoped_ptr<PowerEventObserver> observer_; | 46 scoped_ptr<PowerEventObserver> observer_; |
| 33 | 47 |
| 34 private: | 48 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest); | 49 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest); |
| 36 }; | 50 }; |
| 37 | 51 |
| 38 TEST_F(PowerEventObserverTest, LockBeforeSuspend) { | 52 TEST_F(PowerEventObserverTest, LockBeforeSuspend) { |
| 39 chromeos::PowerManagerClient* client = | 53 chromeos::PowerManagerClient* client = |
| 40 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | 54 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); |
| 41 ASSERT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 55 ASSERT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); |
| 42 | 56 |
| 43 // Check that the observer requests a suspend-readiness callback when it hears | 57 // Check that the observer requests a suspend-readiness callback when it hears |
| 44 // that the system is about to suspend. | 58 // that the system is about to suspend. |
| 45 SetCanLockScreen(true); | 59 SetCanLockScreen(true); |
| 46 SetShouldLockScreenBeforeSuspending(true); | 60 SetShouldLockScreenBeforeSuspending(true); |
| 47 observer_->SuspendImminent(); | 61 observer_->SuspendImminent(); |
| 48 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | 62 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); |
| 49 | 63 |
| 50 // It should run the callback when it hears that the screen is locked. | 64 // It should run the callback when it hears that the screen is locked and the |
| 65 // lock screen animations have completed. |
| 51 observer_->ScreenIsLocked(); | 66 observer_->ScreenIsLocked(); |
| 52 RunAllPendingInMessageLoop(); | 67 observer_->OnLockAnimationsComplete(); |
| 53 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 68 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); |
| 54 | 69 |
| 55 // If the system is already locked, no callback should be requested. | 70 // If the system is already locked, no callback should be requested. |
| 56 observer_->SuspendDone(base::TimeDelta()); | 71 observer_->SuspendDone(base::TimeDelta()); |
| 57 observer_->ScreenIsUnlocked(); | 72 observer_->ScreenIsUnlocked(); |
| 58 observer_->ScreenIsLocked(); | 73 observer_->ScreenIsLocked(); |
| 74 observer_->OnLockAnimationsComplete(); |
| 59 observer_->SuspendImminent(); | 75 observer_->SuspendImminent(); |
| 60 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 76 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); |
| 61 | 77 |
| 62 // It also shouldn't request a callback if it isn't instructed to lock the | 78 // It also shouldn't request a callback if it isn't instructed to lock the |
| 63 // screen. | 79 // screen. |
| 64 observer_->SuspendDone(base::TimeDelta()); | 80 observer_->SuspendDone(base::TimeDelta()); |
| 65 SetShouldLockScreenBeforeSuspending(false); | 81 SetShouldLockScreenBeforeSuspending(false); |
| 66 observer_->SuspendImminent(); | 82 observer_->SuspendImminent(); |
| 67 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 83 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); |
| 68 } | 84 } |
| 69 | 85 |
| 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()); |
| 90 |
| 91 observer_->SuspendImminent(); |
| 92 EXPECT_EQ(0, GetNumVisibleCompositors()); |
| 93 observer_->SuspendDone(base::TimeDelta()); |
| 94 |
| 95 // Tests that all the Compositors are marked invisible _after_ the screen lock |
| 96 // animations have completed. |
| 97 SetCanLockScreen(true); |
| 98 SetShouldLockScreenBeforeSuspending(true); |
| 99 |
| 100 observer_->SuspendImminent(); |
| 101 EXPECT_EQ(1, GetNumVisibleCompositors()); |
| 102 |
| 103 observer_->ScreenIsLocked(); |
| 104 EXPECT_EQ(1, GetNumVisibleCompositors()); |
| 105 |
| 106 observer_->OnLockAnimationsComplete(); |
| 107 EXPECT_EQ(0, GetNumVisibleCompositors()); |
| 108 |
| 109 observer_->SuspendDone(base::TimeDelta()); |
| 110 EXPECT_EQ(1, GetNumVisibleCompositors()); |
| 111 } |
| 112 |
| 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()); |
| 120 |
| 121 observer_->SuspendDone(base::TimeDelta()); |
| 122 observer_->ScreenIsLocked(); |
| 123 observer_->OnLockAnimationsComplete(); |
| 124 EXPECT_EQ(1, GetNumVisibleCompositors()); |
| 125 } |
| 126 |
| 127 TEST_F(PowerEventObserverTest, DelayResuspendForLockAnimations) { |
| 128 // Tests that the following order of events is handled correctly: |
| 129 // |
| 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. |
| 135 // |
| 136 // In this case, the observer should block the second suspend request until |
| 137 // the animations have completed. |
| 138 SetCanLockScreen(true); |
| 139 SetShouldLockScreenBeforeSuspending(true); |
| 140 |
| 141 chromeos::PowerManagerClient* client = |
| 142 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); |
| 143 observer_->SuspendImminent(); |
| 144 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); |
| 145 |
| 146 observer_->ScreenIsLocked(); |
| 147 observer_->SuspendDone(base::TimeDelta()); |
| 148 observer_->SuspendImminent(); |
| 149 |
| 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()); |
| 155 |
| 156 observer_->OnLockAnimationsComplete(); |
| 157 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); |
| 158 EXPECT_EQ(0, GetNumVisibleCompositors()); |
| 159 } |
| 160 |
| 70 } // namespace ash | 161 } // namespace ash |
| OLD | NEW |