Home | History | Annotate | Download | only in dbus
      1 // Copyright (c) 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 "chromeos/dbus/power_policy_controller.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "chromeos/dbus/dbus_thread_manager.h"
      9 #include "chromeos/dbus/fake_power_manager_client.h"
     10 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
     11 #include "testing/gmock/include/gmock/gmock.h"
     12 #include "testing/gtest/include/gtest/gtest.h"
     13 
     14 using ::testing::_;
     15 using ::testing::SaveArg;
     16 
     17 namespace chromeos {
     18 
     19 class PowerPolicyControllerTest : public testing::Test {
     20  public:
     21   PowerPolicyControllerTest() {}
     22   virtual ~PowerPolicyControllerTest() {}
     23 
     24   virtual void SetUp() OVERRIDE {
     25     dbus_manager_ = new MockDBusThreadManagerWithoutGMock;
     26     DBusThreadManager::InitializeForTesting(dbus_manager_);  // Takes ownership.
     27 
     28     policy_controller_.reset(
     29         new PowerPolicyController(dbus_manager_, &fake_power_client_));
     30   }
     31 
     32   virtual void TearDown() OVERRIDE {
     33     policy_controller_.reset();
     34     DBusThreadManager::Shutdown();
     35   }
     36 
     37  protected:
     38   MockDBusThreadManagerWithoutGMock* dbus_manager_;  // Not owned.
     39   FakePowerManagerClient fake_power_client_;
     40   scoped_ptr<PowerPolicyController> policy_controller_;
     41 };
     42 
     43 TEST_F(PowerPolicyControllerTest, Prefs) {
     44   PowerPolicyController::PrefValues prefs;
     45   prefs.ac_screen_dim_delay_ms = 600000;
     46   prefs.ac_screen_off_delay_ms = 660000;
     47   prefs.ac_idle_delay_ms = 720000;
     48   prefs.battery_screen_dim_delay_ms = 300000;
     49   prefs.battery_screen_off_delay_ms = 360000;
     50   prefs.battery_idle_delay_ms = 420000;
     51   prefs.ac_idle_action = PowerPolicyController::ACTION_SUSPEND;
     52   prefs.battery_idle_action = PowerPolicyController::ACTION_STOP_SESSION;
     53   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
     54   prefs.use_audio_activity = true;
     55   prefs.use_video_activity = true;
     56   prefs.enable_screen_lock = false;
     57   prefs.presentation_screen_dim_delay_factor = 3.0;
     58   prefs.user_activity_screen_dim_delay_factor = 2.0;
     59   policy_controller_->ApplyPrefs(prefs);
     60 
     61   power_manager::PowerManagementPolicy expected_policy;
     62   expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000);
     63   expected_policy.mutable_ac_delays()->set_screen_off_ms(660000);
     64   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
     65   expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1);
     66   expected_policy.mutable_ac_delays()->set_idle_ms(720000);
     67   expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000);
     68   expected_policy.mutable_battery_delays()->set_screen_off_ms(360000);
     69   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
     70   expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1);
     71   expected_policy.mutable_battery_delays()->set_idle_ms(420000);
     72   expected_policy.set_ac_idle_action(
     73       power_manager::PowerManagementPolicy_Action_SUSPEND);
     74   expected_policy.set_battery_idle_action(
     75       power_manager::PowerManagementPolicy_Action_STOP_SESSION);
     76   expected_policy.set_lid_closed_action(
     77       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
     78   expected_policy.set_use_audio_activity(true);
     79   expected_policy.set_use_video_activity(true);
     80   expected_policy.set_presentation_screen_dim_delay_factor(3.0);
     81   expected_policy.set_user_activity_screen_dim_delay_factor(2.0);
     82   expected_policy.set_reason("Prefs");
     83   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
     84             PowerPolicyController::GetPolicyDebugString(
     85                 fake_power_client_.get_policy()));
     86 
     87   // Change some prefs and check that an updated policy is sent.
     88   prefs.ac_idle_warning_delay_ms = 700000;
     89   prefs.battery_idle_warning_delay_ms = 400000;
     90   prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND;
     91   policy_controller_->ApplyPrefs(prefs);
     92   expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000);
     93   expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000);
     94   expected_policy.set_lid_closed_action(
     95       power_manager::PowerManagementPolicy_Action_SUSPEND);
     96   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
     97             PowerPolicyController::GetPolicyDebugString(
     98                 fake_power_client_.get_policy()));
     99 
    100   // The enable-screen-lock pref should force the screen-lock delays to
    101   // match the screen-off delays plus a constant value.
    102   prefs.enable_screen_lock = true;
    103   policy_controller_->ApplyPrefs(prefs);
    104   expected_policy.mutable_ac_delays()->set_screen_lock_ms(
    105       660000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
    106   expected_policy.mutable_battery_delays()->set_screen_lock_ms(
    107       360000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
    108   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    109             PowerPolicyController::GetPolicyDebugString(
    110                 fake_power_client_.get_policy()));
    111 
    112   // If the screen-lock-delay prefs are set to lower values than the
    113   // screen-off delays plus the constant, the lock prefs should take
    114   // precedence.
    115   prefs.ac_screen_lock_delay_ms = 70000;
    116   prefs.battery_screen_lock_delay_ms = 60000;
    117   policy_controller_->ApplyPrefs(prefs);
    118   expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000);
    119   expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000);
    120   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    121             PowerPolicyController::GetPolicyDebugString(
    122                 fake_power_client_.get_policy()));
    123 
    124   // If the artificial screen-lock delays would exceed the idle delay, they
    125   // shouldn't be set -- the power manager would ignore them since the
    126   // idle action should lock the screen in this case.
    127   prefs.ac_screen_off_delay_ms = prefs.ac_idle_delay_ms - 1;
    128   prefs.battery_screen_off_delay_ms = prefs.battery_idle_delay_ms - 1;
    129   prefs.ac_screen_lock_delay_ms = -1;
    130   prefs.battery_screen_lock_delay_ms = -1;
    131   policy_controller_->ApplyPrefs(prefs);
    132   expected_policy.mutable_ac_delays()->set_screen_off_ms(
    133       prefs.ac_screen_off_delay_ms);
    134   expected_policy.mutable_battery_delays()->set_screen_off_ms(
    135       prefs.battery_screen_off_delay_ms);
    136   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
    137   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
    138   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    139             PowerPolicyController::GetPolicyDebugString(
    140                 fake_power_client_.get_policy()));
    141 
    142   // Set the "allow screen wake locks" pref to false.  The system should be
    143   // prevented from suspending due to user inactivity on AC power but the
    144   // pref-supplied screen-related delays should be left untouched.
    145   prefs.allow_screen_wake_locks = false;
    146   policy_controller_->ApplyPrefs(prefs);
    147   policy_controller_->AddScreenWakeLock("Screen");
    148   expected_policy.set_ac_idle_action(
    149       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    150   expected_policy.set_reason("Prefs, Screen");
    151   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    152             PowerPolicyController::GetPolicyDebugString(
    153                 fake_power_client_.get_policy()));
    154 }
    155 
    156 TEST_F(PowerPolicyControllerTest, WakeLocks) {
    157   const char kSystemWakeLockReason[] = "system";
    158   const int system_id =
    159       policy_controller_->AddSystemWakeLock(kSystemWakeLockReason);
    160   power_manager::PowerManagementPolicy expected_policy;
    161   expected_policy.set_ac_idle_action(
    162       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    163   expected_policy.set_battery_idle_action(
    164       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    165   expected_policy.set_reason(kSystemWakeLockReason);
    166   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    167             PowerPolicyController::GetPolicyDebugString(
    168                 fake_power_client_.get_policy()));
    169 
    170   const char kScreenWakeLockReason[] = "screen";
    171   const int screen_id = policy_controller_->AddScreenWakeLock(
    172       kScreenWakeLockReason);
    173   expected_policy.mutable_ac_delays()->set_screen_dim_ms(0);
    174   expected_policy.mutable_ac_delays()->set_screen_off_ms(0);
    175   expected_policy.mutable_ac_delays()->set_screen_lock_ms(0);
    176   expected_policy.mutable_battery_delays()->set_screen_dim_ms(0);
    177   expected_policy.mutable_battery_delays()->set_screen_off_ms(0);
    178   expected_policy.mutable_battery_delays()->set_screen_lock_ms(0);
    179   expected_policy.set_reason(
    180       std::string(kScreenWakeLockReason) + ", " + kSystemWakeLockReason);
    181   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    182             PowerPolicyController::GetPolicyDebugString(
    183                 fake_power_client_.get_policy()));
    184 
    185   policy_controller_->RemoveWakeLock(system_id);
    186   expected_policy.set_reason(kScreenWakeLockReason);
    187   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    188             PowerPolicyController::GetPolicyDebugString(
    189                 fake_power_client_.get_policy()));
    190 
    191   policy_controller_->RemoveWakeLock(screen_id);
    192   expected_policy.Clear();
    193   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    194             PowerPolicyController::GetPolicyDebugString(
    195                 fake_power_client_.get_policy()));
    196 }
    197 
    198 }  // namespace chromeos
    199