Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: chrome/browser/chromeos/power/power_prefs_unittest.cc

Issue 18153007: Add policies to control power management on the Chrome OS login screen (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments addressed. Rebased. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/extensions/extension_special_storage_policy.h"
14 #include "chrome/browser/prefs/browser_prefs.h"
15 #include "chrome/browser/prefs/pref_service_syncable.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/chrome_notification_types.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
Mattias Nissler (ping if slow) 2013/07/04 11:53:04 nit: remove extra blank line.
bartfab (slow) 2013/07/05 13:52:57 Done.
204
205 TEST_F(PowerPrefsTest, UserSession) {
206 // Set up user profile.
207 TestingProfile* user_profile = profile_manager_.CreateTestingProfile("user");
208 CommandLine::ForCurrentProcess()->AppendSwitchASCII(switches::kLoginProfile,
209 "user");
210 profile_manager_.SetLoggedIn(true);
211 ProfileManager::AllowGetDefaultProfile();
212
213 // Inform power_prefs_ that a session has started.
214 power_prefs_->Observe(chrome::NOTIFICATION_SESSION_STARTED,
215 content::Source<PowerPrefsTest>(this),
216 content::NotificationService::NoDetails());
217
218 EXPECT_EQ(user_profile, GetProfile());
219 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile),
220 GetCurrentPowerPolicy());
221 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
222 GetCurrentAllowScreenWakeLocks());
223
224 TestingProfile* other_profile =
225 profile_manager_.CreateTestingProfile("other");
226
227 // Inform power_prefs_ that an unrelated profile has been destroyed.
228 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
229 content::Source<Profile>(other_profile),
230 content::NotificationService::NoDetails());
231
232 // Verify that the user profile's power prefs are still being used.
233 EXPECT_EQ(user_profile, GetProfile());
234 EXPECT_EQ(GetExpectedPowerPolicyForProfile(user_profile),
235 GetCurrentPowerPolicy());
236 EXPECT_EQ(GetExpectedAllowScreenWakeLocksForProfile(user_profile),
237 GetCurrentAllowScreenWakeLocks());
238
239 // Inform power_prefs_ that the session has ended and the user profile has
240 // been destroyed.
241 power_prefs_->Observe(chrome::NOTIFICATION_PROFILE_DESTROYED,
242 content::Source<Profile>(user_profile),
243 content::NotificationService::NoDetails());
244
245 EXPECT_FALSE(GetProfile());
246 EXPECT_EQ(PowerPolicyController::GetPolicyDebugString(
247 power_manager::PowerManagementPolicy()),
248 GetCurrentPowerPolicy());
249 }
250
251 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698