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

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

Powered by Google App Engine
This is Rietveld 408576698