Index: ash/system/chromeos/power/tablet_power_button_controller_unittest.cc |
diff --git a/ash/system/chromeos/power/tablet_power_button_controller_unittest.cc b/ash/system/chromeos/power/tablet_power_button_controller_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..70e0e623867263b6d846d401b1dcfc488961b4a2 |
--- /dev/null |
+++ b/ash/system/chromeos/power/tablet_power_button_controller_unittest.cc |
@@ -0,0 +1,255 @@ |
+// 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/system/chromeos/power/tablet_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/test/lock_state_controller_test_api.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 |
+ |
+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(); |
+ tablet_controller_ = TabletPowerButtonController::GetInstance(); |
+ test_api_ = base::MakeUnique<TabletPowerButtonController::TestApi>( |
+ tablet_controller_); |
+ generator_ = &AshTestBase::GetEventGenerator(); |
+ CheckBacklightsForcedOff(false); |
+ } |
+ |
+ void TearDown() override { |
+ generator_ = nullptr; |
+ AshTestBase::TearDown(); |
+ chromeos::DBusThreadManager::Shutdown(); |
+ } |
+ |
+ protected: |
+ void PressPowerButton() { |
+ tablet_controller_->OnPowerButtonEvent(true, base::TimeTicks::Now()); |
+ } |
+ |
+ void ReleasePowerButton() { |
+ tablet_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_; |
+ LockStateController* lock_state_controller_; // Not owned. |
+ TabletPowerButtonController* tablet_controller_; // Not owned. |
+ std::unique_ptr<TabletPowerButtonController::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 shutdown |
+// is timeout. |
+TEST_F(TabletPowerButtonControllerTest, |
+ ReleasePowerButtonBeforeStartingShutdownTimer) { |
+ PressPowerButton(); |
+ EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); |
+ CheckBacklightsForcedOff(true); |
+ |
+ PressPowerButton(); |
+ EXPECT_TRUE(test_api_->ShutdownTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ EXPECT_FALSE(test_api_->ShutdownTimerIsRunning()); |
+ CheckBacklightsForcedOff(false); |
+} |
+ |
+// This test is to test the power button released is done after shutdown timer |
+// is timeout, where it will start shutdown animation timer. |
+TEST_F(TabletPowerButtonControllerTest, |
+ ReleasePowerButtonDuringShutdownAnimation) { |
+ // Initializes LockStateControllerTestApi to observe shutdown animation timer. |
+ std::unique_ptr<LockStateControllerTestApi> lock_state_test_api = |
+ base::MakeUnique<LockStateControllerTestApi>(lock_state_controller_); |
+ |
+ PressPowerButton(); |
+ test_api_->TriggerShutdownTimeout(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(false); |
+ |
+ // Set backlights forced off to true for the next test. |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ |
+ PressPowerButton(); |
+ test_api_->TriggerShutdownTimeout(); |
+ EXPECT_TRUE(lock_state_test_api->shutdown_timer_is_running()); |
+ ReleasePowerButton(); |
+ EXPECT_FALSE(lock_state_test_api->shutdown_timer_is_running()); |
+ 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_manager_client_->SendBrightnessChanged(0, false); |
+ PressPowerButton(); |
+ CheckBacklightsForcedOff(false); |
+ ReleasePowerButton(); |
+ |
+ CheckBacklightsForcedOff(false); |
+ power_manager_client_->SendBrightnessChanged(10, false); |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+} |
+ |
+// For convertible device working on laptop mode, test keyboard/mouse event |
+// when screen is off. |
+TEST_F(TabletPowerButtonControllerTest, ConvertibleOnLaptopMode) { |
+ EnableMaximizeMode(false); |
+ |
+ // KeyEvent should SetBacklightsForcedOff(false). |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->PressKey(ui::VKEY_L, ui::EF_NONE); |
+ CheckBacklightsForcedOff(false); |
+ |
+ // Regular mouse event should SetBacklightsForcedOff(false). |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->MoveMouseBy(1, 1); |
+ CheckBacklightsForcedOff(false); |
+ |
+ // Touchpad mouse event should SetBacklightsForcedOff(false). |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->PressTouch(); |
+ CheckBacklightsForcedOff(false); |
+ |
+ // Stylus mouse event should not SetBacklightsForcedOff(false). |
+ PressPowerButton(); |
+ ReleasePowerButton(); |
+ CheckBacklightsForcedOff(true); |
+ generator_->EnterPenPointerMode(); |
+ generator_->MoveMouseBy(1, 1); |
+ CheckBacklightsForcedOff(true); |
+ generator_->ExitPenPointerMode(); |
+} |
+ |
+// 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); |
+ |
+ generator_->PressTouch(); |
+ CheckBacklightsForcedOff(true); |
+ |
+ generator_->EnterPenPointerMode(); |
+ generator_->MoveMouseBy(1, 1); |
+ CheckBacklightsForcedOff(true); |
+ generator_->ExitPenPointerMode(); |
+} |
+ |
+} // namespace test |
+} // namespace ash |