Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / dbus / power_policy_controller_unittest.cc
blob3352f21c1d62ac2ddffaeb34bc4f1678570d1b97
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"
12 namespace chromeos {
14 class PowerPolicyControllerTest : public testing::Test {
15 public:
16 PowerPolicyControllerTest()
17 : fake_power_client_(new FakePowerManagerClient) {}
19 ~PowerPolicyControllerTest() override {}
21 void SetUp() override {
22 PowerPolicyController::Initialize(fake_power_client_.get());
23 ASSERT_TRUE(PowerPolicyController::IsInitialized());
24 policy_controller_ = PowerPolicyController::Get();
27 void TearDown() override {
28 if (PowerPolicyController::IsInitialized())
29 PowerPolicyController::Shutdown();
32 protected:
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 prefs.force_nonzero_brightness_for_user_activity = false;
58 policy_controller_->ApplyPrefs(prefs);
60 power_manager::PowerManagementPolicy expected_policy;
61 expected_policy.mutable_ac_delays()->set_screen_dim_ms(600000);
62 expected_policy.mutable_ac_delays()->set_screen_off_ms(660000);
63 expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
64 expected_policy.mutable_ac_delays()->set_idle_warning_ms(-1);
65 expected_policy.mutable_ac_delays()->set_idle_ms(720000);
66 expected_policy.mutable_battery_delays()->set_screen_dim_ms(300000);
67 expected_policy.mutable_battery_delays()->set_screen_off_ms(360000);
68 expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
69 expected_policy.mutable_battery_delays()->set_idle_warning_ms(-1);
70 expected_policy.mutable_battery_delays()->set_idle_ms(420000);
71 expected_policy.set_ac_idle_action(
72 power_manager::PowerManagementPolicy_Action_SUSPEND);
73 expected_policy.set_battery_idle_action(
74 power_manager::PowerManagementPolicy_Action_STOP_SESSION);
75 expected_policy.set_lid_closed_action(
76 power_manager::PowerManagementPolicy_Action_SHUT_DOWN);
77 expected_policy.set_use_audio_activity(true);
78 expected_policy.set_use_video_activity(true);
79 expected_policy.set_ac_brightness_percent(87.0);
80 expected_policy.set_battery_brightness_percent(43.0);
81 expected_policy.set_presentation_screen_dim_delay_factor(3.0);
82 expected_policy.set_user_activity_screen_dim_delay_factor(2.0);
83 expected_policy.set_wait_for_initial_user_activity(true);
84 expected_policy.set_force_nonzero_brightness_for_user_activity(false);
85 expected_policy.set_reason(PowerPolicyController::kPrefsReason);
86 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
87 PowerPolicyController::GetPolicyDebugString(
88 fake_power_client_->policy()));
90 // Change some prefs and check that an updated policy is sent.
91 prefs.ac_idle_warning_delay_ms = 700000;
92 prefs.battery_idle_warning_delay_ms = 400000;
93 prefs.lid_closed_action = PowerPolicyController::ACTION_SUSPEND;
94 prefs.ac_brightness_percent = -1.0;
95 prefs.force_nonzero_brightness_for_user_activity = true;
96 policy_controller_->ApplyPrefs(prefs);
97 expected_policy.mutable_ac_delays()->set_idle_warning_ms(700000);
98 expected_policy.mutable_battery_delays()->set_idle_warning_ms(400000);
99 expected_policy.set_lid_closed_action(
100 power_manager::PowerManagementPolicy_Action_SUSPEND);
101 expected_policy.clear_ac_brightness_percent();
102 expected_policy.set_force_nonzero_brightness_for_user_activity(true);
103 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
104 PowerPolicyController::GetPolicyDebugString(
105 fake_power_client_->policy()));
107 // The enable-auto-screen-lock pref should force the screen-lock delays to
108 // match the screen-off delays plus a constant value.
109 prefs.enable_auto_screen_lock = true;
110 policy_controller_->ApplyPrefs(prefs);
111 expected_policy.mutable_ac_delays()->set_screen_lock_ms(
112 660000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
113 expected_policy.mutable_battery_delays()->set_screen_lock_ms(
114 360000 + PowerPolicyController::kScreenLockAfterOffDelayMs);
115 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
116 PowerPolicyController::GetPolicyDebugString(
117 fake_power_client_->policy()));
119 // If the screen-lock-delay prefs are set to lower values than the
120 // screen-off delays plus the constant, the lock prefs should take
121 // precedence.
122 prefs.ac_screen_lock_delay_ms = 70000;
123 prefs.battery_screen_lock_delay_ms = 60000;
124 policy_controller_->ApplyPrefs(prefs);
125 expected_policy.mutable_ac_delays()->set_screen_lock_ms(70000);
126 expected_policy.mutable_battery_delays()->set_screen_lock_ms(60000);
127 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
128 PowerPolicyController::GetPolicyDebugString(
129 fake_power_client_->policy()));
131 // If the artificial screen-lock delays would exceed the idle delay, they
132 // shouldn't be set -- the power manager would ignore them since the
133 // idle action should lock the screen in this case.
134 prefs.ac_screen_off_delay_ms = prefs.ac_idle_delay_ms - 1;
135 prefs.battery_screen_off_delay_ms = prefs.battery_idle_delay_ms - 1;
136 prefs.ac_screen_lock_delay_ms = -1;
137 prefs.battery_screen_lock_delay_ms = -1;
138 policy_controller_->ApplyPrefs(prefs);
139 expected_policy.mutable_ac_delays()->set_screen_off_ms(
140 prefs.ac_screen_off_delay_ms);
141 expected_policy.mutable_battery_delays()->set_screen_off_ms(
142 prefs.battery_screen_off_delay_ms);
143 expected_policy.mutable_ac_delays()->set_screen_lock_ms(-1);
144 expected_policy.mutable_battery_delays()->set_screen_lock_ms(-1);
145 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
146 PowerPolicyController::GetPolicyDebugString(
147 fake_power_client_->policy()));
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
151 // pref-supplied screen-related delays should be left untouched.
152 prefs.allow_screen_wake_locks = false;
153 policy_controller_->ApplyPrefs(prefs);
154 policy_controller_->AddScreenWakeLock(PowerPolicyController::REASON_OTHER,
155 "Screen");
156 expected_policy.set_ac_idle_action(
157 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
158 expected_policy.set_reason(std::string(PowerPolicyController::kPrefsReason) +
159 ", Screen");
160 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
161 PowerPolicyController::GetPolicyDebugString(
162 fake_power_client_->policy()));
165 TEST_F(PowerPolicyControllerTest, WakeLocks) {
166 const char kSystemWakeLockReason[] = "system";
167 const int system_id = policy_controller_->AddSystemWakeLock(
168 PowerPolicyController::REASON_OTHER, kSystemWakeLockReason);
169 power_manager::PowerManagementPolicy expected_policy;
170 expected_policy.set_ac_idle_action(
171 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
172 expected_policy.set_battery_idle_action(
173 power_manager::PowerManagementPolicy_Action_DO_NOTHING);
174 expected_policy.set_reason(kSystemWakeLockReason);
175 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
176 PowerPolicyController::GetPolicyDebugString(
177 fake_power_client_->policy()));
179 const char kScreenWakeLockReason[] = "screen";
180 const int screen_id = policy_controller_->AddScreenWakeLock(
181 PowerPolicyController::REASON_OTHER, kScreenWakeLockReason);
182 expected_policy.mutable_ac_delays()->set_screen_dim_ms(0);
183 expected_policy.mutable_ac_delays()->set_screen_off_ms(0);
184 expected_policy.mutable_ac_delays()->set_screen_lock_ms(0);
185 expected_policy.mutable_battery_delays()->set_screen_dim_ms(0);
186 expected_policy.mutable_battery_delays()->set_screen_off_ms(0);
187 expected_policy.mutable_battery_delays()->set_screen_lock_ms(0);
188 expected_policy.set_reason(std::string(kSystemWakeLockReason) + ", " +
189 kScreenWakeLockReason);
190 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
191 PowerPolicyController::GetPolicyDebugString(
192 fake_power_client_->policy()));
194 policy_controller_->RemoveWakeLock(system_id);
195 expected_policy.set_reason(kScreenWakeLockReason);
196 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
197 PowerPolicyController::GetPolicyDebugString(
198 fake_power_client_->policy()));
200 policy_controller_->RemoveWakeLock(screen_id);
201 expected_policy.Clear();
202 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(expected_policy),
203 PowerPolicyController::GetPolicyDebugString(
204 fake_power_client_->policy()));
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();
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);
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");
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()));
232 // Non-media screen wake locks should still be honored.
233 const int other_id = policy_controller_->AddScreenWakeLock(
234 PowerPolicyController::REASON_OTHER, "other");
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()));
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);
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()));
269 policy_controller_->RemoveWakeLock(audio_id);
270 policy_controller_->RemoveWakeLock(video_id);
273 TEST_F(PowerPolicyControllerTest, AvoidSendingEmptyPolicies) {
274 // Check that empty policies aren't sent when PowerPolicyController is created
275 // or destroyed.
276 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls());
277 PowerPolicyController::Shutdown();
278 EXPECT_EQ(0, fake_power_client_->num_set_policy_calls());
281 } // namespace chromeos