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/power_policy_controller.h"
6 
7 #include <memory>
8 
9 #include "base/run_loop.h"
10 #include "base/test/task_environment.h"
11 #include "chromeos/dbus/power/fake_power_manager_client.h"
12 #include "chromeos/dbus/power_manager/backlight.pb.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace chromeos {
16 
17 class PowerPolicyControllerTest : public testing::Test {
18  public:
19   PowerPolicyControllerTest() = default;
20   ~PowerPolicyControllerTest() override = default;
21 
SetUp()22   void SetUp() override {
23     PowerManagerClient::InitializeFake();
24     PowerPolicyController::Initialize(FakePowerManagerClient::Get());
25     ASSERT_TRUE(PowerPolicyController::IsInitialized());
26     policy_controller_ = PowerPolicyController::Get();
27   }
28 
TearDown()29   void TearDown() override {
30     if (PowerPolicyController::IsInitialized())
31       PowerPolicyController::Shutdown();
32     PowerManagerClient::Shutdown();
33   }
34 
35  protected:
power_manager()36   FakePowerManagerClient* power_manager() {
37     return FakePowerManagerClient::Get();
38   }
39 
40   PowerPolicyController* policy_controller_;
41   base::test::SingleThreadTaskEnvironment task_environment_;
42 
43  private:
44   DISALLOW_COPY_AND_ASSIGN(PowerPolicyControllerTest);
45 };
46 
TEST_F(PowerPolicyControllerTest,Prefs)47 TEST_F(PowerPolicyControllerTest, Prefs) {
48   PowerPolicyController::PrefValues prefs;
49   prefs.ac_screen_dim_delay_ms = 600000;
50   prefs.ac_screen_off_delay_ms = 660000;
51   prefs.ac_idle_delay_ms = 720000;
52   prefs.battery_screen_dim_delay_ms = 300000;
53   prefs.battery_screen_off_delay_ms = 360000;
54   prefs.battery_idle_delay_ms = 420000;
55   prefs.ac_idle_action = PowerPolicyController::ACTION_SUSPEND;
56   prefs.battery_idle_action = PowerPolicyController::ACTION_STOP_SESSION;
57   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
58   prefs.use_audio_activity = true;
59   prefs.use_video_activity = true;
60   prefs.ac_brightness_percent = 87.0;
61   prefs.battery_brightness_percent = 43.0;
62   prefs.enable_auto_screen_lock = false;
63   prefs.presentation_screen_dim_delay_factor = 3.0;
64   prefs.user_activity_screen_dim_delay_factor = 2.0;
65   prefs.wait_for_initial_user_activity = true;
66   prefs.force_nonzero_brightness_for_user_activity = false;
67   prefs.boot_on_ac = true;
68   prefs.usb_power_share = false;
69   policy_controller_->ApplyPrefs(prefs);
70 
71   power_manager::PowerManagementPolicy expected_policy;
72   expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000);
73   expected_policy.mutable_ac_delays()->set_screen_off_ms(660000);
74   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
75   expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1);
76   expected_policy.mutable_ac_delays()->set_idle_ms(720000);
77   expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000);
78   expected_policy.mutable_battery_delays()->set_screen_off_ms(360000);
79   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
80   expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1);
81   expected_policy.mutable_battery_delays()->set_idle_ms(420000);
82   expected_policy.set_ac_idle_action(
83       power_manager::PowerManagementPolicy_Action_SUSPEND);
84   expected_policy.set_battery_idle_action(
85       power_manager::PowerManagementPolicy_Action_STOP_SESSION);
86   expected_policy.set_lid_closed_action(
87       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
88   expected_policy.set_use_audio_activity(true);
89   expected_policy.set_use_video_activity(true);
90   expected_policy.set_ac_brightness_percent(87.0);
91   expected_policy.set_battery_brightness_percent(43.0);
92   expected_policy.set_presentation_screen_dim_delay_factor(3.0);
93   expected_policy.set_user_activity_screen_dim_delay_factor(2.0);
94   expected_policy.set_wait_for_initial_user_activity(true);
95   expected_policy.set_force_nonzero_brightness_for_user_activity(false);
96   expected_policy.set_boot_on_ac(true);
97   expected_policy.set_usb_power_share(false);
98   expected_policy.mutable_battery_charge_mode()->set_mode(
99       power_manager::PowerManagementPolicy::BatteryChargeMode::ADAPTIVE);
100 
101   expected_policy.set_reason(PowerPolicyController::kPrefsReason);
102   EXPECT_EQ(
103       PowerPolicyController::GetPolicyDebugString(expected_policy),
104       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
105 
106   // Change some prefs and check that an updated policy is sent.
107   prefs.ac_idle_warning_delay_ms = 700000;
108   prefs.battery_idle_warning_delay_ms = 400000;
109   prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND;
110   prefs.ac_brightness_percent = -1.0;
111   prefs.force_nonzero_brightness_for_user_activity = true;
112   policy_controller_->ApplyPrefs(prefs);
113   expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000);
114   expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000);
115   expected_policy.set_lid_closed_action(
116       power_manager::PowerManagementPolicy_Action_SUSPEND);
117   expected_policy.clear_ac_brightness_percent();
118   expected_policy.set_force_nonzero_brightness_for_user_activity(true);
119   EXPECT_EQ(
120       PowerPolicyController::GetPolicyDebugString(expected_policy),
121       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
122 
123   // The enable-auto-screen-lock pref should force the screen-lock delays to
124   // match the screen-off delays plus a constant value.
125   prefs.enable_auto_screen_lock = true;
126   policy_controller_->ApplyPrefs(prefs);
127   expected_policy.mutable_ac_delays()->set_screen_lock_ms(
128       660000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
129   expected_policy.mutable_battery_delays()->set_screen_lock_ms(
130       360000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
131   EXPECT_EQ(
132       PowerPolicyController::GetPolicyDebugString(expected_policy),
133       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
134 
135   // If the screen-lock-delay prefs are set to lower values than the
136   // screen-off delays plus the constant, the lock prefs should take
137   // precedence.
138   prefs.ac_screen_lock_delay_ms = 70000;
139   prefs.battery_screen_lock_delay_ms = 60000;
140   policy_controller_->ApplyPrefs(prefs);
141   expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000);
142   expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000);
143   EXPECT_EQ(
144       PowerPolicyController::GetPolicyDebugString(expected_policy),
145       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
146 
147   // If the artificial screen-lock delays would exceed the idle delay, they
148   // shouldn't be set -- the power manager would ignore them since the
149   // idle action should lock the screen in this case.
150   prefs.ac_screen_off_delay_ms = prefs.ac_idle_delay_ms - 1;
151   prefs.battery_screen_off_delay_ms = prefs.battery_idle_delay_ms - 1;
152   prefs.ac_screen_lock_delay_ms = -1;
153   prefs.battery_screen_lock_delay_ms = -1;
154   policy_controller_->ApplyPrefs(prefs);
155   expected_policy.mutable_ac_delays()->set_screen_off_ms(
156       prefs.ac_screen_off_delay_ms);
157   expected_policy.mutable_battery_delays()->set_screen_off_ms(
158       prefs.battery_screen_off_delay_ms);
159   expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
160   expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
161   EXPECT_EQ(
162       PowerPolicyController::GetPolicyDebugString(expected_policy),
163       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
164 
165   // Set the "allow screen wake locks" pref to false and add a screen wake lock.
166   // It should be downgraded to a system wake lock, and the pref-supplied delays
167   // should be left untouched.
168   prefs.allow_screen_wake_locks = false;
169   policy_controller_->ApplyPrefs(prefs);
170   policy_controller_->AddScreenWakeLock(PowerPolicyController::REASON_OTHER,
171                                         "Screen");
172   expected_policy.set_system_wake_lock(true);
173   expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) +
174                              ", Screen");
175   EXPECT_EQ(
176       PowerPolicyController::GetPolicyDebugString(expected_policy),
177       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
178 
179   // Set the "allow wake locks" pref to false and add a screen wake lock.
180   // It should be ignored.
181   prefs.allow_wake_locks = false;
182   policy_controller_->ApplyPrefs(prefs);
183   policy_controller_->AddScreenWakeLock(PowerPolicyController::REASON_OTHER,
184                                         "Screen");
185   expected_policy.clear_system_wake_lock();
186   expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason));
187   EXPECT_EQ(
188       PowerPolicyController::GetPolicyDebugString(expected_policy),
189       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
190 
191   // Set PeakShift prefs.
192   prefs.peak_shift_enabled = true;
193   prefs.peak_shift_battery_threshold = 20;
194 
195   power_manager::PowerManagementPolicy::PeakShiftDayConfig peak_shift_config;
196   peak_shift_config.set_day(power_manager::PowerManagementPolicy::TUESDAY);
197   peak_shift_config.mutable_start_time()->set_hour(10);
198   peak_shift_config.mutable_start_time()->set_minute(0);
199   peak_shift_config.mutable_end_time()->set_hour(20);
200   peak_shift_config.mutable_end_time()->set_minute(15);
201   peak_shift_config.mutable_charge_start_time()->set_hour(23);
202   peak_shift_config.mutable_charge_start_time()->set_minute(45);
203 
204   prefs.peak_shift_day_configs.push_back(peak_shift_config);
205   policy_controller_->ApplyPrefs(prefs);
206 
207   expected_policy.set_peak_shift_battery_percent_threshold(20);
208   *expected_policy.mutable_peak_shift_day_configs()->Add() = peak_shift_config;
209 
210   EXPECT_EQ(
211       PowerPolicyController::GetPolicyDebugString(expected_policy),
212       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
213 
214   // Set AdvancedBatteryChargeMode prefs.
215   prefs.advanced_battery_charge_mode_enabled = true;
216 
217   power_manager::PowerManagementPolicy::AdvancedBatteryChargeModeDayConfig
218       advanced_mode_config;
219   advanced_mode_config.set_day(power_manager::PowerManagementPolicy::FRIDAY);
220   advanced_mode_config.mutable_charge_start_time()->set_hour(10);
221   advanced_mode_config.mutable_charge_start_time()->set_minute(0);
222   advanced_mode_config.mutable_charge_end_time()->set_hour(23);
223   advanced_mode_config.mutable_charge_end_time()->set_minute(45);
224 
225   prefs.advanced_battery_charge_mode_day_configs.push_back(
226       advanced_mode_config);
227   policy_controller_->ApplyPrefs(prefs);
228 
229   *expected_policy.mutable_advanced_battery_charge_mode_day_configs()->Add() =
230       advanced_mode_config;
231 
232   EXPECT_EQ(
233       PowerPolicyController::GetPolicyDebugString(expected_policy),
234       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
235 
236   // Set BatteryChargeMode prefs.
237   prefs.battery_charge_mode =
238       power_manager::PowerManagementPolicy::BatteryChargeMode::PRIMARILY_AC_USE;
239 
240   policy_controller_->ApplyPrefs(prefs);
241 
242   expected_policy.mutable_battery_charge_mode()->set_mode(
243       power_manager::PowerManagementPolicy::BatteryChargeMode::
244           PRIMARILY_AC_USE);
245 
246   EXPECT_EQ(
247       PowerPolicyController::GetPolicyDebugString(expected_policy),
248       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
249 
250   // Set BatteryChargeMode prefs.
251   prefs.battery_charge_mode =
252       power_manager::PowerManagementPolicy::BatteryChargeMode::CUSTOM;
253   prefs.custom_charge_start = 51;
254   prefs.custom_charge_stop = 97;
255 
256   policy_controller_->ApplyPrefs(prefs);
257 
258   expected_policy.mutable_battery_charge_mode()->set_mode(
259       power_manager::PowerManagementPolicy::BatteryChargeMode::CUSTOM);
260   expected_policy.mutable_battery_charge_mode()->set_custom_charge_start(51);
261   expected_policy.mutable_battery_charge_mode()->set_custom_charge_stop(97);
262 
263   EXPECT_EQ(
264       PowerPolicyController::GetPolicyDebugString(expected_policy),
265       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
266 }
267 
TEST_F(PowerPolicyControllerTest,SystemWakeLock)268 TEST_F(PowerPolicyControllerTest, SystemWakeLock) {
269   policy_controller_->AddSystemWakeLock(PowerPolicyController::REASON_OTHER,
270                                         "1");
271   power_manager::PowerManagementPolicy expected_policy;
272   expected_policy.set_system_wake_lock(true);
273   expected_policy.set_reason("1");
274   EXPECT_EQ(
275       PowerPolicyController::GetPolicyDebugString(expected_policy),
276       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
277 }
278 
TEST_F(PowerPolicyControllerTest,DimWakeLock)279 TEST_F(PowerPolicyControllerTest, DimWakeLock) {
280   policy_controller_->AddDimWakeLock(PowerPolicyController::REASON_OTHER, "1");
281   power_manager::PowerManagementPolicy expected_policy;
282   expected_policy.set_dim_wake_lock(true);
283   expected_policy.set_reason("1");
284   EXPECT_EQ(
285       PowerPolicyController::GetPolicyDebugString(expected_policy),
286       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
287 }
288 
TEST_F(PowerPolicyControllerTest,ScreenWakeLock)289 TEST_F(PowerPolicyControllerTest, ScreenWakeLock) {
290   policy_controller_->AddScreenWakeLock(PowerPolicyController::REASON_OTHER,
291                                         "1");
292   power_manager::PowerManagementPolicy expected_policy;
293   expected_policy.set_screen_wake_lock(true);
294   expected_policy.set_reason("1");
295   EXPECT_EQ(
296       PowerPolicyController::GetPolicyDebugString(expected_policy),
297       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
298 }
299 
TEST_F(PowerPolicyControllerTest,IgnoreMediaWakeLocksWhenRequested)300 TEST_F(PowerPolicyControllerTest, IgnoreMediaWakeLocksWhenRequested) {
301   PowerPolicyController::PrefValues prefs;
302   policy_controller_->ApplyPrefs(prefs);
303   const power_manager::PowerManagementPolicy kDefaultPolicy =
304       power_manager()->policy();
305 
306   // Wake locks created for audio or video playback should be ignored when the
307   // |use_audio_activity| or |use_video_activity| prefs are unset.
308   prefs.use_audio_activity = false;
309   prefs.use_video_activity = false;
310   policy_controller_->ApplyPrefs(prefs);
311 
312   const int audio_id = policy_controller_->AddSystemWakeLock(
313       PowerPolicyController::REASON_AUDIO_PLAYBACK, "audio");
314   const int video_id = policy_controller_->AddScreenWakeLock(
315       PowerPolicyController::REASON_VIDEO_PLAYBACK, "video");
316 
317   power_manager::PowerManagementPolicy expected_policy = kDefaultPolicy;
318   expected_policy.set_use_audio_activity(false);
319   expected_policy.set_use_video_activity(false);
320   expected_policy.set_reason(PowerPolicyController::kPrefsReason);
321   EXPECT_EQ(
322       PowerPolicyController::GetPolicyDebugString(expected_policy),
323       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
324 
325   // Non-media screen wake locks should still be honored.
326   const int other_id = policy_controller_->AddScreenWakeLock(
327       PowerPolicyController::REASON_OTHER, "other");
328 
329   expected_policy.set_screen_wake_lock(true);
330   expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) +
331                              ", other");
332   EXPECT_EQ(
333       PowerPolicyController::GetPolicyDebugString(expected_policy),
334       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
335 
336   // Start honoring audio activity and check that the audio wake lock is used.
337   policy_controller_->RemoveWakeLock(other_id);
338   prefs.use_audio_activity = true;
339   policy_controller_->ApplyPrefs(prefs);
340 
341   expected_policy = kDefaultPolicy;
342   expected_policy.set_use_video_activity(false);
343   expected_policy.set_system_wake_lock(true);
344   expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) +
345                              ", audio");
346   EXPECT_EQ(
347       PowerPolicyController::GetPolicyDebugString(expected_policy),
348       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
349 
350   // Now honor video activity as well.
351   prefs.use_video_activity = true;
352   policy_controller_->ApplyPrefs(prefs);
353 
354   expected_policy = kDefaultPolicy;
355   expected_policy.set_screen_wake_lock(true);
356   expected_policy.set_system_wake_lock(true);
357   expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) +
358                              ", audio, video");
359   EXPECT_EQ(
360       PowerPolicyController::GetPolicyDebugString(expected_policy),
361       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
362 
363   policy_controller_->RemoveWakeLock(audio_id);
364   policy_controller_->RemoveWakeLock(video_id);
365 }
366 
TEST_F(PowerPolicyControllerTest,AvoidSendingEmptyPolicies)367 TEST_F(PowerPolicyControllerTest, AvoidSendingEmptyPolicies) {
368   // Check that empty policies aren't sent when PowerPolicyController is created
369   // or destroyed.
370   EXPECT_EQ(0, power_manager()->num_set_policy_calls());
371   PowerPolicyController::Shutdown();
372   EXPECT_EQ(0, power_manager()->num_set_policy_calls());
373 }
374 
TEST_F(PowerPolicyControllerTest,DoNothingOnLidClosedWhileSigningOut)375 TEST_F(PowerPolicyControllerTest, DoNothingOnLidClosedWhileSigningOut) {
376   PowerPolicyController::PrefValues prefs;
377   policy_controller_->ApplyPrefs(prefs);
378   const power_manager::PowerManagementPolicy kDefaultPolicy =
379       power_manager()->policy();
380 
381   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
382   policy_controller_->ApplyPrefs(prefs);
383 
384   power_manager::PowerManagementPolicy expected_policy;
385   expected_policy = kDefaultPolicy;
386   expected_policy.set_lid_closed_action(
387       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
388   // Sanity check.
389   EXPECT_EQ(
390       PowerPolicyController::GetPolicyDebugString(expected_policy),
391       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
392 
393   policy_controller_->NotifyChromeIsExiting();
394 
395   expected_policy.set_lid_closed_action(
396       power_manager::PowerManagementPolicy_Action_DO_NOTHING);
397   // Lid-closed action successfully changed to "do nothing".
398   EXPECT_EQ(
399       PowerPolicyController::GetPolicyDebugString(expected_policy),
400       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
401 }
402 
TEST_F(PowerPolicyControllerTest,SuspendOnLidClosedWhileSignedOut)403 TEST_F(PowerPolicyControllerTest, SuspendOnLidClosedWhileSignedOut) {
404   PowerPolicyController::PrefValues prefs;
405   policy_controller_->ApplyPrefs(prefs);
406   const power_manager::PowerManagementPolicy kDefaultPolicy =
407       power_manager()->policy();
408 
409   prefs.lid_closed_action = PowerPolicyController::ACTION_SHUT_DOWN;
410   policy_controller_->ApplyPrefs(prefs);
411 
412   power_manager::PowerManagementPolicy expected_policy;
413   expected_policy = kDefaultPolicy;
414   expected_policy.set_lid_closed_action(
415       power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
416   // Sanity check
417   EXPECT_EQ(
418       PowerPolicyController::GetPolicyDebugString(expected_policy),
419       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
420 
421   policy_controller_->SetEncryptionMigrationActive(true);
422   expected_policy.set_lid_closed_action(
423       power_manager::PowerManagementPolicy_Action_SUSPEND);
424   expected_policy.set_reason("Prefs, encryption migration");
425   // Lid-closed action successfully changed to "suspend".
426   EXPECT_EQ(
427       PowerPolicyController::GetPolicyDebugString(expected_policy),
428       PowerPolicyController::GetPolicyDebugString(power_manager()->policy()));
429 }
430 
TEST_F(PowerPolicyControllerTest,PerSessionScreenBrightnessOverride)431 TEST_F(PowerPolicyControllerTest, PerSessionScreenBrightnessOverride) {
432   const double kAcBrightness = 99.0;
433   const double kBatteryBrightness = 77.0;
434 
435   PowerPolicyController::PrefValues prefs;
436   prefs.ac_brightness_percent = kAcBrightness;
437   prefs.battery_brightness_percent = kBatteryBrightness;
438   policy_controller_->ApplyPrefs(prefs);
439 
440   EXPECT_EQ(kAcBrightness, power_manager()->policy().ac_brightness_percent());
441   EXPECT_EQ(kBatteryBrightness,
442             power_manager()->policy().battery_brightness_percent());
443 
444   // Simulate model triggered brightness change - shouldn't override the policy.
445   power_manager::SetBacklightBrightnessRequest request;
446   request.set_percent(80.0);
447   request.set_cause(power_manager::SetBacklightBrightnessRequest_Cause_MODEL);
448   power_manager()->SetScreenBrightness(request);
449   base::RunLoop().RunUntilIdle();
450   policy_controller_->ApplyPrefs(prefs);
451 
452   EXPECT_EQ(kAcBrightness, power_manager()->policy().ac_brightness_percent());
453   EXPECT_EQ(kBatteryBrightness,
454             power_manager()->policy().battery_brightness_percent());
455 
456   // Simulate user triggered brightness change - should override the policy.
457   request.set_percent(80.0);
458   request.set_cause(
459       power_manager::SetBacklightBrightnessRequest_Cause_USER_REQUEST);
460   power_manager()->SetScreenBrightness(request);
461   base::RunLoop().RunUntilIdle();
462   policy_controller_->ApplyPrefs(prefs);
463 
464   EXPECT_FALSE(power_manager()->policy().has_ac_brightness_percent());
465   EXPECT_FALSE(power_manager()->policy().has_battery_brightness_percent());
466 
467   // Simulate policy values update that should be ignored.
468   prefs.ac_brightness_percent = 98.0;
469   prefs.battery_brightness_percent = 76.0;
470   policy_controller_->ApplyPrefs(prefs);
471 
472   EXPECT_FALSE(power_manager()->policy().has_ac_brightness_percent());
473   EXPECT_FALSE(power_manager()->policy().has_battery_brightness_percent());
474 }
475 
TEST_F(PowerPolicyControllerTest,PolicyAutoScreenLockDelay)476 TEST_F(PowerPolicyControllerTest, PolicyAutoScreenLockDelay) {
477   PowerPolicyController::PrefValues prefs;
478   policy_controller_->ApplyPrefs(prefs);
479 
480   // Autolock disabled.
481   prefs.ac_screen_lock_delay_ms = 4000;
482   prefs.battery_screen_lock_delay_ms = 1000;
483   prefs.enable_auto_screen_lock = false;
484   policy_controller_->ApplyPrefs(prefs);
485   EXPECT_EQ(base::TimeDelta(),
486             policy_controller_->Get()->GetMaxPolicyAutoScreenLockDelay());
487 
488   // Autolock enabled.
489 
490   // Longer AC delay.
491   prefs.enable_auto_screen_lock = true;
492   policy_controller_->ApplyPrefs(prefs);
493   EXPECT_EQ(base::TimeDelta::FromMilliseconds(prefs.ac_screen_lock_delay_ms),
494             policy_controller_->Get()->GetMaxPolicyAutoScreenLockDelay());
495 
496   // Longer battery delay.
497   prefs.ac_screen_lock_delay_ms = 1000;
498   prefs.battery_screen_lock_delay_ms = 4000;
499   policy_controller_->ApplyPrefs(prefs);
500   EXPECT_EQ(
501       base::TimeDelta::FromMilliseconds(prefs.battery_screen_lock_delay_ms),
502       policy_controller_->Get()->GetMaxPolicyAutoScreenLockDelay());
503 }
504 
TEST_F(PowerPolicyControllerTest,FastSuspendWhenBacklightsForcedOff)505 TEST_F(PowerPolicyControllerTest, FastSuspendWhenBacklightsForcedOff) {
506   const int kAcDimMs = 600000;            // 10m
507   const int kAcOffMs = 620000;            // 10m20s
508   const int kAcLockMs = 610000;           // 10m10s
509   const int kAcIdleWarnMs = 650000;       // 10m50s
510   const int kAcIdleMs = 660000;           // 11m
511   const int kBatteryDimMs = 300000;       // 5m
512   const int kBatteryOffMs = 310000;       // 5m10s
513   const int kBatteryLockMs = 320000;      // 5m20s
514   const int kBatteryIdleWarnMs = 355000;  // 5m55s
515   const int kBatteryIdleMs = 360000;      // 6m
516 
517   PowerPolicyController::PrefValues prefs;
518   prefs.ac_screen_dim_delay_ms = kAcDimMs;
519   prefs.ac_screen_off_delay_ms = kAcOffMs;
520   prefs.ac_screen_lock_delay_ms = kAcLockMs;
521   prefs.ac_idle_warning_delay_ms = kAcIdleWarnMs;
522   prefs.ac_idle_delay_ms = kAcIdleMs;
523   prefs.battery_screen_dim_delay_ms = kBatteryDimMs;
524   prefs.battery_screen_off_delay_ms = kBatteryOffMs;
525   prefs.battery_screen_lock_delay_ms = kBatteryLockMs;
526   prefs.battery_idle_warning_delay_ms = kBatteryIdleWarnMs;
527   prefs.battery_idle_delay_ms = kBatteryIdleMs;
528   prefs.ac_idle_action = PowerPolicyController::ACTION_SUSPEND;
529   prefs.battery_idle_action = PowerPolicyController::ACTION_SUSPEND;
530   prefs.fast_suspend_when_backlights_forced_off = true;
531   policy_controller_->ApplyPrefs(prefs);
532 
533   // We should start out with the delays specified by the prefs.
534   power_manager::PowerManagementPolicy policy = power_manager()->policy();
535   EXPECT_EQ(kAcDimMs, policy.ac_delays().screen_dim_ms());
536   EXPECT_EQ(kAcOffMs, policy.ac_delays().screen_off_ms());
537   EXPECT_EQ(kAcLockMs, policy.ac_delays().screen_lock_ms());
538   EXPECT_EQ(kAcIdleWarnMs, policy.ac_delays().idle_warning_ms());
539   EXPECT_EQ(kAcIdleMs, policy.ac_delays().idle_ms());
540   EXPECT_EQ(kBatteryDimMs, policy.battery_delays().screen_dim_ms());
541   EXPECT_EQ(kBatteryOffMs, policy.battery_delays().screen_off_ms());
542   EXPECT_EQ(kBatteryLockMs, policy.battery_delays().screen_lock_ms());
543   EXPECT_EQ(kBatteryIdleWarnMs, policy.battery_delays().idle_warning_ms());
544   EXPECT_EQ(kBatteryIdleMs, policy.battery_delays().idle_ms());
545 
546   // After reporting that the backlights were forced off for a power button
547   // press, the idle and idle-warning delays should be shortened and other
548   // delays should be cleared.
549   policy_controller_->HandleBacklightsForcedOffForPowerButton(true);
550   policy = power_manager()->policy();
551   EXPECT_EQ(0, policy.ac_delays().screen_dim_ms());
552   EXPECT_EQ(0, policy.ac_delays().screen_off_ms());
553   EXPECT_EQ(0, policy.ac_delays().screen_lock_ms());
554   EXPECT_EQ(kAcIdleWarnMs - kAcOffMs, policy.ac_delays().idle_warning_ms());
555   EXPECT_EQ(kAcIdleMs - kAcOffMs, policy.ac_delays().idle_ms());
556   EXPECT_EQ(0, policy.battery_delays().screen_dim_ms());
557   EXPECT_EQ(0, policy.battery_delays().screen_off_ms());
558   EXPECT_EQ(0, policy.battery_delays().screen_lock_ms());
559   EXPECT_EQ(kBatteryIdleWarnMs - kBatteryOffMs,
560             policy.battery_delays().idle_warning_ms());
561   EXPECT_EQ(kBatteryIdleMs - kBatteryOffMs, policy.battery_delays().idle_ms());
562 
563   // If the screen-off delay is equal to the idle delay and longer than the
564   // idle-warning delay, both the idle and idle-warning delays should be set to
565   // 1 (the minimum delay allowed by powerd).
566   prefs.ac_screen_off_delay_ms = kAcIdleMs;
567   policy_controller_->ApplyPrefs(prefs);
568   policy = power_manager()->policy();
569   EXPECT_EQ(0, policy.ac_delays().screen_dim_ms());
570   EXPECT_EQ(0, policy.ac_delays().screen_off_ms());
571   EXPECT_EQ(0, policy.ac_delays().screen_lock_ms());
572   EXPECT_EQ(1, policy.ac_delays().idle_warning_ms());
573   EXPECT_EQ(1, policy.ac_delays().idle_ms());
574 }
575 
576 }  // namespace chromeos
577