OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/power/power_prefs.h" | 5 #include "chrome/browser/chromeos/power/power_prefs.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 #include "components/pref_registry/pref_registry_syncable.h" | 31 #include "components/pref_registry/pref_registry_syncable.h" |
32 #include "content/public/browser/notification_details.h" | 32 #include "content/public/browser/notification_details.h" |
33 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
34 #include "content/public/browser/notification_source.h" | 34 #include "content/public/browser/notification_source.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
36 | 36 |
37 namespace chromeos { | 37 namespace chromeos { |
38 | 38 |
39 class PowerPrefsTest : public testing::Test { | 39 class PowerPrefsTest : public testing::Test { |
40 protected: | 40 protected: |
41 // Screen lock state that determines which delays are used by | |
42 // GetExpectedPowerPolicyForProfile(). | |
43 enum ScreenLockState { | |
44 LOCKED, | |
45 UNLOCKED, | |
46 }; | |
47 | |
41 PowerPrefsTest(); | 48 PowerPrefsTest(); |
42 | 49 |
43 // testing::Test: | 50 // testing::Test: |
44 virtual void SetUp() override; | 51 virtual void SetUp() override; |
45 virtual void TearDown() override; | 52 virtual void TearDown() override; |
46 | 53 |
47 const Profile* GetProfile() const; | 54 const Profile* GetProfile() const; |
48 | 55 |
49 std::string GetExpectedPowerPolicyForProfile(Profile* profile) const; | 56 std::string GetExpectedPowerPolicyForProfile( |
57 Profile* profile, | |
58 ScreenLockState screen_lock_state) const; | |
50 std::string GetCurrentPowerPolicy() const; | 59 std::string GetCurrentPowerPolicy() const; |
51 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const; | 60 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const; |
52 bool GetCurrentAllowScreenWakeLocks() const; | 61 bool GetCurrentAllowScreenWakeLocks() const; |
53 | 62 |
54 TestingProfileManager profile_manager_; | 63 TestingProfileManager profile_manager_; |
55 PowerPolicyController* power_policy_controller_; // Not owned. | 64 PowerPolicyController* power_policy_controller_; // Not owned. |
56 scoped_ptr<FakePowerManagerClient> fake_power_manager_client_; | 65 scoped_ptr<FakePowerManagerClient> fake_power_manager_client_; |
57 | 66 |
58 scoped_ptr<PowerPrefs> power_prefs_; | 67 scoped_ptr<PowerPrefs> power_prefs_; |
59 | 68 |
(...skipping 25 matching lines...) Expand all Loading... | |
85 power_prefs_.reset(); | 94 power_prefs_.reset(); |
86 PowerPolicyController::Shutdown(); | 95 PowerPolicyController::Shutdown(); |
87 testing::Test::TearDown(); | 96 testing::Test::TearDown(); |
88 } | 97 } |
89 | 98 |
90 const Profile* PowerPrefsTest::GetProfile() const { | 99 const Profile* PowerPrefsTest::GetProfile() const { |
91 return power_prefs_->profile_; | 100 return power_prefs_->profile_; |
92 } | 101 } |
93 | 102 |
94 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( | 103 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( |
95 Profile* profile) const { | 104 Profile* profile, |
105 ScreenLockState screen_lock_state) const { | |
96 const PrefService* prefs = profile->GetPrefs(); | 106 const PrefService* prefs = profile->GetPrefs(); |
97 power_manager::PowerManagementPolicy expected_policy; | 107 power_manager::PowerManagementPolicy expected_policy; |
98 expected_policy.mutable_ac_delays()->set_screen_dim_ms( | 108 expected_policy.mutable_ac_delays()->set_screen_dim_ms(prefs->GetInteger( |
99 prefs->GetInteger(prefs::kPowerAcScreenDimDelayMs)); | 109 screen_lock_state == LOCKED ? prefs::kPowerLockScreenDimDelayMs |
Daniel Erat
2014/12/12 17:38:18
weird (to my eyes) formatting is courtesy of clang
| |
100 expected_policy.mutable_ac_delays()->set_screen_off_ms( | 110 : prefs::kPowerAcScreenDimDelayMs)); |
101 prefs->GetInteger(prefs::kPowerAcScreenOffDelayMs)); | 111 expected_policy.mutable_ac_delays()->set_screen_off_ms(prefs->GetInteger( |
112 screen_lock_state == LOCKED ? prefs::kPowerLockScreenOffDelayMs | |
113 : prefs::kPowerAcScreenOffDelayMs)); | |
102 expected_policy.mutable_ac_delays()->set_screen_lock_ms( | 114 expected_policy.mutable_ac_delays()->set_screen_lock_ms( |
103 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); | 115 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); |
104 expected_policy.mutable_ac_delays()->set_idle_warning_ms( | 116 expected_policy.mutable_ac_delays()->set_idle_warning_ms( |
105 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); | 117 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); |
106 expected_policy.mutable_ac_delays()->set_idle_ms( | 118 expected_policy.mutable_ac_delays()->set_idle_ms( |
107 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); | 119 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); |
108 expected_policy.mutable_battery_delays()->set_screen_dim_ms( | 120 expected_policy.mutable_battery_delays()->set_screen_dim_ms(prefs->GetInteger( |
109 prefs->GetInteger(prefs::kPowerBatteryScreenDimDelayMs)); | 121 screen_lock_state == LOCKED ? prefs::kPowerLockScreenDimDelayMs |
110 expected_policy.mutable_battery_delays()->set_screen_off_ms( | 122 : prefs::kPowerBatteryScreenDimDelayMs)); |
111 prefs->GetInteger(prefs::kPowerBatteryScreenOffDelayMs)); | 123 expected_policy.mutable_battery_delays()->set_screen_off_ms(prefs->GetInteger( |
124 screen_lock_state == LOCKED ? prefs::kPowerLockScreenOffDelayMs | |
125 : prefs::kPowerBatteryScreenOffDelayMs)); | |
112 expected_policy.mutable_battery_delays()->set_screen_lock_ms( | 126 expected_policy.mutable_battery_delays()->set_screen_lock_ms( |
113 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); | 127 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); |
114 expected_policy.mutable_battery_delays()->set_idle_warning_ms( | 128 expected_policy.mutable_battery_delays()->set_idle_warning_ms( |
115 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); | 129 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); |
116 expected_policy.mutable_battery_delays()->set_idle_ms( | 130 expected_policy.mutable_battery_delays()->set_idle_ms( |
117 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); | 131 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); |
118 expected_policy.set_ac_idle_action( | 132 expected_policy.set_ac_idle_action( |
119 static_cast<power_manager::PowerManagementPolicy_Action>( | 133 static_cast<power_manager::PowerManagementPolicy_Action>( |
120 prefs->GetInteger(prefs::kPowerAcIdleAction))); | 134 prefs->GetInteger(prefs::kPowerAcIdleAction))); |
121 expected_policy.set_battery_idle_action( | 135 expected_policy.set_battery_idle_action( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
163 builder.SetPrefService(login_profile_prefs.Pass()); | 177 builder.SetPrefService(login_profile_prefs.Pass()); |
164 TestingProfile* login_profile = builder.BuildIncognito( | 178 TestingProfile* login_profile = builder.BuildIncognito( |
165 profile_manager_.CreateTestingProfile(chrome::kInitialProfile)); | 179 profile_manager_.CreateTestingProfile(chrome::kInitialProfile)); |
166 | 180 |
167 // Inform power_prefs_ that the login screen is being shown. | 181 // Inform power_prefs_ that the login screen is being shown. |
168 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 182 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
169 content::Source<PowerPrefsTest>(this), | 183 content::Source<PowerPrefsTest>(this), |
170 content::NotificationService::NoDetails()); | 184 content::NotificationService::NoDetails()); |
171 | 185 |
172 EXPECT_EQ(login_profile, GetProfile()); | 186 EXPECT_EQ(login_profile, GetProfile()); |
173 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 187 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED), |
174 GetCurrentPowerPolicy()); | 188 GetCurrentPowerPolicy()); |
175 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), | 189 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
176 GetCurrentAllowScreenWakeLocks()); | 190 GetCurrentAllowScreenWakeLocks()); |
177 | 191 |
178 TestingProfile* other_profile = | 192 TestingProfile* other_profile = |
179 profile_manager_.CreateTestingProfile("other"); | 193 profile_manager_.CreateTestingProfile("other"); |
180 | 194 |
181 // Inform power_prefs_ that an unrelated profile has been destroyed. | 195 // Inform power_prefs_ that an unrelated profile has been destroyed. |
182 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 196 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
183 content::Source<Profile>(other_profile), | 197 content::Source<Profile>(other_profile), |
184 content::NotificationService::NoDetails()); | 198 content::NotificationService::NoDetails()); |
185 | 199 |
186 // Verify that the login profile's power prefs are still being used. | 200 // Verify that the login profile's power prefs are still being used. |
187 EXPECT_EQ(login_profile, GetProfile()); | 201 EXPECT_EQ(login_profile, GetProfile()); |
188 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 202 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED), |
189 GetCurrentPowerPolicy()); | 203 GetCurrentPowerPolicy()); |
190 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), | 204 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
191 GetCurrentAllowScreenWakeLocks()); | 205 GetCurrentAllowScreenWakeLocks()); |
192 | 206 |
207 // Lock the screen and check that the expected delays are used. | |
208 bool screen_is_locked = true; | |
209 power_prefs_->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
210 content::Source<Profile>(login_profile), | |
211 content::Details<bool>(&screen_is_locked)); | |
212 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, LOCKED), | |
213 GetCurrentPowerPolicy()); | |
214 | |
215 // Unlock the screen. | |
216 screen_is_locked = false; | |
217 power_prefs_->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
218 content::Source<Profile>(login_profile), | |
219 content::Details<bool>(&screen_is_locked)); | |
220 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED), | |
221 GetCurrentPowerPolicy()); | |
222 | |
193 // Inform power_prefs_ that the login profile has been destroyed. | 223 // Inform power_prefs_ that the login profile has been destroyed. |
194 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 224 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
195 content::Source<Profile>(login_profile), | 225 content::Source<Profile>(login_profile), |
196 content::NotificationService::NoDetails()); | 226 content::NotificationService::NoDetails()); |
197 | 227 |
198 // The login profile's prefs should still be used. | 228 // The login profile's prefs should still be used. |
199 EXPECT_FALSE(GetProfile()); | 229 EXPECT_FALSE(GetProfile()); |
200 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 230 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED), |
201 GetCurrentPowerPolicy()); | 231 GetCurrentPowerPolicy()); |
202 } | 232 } |
203 | 233 |
204 TEST_F(PowerPrefsTest, UserSession) { | 234 TEST_F(PowerPrefsTest, UserSession) { |
205 FakeUserManager* user_manager = new FakeUserManager(); | 235 FakeUserManager* user_manager = new FakeUserManager(); |
206 ScopedUserManagerEnabler user_manager_enabler(user_manager); | 236 ScopedUserManagerEnabler user_manager_enabler(user_manager); |
207 | 237 |
208 // Set up user profile. | 238 // Set up user profile. |
209 const char test_user1[] = "test-user1@example.com"; | 239 const char test_user1[] = "test-user1@example.com"; |
210 user_manager->AddUser(test_user1); | 240 user_manager->AddUser(test_user1); |
211 user_manager->LoginUser(test_user1); | 241 user_manager->LoginUser(test_user1); |
212 TestingProfile* user_profile = | 242 TestingProfile* user_profile = |
213 profile_manager_.CreateTestingProfile(test_user1); | 243 profile_manager_.CreateTestingProfile(test_user1); |
214 | 244 |
215 profile_manager_.SetLoggedIn(true); | 245 profile_manager_.SetLoggedIn(true); |
216 | 246 |
217 // Inform power_prefs_ that a session has started. | 247 // Inform power_prefs_ that a session has started. |
218 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, | 248 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, |
219 content::Source<PowerPrefsTest>(this), | 249 content::Source<PowerPrefsTest>(this), |
220 content::NotificationService::NoDetails()); | 250 content::NotificationService::NoDetails()); |
221 | 251 |
222 EXPECT_EQ(user_profile, GetProfile()); | 252 EXPECT_EQ(user_profile, GetProfile()); |
223 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 253 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED), |
224 GetCurrentPowerPolicy()); | 254 GetCurrentPowerPolicy()); |
225 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 255 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
226 GetCurrentAllowScreenWakeLocks()); | 256 GetCurrentAllowScreenWakeLocks()); |
227 | 257 |
228 const char test_user2[] = "test-user2@example.com"; | 258 const char test_user2[] = "test-user2@example.com"; |
229 user_manager->AddUser(test_user2); | 259 user_manager->AddUser(test_user2); |
230 user_manager->LoginUser(test_user2); | 260 user_manager->LoginUser(test_user2); |
231 TestingProfile* other_profile = | 261 TestingProfile* other_profile = |
232 profile_manager_.CreateTestingProfile(test_user2); | 262 profile_manager_.CreateTestingProfile(test_user2); |
233 | 263 |
234 // Inform power_prefs_ that an unrelated profile has been destroyed. | 264 // Inform power_prefs_ that an unrelated profile has been destroyed. |
235 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 265 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
236 content::Source<Profile>(other_profile), | 266 content::Source<Profile>(other_profile), |
237 content::NotificationService::NoDetails()); | 267 content::NotificationService::NoDetails()); |
238 | 268 |
239 // Verify that the user profile's power prefs are still being used. | 269 // Verify that the user profile's power prefs are still being used. |
240 EXPECT_EQ(user_profile, GetProfile()); | 270 EXPECT_EQ(user_profile, GetProfile()); |
241 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 271 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED), |
242 GetCurrentPowerPolicy()); | 272 GetCurrentPowerPolicy()); |
243 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 273 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
244 GetCurrentAllowScreenWakeLocks()); | 274 GetCurrentAllowScreenWakeLocks()); |
245 | 275 |
246 // Simulate the login screen coming up as part of screen locking. | 276 // Simulate the login screen coming up as part of screen locking. |
247 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 277 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
248 content::Source<PowerPrefsTest>(this), | 278 content::Source<PowerPrefsTest>(this), |
249 content::NotificationService::NoDetails()); | 279 content::NotificationService::NoDetails()); |
250 | 280 |
251 // Verify that power policy didn't revert to login screen settings. | 281 // Verify that power policy didn't revert to login screen settings. |
252 EXPECT_EQ(user_profile, GetProfile()); | 282 EXPECT_EQ(user_profile, GetProfile()); |
253 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 283 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED), |
254 GetCurrentPowerPolicy()); | 284 GetCurrentPowerPolicy()); |
255 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 285 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
256 GetCurrentAllowScreenWakeLocks()); | 286 GetCurrentAllowScreenWakeLocks()); |
257 | 287 |
258 // Inform power_prefs_ that the session has ended and the user profile has | 288 // Inform power_prefs_ that the session has ended and the user profile has |
259 // been destroyed. | 289 // been destroyed. |
260 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 290 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
261 content::Source<Profile>(user_profile), | 291 content::Source<Profile>(user_profile), |
262 content::NotificationService::NoDetails()); | 292 content::NotificationService::NoDetails()); |
263 | 293 |
264 // The user profile's prefs should still be used. | 294 // The user profile's prefs should still be used. |
265 EXPECT_FALSE(GetProfile()); | 295 EXPECT_FALSE(GetProfile()); |
266 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 296 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED), |
267 GetCurrentPowerPolicy()); | 297 GetCurrentPowerPolicy()); |
268 } | 298 } |
269 | 299 |
270 } // namespace chromeos | 300 } // namespace chromeos |
OLD | NEW |