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