Chromium Code Reviews| 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 28 matching lines...) Expand all Loading... | |
| 39 class PowerPrefsTest : public testing::Test { | 39 class PowerPrefsTest : public testing::Test { |
| 40 protected: | 40 protected: |
| 41 PowerPrefsTest(); | 41 PowerPrefsTest(); |
| 42 | 42 |
| 43 // testing::Test: | 43 // testing::Test: |
| 44 virtual void SetUp() override; | 44 virtual void SetUp() override; |
| 45 virtual void TearDown() override; | 45 virtual void TearDown() override; |
| 46 | 46 |
| 47 const Profile* GetProfile() const; | 47 const Profile* GetProfile() const; |
| 48 | 48 |
| 49 std::string GetExpectedPowerPolicyForProfile(Profile* profile) const; | 49 std::string GetExpectedPowerPolicyForProfile(Profile* profile, |
| 50 bool screen_is_locked) const; | |
| 50 std::string GetCurrentPowerPolicy() const; | 51 std::string GetCurrentPowerPolicy() const; |
| 51 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const; | 52 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const; |
| 52 bool GetCurrentAllowScreenWakeLocks() const; | 53 bool GetCurrentAllowScreenWakeLocks() const; |
| 53 | 54 |
| 54 TestingProfileManager profile_manager_; | 55 TestingProfileManager profile_manager_; |
| 55 PowerPolicyController* power_policy_controller_; // Not owned. | 56 PowerPolicyController* power_policy_controller_; // Not owned. |
| 56 scoped_ptr<FakePowerManagerClient> fake_power_manager_client_; | 57 scoped_ptr<FakePowerManagerClient> fake_power_manager_client_; |
| 57 | 58 |
| 58 scoped_ptr<PowerPrefs> power_prefs_; | 59 scoped_ptr<PowerPrefs> power_prefs_; |
| 59 | 60 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 85 power_prefs_.reset(); | 86 power_prefs_.reset(); |
| 86 PowerPolicyController::Shutdown(); | 87 PowerPolicyController::Shutdown(); |
| 87 testing::Test::TearDown(); | 88 testing::Test::TearDown(); |
| 88 } | 89 } |
| 89 | 90 |
| 90 const Profile* PowerPrefsTest::GetProfile() const { | 91 const Profile* PowerPrefsTest::GetProfile() const { |
| 91 return power_prefs_->profile_; | 92 return power_prefs_->profile_; |
| 92 } | 93 } |
| 93 | 94 |
| 94 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( | 95 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( |
| 95 Profile* profile) const { | 96 Profile* profile, |
| 97 bool screen_is_locked) const { | |
| 96 const PrefService* prefs = profile->GetPrefs(); | 98 const PrefService* prefs = profile->GetPrefs(); |
| 97 power_manager::PowerManagementPolicy expected_policy; | 99 power_manager::PowerManagementPolicy expected_policy; |
| 98 expected_policy.mutable_ac_delays()->set_screen_dim_ms( | 100 expected_policy.mutable_ac_delays()->set_screen_dim_ms( |
| 99 prefs->GetInteger(prefs::kPowerAcScreenDimDelayMs)); | 101 prefs->GetInteger(screen_is_locked ? prefs::kPowerLockScreenDimDelayMs : |
| 102 prefs::kPowerAcScreenDimDelayMs)); | |
| 100 expected_policy.mutable_ac_delays()->set_screen_off_ms( | 103 expected_policy.mutable_ac_delays()->set_screen_off_ms( |
| 101 prefs->GetInteger(prefs::kPowerAcScreenOffDelayMs)); | 104 prefs->GetInteger(screen_is_locked ? prefs::kPowerLockScreenOffDelayMs : |
| 105 prefs::kPowerAcScreenOffDelayMs)); | |
| 102 expected_policy.mutable_ac_delays()->set_screen_lock_ms( | 106 expected_policy.mutable_ac_delays()->set_screen_lock_ms( |
| 103 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); | 107 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); |
| 104 expected_policy.mutable_ac_delays()->set_idle_warning_ms( | 108 expected_policy.mutable_ac_delays()->set_idle_warning_ms( |
| 105 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); | 109 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); |
| 106 expected_policy.mutable_ac_delays()->set_idle_ms( | 110 expected_policy.mutable_ac_delays()->set_idle_ms( |
| 107 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); | 111 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); |
| 108 expected_policy.mutable_battery_delays()->set_screen_dim_ms( | 112 expected_policy.mutable_battery_delays()->set_screen_dim_ms( |
| 109 prefs->GetInteger(prefs::kPowerBatteryScreenDimDelayMs)); | 113 prefs->GetInteger(screen_is_locked ? prefs::kPowerLockScreenDimDelayMs : |
| 114 prefs::kPowerBatteryScreenDimDelayMs)); | |
| 110 expected_policy.mutable_battery_delays()->set_screen_off_ms( | 115 expected_policy.mutable_battery_delays()->set_screen_off_ms( |
| 111 prefs->GetInteger(prefs::kPowerBatteryScreenOffDelayMs)); | 116 prefs->GetInteger(screen_is_locked ? prefs::kPowerLockScreenOffDelayMs : |
| 117 prefs::kPowerBatteryScreenOffDelayMs)); | |
| 112 expected_policy.mutable_battery_delays()->set_screen_lock_ms( | 118 expected_policy.mutable_battery_delays()->set_screen_lock_ms( |
| 113 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); | 119 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); |
| 114 expected_policy.mutable_battery_delays()->set_idle_warning_ms( | 120 expected_policy.mutable_battery_delays()->set_idle_warning_ms( |
| 115 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); | 121 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); |
| 116 expected_policy.mutable_battery_delays()->set_idle_ms( | 122 expected_policy.mutable_battery_delays()->set_idle_ms( |
| 117 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); | 123 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); |
| 118 expected_policy.set_ac_idle_action( | 124 expected_policy.set_ac_idle_action( |
| 119 static_cast<power_manager::PowerManagementPolicy_Action>( | 125 static_cast<power_manager::PowerManagementPolicy_Action>( |
| 120 prefs->GetInteger(prefs::kPowerAcIdleAction))); | 126 prefs->GetInteger(prefs::kPowerAcIdleAction))); |
| 121 expected_policy.set_battery_idle_action( | 127 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()); | 169 builder.SetPrefService(login_profile_prefs.Pass()); |
| 164 TestingProfile* login_profile = builder.BuildIncognito( | 170 TestingProfile* login_profile = builder.BuildIncognito( |
| 165 profile_manager_.CreateTestingProfile(chrome::kInitialProfile)); | 171 profile_manager_.CreateTestingProfile(chrome::kInitialProfile)); |
| 166 | 172 |
| 167 // Inform power_prefs_ that the login screen is being shown. | 173 // Inform power_prefs_ that the login screen is being shown. |
| 168 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 174 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 169 content::Source<PowerPrefsTest>(this), | 175 content::Source<PowerPrefsTest>(this), |
| 170 content::NotificationService::NoDetails()); | 176 content::NotificationService::NoDetails()); |
| 171 | 177 |
| 172 EXPECT_EQ(login_profile, GetProfile()); | 178 EXPECT_EQ(login_profile, GetProfile()); |
| 173 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 179 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, false), |
|
Daniel Erat
2014/12/11 00:26:33
i suspect that you'll ask me to comment all of the
bartfab (slow)
2014/12/12 12:22:37
Well, there are many simple ways around it:
1) Us
| |
| 174 GetCurrentPowerPolicy()); | 180 GetCurrentPowerPolicy()); |
| 175 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), | 181 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
| 176 GetCurrentAllowScreenWakeLocks()); | 182 GetCurrentAllowScreenWakeLocks()); |
| 177 | 183 |
| 178 TestingProfile* other_profile = | 184 TestingProfile* other_profile = |
| 179 profile_manager_.CreateTestingProfile("other"); | 185 profile_manager_.CreateTestingProfile("other"); |
| 180 | 186 |
| 181 // Inform power_prefs_ that an unrelated profile has been destroyed. | 187 // Inform power_prefs_ that an unrelated profile has been destroyed. |
| 182 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 188 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 183 content::Source<Profile>(other_profile), | 189 content::Source<Profile>(other_profile), |
| 184 content::NotificationService::NoDetails()); | 190 content::NotificationService::NoDetails()); |
| 185 | 191 |
| 186 // Verify that the login profile's power prefs are still being used. | 192 // Verify that the login profile's power prefs are still being used. |
| 187 EXPECT_EQ(login_profile, GetProfile()); | 193 EXPECT_EQ(login_profile, GetProfile()); |
| 188 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 194 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, false), |
| 189 GetCurrentPowerPolicy()); | 195 GetCurrentPowerPolicy()); |
| 190 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), | 196 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
| 191 GetCurrentAllowScreenWakeLocks()); | 197 GetCurrentAllowScreenWakeLocks()); |
| 192 | 198 |
| 199 // Lock the screen and check that the expected delays are used. | |
| 200 bool screen_is_locked = true; | |
| 201 power_prefs_->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED, | |
| 202 content::Source<Profile>(login_profile), | |
| 203 content::Details<bool>(&screen_is_locked)); | |
| 204 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, true), | |
| 205 GetCurrentPowerPolicy()); | |
| 206 | |
| 207 // Unlock the screen. | |
| 208 screen_is_locked = false; | |
| 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, false), | |
| 213 GetCurrentPowerPolicy()); | |
| 214 | |
| 193 // Inform power_prefs_ that the login profile has been destroyed. | 215 // Inform power_prefs_ that the login profile has been destroyed. |
| 194 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 216 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 195 content::Source<Profile>(login_profile), | 217 content::Source<Profile>(login_profile), |
| 196 content::NotificationService::NoDetails()); | 218 content::NotificationService::NoDetails()); |
| 197 | 219 |
| 198 // The login profile's prefs should still be used. | 220 // The login profile's prefs should still be used. |
| 199 EXPECT_FALSE(GetProfile()); | 221 EXPECT_FALSE(GetProfile()); |
| 200 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), | 222 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, false), |
| 201 GetCurrentPowerPolicy()); | 223 GetCurrentPowerPolicy()); |
| 202 } | 224 } |
| 203 | 225 |
| 204 TEST_F(PowerPrefsTest, UserSession) { | 226 TEST_F(PowerPrefsTest, UserSession) { |
| 205 FakeUserManager* user_manager = new FakeUserManager(); | 227 FakeUserManager* user_manager = new FakeUserManager(); |
| 206 ScopedUserManagerEnabler user_manager_enabler(user_manager); | 228 ScopedUserManagerEnabler user_manager_enabler(user_manager); |
| 207 | 229 |
| 208 // Set up user profile. | 230 // Set up user profile. |
| 209 const char test_user1[] = "test-user1@example.com"; | 231 const char test_user1[] = "test-user1@example.com"; |
| 210 user_manager->AddUser(test_user1); | 232 user_manager->AddUser(test_user1); |
| 211 user_manager->LoginUser(test_user1); | 233 user_manager->LoginUser(test_user1); |
| 212 TestingProfile* user_profile = | 234 TestingProfile* user_profile = |
| 213 profile_manager_.CreateTestingProfile(test_user1); | 235 profile_manager_.CreateTestingProfile(test_user1); |
| 214 | 236 |
| 215 profile_manager_.SetLoggedIn(true); | 237 profile_manager_.SetLoggedIn(true); |
| 216 | 238 |
| 217 // Inform power_prefs_ that a session has started. | 239 // Inform power_prefs_ that a session has started. |
| 218 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, | 240 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, |
| 219 content::Source<PowerPrefsTest>(this), | 241 content::Source<PowerPrefsTest>(this), |
| 220 content::NotificationService::NoDetails()); | 242 content::NotificationService::NoDetails()); |
| 221 | 243 |
| 222 EXPECT_EQ(user_profile, GetProfile()); | 244 EXPECT_EQ(user_profile, GetProfile()); |
| 223 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 245 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, false), |
| 224 GetCurrentPowerPolicy()); | 246 GetCurrentPowerPolicy()); |
| 225 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 247 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
| 226 GetCurrentAllowScreenWakeLocks()); | 248 GetCurrentAllowScreenWakeLocks()); |
| 227 | 249 |
| 228 const char test_user2[] = "test-user2@example.com"; | 250 const char test_user2[] = "test-user2@example.com"; |
| 229 user_manager->AddUser(test_user2); | 251 user_manager->AddUser(test_user2); |
| 230 user_manager->LoginUser(test_user2); | 252 user_manager->LoginUser(test_user2); |
| 231 TestingProfile* other_profile = | 253 TestingProfile* other_profile = |
| 232 profile_manager_.CreateTestingProfile(test_user2); | 254 profile_manager_.CreateTestingProfile(test_user2); |
| 233 | 255 |
| 234 // Inform power_prefs_ that an unrelated profile has been destroyed. | 256 // Inform power_prefs_ that an unrelated profile has been destroyed. |
| 235 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 257 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 236 content::Source<Profile>(other_profile), | 258 content::Source<Profile>(other_profile), |
| 237 content::NotificationService::NoDetails()); | 259 content::NotificationService::NoDetails()); |
| 238 | 260 |
| 239 // Verify that the user profile's power prefs are still being used. | 261 // Verify that the user profile's power prefs are still being used. |
| 240 EXPECT_EQ(user_profile, GetProfile()); | 262 EXPECT_EQ(user_profile, GetProfile()); |
| 241 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 263 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, false), |
| 242 GetCurrentPowerPolicy()); | 264 GetCurrentPowerPolicy()); |
| 243 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 265 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
| 244 GetCurrentAllowScreenWakeLocks()); | 266 GetCurrentAllowScreenWakeLocks()); |
| 245 | 267 |
| 246 // Simulate the login screen coming up as part of screen locking. | 268 // Simulate the login screen coming up as part of screen locking. |
| 247 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, | 269 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, |
| 248 content::Source<PowerPrefsTest>(this), | 270 content::Source<PowerPrefsTest>(this), |
| 249 content::NotificationService::NoDetails()); | 271 content::NotificationService::NoDetails()); |
| 250 | 272 |
| 251 // Verify that power policy didn't revert to login screen settings. | 273 // Verify that power policy didn't revert to login screen settings. |
| 252 EXPECT_EQ(user_profile, GetProfile()); | 274 EXPECT_EQ(user_profile, GetProfile()); |
| 253 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 275 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, false), |
| 254 GetCurrentPowerPolicy()); | 276 GetCurrentPowerPolicy()); |
| 255 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), | 277 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
| 256 GetCurrentAllowScreenWakeLocks()); | 278 GetCurrentAllowScreenWakeLocks()); |
| 257 | 279 |
| 258 // Inform power_prefs_ that the session has ended and the user profile has | 280 // Inform power_prefs_ that the session has ended and the user profile has |
| 259 // been destroyed. | 281 // been destroyed. |
| 260 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, | 282 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 261 content::Source<Profile>(user_profile), | 283 content::Source<Profile>(user_profile), |
| 262 content::NotificationService::NoDetails()); | 284 content::NotificationService::NoDetails()); |
| 263 | 285 |
| 264 // The user profile's prefs should still be used. | 286 // The user profile's prefs should still be used. |
| 265 EXPECT_FALSE(GetProfile()); | 287 EXPECT_FALSE(GetProfile()); |
| 266 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), | 288 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, false), |
| 267 GetCurrentPowerPolicy()); | 289 GetCurrentPowerPolicy()); |
| 268 } | 290 } |
| 269 | 291 |
| 270 } // namespace chromeos | 292 } // namespace chromeos |
| OLD | NEW |