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. 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_dbus_thread_manager.h" 27 #include "chromeos/dbus/fake_power_manager_client.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 FakeDBusThreadManager fake_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_(new PowerPolicyController), 66 fake_power_manager_client_(new FakePowerManagerClient) { 67 fake_dbus_thread_manager_.SetPowerManagerClient( 68 scoped_ptr<PowerManagerClient>(fake_power_manager_client_)); 69 fake_dbus_thread_manager_.SetPowerPolicyController( 70 scoped_ptr<PowerPolicyController>(power_policy_controller_)); 71 power_policy_controller_->Init(&fake_dbus_thread_manager_); 72 } 73 74 void PowerPrefsTest::SetUp() { 75 testing::Test::SetUp(); 76 ASSERT_TRUE(profile_manager_.SetUp()); 77 78 power_prefs_.reset(new PowerPrefs(power_policy_controller_)); 79 EXPECT_FALSE(GetProfile()); 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::GetProfile() const { 91 return power_prefs_->profile_; 92 } 93 94 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile( 95 Profile* profile) const { 96 const PrefService* prefs = profile->GetPrefs(); 97 power_manager::PowerManagementPolicy expected_policy; 98 expected_policy.mutable_ac_delays()->set_screen_dim_ms( 99 prefs->GetInteger(prefs::kPowerAcScreenDimDelayMs)); 100 expected_policy.mutable_ac_delays()->set_screen_off_ms( 101 prefs->GetInteger(prefs::kPowerAcScreenOffDelayMs)); 102 expected_policy.mutable_ac_delays()->set_screen_lock_ms( 103 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs)); 104 expected_policy.mutable_ac_delays()->set_idle_warning_ms( 105 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs)); 106 expected_policy.mutable_ac_delays()->set_idle_ms( 107 prefs->GetInteger(prefs::kPowerAcIdleDelayMs)); 108 expected_policy.mutable_battery_delays()->set_screen_dim_ms( 109 prefs->GetInteger(prefs::kPowerBatteryScreenDimDelayMs)); 110 expected_policy.mutable_battery_delays()->set_screen_off_ms( 111 prefs->GetInteger(prefs::kPowerBatteryScreenOffDelayMs)); 112 expected_policy.mutable_battery_delays()->set_screen_lock_ms( 113 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs)); 114 expected_policy.mutable_battery_delays()->set_idle_warning_ms( 115 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs)); 116 expected_policy.mutable_battery_delays()->set_idle_ms( 117 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs)); 118 expected_policy.set_ac_idle_action( 119 static_cast<power_manager::PowerManagementPolicy_Action>( 120 prefs->GetInteger(prefs::kPowerAcIdleAction))); 121 expected_policy.set_battery_idle_action( 122 static_cast<power_manager::PowerManagementPolicy_Action>( 123 prefs->GetInteger(prefs::kPowerBatteryIdleAction))); 124 expected_policy.set_lid_closed_action( 125 static_cast<power_manager::PowerManagementPolicy_Action>( 126 prefs->GetInteger(prefs::kPowerLidClosedAction))); 127 expected_policy.set_use_audio_activity( 128 prefs->GetBoolean(prefs::kPowerUseAudioActivity)); 129 expected_policy.set_use_video_activity( 130 prefs->GetBoolean(prefs::kPowerUseVideoActivity)); 131 expected_policy.set_presentation_screen_dim_delay_factor( 132 prefs->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor)); 133 expected_policy.set_user_activity_screen_dim_delay_factor( 134 prefs->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor)); 135 expected_policy.set_wait_for_initial_user_activity( 136 prefs->GetBoolean(prefs::kPowerWaitForInitialUserActivity)); 137 expected_policy.set_reason("Prefs"); 138 return PowerPolicyController::GetPolicyDebugString(expected_policy); 139 } 140 141 std::string PowerPrefsTest::GetCurrentPowerPolicy() const { 142 return PowerPolicyController::GetPolicyDebugString( 143 fake_power_manager_client_->get_policy()); 144 } 145 146 bool PowerPrefsTest::GetCurrentAllowScreenWakeLocks() const { 147 return power_policy_controller_->honor_screen_wake_locks_; 148 } 149 150 bool PowerPrefsTest::GetExpectedAllowScreenWakeLocksForProfile( 151 Profile* profile) const { 152 return profile->GetPrefs()->GetBoolean(prefs::kPowerAllowScreenWakeLocks); 153 } 154 155 TEST_F(PowerPrefsTest, LoginScreen) { 156 // Set up login profile. 157 scoped_ptr<TestingPrefServiceSyncable> login_profile_prefs( 158 new TestingPrefServiceSyncable); 159 chrome::RegisterLoginProfilePrefs(login_profile_prefs->registry()); 160 TestingProfile::Builder builder; 161 builder.SetPath( 162 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile)); 163 builder.SetPrefService(login_profile_prefs.PassAs<PrefServiceSyncable>()); 164 builder.SetIncognito(); 165 scoped_ptr<TestingProfile> login_profile_owner(builder.Build()); 166 167 TestingProfile* login_profile = login_profile_owner.get(); 168 TestingProfile* login_profile_parent = profile_manager_.CreateTestingProfile( 169 chrome::kInitialProfile); 170 login_profile_parent->SetOffTheRecordProfile( 171 login_profile_owner.PassAs<Profile>()); 172 login_profile->SetOriginalProfile(login_profile_parent); 173 174 // Inform power_prefs_ that the login screen is being shown. 175 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 176 content::Source<PowerPrefsTest>(this), 177 content::NotificationService::NoDetails()); 178 179 EXPECT_EQ(login_profile, GetProfile()); 180 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), 181 GetCurrentPowerPolicy()); 182 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), 183 GetCurrentAllowScreenWakeLocks()); 184 185 TestingProfile* other_profile = 186 profile_manager_.CreateTestingProfile("other"); 187 188 // Inform power_prefs_ that an unrelated profile has been destroyed. 189 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, 190 content::Source<Profile>(other_profile), 191 content::NotificationService::NoDetails()); 192 193 // Verify that the login profile's power prefs are still being used. 194 EXPECT_EQ(login_profile, GetProfile()); 195 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile), 196 GetCurrentPowerPolicy()); 197 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile), 198 GetCurrentAllowScreenWakeLocks()); 199 200 // Inform power_prefs_ that the login profile has been destroyed. 201 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, 202 content::Source<Profile>(login_profile), 203 content::NotificationService::NoDetails()); 204 205 EXPECT_FALSE(GetProfile()); 206 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString( 207 power_manager::PowerManagementPolicy()), 208 GetCurrentPowerPolicy()); 209 } 210 211 TEST_F(PowerPrefsTest, UserSession) { 212 // Set up user profile. 213 TestingProfile* user_profile = profile_manager_.CreateTestingProfile("user"); 214 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kLoginProfile, 215 "user"); 216 profile_manager_.SetLoggedIn(true); 217 ProfileManager::AllowGetDefaultProfile(); 218 219 // Inform power_prefs_ that a session has started. 220 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED, 221 content::Source<PowerPrefsTest>(this), 222 content::NotificationService::NoDetails()); 223 224 EXPECT_EQ(user_profile, GetProfile()); 225 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), 226 GetCurrentPowerPolicy()); 227 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), 228 GetCurrentAllowScreenWakeLocks()); 229 230 TestingProfile* other_profile = 231 profile_manager_.CreateTestingProfile("other"); 232 233 // Inform power_prefs_ that an unrelated profile has been destroyed. 234 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, 235 content::Source<Profile>(other_profile), 236 content::NotificationService::NoDetails()); 237 238 // Verify that the user profile's power prefs are still being used. 239 EXPECT_EQ(user_profile, GetProfile()); 240 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), 241 GetCurrentPowerPolicy()); 242 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), 243 GetCurrentAllowScreenWakeLocks()); 244 245 // Simulate the login screen coming up as part of screen locking. 246 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE, 247 content::Source<PowerPrefsTest>(this), 248 content::NotificationService::NoDetails()); 249 250 // Verify that power policy didn't revert to login screen settings. 251 EXPECT_EQ(user_profile, GetProfile()); 252 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile), 253 GetCurrentPowerPolicy()); 254 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile), 255 GetCurrentAllowScreenWakeLocks()); 256 257 // Inform power_prefs_ that the session has ended and the user profile has 258 // been destroyed. 259 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED, 260 content::Source<Profile>(user_profile), 261 content::NotificationService::NoDetails()); 262 263 EXPECT_FALSE(GetProfile()); 264 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString( 265 power_manager::PowerManagementPolicy()), 266 GetCurrentPowerPolicy()); 267 } 268 269 } // namespace chromeos 270