| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/chromeos/power/power_prefs.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 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_change_registrar.h" |
| 13 #include "base/prefs/pref_service.h" |
| 14 #include "chrome/browser/extensions/extension_special_storage_policy.h" |
| 15 #include "chrome/browser/prefs/browser_prefs.h" |
| 16 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/common/chrome_constants.h" |
| 20 #include "chrome/common/chrome_notification_types.h" |
| 21 #include "chrome/common/pref_names.h" |
| 22 #include "chrome/test/base/testing_browser_process.h" |
| 23 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 24 #include "chrome/test/base/testing_profile.h" |
| 25 #include "chrome/test/base/testing_profile_manager.h" |
| 26 #include "chromeos/chromeos_switches.h" |
| 27 #include "chromeos/dbus/fake_power_manager_client.h" |
| 28 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h" |
| 29 #include "chromeos/dbus/power_manager/policy.pb.h" |
| 30 #include "chromeos/dbus/power_policy_controller.h" |
| 31 #include "components/user_prefs/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" |
| 36 |
| 37 namespace chromeos { |
| 38 |
| 39 class PowerPrefsTest : public testing::Test { |
| 40 protected: |
| 41 PowerPrefsTest(); |
| 42 |
| 43 // testing::Test: |
| 44 virtual void SetUp() OVERRIDE; |
| 45 virtual void TearDown() OVERRIDE; |
| 46 |
| 47 const Profile* GetProfileUsedByPowerPrefs() const; |
| 48 const PrefChangeRegistrar* GetPrefChangeRegistrarUsedByPowerPrefs() const; |
| 49 |
| 50 std::string GetExpectedPowerPolicyForProfile(Profile* profile) const; |
| 51 std::string GetCurrentPowerPolicy() const; |
| 52 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const; |
| 53 bool GetCurrentAllowScreenWakeLocks() const; |
| 54 |
| 55 TestingProfileManager profile_manager_; |
| 56 MockDBusThreadManagerWithoutGMock mock_dbus_thread_manager_; |
| 57 PowerPolicyController* power_policy_controller_; // Not owned. |
| 58 FakePowerManagerClient* fake_power_manager_client_; // Not owned. |
| 59 |
| 60 scoped_ptr<PowerPrefs> power_prefs_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(PowerPrefsTest); |
| 63 }; |
| 64 |
| 65 PowerPrefsTest::PowerPrefsTest() |
| 66 : profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 67 power_policy_controller_( |
| 68 mock_dbus_thread_manager_.GetPowerPolicyController()), |
| 69 fake_power_manager_client_( |
| 70 mock_dbus_thread_manager_.fake_power_manager_client()) { |
| 71 } |
| 72 |
| 73 void PowerPrefsTest::SetUp() { |
| 74 testing::Test::SetUp(); |
| 75 ASSERT_TRUE(profile_manager_.SetUp()); |
| 76 |
| 77 power_prefs_.reset(new PowerPrefs(power_policy_controller_)); |
| 78 EXPECT_FALSE(GetProfileUsedByPowerPrefs()); |
| 79 EXPECT_FALSE(GetPrefChangeRegistrarUsedByPowerPrefs()); |
| 80 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString( |
| 81 power_manager::PowerManagementPolicy()), |
| 82 GetCurrentPowerPolicy()); |
| 83 } |
| 84 |
| 85 void PowerPrefsTest::TearDown() { |
| 86 power_prefs_.reset(); |
| 87 testing::Test::TearDown(); |
| 88 } |
| 89 |
| 90 const Profile* PowerPrefsTest::GetProfileUsedByPowerPrefs() const { |
| 91 return power_prefs_->profile_; |
| 92 } |
| 93 |
| 94 const PrefChangeRegistrar* |
| 95 PowerPrefsTest::GetPrefChangeRegistrarUsedByPowerPrefs() const { |
| 96 return power_prefs_->pref_change_registrar_.get(); |
| 97 } |
| 98 |
| 99 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( |
| 100 Profile* profile) const { |
| 101 const PrefService* prefs = profile->GetPrefs(); |
| 102 power_manager::PowerManagementPolicy expected_policy; |
| 103 expected_policy.mutable_ac_delays()->set_screen_dim_ms( |
| 104 prefs->GetInteger(prefs::kPowerAcScreenDimDelayMs)); |
| 105 expected_policy.mutable_ac_delays()->set_screen_off_ms( |
| 106 prefs->GetInteger(prefs::kPowerAcScreenOffDelayMs)); |
| 107 expected_policy.mutable_ac_delays()->set_screen_lock_ms( |
| 108 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); |
| 109 expected_policy.mutable_ac_delays()->set_idle_warning_ms( |
| 110 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); |
| 111 expected_policy.mutable_ac_delays()->set_idle_ms( |
| 112 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); |
| 113 expected_policy.mutable_battery_delays()->set_screen_dim_ms( |
| 114 prefs->GetInteger(prefs::kPowerBatteryScreenDimDelayMs)); |
| 115 expected_policy.mutable_battery_delays()->set_screen_off_ms( |
| 116 prefs->GetInteger(prefs::kPowerBatteryScreenOffDelayMs)); |
| 117 expected_policy.mutable_battery_delays()->set_screen_lock_ms( |
| 118 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); |
| 119 expected_policy.mutable_battery_delays()->set_idle_warning_ms( |
| 120 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); |
| 121 expected_policy.mutable_battery_delays()->set_idle_ms( |
| 122 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); |
| 123 expected_policy.set_ac_idle_action( |
| 124 static_cast<power_manager::PowerManagementPolicy_Action>( |
| 125 prefs->GetInteger(prefs::kPowerAcIdleAction))); |
| 126 expected_policy.set_battery_idle_action( |
| 127 static_cast<power_manager::PowerManagementPolicy_Action>( |
| 128 prefs->GetInteger(prefs::kPowerBatteryIdleAction))); |
| 129 expected_policy.set_lid_closed_action( |
| 130 static_cast<power_manager::PowerManagementPolicy_Action>( |
| 131 prefs->GetInteger(prefs::kPowerLidClosedAction))); |
| 132 expected_policy.set_use_audio_activity( |
| 133 prefs->GetBoolean(prefs::kPowerUseAudioActivity)); |
| 134 expected_policy.set_use_video_activity( |
| 135 prefs->GetBoolean(prefs::kPowerUseVideoActivity)); |
| 136 expected_policy.set_presentation_screen_dim_delay_factor( |
| 137 prefs->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor)); |
| 138 expected_policy.set_user_activity_screen_dim_delay_factor( |
| 139 prefs->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor)); |
| 140 expected_policy.set_reason("Prefs"); |
| 141 return PowerPolicyController::GetPolicyDebugString(expected_policy); |
| 142 } |
| 143 |
| 144 std::string PowerPrefsTest::GetCurrentPowerPolicy() const { |
| 145 return PowerPolicyController::GetPolicyDebugString( |
| 146 fake_power_manager_client_->get_policy()); |
| 147 } |
| 148 |
| 149 bool PowerPrefsTest::GetCurrentAllowScreenWakeLocks() const { |
| 150 return power_policy_controller_->honor_screen_wake_locks_; |
| 151 } |
| 152 |
| 153 bool PowerPrefsTest::GetExpectedAllowScreenWakeLocksForProfile( |
| 154 Profile* profile) const { |
| 155 return profile->GetPrefs()->GetBoolean(prefs::kPowerAllowScreenWakeLocks); |
| 156 } |
| 157 |
| 158 TEST_F(PowerPrefsTest, LoginScreen) { |
| 159 // Set up login profile. |
| 160 scoped_ptr<TestingPrefServiceSyncable> login_profile_prefs( |
| 161 new TestingPrefServiceSyncable); |
| 162 chrome::RegisterUserPrefs(login_profile_prefs->registry(), true); |
| 163 scoped_ptr<TestingProfile> login_profile_owner(new TestingProfile( |
| 164 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile), |
| 165 NULL, |
| 166 scoped_refptr<ExtensionSpecialStoragePolicy>(), |
| 167 scoped_ptr<PrefServiceSyncable>(login_profile_prefs.release()))); |
| 168 TestingProfile* login_profile = login_profile_owner.get(); |
| 169 TestingProfile* login_profile_parent = profile_manager_.CreateTestingProfile( |
| 170 chrome::kInitialProfile); |
| 171 login_profile_parent->SetOffTheRecordProfile(login_profile_owner.release()); |
| 172 login_profile->SetOriginalProfile(login_profile_parent); |
| 173 login_profile->set_incognito(true); |
| 174 |
| 175 // Inform power_prefs_ that the login screen is being shown. |
| 176 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_WEBUI_VISIBLE, |
| 177 content::Source<PowerPrefsTest>(this), |
| 178 content::NotificationService::NoDetails()); |
| 179 |
| 180 EXPECT_EQ(login_profile, GetProfileUsedByPowerPrefs()); |
| 181 const PrefChangeRegistrar* pref_change_registrar = |
| 182 GetPrefChangeRegistrarUsedByPowerPrefs(); |
| 183 ASSERT_TRUE(pref_change_registrar); |
| 184 EXPECT_EQ(login_profile->GetPrefs(), pref_change_registrar->prefs()); |
| 185 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), |
| 186 GetCurrentPowerPolicy()); |
| 187 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
| 188 GetCurrentAllowScreenWakeLocks()); |
| 189 |
| 190 TestingProfile* other_profile = |
| 191 profile_manager_.CreateTestingProfile("other"); |
| 192 |
| 193 // Inform power_prefs_ that an unrelated profile has been destroyed. |
| 194 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 195 content::Source<Profile>(other_profile), |
| 196 content::NotificationService::NoDetails()); |
| 197 |
| 198 // Verify that the login profile's power prefs are still being used. |
| 199 EXPECT_EQ(login_profile, GetProfileUsedByPowerPrefs()); |
| 200 pref_change_registrar = GetPrefChangeRegistrarUsedByPowerPrefs(); |
| 201 ASSERT_TRUE(pref_change_registrar); |
| 202 EXPECT_EQ(login_profile->GetPrefs(), pref_change_registrar->prefs()); |
| 203 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), |
| 204 GetCurrentPowerPolicy()); |
| 205 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), |
| 206 GetCurrentAllowScreenWakeLocks()); |
| 207 |
| 208 // Inform power_prefs_ that the login profile has been destroyed. |
| 209 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 210 content::Source<Profile>(login_profile), |
| 211 content::NotificationService::NoDetails()); |
| 212 |
| 213 EXPECT_FALSE(GetProfileUsedByPowerPrefs()); |
| 214 EXPECT_FALSE(GetPrefChangeRegistrarUsedByPowerPrefs()); |
| 215 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString( |
| 216 power_manager::PowerManagementPolicy()), |
| 217 GetCurrentPowerPolicy()); |
| 218 } |
| 219 |
| 220 |
| 221 TEST_F(PowerPrefsTest, UserSession) { |
| 222 // Set up user profile. |
| 223 TestingProfile* user_profile = profile_manager_.CreateTestingProfile("user"); |
| 224 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kLoginProfile, |
| 225 "user"); |
| 226 profile_manager_.SetLoggedIn(true); |
| 227 ProfileManager::AllowGetDefaultProfile(); |
| 228 |
| 229 // Inform power_prefs_ that a session has started. |
| 230 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, |
| 231 content::Source<PowerPrefsTest>(this), |
| 232 content::NotificationService::NoDetails()); |
| 233 |
| 234 EXPECT_EQ(user_profile, GetProfileUsedByPowerPrefs()); |
| 235 const PrefChangeRegistrar* pref_change_registrar = |
| 236 GetPrefChangeRegistrarUsedByPowerPrefs(); |
| 237 ASSERT_TRUE(pref_change_registrar); |
| 238 EXPECT_EQ(user_profile->GetPrefs(), pref_change_registrar->prefs()); |
| 239 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), |
| 240 GetCurrentPowerPolicy()); |
| 241 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
| 242 GetCurrentAllowScreenWakeLocks()); |
| 243 |
| 244 TestingProfile* other_profile = |
| 245 profile_manager_.CreateTestingProfile("other"); |
| 246 |
| 247 // Inform power_prefs_ that an unrelated profile has been destroyed. |
| 248 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 249 content::Source<Profile>(other_profile), |
| 250 content::NotificationService::NoDetails()); |
| 251 |
| 252 // Verify that the user profile's power prefs are still being used. |
| 253 EXPECT_EQ(user_profile, GetProfileUsedByPowerPrefs()); |
| 254 pref_change_registrar = GetPrefChangeRegistrarUsedByPowerPrefs(); |
| 255 ASSERT_TRUE(pref_change_registrar); |
| 256 EXPECT_EQ(user_profile->GetPrefs(), pref_change_registrar->prefs()); |
| 257 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), |
| 258 GetCurrentPowerPolicy()); |
| 259 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), |
| 260 GetCurrentAllowScreenWakeLocks()); |
| 261 |
| 262 // Inform power_prefs_ that the session has ended and the user profile has |
| 263 // been destroyed. |
| 264 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 265 content::Source<Profile>(user_profile), |
| 266 content::NotificationService::NoDetails()); |
| 267 |
| 268 EXPECT_FALSE(GetProfileUsedByPowerPrefs()); |
| 269 EXPECT_FALSE(GetPrefChangeRegistrarUsedByPowerPrefs()); |
| 270 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString( |
| 271 power_manager::PowerManagementPolicy()), |
| 272 GetCurrentPowerPolicy()); |
| 273 } |
| 274 |
| 275 } // namespace chromeos |
| OLD | NEW |