Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / recommendation_restorer_unittest.cc
blob82c877680f61c79c82fc493bfb5e3ebc51a78529
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/policy/recommendation_restorer.h"
7 #include "ash/magnifier/magnifier_constants.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_notifier_impl.h"
10 #include "base/prefs/testing_pref_store.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/test/test_simple_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/time/time.h"
15 #include "base/values.h"
16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/chromeos/policy/recommendation_restorer_factory.h"
18 #include "chrome/browser/prefs/browser_prefs.h"
19 #include "chrome/browser/prefs/pref_service_syncable.h"
20 #include "chrome/common/chrome_constants.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 "components/user_prefs/pref_registry_syncable.h"
27 #include "content/public/browser/notification_details.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 namespace policy {
34 namespace {
35 // The amount of idle time after which recommended values are restored.
36 const int kRestoreDelayInMs = 60 * 1000; // 1 minute.
37 } // namespace
39 class RecommendationRestorerTest : public testing::Test {
40 protected:
41 RecommendationRestorerTest();
43 // testing::Test:
44 virtual void SetUp() OVERRIDE;
46 void RegisterUserProfilePrefs();
47 void RegisterLoginProfilePrefs();
49 void SetRecommendedValues();
50 void SetUserSettings();
52 void CreateLoginProfile();
53 void CreateUserProfile();
55 void NotifyOfSessionStart();
56 void NotifyOfUserActivity();
58 void VerifyPrefFollowsUser(const char* pref_name,
59 const base::Value& expected_value) const;
60 void VerifyPrefsFollowUser() const;
61 void VerifyPrefFollowsRecommendation(const char* pref_name,
62 const base::Value& expected_value) const;
63 void VerifyPrefsFollowRecommendations() const;
65 void VerifyNotListeningForNotifications() const;
66 void VerifyTimerIsStopped() const;
67 void VerifyTimerIsRunning() const;
69 TestingPrefStore* recommended_prefs_; // Not owned.
70 TestingPrefServiceSyncable* prefs_; // Not owned.
71 RecommendationRestorer* restorer_; // Not owned.
73 scoped_refptr<base::TestSimpleTaskRunner> runner_;
74 base::ThreadTaskRunnerHandle runner_handler_;
76 private:
77 scoped_ptr<PrefServiceSyncable> prefs_owner_;
79 TestingProfileManager profile_manager_;
81 DISALLOW_COPY_AND_ASSIGN(RecommendationRestorerTest);
84 RecommendationRestorerTest::RecommendationRestorerTest()
85 : recommended_prefs_(new TestingPrefStore),
86 prefs_(new TestingPrefServiceSyncable(
87 new TestingPrefStore,
88 new TestingPrefStore,
89 recommended_prefs_,
90 new user_prefs::PrefRegistrySyncable,
91 new PrefNotifierImpl)),
92 restorer_(NULL),
93 runner_(new base::TestSimpleTaskRunner),
94 runner_handler_(runner_),
95 prefs_owner_(prefs_),
96 profile_manager_(TestingBrowserProcess::GetGlobal()) {
99 void RecommendationRestorerTest::SetUp() {
100 testing::Test::SetUp();
101 ASSERT_TRUE(profile_manager_.SetUp());
104 void RecommendationRestorerTest::RegisterUserProfilePrefs() {
105 chrome::RegisterUserProfilePrefs(prefs_->registry());
108 void RecommendationRestorerTest::RegisterLoginProfilePrefs() {
109 chrome::RegisterLoginProfilePrefs(prefs_->registry());
112 void RecommendationRestorerTest::SetRecommendedValues() {
113 recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
114 recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
115 recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
116 recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
117 recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
120 void RecommendationRestorerTest::SetUserSettings() {
121 prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
122 prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
123 prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
124 prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
125 prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
128 void RecommendationRestorerTest::CreateLoginProfile() {
129 ASSERT_FALSE(restorer_);
130 TestingProfile* profile = profile_manager_.CreateTestingProfile(
131 chrome::kInitialProfile, prefs_owner_.Pass(),
132 base::UTF8ToUTF16(chrome::kInitialProfile), 0, std::string(),
133 TestingProfile::TestingFactories());
134 restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
135 EXPECT_TRUE(restorer_);
138 void RecommendationRestorerTest::CreateUserProfile() {
139 ASSERT_FALSE(restorer_);
140 TestingProfile* profile = profile_manager_.CreateTestingProfile(
141 "user", prefs_owner_.Pass(), base::UTF8ToUTF16("user"), 0, std::string(),
142 TestingProfile::TestingFactories());
143 restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
144 EXPECT_TRUE(restorer_);
147 void RecommendationRestorerTest::NotifyOfSessionStart() {
148 ASSERT_TRUE(restorer_);
149 restorer_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
150 content::Source<RecommendationRestorerTest>(this),
151 content::NotificationService::NoDetails());
154 void RecommendationRestorerTest::NotifyOfUserActivity() {
155 ASSERT_TRUE(restorer_);
156 restorer_->OnUserActivity(NULL);
159 void RecommendationRestorerTest::VerifyPrefFollowsUser(
160 const char* pref_name,
161 const base::Value& expected_value) const {
162 const PrefServiceSyncable::Preference* pref =
163 prefs_->FindPreference(pref_name);
164 ASSERT_TRUE(pref);
165 EXPECT_TRUE(pref->HasUserSetting());
166 const base::Value* value = pref->GetValue();
167 ASSERT_TRUE(value);
168 EXPECT_TRUE(expected_value.Equals(value));
171 void RecommendationRestorerTest::VerifyPrefsFollowUser() const {
172 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
173 base::FundamentalValue(true));
174 VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
175 base::FundamentalValue(true));
176 VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
177 base::FundamentalValue(true));
178 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
179 base::FundamentalValue(true));
180 VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
181 base::FundamentalValue(ash::MAGNIFIER_FULL));
184 void RecommendationRestorerTest::VerifyPrefFollowsRecommendation(
185 const char* pref_name,
186 const base::Value& expected_value) const {
187 const PrefServiceSyncable::Preference* pref =
188 prefs_->FindPreference(pref_name);
189 ASSERT_TRUE(pref);
190 EXPECT_TRUE(pref->IsRecommended());
191 EXPECT_FALSE(pref->HasUserSetting());
192 const base::Value* value = pref->GetValue();
193 ASSERT_TRUE(value);
194 EXPECT_TRUE(expected_value.Equals(value));
197 void RecommendationRestorerTest::VerifyPrefsFollowRecommendations() const {
198 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
199 base::FundamentalValue(false));
200 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
201 base::FundamentalValue(false));
202 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
203 base::FundamentalValue(false));
204 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
205 base::FundamentalValue(false));
206 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
207 base::FundamentalValue(0));
210 void RecommendationRestorerTest::VerifyNotListeningForNotifications() const {
211 ASSERT_TRUE(restorer_);
212 EXPECT_TRUE(restorer_->pref_change_registrar_.IsEmpty());
213 EXPECT_TRUE(restorer_->notification_registrar_.IsEmpty());
216 void RecommendationRestorerTest::VerifyTimerIsStopped() const {
217 ASSERT_TRUE(restorer_);
218 EXPECT_FALSE(restorer_->restore_timer_.IsRunning());
221 void RecommendationRestorerTest::VerifyTimerIsRunning() const {
222 ASSERT_TRUE(restorer_);
223 EXPECT_TRUE(restorer_->restore_timer_.IsRunning());
224 EXPECT_EQ(base::TimeDelta::FromMilliseconds(kRestoreDelayInMs),
225 restorer_->restore_timer_.GetCurrentDelay());
228 TEST_F(RecommendationRestorerTest, CreateForUserProfile) {
229 // Verifies that when a RecommendationRestorer is created for a user profile,
230 // it does not start listening for any notifications, does not clear user
231 // settings on initialization and does not start a timer that will clear user
232 // settings eventually.
233 RegisterUserProfilePrefs();
234 SetRecommendedValues();
235 SetUserSettings();
237 CreateUserProfile();
238 VerifyNotListeningForNotifications();
239 VerifyPrefsFollowUser();
240 VerifyTimerIsStopped();
243 TEST_F(RecommendationRestorerTest, NoRecommendations) {
244 // Verifies that when no recommended values have been set and a
245 // RecommendationRestorer is created for the login profile, it does not clear
246 // user settings on initialization and does not start a timer that will clear
247 // user settings eventually.
248 RegisterLoginProfilePrefs();
249 SetUserSettings();
251 CreateLoginProfile();
252 VerifyPrefsFollowUser();
253 VerifyTimerIsStopped();
256 TEST_F(RecommendationRestorerTest, RestoreOnStartup) {
257 // Verifies that when recommended values have been set and a
258 // RecommendationRestorer is created for the login profile, it clears user
259 // settings on initialization.
260 RegisterLoginProfilePrefs();
261 SetRecommendedValues();
262 SetUserSettings();
264 CreateLoginProfile();
265 VerifyPrefsFollowRecommendations();
266 VerifyTimerIsStopped();
269 TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeOnLoginScreen) {
270 // Verifies that if recommended values change while the login screen is being
271 // shown, a timer is started that will clear user settings eventually.
272 RegisterLoginProfilePrefs();
273 SetUserSettings();
275 CreateLoginProfile();
277 VerifyTimerIsStopped();
278 recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
279 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
280 base::FundamentalValue(true));
281 VerifyTimerIsRunning();
282 runner_->RunUntilIdle();
283 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
284 base::FundamentalValue(false));
286 VerifyTimerIsStopped();
287 recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
288 VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
289 base::FundamentalValue(true));
290 VerifyTimerIsRunning();
291 runner_->RunUntilIdle();
292 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
293 base::FundamentalValue(false));
295 VerifyTimerIsStopped();
296 recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
297 VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
298 base::FundamentalValue(true));
299 VerifyTimerIsRunning();
300 runner_->RunUntilIdle();
301 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
302 base::FundamentalValue(false));
304 VerifyTimerIsStopped();
305 recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
306 recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
307 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
308 base::FundamentalValue(true));
309 VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
310 base::FundamentalValue(ash::MAGNIFIER_FULL));
311 VerifyTimerIsRunning();
312 runner_->RunUntilIdle();
313 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
314 base::FundamentalValue(false));
315 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
316 base::FundamentalValue(0));
318 VerifyTimerIsStopped();
321 TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeInUserSession) {
322 // Verifies that if recommended values change while a user session is in
323 // progress, user settings are cleared immediately.
324 RegisterLoginProfilePrefs();
325 SetUserSettings();
327 CreateLoginProfile();
328 NotifyOfSessionStart();
330 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
331 base::FundamentalValue(true));
332 recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
333 VerifyTimerIsStopped();
334 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
335 base::FundamentalValue(false));
337 VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
338 base::FundamentalValue(true));
339 recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
340 VerifyTimerIsStopped();
341 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
342 base::FundamentalValue(false));
344 VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
345 base::FundamentalValue(true));
346 recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
347 VerifyTimerIsStopped();
348 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
349 base::FundamentalValue(false));
351 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
352 base::FundamentalValue(true));
353 VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
354 base::FundamentalValue(ash::MAGNIFIER_FULL));
355 recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
356 recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
357 VerifyTimerIsStopped();
358 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
359 base::FundamentalValue(false));
360 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
361 base::FundamentalValue(0));
364 TEST_F(RecommendationRestorerTest, DoNothingOnUserChange) {
365 // Verifies that if no recommended values have been set and user settings
366 // change, the user settings are not cleared immediately and no timer is
367 // started that will clear the user settings eventually.
368 RegisterLoginProfilePrefs();
369 CreateLoginProfile();
371 prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
372 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
373 base::FundamentalValue(true));
374 VerifyTimerIsStopped();
376 prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
377 VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
378 base::FundamentalValue(true));
379 VerifyTimerIsStopped();
381 prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
382 VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
383 base::FundamentalValue(true));
384 VerifyTimerIsStopped();
386 prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
387 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
388 base::FundamentalValue(true));
389 VerifyTimerIsStopped();
391 prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
392 prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
393 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
394 base::FundamentalValue(true));
395 VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
396 base::FundamentalValue(ash::MAGNIFIER_FULL));
397 VerifyTimerIsStopped();
400 TEST_F(RecommendationRestorerTest, RestoreOnUserChange) {
401 // Verifies that if recommended values have been set and user settings change
402 // while the login screen is being shown, a timer is started that will clear
403 // the user settings eventually.
404 RegisterLoginProfilePrefs();
405 SetRecommendedValues();
407 CreateLoginProfile();
409 VerifyTimerIsStopped();
410 prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
411 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
412 base::FundamentalValue(true));
413 VerifyTimerIsRunning();
414 runner_->RunUntilIdle();
415 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
416 base::FundamentalValue(false));
418 VerifyTimerIsStopped();
419 prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
420 VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
421 base::FundamentalValue(true));
422 VerifyTimerIsRunning();
423 runner_->RunUntilIdle();
424 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
425 base::FundamentalValue(false));
427 VerifyTimerIsStopped();
428 prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
429 VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
430 base::FundamentalValue(true));
431 VerifyTimerIsRunning();
432 runner_->RunUntilIdle();
433 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
434 base::FundamentalValue(false));
436 VerifyTimerIsStopped();
437 prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
438 prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
439 VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
440 base::FundamentalValue(true));
441 VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
442 base::FundamentalValue(ash::MAGNIFIER_FULL));
443 VerifyTimerIsRunning();
444 runner_->RunUntilIdle();
445 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
446 base::FundamentalValue(false));
447 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
448 base::FundamentalValue(0));
450 VerifyTimerIsStopped();
453 TEST_F(RecommendationRestorerTest, RestoreOnSessionStart) {
454 // Verifies that if recommended values have been set, user settings have
455 // changed and a session is then started, the user settings are cleared
456 // immediately and the timer that would have cleared them eventually on the
457 // login screen is stopped.
458 RegisterLoginProfilePrefs();
459 SetRecommendedValues();
461 CreateLoginProfile();
462 SetUserSettings();
464 NotifyOfSessionStart();
465 VerifyPrefsFollowRecommendations();
466 VerifyTimerIsStopped();
469 TEST_F(RecommendationRestorerTest, DoNothingOnSessionStart) {
470 // Verifies that if recommended values have not been set, user settings have
471 // changed and a session is then started, the user settings are not cleared
472 // immediately.
473 RegisterLoginProfilePrefs();
474 CreateLoginProfile();
475 SetUserSettings();
477 NotifyOfSessionStart();
478 VerifyPrefsFollowUser();
479 VerifyTimerIsStopped();
482 TEST_F(RecommendationRestorerTest, UserActivityResetsTimer) {
483 // Verifies that user activity resets the timer which clears user settings.
484 RegisterLoginProfilePrefs();
486 recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
488 CreateLoginProfile();
490 prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
491 VerifyTimerIsRunning();
493 // Notify that there is user activity, then fast forward until the originally
494 // set timer fires.
495 NotifyOfUserActivity();
496 runner_->RunPendingTasks();
497 VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
498 base::FundamentalValue(true));
500 // Fast forward until the reset timer fires.
501 VerifyTimerIsRunning();
502 runner_->RunUntilIdle();
503 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
504 base::FundamentalValue(false));
505 VerifyTimerIsStopped();
508 } // namespace policy