Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / chromeos / power / power_prefs_unittest.cc
blobdbf34eae15db759cdc8193d268dc65aed0b46118
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"
7 #include <string>
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/chromeos/login/users/fake_chrome_user_manager.h"
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
16 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 #include "chrome/browser/extensions/extension_special_storage_policy.h"
18 #include "chrome/browser/prefs/browser_prefs.h"
19 #include "chrome/browser/prefs/pref_service_syncable.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "chrome/test/base/testing_pref_service_syncable.h"
26 #include "chrome/test/base/testing_profile.h"
27 #include "chrome/test/base/testing_profile_manager.h"
28 #include "chromeos/dbus/fake_power_manager_client.h"
29 #include "chromeos/dbus/power_manager/policy.pb.h"
30 #include "chromeos/dbus/power_policy_controller.h"
31 #include "components/pref_registry/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 "content/public/test/test_browser_thread_bundle.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 namespace chromeos {
40 class PowerPrefsTest : public testing::Test {
41 protected:
42 // Screen lock state that determines which delays are used by
43 // GetExpectedPowerPolicyForProfile().
44 enum ScreenLockState {
45 LOCKED,
46 UNLOCKED,
49 PowerPrefsTest();
51 // testing::Test:
52 void SetUp() override;
53 void TearDown() override;
55 const Profile* GetProfile() const;
57 std::string GetExpectedPowerPolicyForProfile(
58 Profile* profile,
59 ScreenLockState screen_lock_state) const;
60 std::string GetCurrentPowerPolicy() const;
61 bool GetExpectedAllowScreenWakeLocksForProfile(Profile* profile) const;
62 bool GetCurrentAllowScreenWakeLocks() const;
64 content::TestBrowserThreadBundle thread_bundle_;
65 TestingProfileManager profile_manager_;
66 PowerPolicyController* power_policy_controller_; // Not owned.
67 scoped_ptr<FakePowerManagerClient> fake_power_manager_client_;
69 scoped_ptr<PowerPrefs> power_prefs_;
71 DISALLOW_COPY_AND_ASSIGN(PowerPrefsTest);
74 PowerPrefsTest::PowerPrefsTest()
75 : profile_manager_(TestingBrowserProcess::GetGlobal()),
76 power_policy_controller_(NULL),
77 fake_power_manager_client_(new FakePowerManagerClient) {
80 void PowerPrefsTest::SetUp() {
81 testing::Test::SetUp();
83 PowerPolicyController::Initialize(fake_power_manager_client_.get());
84 power_policy_controller_ = PowerPolicyController::Get();
86 ASSERT_TRUE(profile_manager_.SetUp());
88 power_prefs_.reset(new PowerPrefs(power_policy_controller_));
89 EXPECT_FALSE(GetProfile());
90 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
91 power_manager::PowerManagementPolicy()),
92 GetCurrentPowerPolicy());
95 void PowerPrefsTest::TearDown() {
96 power_prefs_.reset();
97 PowerPolicyController::Shutdown();
98 testing::Test::TearDown();
101 const Profile* PowerPrefsTest::GetProfile() const {
102 return power_prefs_->profile_;
105 std::string PowerPrefsTest::GetExpectedPowerPolicyForProfile(
106 Profile* profile,
107 ScreenLockState screen_lock_state) const {
108 const PrefService* prefs = profile->GetPrefs();
109 power_manager::PowerManagementPolicy expected_policy;
110 expected_policy.mutable_ac_delays()->set_screen_dim_ms(prefs->GetInteger(
111 screen_lock_state == LOCKED ? prefs::kPowerLockScreenDimDelayMs
112 : prefs::kPowerAcScreenDimDelayMs));
113 expected_policy.mutable_ac_delays()->set_screen_off_ms(prefs->GetInteger(
114 screen_lock_state == LOCKED ? prefs::kPowerLockScreenOffDelayMs
115 : prefs::kPowerAcScreenOffDelayMs));
116 expected_policy.mutable_ac_delays()->set_screen_lock_ms(
117 prefs->GetInteger(prefs::kPowerAcScreenLockDelayMs));
118 expected_policy.mutable_ac_delays()->set_idle_warning_ms(
119 prefs->GetInteger(prefs::kPowerAcIdleWarningDelayMs));
120 expected_policy.mutable_ac_delays()->set_idle_ms(
121 prefs->GetInteger(prefs::kPowerAcIdleDelayMs));
122 expected_policy.mutable_battery_delays()->set_screen_dim_ms(prefs->GetInteger(
123 screen_lock_state == LOCKED ? prefs::kPowerLockScreenDimDelayMs
124 : prefs::kPowerBatteryScreenDimDelayMs));
125 expected_policy.mutable_battery_delays()->set_screen_off_ms(prefs->GetInteger(
126 screen_lock_state == LOCKED ? prefs::kPowerLockScreenOffDelayMs
127 : prefs::kPowerBatteryScreenOffDelayMs));
128 expected_policy.mutable_battery_delays()->set_screen_lock_ms(
129 prefs->GetInteger(prefs::kPowerBatteryScreenLockDelayMs));
130 expected_policy.mutable_battery_delays()->set_idle_warning_ms(
131 prefs->GetInteger(prefs::kPowerBatteryIdleWarningDelayMs));
132 expected_policy.mutable_battery_delays()->set_idle_ms(
133 prefs->GetInteger(prefs::kPowerBatteryIdleDelayMs));
134 expected_policy.set_ac_idle_action(
135 static_cast<power_manager::PowerManagementPolicy_Action>(
136 prefs->GetInteger(prefs::kPowerAcIdleAction)));
137 expected_policy.set_battery_idle_action(
138 static_cast<power_manager::PowerManagementPolicy_Action>(
139 prefs->GetInteger(prefs::kPowerBatteryIdleAction)));
140 expected_policy.set_lid_closed_action(
141 static_cast<power_manager::PowerManagementPolicy_Action>(
142 prefs->GetInteger(prefs::kPowerLidClosedAction)));
143 expected_policy.set_use_audio_activity(
144 prefs->GetBoolean(prefs::kPowerUseAudioActivity));
145 expected_policy.set_use_video_activity(
146 prefs->GetBoolean(prefs::kPowerUseVideoActivity));
147 expected_policy.set_presentation_screen_dim_delay_factor(
148 prefs->GetDouble(prefs::kPowerPresentationScreenDimDelayFactor));
149 expected_policy.set_user_activity_screen_dim_delay_factor(
150 prefs->GetDouble(prefs::kPowerUserActivityScreenDimDelayFactor));
151 expected_policy.set_wait_for_initial_user_activity(
152 prefs->GetBoolean(prefs::kPowerWaitForInitialUserActivity));
153 expected_policy.set_force_nonzero_brightness_for_user_activity(
154 prefs->GetBoolean(prefs::kPowerForceNonzeroBrightnessForUserActivity));
155 expected_policy.set_reason("Prefs");
156 return PowerPolicyController::GetPolicyDebugString(expected_policy);
159 std::string PowerPrefsTest::GetCurrentPowerPolicy() const {
160 return PowerPolicyController::GetPolicyDebugString(
161 fake_power_manager_client_->policy());
164 bool PowerPrefsTest::GetCurrentAllowScreenWakeLocks() const {
165 return power_policy_controller_->honor_screen_wake_locks_;
168 bool PowerPrefsTest::GetExpectedAllowScreenWakeLocksForProfile(
169 Profile* profile) const {
170 return profile->GetPrefs()->GetBoolean(prefs::kPowerAllowScreenWakeLocks);
173 TEST_F(PowerPrefsTest, LoginScreen) {
174 // Set up login profile.
175 scoped_ptr<TestingPrefServiceSyncable> login_profile_prefs(
176 new TestingPrefServiceSyncable);
177 chrome::RegisterLoginProfilePrefs(login_profile_prefs->registry());
178 TestingProfile::Builder builder;
179 builder.SetPath(
180 profile_manager_.profiles_dir().AppendASCII(chrome::kInitialProfile));
181 builder.SetPrefService(login_profile_prefs.Pass());
182 TestingProfile* login_profile = builder.BuildIncognito(
183 profile_manager_.CreateTestingProfile(chrome::kInitialProfile));
185 // Inform power_prefs_ that the login screen is being shown.
186 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
187 content::Source<PowerPrefsTest>(this),
188 content::NotificationService::NoDetails());
190 EXPECT_EQ(login_profile, GetProfile());
191 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED),
192 GetCurrentPowerPolicy());
193 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile),
194 GetCurrentAllowScreenWakeLocks());
196 TestingProfile* other_profile =
197 profile_manager_.CreateTestingProfile("other");
199 // Inform power_prefs_ that an unrelated profile has been destroyed.
200 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
201 content::Source<Profile>(other_profile),
202 content::NotificationService::NoDetails());
204 // Verify that the login profile's power prefs are still being used.
205 EXPECT_EQ(login_profile, GetProfile());
206 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED),
207 GetCurrentPowerPolicy());
208 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(login_profile),
209 GetCurrentAllowScreenWakeLocks());
211 // Lock the screen and check that the expected delays are used.
212 bool screen_is_locked = true;
213 power_prefs_->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
214 content::Source<Profile>(login_profile),
215 content::Details<bool>(&screen_is_locked));
216 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, LOCKED),
217 GetCurrentPowerPolicy());
219 // Unlock the screen.
220 screen_is_locked = false;
221 power_prefs_->Observe(chrome::NOTIFICATION_SCREEN_LOCK_STATE_CHANGED,
222 content::Source<Profile>(login_profile),
223 content::Details<bool>(&screen_is_locked));
224 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED),
225 GetCurrentPowerPolicy());
227 // Inform power_prefs_ that the login profile has been destroyed.
228 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
229 content::Source<Profile>(login_profile),
230 content::NotificationService::NoDetails());
232 // The login profile's prefs should still be used.
233 EXPECT_FALSE(GetProfile());
234 EXPECT_EQ(GetExpectedPowerPolicyForProfile(login_profile, UNLOCKED),
235 GetCurrentPowerPolicy());
238 TEST_F(PowerPrefsTest, UserSession) {
239 FakeChromeUserManager* user_manager = new FakeChromeUserManager();
240 ScopedUserManagerEnabler user_manager_enabler(user_manager);
242 // Set up user profile.
243 const char test_user1[] = "test-user1@example.com";
244 user_manager->AddUser(test_user1);
245 user_manager->LoginUser(test_user1);
246 TestingProfile* user_profile =
247 profile_manager_.CreateTestingProfile(test_user1);
249 profile_manager_.SetLoggedIn(true);
251 // Inform power_prefs_ that a session has started.
252 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED,
253 content::Source<PowerPrefsTest>(this),
254 content::NotificationService::NoDetails());
256 EXPECT_EQ(user_profile, GetProfile());
257 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED),
258 GetCurrentPowerPolicy());
259 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
260 GetCurrentAllowScreenWakeLocks());
262 const char test_user2[] = "test-user2@example.com";
263 user_manager->AddUser(test_user2);
264 user_manager->LoginUser(test_user2);
265 TestingProfile* other_profile =
266 profile_manager_.CreateTestingProfile(test_user2);
268 // Inform power_prefs_ that an unrelated profile has been destroyed.
269 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
270 content::Source<Profile>(other_profile),
271 content::NotificationService::NoDetails());
273 // Verify that the user profile's power prefs are still being used.
274 EXPECT_EQ(user_profile, GetProfile());
275 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED),
276 GetCurrentPowerPolicy());
277 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
278 GetCurrentAllowScreenWakeLocks());
280 // Simulate the login screen coming up as part of screen locking.
281 power_prefs_->Observe(chrome::NOTIFICATION_LOGIN_OR_LOCK_WEBUI_VISIBLE,
282 content::Source<PowerPrefsTest>(this),
283 content::NotificationService::NoDetails());
285 // Verify that power policy didn't revert to login screen settings.
286 EXPECT_EQ(user_profile, GetProfile());
287 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED),
288 GetCurrentPowerPolicy());
289 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
290 GetCurrentAllowScreenWakeLocks());
292 // Inform power_prefs_ that the session has ended and the user profile has
293 // been destroyed.
294 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
295 content::Source<Profile>(user_profile),
296 content::NotificationService::NoDetails());
298 // The user profile's prefs should still be used.
299 EXPECT_FALSE(GetProfile());
300 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile, UNLOCKED),
301 GetCurrentPowerPolicy());
304 } // namespace chromeos