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 "base/message_loop/message_loop.h"
      9 #include "chromeos/dbus/dbus_thread_manager.h"
     10 #include "chromeos/dbus/fake_power_manager_client.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     scoped_ptr<DBusThreadManagerSetter> dbus_setter =
     26         chromeos::DBusThreadManager::GetSetterForTesting();
     27     fake_power_client_ = new FakePowerManagerClient;
     28     dbus_setter->SetPowerManagerClient(
     29         scoped_ptr<PowerManagerClient>(fake_power_client_));
     30 
     31     policy_controller_.reset(new PowerPolicyController);
     32     policy_controller_->Init(DBusThreadManager::Get());
     33   }
     34 
     35   virtual void TearDown() OVERRIDE {
     36     policy_controller_.reset();
     37     DBusThreadManager::Shutdown();
     38   }
     39 
     40  protected:
     41   FakePowerManagerClient* fake_power_client_;
     42   scoped_ptr<PowerPolicyController> policy_controller_;
     43   base::MessageLoop message_loop_;
     44 };
     45 
     46 TEST_F(PowerPolicyControllerTest, Prefs) {
     47   PowerPolicyController::PrefValues prefs;
     48   prefs.ac_screen_dim_delay_ms = 600000;
     49   prefs.ac_screen_off_delay_ms = 660000;
     50   prefs.ac_idle_delay_ms = 720000;
     51   prefs.battery_screen_dim_delay_ms = 300000;
     52   prefs.battery_screen_off_delay_ms = 360000;
     53   prefs.battery_idle_delay_ms = 420000;
     54   prefs.ac_idle_action = PowerPolicyController::ACTION_SUSPEND;
     55   prefs.battery_idle_action = PowerPolicyController::ACTION_STOP_SESSION;
     56   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
     57   prefs.use_audio_activity = true;
     58   prefs.use_video_activity = true;
     59   prefs.ac_brightness_percent = 87.0;
     60   prefs.battery_brightness_percent = 43.0;
     61   prefs.enable_auto_screen_lock = false;
     62   prefs.presentation_screen_dim_delay_factor = 3.0;
     63   prefs.user_activity_screen_dim_delay_factor = 2.0;
     64   prefs.wait_for_initial_user_activity = true;
     65   policy_controller_->ApplyPrefs(prefs);
     66 
     67   power_manager::PowerManagementPolicy expected_policy;
     68   expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000);
     69   expected_policy.mutable_ac_delays()->set_screen_off_ms(660000);
     70   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
     71   expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1);
     72   expected_policy.mutable_ac_delays()->set_idle_ms(720000);
     73   expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000);
     74   expected_policy.mutable_battery_delays()->set_screen_off_ms(360000);
     75   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
     76   expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1);
     77   expected_policy.mutable_battery_delays()->set_idle_ms(420000);
     78   expected_policy.set_ac_idle_action(
     79       power_manager::PowerManagementPolicy_Action_SUSPEND);
     80   expected_policy.set_battery_idle_action(
     81       power_manager::PowerManagementPolicy_Action_STOP_SESSION);
     82   expected_policy.set_lid_closed_action(
     83       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
     84   expected_policy.set_use_audio_activity(true);
     85   expected_policy.set_use_video_activity(true);
     86   expected_policy.set_ac_brightness_percent(87.0);
     87   expected_policy.set_battery_brightness_percent(43.0);
     88   expected_policy.set_presentation_screen_dim_delay_factor(3.0);
     89   expected_policy.set_user_activity_screen_dim_delay_factor(2.0);
     90   expected_policy.set_wait_for_initial_user_activity(true);
     91   expected_policy.set_reason("Prefs");
     92   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
     93             PowerPolicyController::GetPolicyDebugString(
     94                 fake_power_client_->policy()));
     95 
     96   // Change some prefs and check that an updated policy is sent.
     97   prefs.ac_idle_warning_delay_ms = 700000;
     98   prefs.battery_idle_warning_delay_ms = 400000;
     99   prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND;
    100   prefs.ac_brightness_percent = -1.0;
    101   policy_controller_->ApplyPrefs(prefs);
    102   expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000);
    103   expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000);
    104   expected_policy.set_lid_closed_action(
    105       power_manager::PowerManagementPolicy_Action_SUSPEND);
    106   expected_policy.clear_ac_brightness_percent();
    107   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    108             PowerPolicyController::GetPolicyDebugString(
    109                 fake_power_client_->policy()));
    110 
    111   // The enable-auto-screen-lock pref should force the screen-lock delays to
    112   // match the screen-off delays plus a constant value.
    113   prefs.enable_auto_screen_lock = true;
    114   policy_controller_->ApplyPrefs(prefs);
    115   expected_policy.mutable_ac_delays()->set_screen_lock_ms(
    116       660000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
    117   expected_policy.mutable_battery_delays()->set_screen_lock_ms(
    118       360000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
    119   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    120             PowerPolicyController::GetPolicyDebugString(
    121                 fake_power_client_->policy()));
    122 
    123   // If the screen-lock-delay prefs are set to lower values than the
    124   // screen-off delays plus the constant, the lock prefs should take
    125   // precedence.
    126   prefs.ac_screen_lock_delay_ms = 70000;
    127   prefs.battery_screen_lock_delay_ms = 60000;
    128   policy_controller_->ApplyPrefs(prefs);
    129   expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000);
    130   expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000);
    131   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    132             PowerPolicyController::GetPolicyDebugString(
    133                 fake_power_client_->policy()));
    134 
    135   // If the artificial screen-lock delays would exceed the idle delay, they
    136   // shouldn't be set -- the power manager would ignore them since the
    137   // idle action should lock the screen in this case.
    138   prefs.ac_screen_off_delay_ms = prefs.ac_idle_delay_ms - 1;
    139   prefs.battery_screen_off_delay_ms = prefs.battery_idle_delay_ms - 1;
    140   prefs.ac_screen_lock_delay_ms = -1;
    141   prefs.battery_screen_lock_delay_ms = -1;
    142   policy_controller_->ApplyPrefs(prefs);
    143   expected_policy.mutable_ac_delays()->set_screen_off_ms(
    144       prefs.ac_screen_off_delay_ms);
    145   expected_policy.mutable_battery_delays()->set_screen_off_ms(
    146       prefs.battery_screen_off_delay_ms);
    147   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
    148   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
    149   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    150             PowerPolicyController::GetPolicyDebugString(
    151                 fake_power_client_->policy()));
    152 
    153   // Set the "allow screen wake locks" pref to false.  The system should be
    154   // prevented from suspending due to user inactivity on AC power but the
    155   // pref-supplied screen-related delays should be left untouched.
    156   prefs.allow_screen_wake_locks = false;
    157   policy_controller_->ApplyPrefs(prefs);
    158   policy_controller_->AddScreenWakeLock("Screen");
    159   expected_policy.set_ac_idle_action(
    160       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    161   expected_policy.set_reason("Prefs, Screen");
    162   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    163             PowerPolicyController::GetPolicyDebugString(
    164                 fake_power_client_->policy()));
    165 }
    166 
    167 TEST_F(PowerPolicyControllerTest, WakeLocks) {
    168   const char kSystemWakeLockReason[] = "system";
    169   const int system_id =
    170       policy_controller_->AddSystemWakeLock(kSystemWakeLockReason);
    171   power_manager::PowerManagementPolicy expected_policy;
    172   expected_policy.set_ac_idle_action(
    173       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    174   expected_policy.set_battery_idle_action(
    175       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
    176   expected_policy.set_reason(kSystemWakeLockReason);
    177   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    178             PowerPolicyController::GetPolicyDebugString(
    179                 fake_power_client_->policy()));
    180 
    181   const char kScreenWakeLockReason[] = "screen";
    182   const int screen_id = policy_controller_->AddScreenWakeLock(
    183       kScreenWakeLockReason);
    184   expected_policy.mutable_ac_delays()->set_screen_dim_ms(0);
    185   expected_policy.mutable_ac_delays()->set_screen_off_ms(0);
    186   expected_policy.mutable_ac_delays()->set_screen_lock_ms(0);
    187   expected_policy.mutable_battery_delays()->set_screen_dim_ms(0);
    188   expected_policy.mutable_battery_delays()->set_screen_off_ms(0);
    189   expected_policy.mutable_battery_delays()->set_screen_lock_ms(0);
    190   expected_policy.set_reason(
    191       std::string(kScreenWakeLockReason) + ", " + kSystemWakeLockReason);
    192   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    193             PowerPolicyController::GetPolicyDebugString(
    194                 fake_power_client_->policy()));
    195 
    196   policy_controller_->RemoveWakeLock(system_id);
    197   expected_policy.set_reason(kScreenWakeLockReason);
    198   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    199             PowerPolicyController::GetPolicyDebugString(
    200                 fake_power_client_->policy()));
    201 
    202   policy_controller_->RemoveWakeLock(screen_id);
    203   expected_policy.Clear();
    204   EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
    205             PowerPolicyController::GetPolicyDebugString(
    206                 fake_power_client_->policy()));
    207 }
    208 
    209 TEST_F(PowerPolicyControllerTest, AvoidSendingEmptyPolicies) {
    210   // Check that empty policies aren't sent when PowerPolicyController is created
    211   // or destroyed.
    212   EXPECT_EQ(0, fake_power_client_->num_set_policy_calls());
    213   policy_controller_.reset();
    214   EXPECT_EQ(0, fake_power_client_->num_set_policy_calls());
    215 }
    216 
    217 }  // namespace chromeos
    218