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 "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "chromeos/dbus/fake_power_manager_client.h" | 9 #include "chromeos/dbus/fake_power_manager_client.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 expected_policy.set_lid_closed_action( | 75 expected_policy.set_lid_closed_action( |
76 power_manager::PowerManagementPolicy_Action_SHUT_DOWN); | 76 power_manager::PowerManagementPolicy_Action_SHUT_DOWN); |
77 expected_policy.set_use_audio_activity(true); | 77 expected_policy.set_use_audio_activity(true); |
78 expected_policy.set_use_video_activity(true); | 78 expected_policy.set_use_video_activity(true); |
79 expected_policy.set_ac_brightness_percent(87.0); | 79 expected_policy.set_ac_brightness_percent(87.0); |
80 expected_policy.set_battery_brightness_percent(43.0); | 80 expected_policy.set_battery_brightness_percent(43.0); |
81 expected_policy.set_presentation_screen_dim_delay_factor(3.0); | 81 expected_policy.set_presentation_screen_dim_delay_factor(3.0); |
82 expected_policy.set_user_activity_screen_dim_delay_factor(2.0); | 82 expected_policy.set_user_activity_screen_dim_delay_factor(2.0); |
83 expected_policy.set_wait_for_initial_user_activity(true); | 83 expected_policy.set_wait_for_initial_user_activity(true); |
84 expected_policy.set_force_nonzero_brightness_for_user_activity(false); | 84 expected_policy.set_force_nonzero_brightness_for_user_activity(false); |
85 expected_policy.set_reason("Prefs"); | 85 expected_policy.set_reason(PowerPolicyController::kPrefsReason); |
86 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 86 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
87 PowerPolicyController::GetPolicyDebugString( | 87 PowerPolicyController::GetPolicyDebugString( |
88 fake_power_client_->policy())); | 88 fake_power_client_->policy())); |
89 | 89 |
90 // Change some prefs and check that an updated policy is sent. | 90 // Change some prefs and check that an updated policy is sent. |
91 prefs.ac_idle_warning_delay_ms = 700000; | 91 prefs.ac_idle_warning_delay_ms = 700000; |
92 prefs.battery_idle_warning_delay_ms = 400000; | 92 prefs.battery_idle_warning_delay_ms = 400000; |
93 prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND; | 93 prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND; |
94 prefs.ac_brightness_percent = -1.0; | 94 prefs.ac_brightness_percent = -1.0; |
95 prefs.force_nonzero_brightness_for_user_activity = true; | 95 prefs.force_nonzero_brightness_for_user_activity = true; |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1); | 144 expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1); |
145 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 145 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
146 PowerPolicyController::GetPolicyDebugString( | 146 PowerPolicyController::GetPolicyDebugString( |
147 fake_power_client_->policy())); | 147 fake_power_client_->policy())); |
148 | 148 |
149 // Set the "allow screen wake locks" pref to false. The system should be | 149 // Set the "allow screen wake locks" pref to false. The system should be |
150 // prevented from suspending due to user inactivity on AC power but the | 150 // prevented from suspending due to user inactivity on AC power but the |
151 // pref-supplied screen-related delays should be left untouched. | 151 // pref-supplied screen-related delays should be left untouched. |
152 prefs.allow_screen_wake_locks = false; | 152 prefs.allow_screen_wake_locks = false; |
153 policy_controller_->ApplyPrefs(prefs); | 153 policy_controller_->ApplyPrefs(prefs); |
154 policy_controller_->AddScreenWakeLock("Screen"); | 154 policy_controller_->AddScreenWakeLock(PowerPolicyController::REASON_OTHER, |
| 155 "Screen"); |
155 expected_policy.set_ac_idle_action( | 156 expected_policy.set_ac_idle_action( |
156 power_manager::PowerManagementPolicy_Action_DO_NOTHING); | 157 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
157 expected_policy.set_reason("Prefs, Screen"); | 158 expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) + |
| 159 ", Screen"); |
158 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 160 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
159 PowerPolicyController::GetPolicyDebugString( | 161 PowerPolicyController::GetPolicyDebugString( |
160 fake_power_client_->policy())); | 162 fake_power_client_->policy())); |
161 } | 163 } |
162 | 164 |
163 TEST_F(PowerPolicyControllerTest, WakeLocks) { | 165 TEST_F(PowerPolicyControllerTest, WakeLocks) { |
164 const char kSystemWakeLockReason[] = "system"; | 166 const char kSystemWakeLockReason[] = "system"; |
165 const int system_id = | 167 const int system_id = policy_controller_->AddSystemWakeLock( |
166 policy_controller_->AddSystemWakeLock(kSystemWakeLockReason); | 168 PowerPolicyController::REASON_OTHER, kSystemWakeLockReason); |
167 power_manager::PowerManagementPolicy expected_policy; | 169 power_manager::PowerManagementPolicy expected_policy; |
168 expected_policy.set_ac_idle_action( | 170 expected_policy.set_ac_idle_action( |
169 power_manager::PowerManagementPolicy_Action_DO_NOTHING); | 171 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
170 expected_policy.set_battery_idle_action( | 172 expected_policy.set_battery_idle_action( |
171 power_manager::PowerManagementPolicy_Action_DO_NOTHING); | 173 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
172 expected_policy.set_reason(kSystemWakeLockReason); | 174 expected_policy.set_reason(kSystemWakeLockReason); |
173 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 175 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
174 PowerPolicyController::GetPolicyDebugString( | 176 PowerPolicyController::GetPolicyDebugString( |
175 fake_power_client_->policy())); | 177 fake_power_client_->policy())); |
176 | 178 |
177 const char kScreenWakeLockReason[] = "screen"; | 179 const char kScreenWakeLockReason[] = "screen"; |
178 const int screen_id = policy_controller_->AddScreenWakeLock( | 180 const int screen_id = policy_controller_->AddScreenWakeLock( |
179 kScreenWakeLockReason); | 181 PowerPolicyController::REASON_OTHER, kScreenWakeLockReason); |
180 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0); | 182 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0); |
181 expected_policy.mutable_ac_delays()->set_screen_off_ms(0); | 183 expected_policy.mutable_ac_delays()->set_screen_off_ms(0); |
182 expected_policy.mutable_ac_delays()->set_screen_lock_ms(0); | 184 expected_policy.mutable_ac_delays()->set_screen_lock_ms(0); |
183 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0); | 185 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0); |
184 expected_policy.mutable_battery_delays()->set_screen_off_ms(0); | 186 expected_policy.mutable_battery_delays()->set_screen_off_ms(0); |
185 expected_policy.mutable_battery_delays()->set_screen_lock_ms(0); | 187 expected_policy.mutable_battery_delays()->set_screen_lock_ms(0); |
186 expected_policy.set_reason( | 188 expected_policy.set_reason(std::string(kSystemWakeLockReason) + ", " + |
187 std::string(kScreenWakeLockReason) + ", " + kSystemWakeLockReason); | 189 kScreenWakeLockReason); |
188 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 190 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
189 PowerPolicyController::GetPolicyDebugString( | 191 PowerPolicyController::GetPolicyDebugString( |
190 fake_power_client_->policy())); | 192 fake_power_client_->policy())); |
191 | 193 |
192 policy_controller_->RemoveWakeLock(system_id); | 194 policy_controller_->RemoveWakeLock(system_id); |
193 expected_policy.set_reason(kScreenWakeLockReason); | 195 expected_policy.set_reason(kScreenWakeLockReason); |
194 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 196 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
195 PowerPolicyController::GetPolicyDebugString( | 197 PowerPolicyController::GetPolicyDebugString( |
196 fake_power_client_->policy())); | 198 fake_power_client_->policy())); |
197 | 199 |
198 policy_controller_->RemoveWakeLock(screen_id); | 200 policy_controller_->RemoveWakeLock(screen_id); |
199 expected_policy.Clear(); | 201 expected_policy.Clear(); |
200 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), | 202 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
201 PowerPolicyController::GetPolicyDebugString( | 203 PowerPolicyController::GetPolicyDebugString( |
202 fake_power_client_->policy())); | 204 fake_power_client_->policy())); |
203 } | 205 } |
204 | 206 |
| 207 TEST_F(PowerPolicyControllerTest, IgnoreMediaWakeLocksWhenRequested) { |
| 208 PowerPolicyController::PrefValues prefs; |
| 209 policy_controller_->ApplyPrefs(prefs); |
| 210 const power_manager::PowerManagementPolicy kDefaultPolicy = |
| 211 fake_power_client_->policy(); |
| 212 |
| 213 // Wake locks created for audio or video playback should be ignored when the |
| 214 // |use_audio_activity| or |use_video_activity| prefs are unset. |
| 215 prefs.use_audio_activity = false; |
| 216 prefs.use_video_activity = false; |
| 217 policy_controller_->ApplyPrefs(prefs); |
| 218 |
| 219 const int audio_id = policy_controller_->AddSystemWakeLock( |
| 220 PowerPolicyController::REASON_AUDIO_PLAYBACK, "audio"); |
| 221 const int video_id = policy_controller_->AddScreenWakeLock( |
| 222 PowerPolicyController::REASON_VIDEO_PLAYBACK, "video"); |
| 223 |
| 224 power_manager::PowerManagementPolicy expected_policy = kDefaultPolicy; |
| 225 expected_policy.set_use_audio_activity(false); |
| 226 expected_policy.set_use_video_activity(false); |
| 227 expected_policy.set_reason(PowerPolicyController::kPrefsReason); |
| 228 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 229 PowerPolicyController::GetPolicyDebugString( |
| 230 fake_power_client_->policy())); |
| 231 |
| 232 // Non-media screen wake locks should still be honored. |
| 233 const int other_id = policy_controller_->AddScreenWakeLock( |
| 234 PowerPolicyController::REASON_OTHER, "other"); |
| 235 |
| 236 expected_policy.set_ac_idle_action( |
| 237 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
| 238 expected_policy.set_battery_idle_action( |
| 239 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
| 240 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0); |
| 241 expected_policy.mutable_ac_delays()->set_screen_off_ms(0); |
| 242 expected_policy.mutable_ac_delays()->set_screen_lock_ms(0); |
| 243 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0); |
| 244 expected_policy.mutable_battery_delays()->set_screen_off_ms(0); |
| 245 expected_policy.mutable_battery_delays()->set_screen_lock_ms(0); |
| 246 expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) + |
| 247 ", other"); |
| 248 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 249 PowerPolicyController::GetPolicyDebugString( |
| 250 fake_power_client_->policy())); |
| 251 |
| 252 // Start honoring audio activity and check that the audio wake lock is used. |
| 253 policy_controller_->RemoveWakeLock(other_id); |
| 254 prefs.use_audio_activity = true; |
| 255 policy_controller_->ApplyPrefs(prefs); |
| 256 |
| 257 expected_policy = kDefaultPolicy; |
| 258 expected_policy.set_use_video_activity(false); |
| 259 expected_policy.set_ac_idle_action( |
| 260 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
| 261 expected_policy.set_battery_idle_action( |
| 262 power_manager::PowerManagementPolicy_Action_DO_NOTHING); |
| 263 expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) + |
| 264 ", audio"); |
| 265 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy), |
| 266 PowerPolicyController::GetPolicyDebugString( |
| 267 fake_power_client_->policy())); |
| 268 |
| 269 policy_controller_->RemoveWakeLock(audio_id); |
| 270 policy_controller_->RemoveWakeLock(video_id); |
| 271 } |
| 272 |
205 TEST_F(PowerPolicyControllerTest, AvoidSendingEmptyPolicies) { | 273 TEST_F(PowerPolicyControllerTest, AvoidSendingEmptyPolicies) { |
206 // Check that empty policies aren't sent when PowerPolicyController is created | 274 // Check that empty policies aren't sent when PowerPolicyController is created |
207 // or destroyed. | 275 // or destroyed. |
208 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls()); | 276 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls()); |
209 PowerPolicyController::Shutdown(); | 277 PowerPolicyController::Shutdown(); |
210 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls()); | 278 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls()); |
211 } | 279 } |
212 | 280 |
213 } // namespace chromeos | 281 } // namespace chromeos |
OLD | NEW |