Index: ash/wm/power_button_controller_unittest.cc |
diff --git a/ash/wm/power_button_controller_unittest.cc b/ash/wm/power_button_controller_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9844d791af8db3d7969d93c0f6c754e5112ed6f9 |
--- /dev/null |
+++ b/ash/wm/power_button_controller_unittest.cc |
@@ -0,0 +1,223 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/wm/power_button_controller.h" |
+ |
+#include "ash/common/ash_switches.h" |
+#include "ash/common/session/session_state_delegate.h" |
+#include "ash/common/wm/maximize_mode/maximize_mode_controller.h" |
+#include "ash/common/wm_shell.h" |
+#include "ash/shell.h" |
+#include "ash/test/ash_test_base.h" |
+#include "ash/wm/lock_state_controller.h" |
+#include "base/command_line.h" |
+#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "chromeos/dbus/fake_power_manager_client.h" |
+#include "ui/events/test/event_generator.h" |
+ |
+namespace ash { |
+namespace test { |
+ |
+namespace { |
+ |
+void CheckBoolEquals(bool result, bool expected) { |
+ EXPECT_EQ(result, expected); |
+} |
+ |
+} // namespace |
+ |
+// For non-convertible/tablet device, power button controller test is |
+// degenerated to lock state controller test. |
+class TabletPowerButtonControllerTest : public AshTestBase { |
+ public: |
+ TabletPowerButtonControllerTest() {} |
+ ~TabletPowerButtonControllerTest() override {} |
+ |
+ void SetUp() override { |
+ // This also initializes DBusThreadManager. |
+ std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter = |
+ chromeos::DBusThreadManager::GetSetterForTesting(); |
+ power_manager_client_ = new chromeos::FakePowerManagerClient(); |
+ dbus_setter->SetPowerManagerClient(base::WrapUnique(power_manager_client_)); |
+ base::CommandLine::ForCurrentProcess()->AppendSwitch( |
+ switches::kAshEnableTouchViewTesting); |
+ AshTestBase::SetUp(); |
+ |
+ lock_state_controller_ = Shell::GetInstance()->lock_state_controller(); |
+ power_button_controller_ = Shell::GetInstance()->power_button_controller(); |
+ test_api_ = base::MakeUnique<PowerButtonController::TestApi>( |
+ power_button_controller_); |
+ generator_ = &AshTestBase::GetEventGenerator(); |
+ CheckBacklightsForcedOff(false); |
+ } |
+ |
+ void TearDown() override { |
+ generator_ = nullptr; |
+ AshTestBase::TearDown(); |
+ chromeos::DBusThreadManager::Shutdown(); |
+ } |
+ |
+ protected: |
+ void PressPowerButton() { |
+ power_button_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
+ } |
+ |
+ void ReleasePowerButton() { |
+ power_button_controller_->OnPowerButtonEvent(false, base::TimeTicks::Now()); |
+ } |
+ |
+ void SystemUnlocks() { |
+ lock_state_controller_->OnLockStateChanged(false); |
+ WmShell::Get()->GetSessionStateDelegate()->UnlockScreen(); |
+ } |
+ |
+ void Initialize(LoginStatus status) { |
+ lock_state_controller_->OnLoginStateChanged(status); |
+ SetUserLoggedIn(status != LoginStatus::NOT_LOGGED_IN); |
+ lock_state_controller_->OnLockStateChanged(false); |
+ } |
+ |
+ void EnableMaximizeMode(bool enabled) { |
+ WmShell::Get()->maximize_mode_controller()->EnableMaximizeModeWindowManager( |
+ enabled); |
+ } |
+ |
+ void CheckLockedState(bool expected_locked) { |
+ EXPECT_EQ(WmShell::Get()->GetSessionStateDelegate()->IsScreenLocked(), |
+ expected_locked); |
+ } |
+ |
+ void CheckBacklightsForcedOff(bool expected_forced_off) { |
+ power_manager_client_->GetBacklightsForcedOff( |
+ base::Bind(&CheckBoolEquals, expected_forced_off)); |
+ } |
+ |
+ // Ownership is passed on to chromeos::DBusThreadManager. |
+ chromeos::FakePowerManagerClient* power_manager_client_; |
+ PowerButtonController* power_button_controller_; // Not owned. |
+ LockStateController* lock_state_controller_; // Not owned. |
+ std::unique_ptr<PowerButtonController::TestApi> test_api_; |
+ ui::test::EventGenerator* generator_ = nullptr; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TabletPowerButtonControllerTest); |
+}; |
+ |
+TEST_F(TabletPowerButtonControllerTest, LockScreenIfRequired) { |
+ Initialize(LoginStatus::USER); |
+ SetShouldLockScreenAutomatically(true); |
+ CheckLockedState(false); |
+ |
+ // On User logged in status, power-button-press-release should lock screen if |
+ // should lock screen automatically. |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckLockedState(true); |
+ |
+ // On locked state, power-button-press-release should do nothing. |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckLockedState(true); |
+ |
+ // Unlock the sceen. |
+ SystemUnlocks(); |
+ CheckLockedState(false); |
+ |
+ // power-button-press-release should not lock the screen if should not lock |
+ // screen automatically. |
+ SetShouldLockScreenAutomatically(false); |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckLockedState(false); |
+} |
+ |
+// This test is to test the power button released is done before |
+// |tablet_pre_start_shutdown_animation_timer_| is timeout. |
+TEST_F(TabletPowerButtonControllerTest, QuickTapTest) { |
+ PressPowerButton(); |
+ EXPECT_TRUE(test_api_->TabletPowerButtonTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ EXPECT_FALSE(test_api_->TabletPowerButtonTimerIsRunning()); |
+ CheckBacklightsForcedOff(true); |
+ |
+ PressPowerButton(); |
+ EXPECT_TRUE(test_api_->TabletPowerButtonTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ EXPECT_FALSE(test_api_->TabletPowerButtonTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+} |
+ |
+// This test is to test the power button released is done after |
+// |tablet_pre_start_shutdown_animation_timer_| is timeout. |
+TEST_F(TabletPowerButtonControllerTest, |
+ ReleasePowerButtonDuringShutdownAnimation) { |
+ PressPowerButton(); |
+ test_api_->TriggerTabletPowerButtonTimeout(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(false); |
+ |
+ // Set backlights forced off to true for the next test. |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ |
+ PressPowerButton(); |
+ test_api_->TriggerTabletPowerButtonTimeout(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(false); |
+} |
+ |
+// When screen is turned off, even it is not set to forced off by power button |
+// (can be caused by idle screen off, suspended), we should consider it as |
+// "backlights forced off" to set backlights forced off state when tapping power |
+// button. |
+TEST_F(TabletPowerButtonControllerTest, TappingPowerButtonWhenScreenIsOff) { |
+ power_button_controller_->OnScreenBrightnessChanged(0.0); |
+ PressPowerButton(); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ |
+ CheckBacklightsForcedOff(false); |
+ power_button_controller_->OnScreenBrightnessChanged(10.0); |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+} |
+ |
+// For convertible device working on laptop mode, keyboard/mouse event |
+// should SetBacklightsForcedOff(false) when screen is off. |
+TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { |
+ EnableMaximizeMode(false); |
+ |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
+ CheckBacklightsForcedOff(false); |
+ |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->MoveMouseBy(1, 1); |
+ CheckBacklightsForcedOff(false); |
+} |
+ |
+// For convertible device working on tablet mode, keyboard/mouse event should |
+// not SetBacklightsForcedOff(false) when screen is off. |
+TEST_F(TabletPowerButtonControllerTest, ConvertibleOnMaximizeMode) { |
+ EnableMaximizeMode(true); |
+ |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
+ CheckBacklightsForcedOff(true); |
+ |
+ generator_->MoveMouseBy(1, 1); |
+ CheckBacklightsForcedOff(true); |
+} |
+ |
+} // namespace test |
+} // namespace ash |