Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(387)

Side by Side Diff: chrome/browser/ui/webui/settings/chromeos/device_power_handler_unittest.cc

Issue 2853113004: chromeos: Add settings to control power management prefs. (Closed)
Patch Set: switch back from aria-labelledby to aria-label Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "chrome/browser/ui/webui/settings/chromeos/device_power_handler.h"
6
7 #include <memory>
8
9 #include "ash/system/power/power_status.h"
10 #include "base/json/json_writer.h"
11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h"
13 #include "base/run_loop.h"
14 #include "base/test/scoped_task_environment.h"
15 #include "base/values.h"
16 #include "chrome/browser/chromeos/power/power_prefs.h"
17 #include "chrome/common/pref_names.h"
18 #include "chromeos/dbus/dbus_thread_manager.h"
19 #include "chromeos/dbus/fake_power_manager_client.h"
20 #include "chromeos/dbus/power_policy_controller.h"
21 #include "components/sync_preferences/testing_pref_service_syncable.h"
22 #include "content/public/test/test_web_ui.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace chromeos {
26 namespace settings {
27
28 class TestPowerHandler : public PowerHandler {
29 public:
30 explicit TestPowerHandler(PrefService* prefs) : PowerHandler(prefs) {}
31
32 // Pull WebUIMessageHandler::set_web_ui() into public so tests can call it.
33 using PowerHandler::set_web_ui;
34 };
35
36 class PowerHandlerTest : public testing::Test {
37 public:
38 PowerHandlerTest() {
39 // This initializes chromeos::DBusThreadManager.
40 std::unique_ptr<chromeos::DBusThreadManagerSetter> dbus_setter =
41 chromeos::DBusThreadManager::GetSetterForTesting();
42 dbus_setter->SetPowerManagerClient(
43 base::MakeUnique<chromeos::FakePowerManagerClient>());
44 power_manager_client_ = static_cast<chromeos::FakePowerManagerClient*>(
45 chromeos::DBusThreadManager::Get()->GetPowerManagerClient());
46 ash::PowerStatus::Initialize();
47
48 chromeos::PowerPrefs::RegisterUserProfilePrefs(prefs_.registry());
49
50 handler_ = base::MakeUnique<TestPowerHandler>(&prefs_);
51 test_api_ = base::MakeUnique<PowerHandler::TestAPI>(handler_.get());
52 handler_->set_web_ui(&web_ui_);
53 handler_->RegisterMessages();
54 handler_->AllowJavascriptForTesting();
55 base::RunLoop().RunUntilIdle();
56 }
57
58 ~PowerHandlerTest() override {
59 handler_.reset();
60 ash::PowerStatus::Shutdown();
61 chromeos::DBusThreadManager::Shutdown();
62 }
63
64 protected:
65 // Returns a JSON representation of the contents of the last message sent to
66 // WebUI about settings being changed.
67 std::string GetLastSettingsChangedMessage() WARN_UNUSED_RESULT {
68 for (auto it = web_ui_.call_data().rbegin();
69 it != web_ui_.call_data().rend(); ++it) {
70 const content::TestWebUI::CallData* data = it->get();
71 std::string name;
72 const base::DictionaryValue* dict = nullptr;
73 data->arg1()->GetAsString(&name);
74 if (data->function_name() != "cr.webUIListenerCallback" ||
75 !data->arg1()->GetAsString(&name) ||
76 name != PowerHandler::kPowerManagementSettingsChangedName) {
77 continue;
78 }
79 if (!data->arg2()->GetAsDictionary(&dict)) {
80 ADD_FAILURE() << "Failed to get dict from " << name << " message";
81 continue;
82 }
83 std::string out;
84 EXPECT_TRUE(base::JSONWriter::Write(*dict, &out));
85 return out;
86 }
87
88 ADD_FAILURE() << PowerHandler::kPowerManagementSettingsChangedName
89 << " message was not sent";
90 return std::string();
91 }
92
93 // Returns a string for the given settings that can be compared against the
94 // output of GetLastSettingsChangedMessage().
95 std::string CreateSettingsChangedString(
96 PowerHandler::IdleBehavior idle_behavior,
97 bool idle_controlled,
98 PowerPolicyController::Action lid_closed_behavior,
99 bool lid_closed_controlled,
100 bool has_lid) {
101 base::DictionaryValue dict;
102 dict.SetInteger(PowerHandler::kIdleBehaviorKey,
103 static_cast<int>(idle_behavior));
104 dict.SetBoolean(PowerHandler::kIdleControlledKey, idle_controlled);
105 dict.SetInteger(PowerHandler::kLidClosedBehaviorKey, lid_closed_behavior);
106 dict.SetBoolean(PowerHandler::kLidClosedControlledKey,
107 lid_closed_controlled);
108 dict.SetBoolean(PowerHandler::kHasLidKey, has_lid);
109
110 std::string out;
111 EXPECT_TRUE(base::JSONWriter::Write(dict, &out));
112 return out;
113 }
114
115 // Returns the user-set value of the integer pref identified by |name| or -1
116 // if the pref is unset.
117 int GetIntPref(const std::string& name) {
118 const base::Value* value = prefs_.GetUserPref(name);
119 return value ? value->GetInt() : -1;
120 }
121
122 base::test::ScopedTaskEnvironment scoped_task_environment_;
123 sync_preferences::TestingPrefServiceSyncable prefs_;
124 content::TestWebUI web_ui_;
125
126 // Owned by chromeos::DBusThreadManager.
127 chromeos::FakePowerManagerClient* power_manager_client_;
128
129 std::unique_ptr<TestPowerHandler> handler_;
130 std::unique_ptr<TestPowerHandler::TestAPI> test_api_;
131
132 private:
133 DISALLOW_COPY_AND_ASSIGN(PowerHandlerTest);
134 };
135
136 // Verifies that settings are sent to WebUI when requested.
137 TEST_F(PowerHandlerTest, SendInitialSettings) {
138 test_api_->RequestPowerManagementSettings();
139 EXPECT_EQ(
140 CreateSettingsChangedString(
141 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
142 false /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
143 false /* lid_closed_controlled */, true /* has_lid */),
144 GetLastSettingsChangedMessage());
145 }
146
147 // Verifies that WebUI receives updated settings when the lid state changes.
148 TEST_F(PowerHandlerTest, SendSettingsForLidStateChanges) {
149 power_manager_client_->SetLidState(PowerManagerClient::LidState::NOT_PRESENT,
150 base::TimeTicks());
151 EXPECT_EQ(
152 CreateSettingsChangedString(
153 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
154 false /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
155 false /* lid_closed_controlled */, false /* has_lid */),
156 GetLastSettingsChangedMessage());
157
158 power_manager_client_->SetLidState(PowerManagerClient::LidState::OPEN,
159 base::TimeTicks());
160 EXPECT_EQ(
161 CreateSettingsChangedString(
162 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
163 false /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
164 false /* lid_closed_controlled */, true /* has_lid */),
165 GetLastSettingsChangedMessage());
166 }
167
168 // Verifies that when various prefs are controlled, the corresponding settings
169 // are reported as controlled to WebUI.
170 TEST_F(PowerHandlerTest, SendSettingsForControlledPrefs) {
171 // Making an arbitrary delay pref managed should result in the idle setting
172 // being reported as controlled.
173 prefs_.SetManagedPref(prefs::kPowerAcScreenDimDelayMs,
174 base::MakeUnique<base::Value>(10000));
175 EXPECT_EQ(
176 CreateSettingsChangedString(
177 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
178 true /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
179 false /* lid_closed_controlled */, true /* has_lid */),
180 GetLastSettingsChangedMessage());
181
182 // Ditto for making the lid action pref managed.
183 prefs_.SetManagedPref(
184 prefs::kPowerLidClosedAction,
185 base::MakeUnique<base::Value>(PowerPolicyController::ACTION_SUSPEND));
186 EXPECT_EQ(
187 CreateSettingsChangedString(
188 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
189 true /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
190 true /* lid_closed_controlled */, true /* has_lid */),
191 GetLastSettingsChangedMessage());
192 }
193
194 // Verifies that idle-related prefs are distilled into the proper WebUI
195 // settings.
196 TEST_F(PowerHandlerTest, SendIdleSettingForPrefChanges) {
197 // Set a do-nothing idle action and a nonzero screen-off delay.
198 prefs_.SetUserPref(
199 prefs::kPowerAcIdleAction,
200 base::MakeUnique<base::Value>(PowerPolicyController::ACTION_DO_NOTHING));
201 prefs_.SetUserPref(prefs::kPowerAcScreenOffDelayMs,
202 base::MakeUnique<base::Value>(10000));
203 EXPECT_EQ(
204 CreateSettingsChangedString(
205 PowerHandler::IdleBehavior::DISPLAY_OFF_STAY_AWAKE,
206 false /* idle_controlled */, PowerPolicyController::ACTION_SUSPEND,
207 false /* lid_closed_controlled */, true /* has_lid */),
208 GetLastSettingsChangedMessage());
209
210 // Now set the delay to zero and check that the setting goes to "display on".
211 prefs_.SetUserPref(prefs::kPowerAcScreenOffDelayMs,
212 base::MakeUnique<base::Value>(0));
213 EXPECT_EQ(CreateSettingsChangedString(PowerHandler::IdleBehavior::DISPLAY_ON,
214 false /* idle_controlled */,
215 PowerPolicyController::ACTION_SUSPEND,
216 false /* lid_closed_controlled */,
217 true /* has_lid */),
218 GetLastSettingsChangedMessage());
219
220 // Other idle actions should result in an "other" setting.
221 prefs_.SetUserPref(prefs::kPowerAcIdleAction,
222 base::MakeUnique<base::Value>(
223 PowerPolicyController::ACTION_STOP_SESSION));
224 EXPECT_EQ(CreateSettingsChangedString(
225 PowerHandler::IdleBehavior::OTHER, false /* idle_controlled */,
226 PowerPolicyController::ACTION_SUSPEND,
227 false /* lid_closed_controlled */, true /* has_lid */),
228 GetLastSettingsChangedMessage());
229 }
230
231 // Verifies that the lid-closed pref's value is sent directly to WebUI.
232 TEST_F(PowerHandlerTest, SendLidSettingForPrefChanges) {
233 prefs_.SetUserPref(
234 prefs::kPowerLidClosedAction,
235 base::MakeUnique<base::Value>(PowerPolicyController::ACTION_SHUT_DOWN));
236 EXPECT_EQ(
237 CreateSettingsChangedString(
238 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
239 false /* idle_controlled */, PowerPolicyController::ACTION_SHUT_DOWN,
240 false /* lid_closed_controlled */, true /* has_lid */),
241 GetLastSettingsChangedMessage());
242
243 prefs_.SetUserPref(prefs::kPowerLidClosedAction,
244 base::MakeUnique<base::Value>(
245 PowerPolicyController::ACTION_STOP_SESSION));
246 EXPECT_EQ(CreateSettingsChangedString(
247 PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP,
248 false /* idle_controlled */,
249 PowerPolicyController::ACTION_STOP_SESSION,
250 false /* lid_closed_controlled */, true /* has_lid */),
251 GetLastSettingsChangedMessage());
252 }
253
254 // Verifies that requests from WebUI to update the idle behavior update prefs
255 // appropriately.
256 TEST_F(PowerHandlerTest, SetIdleBehavior) {
257 // Request the "display on" setting and check that prefs are set
258 // appropriately.
259 test_api_->SetIdleBehavior(PowerHandler::IdleBehavior::DISPLAY_ON);
260 EXPECT_EQ(PowerPolicyController::ACTION_DO_NOTHING,
261 GetIntPref(prefs::kPowerAcIdleAction));
262 EXPECT_EQ(0, GetIntPref(prefs::kPowerAcScreenDimDelayMs));
263 EXPECT_EQ(0, GetIntPref(prefs::kPowerAcScreenOffDelayMs));
264 EXPECT_EQ(0, GetIntPref(prefs::kPowerAcScreenLockDelayMs));
265 EXPECT_EQ(PowerPolicyController::ACTION_DO_NOTHING,
266 GetIntPref(prefs::kPowerBatteryIdleAction));
267 EXPECT_EQ(0, GetIntPref(prefs::kPowerBatteryScreenDimDelayMs));
268 EXPECT_EQ(0, GetIntPref(prefs::kPowerBatteryScreenOffDelayMs));
269 EXPECT_EQ(0, GetIntPref(prefs::kPowerBatteryScreenLockDelayMs));
270
271 // "Turn the display off but stay awake" should set the idle prefs but clear
272 // the screen delays.
273 test_api_->SetIdleBehavior(
274 PowerHandler::IdleBehavior::DISPLAY_OFF_STAY_AWAKE);
275 EXPECT_EQ(PowerPolicyController::ACTION_DO_NOTHING,
276 GetIntPref(prefs::kPowerAcIdleAction));
277 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenDimDelayMs));
278 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenOffDelayMs));
279 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenLockDelayMs));
280 EXPECT_EQ(PowerPolicyController::ACTION_DO_NOTHING,
281 GetIntPref(prefs::kPowerBatteryIdleAction));
282 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenDimDelayMs));
283 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenOffDelayMs));
284 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenLockDelayMs));
285
286 // Now switch to the "display on" setting (to set the prefs again) and check
287 // that the "sleep" setting clears all the prefs.
288 test_api_->SetIdleBehavior(PowerHandler::IdleBehavior::DISPLAY_ON);
289 test_api_->SetIdleBehavior(PowerHandler::IdleBehavior::DISPLAY_OFF_SLEEP);
290 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcIdleAction));
291 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenDimDelayMs));
292 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenOffDelayMs));
293 EXPECT_EQ(-1, GetIntPref(prefs::kPowerAcScreenLockDelayMs));
294 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryIdleAction));
295 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenDimDelayMs));
296 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenOffDelayMs));
297 EXPECT_EQ(-1, GetIntPref(prefs::kPowerBatteryScreenLockDelayMs));
298 }
299
300 // Verifies that requests from WebUI to change the lid behavior update the pref.
301 TEST_F(PowerHandlerTest, SetLidBehavior) {
302 // The "do nothing" setting should update the pref.
303 test_api_->SetLidClosedBehavior(PowerPolicyController::ACTION_DO_NOTHING);
304 EXPECT_EQ(PowerPolicyController::ACTION_DO_NOTHING,
305 GetIntPref(prefs::kPowerLidClosedAction));
306
307 // Selecting the "suspend" setting should just clear the pref.
308 test_api_->SetLidClosedBehavior(PowerPolicyController::ACTION_SUSPEND);
309 EXPECT_EQ(-1, GetIntPref(prefs::kPowerLidClosedAction));
310 }
311
312 } // namespace settings
313 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698