1 // Copyright 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 "chrome/browser/chromeos/power/power_prefs.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/prefs/pref_service.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
16 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 #include "chrome/browser/extensions/extension_special_storage_policy.h"
18 #include "chrome/browser/prefs/browser_prefs.h"
19 #include "chrome/browser/prefs/pref_service_syncable.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_pref_service_syncable.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "chrome/test/base/testing_profile_manager.h"
28 #include "chromeos/dbus/fake_power_manager_client.h"
29 #include "chromeos/dbus/power_manager/policy.pb.h"
30 #include "chromeos/dbus/power_policy_controller.h"
31 #include "components/pref_registry/pref_registry_syncable.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_service.h"
34 #include "content/public/browser/notification_source.h"
35 #include "testing/gtest/include/gtest/gtest.h"
39 class PowerPrefsTest
: public testing::Test
{
41 // Screen lock state that determines which delays are used by
42 // GetExpectedPowerPolicyForProfile().
43 enum ScreenLockState
{
51 void SetUp() override
;
52 void TearDown() override
;
54 const Profile
* GetProfile() const;
56 std::string
GetExpectedPowerPolicyForProfile(
58 ScreenLockState screen_lock_state
) const;
59 std::string
GetCurrentPowerPolicy() const;
60 bool GetExpectedAllowScreenWakeLocksForProfile(Profile
* profile
) const;
61 bool GetCurrentAllowScreenWakeLocks() const;
63 TestingProfileManager profile_manager_
;
64 PowerPolicyController
* power_policy_controller_
; // Not owned.
65 scoped_ptr
<FakePowerManagerClient
> fake_power_manager_client_
;
67 scoped_ptr
<PowerPrefs
> power_prefs_
;
69 DISALLOW_COPY_AND_ASSIGN(PowerPrefsTest
);
72 PowerPrefsTest::PowerPrefsTest()
73 : profile_manager_(TestingBrowserProcess::GetGlobal()),
74 power_policy_controller_(NULL
),
75 fake_power_manager_client_(new FakePowerManagerClient
) {
78 void PowerPrefsTest::SetUp() {
79 testing::Test::SetUp();
81 PowerPolicyController::Initialize(fake_power_manager_client_
.get());
82 power_policy_controller_
= PowerPolicyController::Get();
84 ASSERT_TRUE(profile_manager_
.SetUp());
86 power_prefs_
.reset(new PowerPrefs(power_policy_controller_
));
87 EXPECT_FALSE(GetProfile());
88 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
89 power_manager::PowerManagementPolicy()),
90 GetCurrentPowerPolicy());
93 void PowerPrefsTest::TearDown() {
95 PowerPolicyController::Shutdown();
96 testing::Test::TearDown();
99 const Profile
* PowerPrefsTest::GetProfile() const {
100 return power_prefs_
->profile_
;
103 std::string
PowerPrefsTest::GetExpectedPowerPolicyForProfile(
105 ScreenLockState screen_lock_state
) const {
106 const PrefService
* prefs
= profile
->GetPrefs();
107 power_manager::PowerManagementPolicy expected_policy
;
108 expected_policy
.mutable_ac_delays()->set_screen_dim_ms(prefs
->GetInteger(
109 screen_lock_state
== LOCKED
? prefs::kPowerLockScreenDimDelayMs
110 : prefs::kPowerAcScreenDimDelayMs
));
111 expected_policy
.mutable_ac_delays()->set_screen_off_ms(prefs
->GetInteger(
112 screen_lock_state
== LOCKED
? prefs::kPowerLockScreenOffDelayMs
113 : prefs::kPowerAcScreenOffDelayMs
));
114 expected_policy
.mutable_ac_delays()->set_screen_lock_ms(
115 prefs
->GetInteger(prefs::kPowerAcScreenLockDelayMs
));
116 expected_policy
.mutable_ac_delays()->set_idle_warning_ms(
117 prefs
->GetInteger(prefs::kPowerAcIdleWarningDelayMs
));
118 expected_policy
.mutable_ac_delays()->set_idle_ms(
119 prefs
->GetInteger(prefs::kPowerAcIdleDelayMs
));
120 expected_policy
.mutable_battery_delays()->set_screen_dim_ms(prefs
->GetInteger(
121 screen_lock_state
== LOCKED
? prefs::kPowerLockScreenDimDelayMs
122 : prefs::kPowerBatteryScreenDimDelayMs
));
123 expected_policy
.mutable_battery_delays()->set_screen_off_ms(prefs
->GetInteger(
124 screen_lock_state
== LOCKED
? prefs::kPowerLockScreenOffDelayMs
125 : prefs::kPowerBatteryScreenOffDelayMs
));
126 expected_policy
.mutable_battery_delays()->set_screen_lock_ms(
127 prefs
->GetInteger(prefs::kPowerBatteryScreenLockDelayMs
));
128 expected_policy
.mutable_battery_delays()->set_idle_warning_ms(
129 prefs
->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs
));
130 expected_policy
.mutable_battery_delays()->set_idle_ms(
131 prefs
->GetInteger(prefs::kPowerBatteryIdleDelayMs
));
132 expected_policy
.set_ac_idle_action(
133 static_cast<power_manager::PowerManagementPolicy_Action
>(
134 prefs
->GetInteger(prefs::kPowerAcIdleAction
)));
135 expected_policy
.set_battery_idle_action(
136 static_cast<power_manager::PowerManagementPolicy_Action
>(
137 prefs
->GetInteger(prefs::kPowerBatteryIdleAction
)));
138 expected_policy
.set_lid_closed_action(
139 static_cast<power_manager::PowerManagementPolicy_Action
>(
140 prefs
->GetInteger(prefs::kPowerLidClosedAction
)));
141 expected_policy
.set_use_audio_activity(
142 prefs
->GetBoolean(prefs::kPowerUseAudioActivity
));
143 expected_policy
.set_use_video_activity(
144 prefs
->GetBoolean(prefs::kPowerUseVideoActivity
));
145 expected_policy
.set_presentation_screen_dim_delay_factor(
146 prefs
->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor
));
147 expected_policy
.set_user_activity_screen_dim_delay_factor(
148 prefs
->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor
));
149 expected_policy
.set_wait_for_initial_user_activity(
150 prefs
->GetBoolean(prefs::kPowerWaitForInitialUserActivity
));
151 expected_policy
.set_force_nonzero_brightness_for_user_activity(
152 prefs
->GetBoolean(prefs::kPowerForceNonzeroBrightnessForUserActivity
));
153 expected_policy
.set_reason("Prefs");
154 return PowerPolicyController::GetPolicyDebugString(expected_policy
);
157 std::string
PowerPrefsTest::GetCurrentPowerPolicy() const {
158 return PowerPolicyController::GetPolicyDebugString(
159 fake_power_manager_client_
->policy());
162 bool PowerPrefsTest::GetCurrentAllowScreenWakeLocks() const {
163 return power_policy_controller_
->honor_screen_wake_locks_
;
166 bool PowerPrefsTest::GetExpectedAllowScreenWakeLocksForProfile(
167 Profile
* profile
) const {
168 return profile
->GetPrefs()->GetBoolean(prefs::kPowerAllowScreenWakeLocks
);
171 TEST_F(PowerPrefsTest
, LoginScreen
) {
172 // Set up login profile.
173 scoped_ptr
<TestingPrefServiceSyncable
> login_profile_prefs(
174 new TestingPrefServiceSyncable
);
175 chrome::RegisterLoginProfilePrefs(login_profile_prefs
->registry());
176 TestingProfile::Builder builder
;
178 profile_manager_
.profiles_dir().AppendASCII(chrome::kInitialProfile
));
179 builder
.SetPrefService(login_profile_prefs
.Pass());
180 TestingProfile
* login_profile
= builder
.BuildIncognito(
181 profile_manager_
.CreateTestingProfile(chrome::kInitialProfile
));
183 // Inform power_prefs_ that the login screen is being shown.
184 power_prefs_
->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
185 content::Source
<PowerPrefsTest
>(this),
186 content::NotificationService::NoDetails());
188 EXPECT_EQ(login_profile
, GetProfile());
189 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile
, UNLOCKED
),
190 GetCurrentPowerPolicy());
191 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile
),
192 GetCurrentAllowScreenWakeLocks());
194 TestingProfile
* other_profile
=
195 profile_manager_
.CreateTestingProfile("other");
197 // Inform power_prefs_ that an unrelated profile has been destroyed.
198 power_prefs_
->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED
,
199 content::Source
<Profile
>(other_profile
),
200 content::NotificationService::NoDetails());
202 // Verify that the login profile's power prefs are still being used.
203 EXPECT_EQ(login_profile
, GetProfile());
204 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile
, UNLOCKED
),
205 GetCurrentPowerPolicy());
206 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile
),
207 GetCurrentAllowScreenWakeLocks());
209 // Lock the screen and check that the expected delays are used.
210 bool screen_is_locked
= true;
211 power_prefs_
->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
212 content::Source
<Profile
>(login_profile
),
213 content::Details
<bool>(&screen_is_locked
));
214 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile
, LOCKED
),
215 GetCurrentPowerPolicy());
217 // Unlock the screen.
218 screen_is_locked
= false;
219 power_prefs_
->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED
,
220 content::Source
<Profile
>(login_profile
),
221 content::Details
<bool>(&screen_is_locked
));
222 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile
, UNLOCKED
),
223 GetCurrentPowerPolicy());
225 // Inform power_prefs_ that the login profile has been destroyed.
226 power_prefs_
->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED
,
227 content::Source
<Profile
>(login_profile
),
228 content::NotificationService::NoDetails());
230 // The login profile's prefs should still be used.
231 EXPECT_FALSE(GetProfile());
232 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile
, UNLOCKED
),
233 GetCurrentPowerPolicy());
236 TEST_F(PowerPrefsTest
, UserSession
) {
237 FakeChromeUserManager
* user_manager
= new FakeChromeUserManager();
238 ScopedUserManagerEnabler
user_manager_enabler(user_manager
);
240 // Set up user profile.
241 const char test_user1
[] = "test-user1@example.com";
242 user_manager
->AddUser(test_user1
);
243 user_manager
->LoginUser(test_user1
);
244 TestingProfile
* user_profile
=
245 profile_manager_
.CreateTestingProfile(test_user1
);
247 profile_manager_
.SetLoggedIn(true);
249 // Inform power_prefs_ that a session has started.
250 power_prefs_
->Observe(chrome::NOTIFICATION_SESSION_STARTED
,
251 content::Source
<PowerPrefsTest
>(this),
252 content::NotificationService::NoDetails());
254 EXPECT_EQ(user_profile
, GetProfile());
255 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile
, UNLOCKED
),
256 GetCurrentPowerPolicy());
257 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile
),
258 GetCurrentAllowScreenWakeLocks());
260 const char test_user2
[] = "test-user2@example.com";
261 user_manager
->AddUser(test_user2
);
262 user_manager
->LoginUser(test_user2
);
263 TestingProfile
* other_profile
=
264 profile_manager_
.CreateTestingProfile(test_user2
);
266 // Inform power_prefs_ that an unrelated profile has been destroyed.
267 power_prefs_
->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED
,
268 content::Source
<Profile
>(other_profile
),
269 content::NotificationService::NoDetails());
271 // Verify that the user profile's power prefs are still being used.
272 EXPECT_EQ(user_profile
, GetProfile());
273 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile
, UNLOCKED
),
274 GetCurrentPowerPolicy());
275 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile
),
276 GetCurrentAllowScreenWakeLocks());
278 // Simulate the login screen coming up as part of screen locking.
279 power_prefs_
->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE
,
280 content::Source
<PowerPrefsTest
>(this),
281 content::NotificationService::NoDetails());
283 // Verify that power policy didn't revert to login screen settings.
284 EXPECT_EQ(user_profile
, GetProfile());
285 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile
, UNLOCKED
),
286 GetCurrentPowerPolicy());
287 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile
),
288 GetCurrentAllowScreenWakeLocks());
290 // Inform power_prefs_ that the session has ended and the user profile has
292 power_prefs_
->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED
,
293 content::Source
<Profile
>(user_profile
),
294 content::NotificationService::NoDetails());
296 // The user profile's prefs should still be used.
297 EXPECT_FALSE(GetProfile());
298 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile
, UNLOCKED
),
299 GetCurrentPowerPolicy());
302 } // namespace chromeos