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