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.
5 #include "chrome/browser/chromeos/power/power_prefs.h"
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"
38 class PowerPrefsTest : public testing::Test {
43 virtual void SetUp() OVERRIDE;
44 virtual void TearDown() OVERRIDE;
46 const Profile* GetProfile() const;
48 std::string GetExpectedPowerPolicyForProfile(Profile* profile) const;
49 std::string GetCurrentPowerPolicy() const;
50 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const;
51 bool GetCurrentAllowScreenWakeLocks() const;
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.
58 scoped_ptr<PowerPrefs> power_prefs_;
60 DISALLOW_COPY_AND_ASSIGN(PowerPrefsTest);
63 PowerPrefsTest::PowerPrefsTest()
64 : profile_manager_(TestingBrowserProcess::GetGlobal()),
65 power_policy_controller_(
66 fake_dbus_thread_manager_.GetPowerPolicyController()),
67 fake_power_manager_client_(
68 fake_dbus_thread_manager_.fake_power_manager_client()) {
71 void PowerPrefsTest::SetUp() {
72 testing::Test::SetUp();
73 ASSERT_TRUE(profile_manager_.SetUp());
75 power_prefs_.reset(new PowerPrefs(power_policy_controller_));
76 EXPECT_FALSE(GetProfile());
77 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
78 power_manager::PowerManagementPolicy()),
79 GetCurrentPowerPolicy());
82 void PowerPrefsTest::TearDown() {
84 testing::Test::TearDown();
87 const Profile* PowerPrefsTest::GetProfile() const {
88 return power_prefs_->profile_;
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_wait_for_initial_user_activity(
133 prefs->GetBoolean(prefs::kPowerWaitForInitialUserActivity));
134 expected_policy.set_reason("Prefs");
135 return PowerPolicyController::GetPolicyDebugString(expected_policy);
138 std::string PowerPrefsTest::GetCurrentPowerPolicy() const {
139 return PowerPolicyController::GetPolicyDebugString(
140 fake_power_manager_client_->get_policy());
143 bool PowerPrefsTest::GetCurrentAllowScreenWakeLocks() const {
144 return power_policy_controller_->honor_screen_wake_locks_;
147 bool PowerPrefsTest::GetExpectedAllowScreenWakeLocksForProfile(
148 Profile* profile) const {
149 return profile->GetPrefs()->GetBoolean(prefs::kPowerAllowScreenWakeLocks);
152 TEST_F(PowerPrefsTest, LoginScreen) {
153 // Set up login profile.
154 scoped_ptr<TestingPrefServiceSyncable> login_profile_prefs(
155 new TestingPrefServiceSyncable);
156 chrome::RegisterLoginProfilePrefs(login_profile_prefs->registry());
157 TestingProfile::Builder builder;
159 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile));
160 builder.SetPrefService(login_profile_prefs.PassAs<PrefServiceSyncable>());
161 builder.SetIncognito();
162 scoped_ptr<TestingProfile> login_profile_owner(builder.Build());
164 TestingProfile* login_profile = login_profile_owner.get();
165 TestingProfile* login_profile_parent = profile_manager_.CreateTestingProfile(
166 chrome::kInitialProfile);
167 login_profile_parent->SetOffTheRecordProfile(
168 login_profile_owner.PassAs<Profile>());
169 login_profile->SetOriginalProfile(login_profile_parent);
171 // Inform power_prefs_ that the login screen is being shown.
172 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
173 content::Source<PowerPrefsTest>(this),
174 content::NotificationService::NoDetails());
176 EXPECT_EQ(login_profile, GetProfile());
177 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile),
178 GetCurrentPowerPolicy());
179 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile),
180 GetCurrentAllowScreenWakeLocks());
182 TestingProfile* other_profile =
183 profile_manager_.CreateTestingProfile("other");
185 // Inform power_prefs_ that an unrelated profile has been destroyed.
186 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
187 content::Source<Profile>(other_profile),
188 content::NotificationService::NoDetails());
190 // Verify that the login profile's power prefs are still being used.
191 EXPECT_EQ(login_profile, GetProfile());
192 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile),
193 GetCurrentPowerPolicy());
194 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile),
195 GetCurrentAllowScreenWakeLocks());
197 // Inform power_prefs_ that the login profile has been destroyed.
198 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
199 content::Source<Profile>(login_profile),
200 content::NotificationService::NoDetails());
202 EXPECT_FALSE(GetProfile());
203 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
204 power_manager::PowerManagementPolicy()),
205 GetCurrentPowerPolicy());
208 TEST_F(PowerPrefsTest, UserSession) {
209 // Set up user profile.
210 TestingProfile* user_profile = profile_manager_.CreateTestingProfile("user");
211 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kLoginProfile,
213 profile_manager_.SetLoggedIn(true);
214 ProfileManager::AllowGetDefaultProfile();
216 // Inform power_prefs_ that a session has started.
217 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED,
218 content::Source<PowerPrefsTest>(this),
219 content::NotificationService::NoDetails());
221 EXPECT_EQ(user_profile, GetProfile());
222 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile),
223 GetCurrentPowerPolicy());
224 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
225 GetCurrentAllowScreenWakeLocks());
227 TestingProfile* other_profile =
228 profile_manager_.CreateTestingProfile("other");
230 // Inform power_prefs_ that an unrelated profile has been destroyed.
231 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
232 content::Source<Profile>(other_profile),
233 content::NotificationService::NoDetails());
235 // Verify that the user profile's power prefs are still being used.
236 EXPECT_EQ(user_profile, GetProfile());
237 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile),
238 GetCurrentPowerPolicy());
239 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
240 GetCurrentAllowScreenWakeLocks());
242 // Simulate the login screen coming up as part of screen locking.
243 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
244 content::Source<PowerPrefsTest>(this),
245 content::NotificationService::NoDetails());
247 // Verify that power policy didn't revert to login screen settings.
248 EXPECT_EQ(user_profile, GetProfile());
249 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile),
250 GetCurrentPowerPolicy());
251 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
252 GetCurrentAllowScreenWakeLocks());
254 // Inform power_prefs_ that the session has ended and the user profile has
256 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
257 content::Source<Profile>(user_profile),
258 content::NotificationService::NoDetails());
260 EXPECT_FALSE(GetProfile());
261 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
262 power_manager::PowerManagementPolicy()),
263 GetCurrentPowerPolicy());
266 } // namespace chromeos