| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
| 10 #include "ash/test/ash_test_base.h" | 10 #include "ash/test/ash_test_base.h" |
| 11 #include "base/run_loop.h" |
| 11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 12 #include "chromeos/dbus/dbus_thread_manager.h" | 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 13 #include "chromeos/dbus/power_manager_client.h" | 14 #include "chromeos/dbus/fake_power_manager_client.h" |
| 14 #include "ui/aura/window.h" | 15 #include "ui/aura/window.h" |
| 15 #include "ui/aura/window_tree_host.h" | 16 #include "ui/aura/window_tree_host.h" |
| 16 #include "ui/compositor/compositor.h" | 17 #include "ui/compositor/compositor.h" |
| 18 #include "ui/display/chromeos/display_configurator.h" |
| 19 #include "ui/display/chromeos/test/action_logger.h" |
| 20 #include "ui/display/chromeos/test/test_native_display_delegate.h" |
| 17 | 21 |
| 18 namespace ash { | 22 namespace ash { |
| 19 | 23 |
| 20 class PowerEventObserverTest : public test::AshTestBase { | 24 class PowerEventObserverTest : public test::AshTestBase { |
| 21 public: | 25 public: |
| 22 PowerEventObserverTest() {} | 26 PowerEventObserverTest() {} |
| 23 ~PowerEventObserverTest() override {} | 27 ~PowerEventObserverTest() override {} |
| 24 | 28 |
| 25 // test::AshTestBase::SetUp() overrides: | 29 // test::AshTestBase::SetUp() overrides: |
| 26 void SetUp() override { | 30 void SetUp() override { |
| 27 test::AshTestBase::SetUp(); | 31 test::AshTestBase::SetUp(); |
| 32 |
| 33 // Reset any state left in chromeos::FakePowerManagerClient by previous |
| 34 // tests. |
| 35 power_manager_client_ = static_cast<chromeos::FakePowerManagerClient*>( |
| 36 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()); |
| 37 power_manager_client_->Reset(); |
| 38 |
| 28 observer_.reset(new PowerEventObserver()); | 39 observer_.reset(new PowerEventObserver()); |
| 29 } | 40 } |
| 30 | 41 |
| 31 void TearDown() override { | 42 void TearDown() override { |
| 32 observer_.reset(); | 43 observer_.reset(); |
| 33 test::AshTestBase::TearDown(); | 44 test::AshTestBase::TearDown(); |
| 34 } | 45 } |
| 35 | 46 |
| 36 protected: | 47 protected: |
| 37 int GetNumVisibleCompositors() { | 48 int GetNumVisibleCompositors() { |
| 38 int result = 0; | 49 int result = 0; |
| 39 for (auto* window : Shell::GetAllRootWindows()) { | 50 for (auto* window : Shell::GetAllRootWindows()) { |
| 40 if (window->GetHost()->compositor()->IsVisible()) | 51 if (window->GetHost()->compositor()->IsVisible()) |
| 41 ++result; | 52 ++result; |
| 42 } | 53 } |
| 43 | 54 |
| 44 return result; | 55 return result; |
| 45 } | 56 } |
| 46 | 57 |
| 58 chromeos::FakePowerManagerClient* power_manager_client_; // Non-owned. |
| 47 std::unique_ptr<PowerEventObserver> observer_; | 59 std::unique_ptr<PowerEventObserver> observer_; |
| 48 | 60 |
| 49 private: | 61 private: |
| 50 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest); | 62 DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest); |
| 51 }; | 63 }; |
| 52 | 64 |
| 53 TEST_F(PowerEventObserverTest, LockBeforeSuspend) { | 65 TEST_F(PowerEventObserverTest, LockBeforeSuspend) { |
| 54 chromeos::PowerManagerClient* client = | 66 ASSERT_EQ(0, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 55 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
| 56 ASSERT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | |
| 57 | 67 |
| 58 // Check that the observer requests a suspend-readiness callback when it hears | 68 // Check that the observer requests a suspend-readiness callback when it hears |
| 59 // that the system is about to suspend. | 69 // that the system is about to suspend. |
| 60 SetCanLockScreen(true); | 70 SetCanLockScreen(true); |
| 61 SetShouldLockScreenBeforeSuspending(true); | 71 SetShouldLockScreenBeforeSuspending(true); |
| 62 observer_->SuspendImminent(); | 72 observer_->SuspendImminent(); |
| 63 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | 73 EXPECT_EQ(1, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 64 | 74 |
| 65 // It should run the callback when it hears that the screen is locked and the | 75 // It should run the callback when it hears that the screen is locked and the |
| 66 // lock screen animations have completed. | 76 // lock screen animations have completed. |
| 67 observer_->ScreenIsLocked(); | 77 observer_->ScreenIsLocked(); |
| 68 observer_->OnLockAnimationsComplete(); | 78 observer_->OnLockAnimationsComplete(); |
| 69 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 79 EXPECT_EQ(0, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 70 | 80 |
| 71 // If the system is already locked, no callback should be requested. | 81 // If the system is already locked, no callback should be requested. |
| 72 observer_->SuspendDone(base::TimeDelta()); | 82 observer_->SuspendDone(base::TimeDelta()); |
| 73 observer_->ScreenIsUnlocked(); | 83 observer_->ScreenIsUnlocked(); |
| 74 observer_->ScreenIsLocked(); | 84 observer_->ScreenIsLocked(); |
| 75 observer_->OnLockAnimationsComplete(); | 85 observer_->OnLockAnimationsComplete(); |
| 76 observer_->SuspendImminent(); | 86 observer_->SuspendImminent(); |
| 77 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 87 EXPECT_EQ(0, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 78 | 88 |
| 79 // It also shouldn't request a callback if it isn't instructed to lock the | 89 // It also shouldn't request a callback if it isn't instructed to lock the |
| 80 // screen. | 90 // screen. |
| 81 observer_->SuspendDone(base::TimeDelta()); | 91 observer_->SuspendDone(base::TimeDelta()); |
| 82 SetShouldLockScreenBeforeSuspending(false); | 92 SetShouldLockScreenBeforeSuspending(false); |
| 83 observer_->SuspendImminent(); | 93 observer_->SuspendImminent(); |
| 84 EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks()); | 94 EXPECT_EQ(0, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 85 } | 95 } |
| 86 | 96 |
| 87 TEST_F(PowerEventObserverTest, SetInvisibleBeforeSuspend) { | 97 TEST_F(PowerEventObserverTest, SetInvisibleBeforeSuspend) { |
| 88 // Tests that all the Compositors are marked invisible before a suspend | 98 // Tests that all the Compositors are marked invisible before a suspend |
| 89 // request when the screen is not supposed to be locked before a suspend. | 99 // request when the screen is not supposed to be locked before a suspend. |
| 90 EXPECT_EQ(1, GetNumVisibleCompositors()); | 100 EXPECT_EQ(1, GetNumVisibleCompositors()); |
| 91 | 101 |
| 92 observer_->SuspendImminent(); | 102 observer_->SuspendImminent(); |
| 93 EXPECT_EQ(0, GetNumVisibleCompositors()); | 103 EXPECT_EQ(0, GetNumVisibleCompositors()); |
| 94 observer_->SuspendDone(base::TimeDelta()); | 104 observer_->SuspendDone(base::TimeDelta()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 // - The screen is locked. | 142 // - The screen is locked. |
| 133 // - The suspend request is canceled. | 143 // - The suspend request is canceled. |
| 134 // - Another suspend request is started. | 144 // - Another suspend request is started. |
| 135 // - The screen lock animations complete. | 145 // - The screen lock animations complete. |
| 136 // | 146 // |
| 137 // In this case, the observer should block the second suspend request until | 147 // In this case, the observer should block the second suspend request until |
| 138 // the animations have completed. | 148 // the animations have completed. |
| 139 SetCanLockScreen(true); | 149 SetCanLockScreen(true); |
| 140 SetShouldLockScreenBeforeSuspending(true); | 150 SetShouldLockScreenBeforeSuspending(true); |
| 141 | 151 |
| 142 chromeos::PowerManagerClient* client = | |
| 143 chromeos::DBusThreadManager::Get()->GetPowerManagerClient(); | |
| 144 observer_->SuspendImminent(); | 152 observer_->SuspendImminent(); |
| 145 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | 153 EXPECT_EQ(1, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 146 | 154 |
| 147 observer_->ScreenIsLocked(); | 155 observer_->ScreenIsLocked(); |
| 148 observer_->SuspendDone(base::TimeDelta()); | 156 observer_->SuspendDone(base::TimeDelta()); |
| 149 observer_->SuspendImminent(); | 157 observer_->SuspendImminent(); |
| 150 | 158 |
| 151 // The expected number of suspend readiness callbacks is 2 because the | 159 // The expected number of suspend readiness callbacks is 2 because the |
| 152 // observer has not run the callback that it got from the first suspend | 160 // observer has not run the callback that it got from the first suspend |
| 153 // request. The real PowerManagerClient would reset its internal counter in | 161 // request. The real PowerManagerClient would reset its internal counter in |
| 154 // this situation but the stub client is not that smart. | 162 // this situation but the stub client is not that smart. |
| 155 EXPECT_EQ(2, client->GetNumPendingSuspendReadinessCallbacks()); | 163 EXPECT_EQ(2, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 156 | 164 |
| 157 observer_->OnLockAnimationsComplete(); | 165 observer_->OnLockAnimationsComplete(); |
| 158 EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks()); | 166 EXPECT_EQ(1, power_manager_client_->GetNumPendingSuspendReadinessCallbacks()); |
| 159 EXPECT_EQ(0, GetNumVisibleCompositors()); | 167 EXPECT_EQ(0, GetNumVisibleCompositors()); |
| 160 } | 168 } |
| 161 | 169 |
| 170 TEST_F(PowerEventObserverTest, SuspendAndResumeDisplays) { |
| 171 // Configure ui::DisplayConfigurator to do asynchronous configuration. |
| 172 ui::test::ActionLogger logger; |
| 173 ui::test::TestNativeDisplayDelegate* delegate = |
| 174 new ui::test::TestNativeDisplayDelegate(&logger); |
| 175 delegate->set_run_async(true); |
| 176 ui::DisplayConfigurator* configurator = |
| 177 Shell::GetInstance()->display_configurator(); |
| 178 configurator->SetDelegateForTesting( |
| 179 std::unique_ptr<ui::NativeDisplayDelegate>(delegate)); |
| 180 |
| 181 // Start a suspend attempt and let the async display-suspending task run. |
| 182 observer_->SuspendImminent(); |
| 183 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 184 base::RunLoop().RunUntilIdle(); |
| 185 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 186 |
| 187 // When the system resumes, the displays should be resumed. |
| 188 observer_->SuspendDone(base::TimeDelta()); |
| 189 base::RunLoop().RunUntilIdle(); |
| 190 EXPECT_FALSE(configurator->displays_suspended_for_test()); |
| 191 |
| 192 // Now, start another suspend attempt but abort it before the |
| 193 // display-suspending task runs (http://crbug.com/620208). The displays should |
| 194 // still be in the suspended state, since the async task is running and might |
| 195 // undo any resuming that's done at this time. |
| 196 observer_->SuspendImminent(); |
| 197 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 198 observer_->SuspendDone(base::TimeDelta()); |
| 199 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 200 |
| 201 // When the task finally completes, the displays should be resumed. |
| 202 base::RunLoop().RunUntilIdle(); |
| 203 EXPECT_FALSE(configurator->displays_suspended_for_test()); |
| 204 |
| 205 // Start suspending and then cancel immediately again. |
| 206 observer_->SuspendImminent(); |
| 207 observer_->SuspendDone(base::TimeDelta()); |
| 208 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 209 |
| 210 // This time, suspend again before the display-resuming task has a chance to |
| 211 // run. The displays should remain suspended. |
| 212 observer_->SuspendImminent(); |
| 213 base::RunLoop().RunUntilIdle(); |
| 214 EXPECT_TRUE(configurator->displays_suspended_for_test()); |
| 215 } |
| 216 |
| 162 } // namespace ash | 217 } // namespace ash |
| OLD | NEW |