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