summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/policy/recommendation_restorer_unittest.cc
blob: f0e94770c8b319dbe729997cba3e09bd9923fd8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/policy/recommendation_restorer.h"

#include "ash/magnifier/magnifier_constants.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_notifier_impl.h"
#include "base/prefs/testing_pref_store.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_simple_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/chromeos/policy/recommendation_restorer_factory.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/pref_service_syncable.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace policy {

namespace {
  // The amount of idle time after which recommended values are restored.
  const int kRestoreDelayInMs = 60 * 1000;  // 1 minute.
}  // namespace

class RecommendationRestorerTest : public testing::Test {
 protected:
  RecommendationRestorerTest();

  // testing::Test:
  virtual void SetUp() OVERRIDE;

  void RegisterUserProfilePrefs();
  void RegisterLoginProfilePrefs();

  void SetRecommendedValues();
  void SetUserSettings();

  void CreateLoginProfile();
  void CreateUserProfile();

  void NotifyOfSessionStart();
  void NotifyOfUserActivity();

  void VerifyPrefFollowsUser(const char* pref_name,
                             const base::Value& expected_value) const;
  void VerifyPrefsFollowUser() const;
  void VerifyPrefFollowsRecommendation(const char* pref_name,
                                       const base::Value& expected_value) const;
  void VerifyPrefsFollowRecommendations() const;

  void VerifyNotListeningForNotifications() const;
  void VerifyTimerIsStopped() const;
  void VerifyTimerIsRunning() const;

  TestingPrefStore* recommended_prefs_;  // Not owned.
  TestingPrefServiceSyncable* prefs_;    // Not owned.
  RecommendationRestorer* restorer_;     // Not owned.

  scoped_refptr<base::TestSimpleTaskRunner> runner_;
  base::ThreadTaskRunnerHandle runner_handler_;

 private:
  scoped_ptr<PrefServiceSyncable> prefs_owner_;

  TestingProfileManager profile_manager_;

  DISALLOW_COPY_AND_ASSIGN(RecommendationRestorerTest);
};

RecommendationRestorerTest::RecommendationRestorerTest()
    : recommended_prefs_(new TestingPrefStore),
      prefs_(new TestingPrefServiceSyncable(
          new TestingPrefStore,
          new TestingPrefStore,
          recommended_prefs_,
          new user_prefs::PrefRegistrySyncable,
          new PrefNotifierImpl)),
      restorer_(NULL),
      runner_(new base::TestSimpleTaskRunner),
      runner_handler_(runner_),
      prefs_owner_(prefs_),
      profile_manager_(TestingBrowserProcess::GetGlobal()) {
}

void RecommendationRestorerTest::SetUp() {
  testing::Test::SetUp();
  ASSERT_TRUE(profile_manager_.SetUp());
}

void RecommendationRestorerTest::RegisterUserProfilePrefs() {
  chrome::RegisterUserProfilePrefs(prefs_->registry());
}

void RecommendationRestorerTest::RegisterLoginProfilePrefs() {
  chrome::RegisterLoginProfilePrefs(prefs_->registry());
}

void RecommendationRestorerTest::SetRecommendedValues() {
  recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
  recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
  recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
  recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
  recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
}

void RecommendationRestorerTest::SetUserSettings() {
  prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
  prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
  prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
  prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
  prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
}

void RecommendationRestorerTest::CreateLoginProfile() {
  ASSERT_FALSE(restorer_);
  TestingProfile* profile = profile_manager_.CreateTestingProfile(
      chrome::kInitialProfile, prefs_owner_.Pass(),
      UTF8ToUTF16(chrome::kInitialProfile), 0, false);
  restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
  EXPECT_TRUE(restorer_);
}

void RecommendationRestorerTest::CreateUserProfile() {
  ASSERT_FALSE(restorer_);
  TestingProfile* profile = profile_manager_.CreateTestingProfile(
      "user", prefs_owner_.Pass(), UTF8ToUTF16("user"), 0, false);
  restorer_ = RecommendationRestorerFactory::GetForProfile(profile);
  EXPECT_TRUE(restorer_);
}

void RecommendationRestorerTest::NotifyOfSessionStart() {
  ASSERT_TRUE(restorer_);
  restorer_->Observe(chrome::NOTIFICATION_LOGIN_USER_CHANGED,
                     content::Source<RecommendationRestorerTest>(this),
                     content::NotificationService::NoDetails());
}

void RecommendationRestorerTest::NotifyOfUserActivity() {
  ASSERT_TRUE(restorer_);
  restorer_->OnUserActivity(NULL);
}

void RecommendationRestorerTest::VerifyPrefFollowsUser(
    const char* pref_name,
    const base::Value& expected_value) const {
  const PrefServiceSyncable::Preference* pref =
      prefs_->FindPreference(pref_name);
  ASSERT_TRUE(pref);
  EXPECT_TRUE(pref->HasUserSetting());
  const base::Value* value = pref->GetValue();
  ASSERT_TRUE(value);
  EXPECT_TRUE(expected_value.Equals(value));
}

void RecommendationRestorerTest::VerifyPrefsFollowUser() const {
  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
                        base::FundamentalValue(ash::MAGNIFIER_FULL));
}

void RecommendationRestorerTest::VerifyPrefFollowsRecommendation(
    const char* pref_name,
    const base::Value& expected_value) const {
  const PrefServiceSyncable::Preference* pref =
      prefs_->FindPreference(pref_name);
  ASSERT_TRUE(pref);
  EXPECT_TRUE(pref->IsRecommended());
  EXPECT_FALSE(pref->HasUserSetting());
  const base::Value* value = pref->GetValue();
  ASSERT_TRUE(value);
  EXPECT_TRUE(expected_value.Equals(value));
}

void RecommendationRestorerTest::VerifyPrefsFollowRecommendations() const {
  VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
                                  base::FundamentalValue(0));
}

void RecommendationRestorerTest::VerifyNotListeningForNotifications() const {
  ASSERT_TRUE(restorer_);
  EXPECT_TRUE(restorer_->pref_change_registrar_.IsEmpty());
  EXPECT_TRUE(restorer_->notification_registrar_.IsEmpty());
}

void RecommendationRestorerTest::VerifyTimerIsStopped() const {
  ASSERT_TRUE(restorer_);
  EXPECT_FALSE(restorer_->restore_timer_.IsRunning());
}

void RecommendationRestorerTest::VerifyTimerIsRunning() const {
  ASSERT_TRUE(restorer_);
  EXPECT_TRUE(restorer_->restore_timer_.IsRunning());
  EXPECT_EQ(base::TimeDelta::FromMilliseconds(kRestoreDelayInMs),
            restorer_->restore_timer_.GetCurrentDelay());
}

TEST_F(RecommendationRestorerTest, CreateForUserProfile) {
  // Verifies that when a RecommendationRestorer is created for a user profile,
  // it does not start listening for any notifications, does not clear user
  // settings on initialization and does not start a timer that will clear user
  // settings eventually.
  RegisterUserProfilePrefs();
  SetRecommendedValues();
  SetUserSettings();

  CreateUserProfile();
  VerifyNotListeningForNotifications();
  VerifyPrefsFollowUser();
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, NoRecommendations) {
  // Verifies that when no recommended values have been set and a
  // RecommendationRestorer is created for the login profile, it does not clear
  // user settings on initialization and does not start a timer that will clear
  // user settings eventually.
  RegisterLoginProfilePrefs();
  SetUserSettings();

  CreateLoginProfile();
  VerifyPrefsFollowUser();
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, RestoreOnStartup) {
  // Verifies that when recommended values have been set and a
  // RecommendationRestorer is created for the login profile, it clears user
  // settings on initialization.
  RegisterLoginProfilePrefs();
  SetRecommendedValues();
  SetUserSettings();

  CreateLoginProfile();
  VerifyPrefsFollowRecommendations();
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeOnLoginScreen) {
  // Verifies that if recommended values change while the login screen is being
  // shown, a timer is started that will clear user settings eventually.
  RegisterLoginProfilePrefs();
  SetUserSettings();

  CreateLoginProfile();

  VerifyTimerIsStopped();
  recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
  VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
  VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
  recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
                        base::FundamentalValue(ash::MAGNIFIER_FULL));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
                                  base::FundamentalValue(0));

  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, RestoreOnRecommendationChangeInUserSession) {
  // Verifies that if recommended values change while a user session is in
  // progress, user settings are cleared immediately.
  RegisterLoginProfilePrefs();
  SetUserSettings();

  CreateLoginProfile();
  NotifyOfSessionStart();

  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));
  recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);
  VerifyTimerIsStopped();
  VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
                                  base::FundamentalValue(false));

  VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
                        base::FundamentalValue(true));
  recommended_prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, false);
  VerifyTimerIsStopped();
  VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
                                  base::FundamentalValue(false));

  VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
                        base::FundamentalValue(true));
  recommended_prefs_->SetBoolean(prefs::kHighContrastEnabled, false);
  VerifyTimerIsStopped();
  VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
                                  base::FundamentalValue(false));

  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
                        base::FundamentalValue(ash::MAGNIFIER_FULL));
  recommended_prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, false);
  recommended_prefs_->SetInteger(prefs::kScreenMagnifierType, 0);
  VerifyTimerIsStopped();
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
                                  base::FundamentalValue(0));
}

TEST_F(RecommendationRestorerTest, DoNothingOnUserChange) {
  // Verifies that if no recommended values have been set and user settings
  // change, the user settings are not cleared immediately and no timer is
  // started that will clear the user settings eventually.
  RegisterLoginProfilePrefs();
  CreateLoginProfile();

  prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsStopped();

  prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
  VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsStopped();

  prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
  VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsStopped();

  prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsStopped();

  prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
  prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
                        base::FundamentalValue(ash::MAGNIFIER_FULL));
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, RestoreOnUserChange) {
  // Verifies that if recommended values have been set and user settings change
  // while the login screen is being shown, a timer is started that will clear
  // the user settings eventually.
  RegisterLoginProfilePrefs();
  SetRecommendedValues();

  CreateLoginProfile();

  VerifyTimerIsStopped();
  prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  prefs_->SetBoolean(prefs::kSpokenFeedbackEnabled, true);
  VerifyPrefFollowsUser(prefs::kSpokenFeedbackEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  prefs_->SetBoolean(prefs::kHighContrastEnabled, true);
  VerifyPrefFollowsUser(prefs::kHighContrastEnabled,
                        base::FundamentalValue(true));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled,
                                  base::FundamentalValue(false));

  VerifyTimerIsStopped();
  prefs_->SetBoolean(prefs::kScreenMagnifierEnabled, true);
  prefs_->SetInteger(prefs::kScreenMagnifierType, ash::MAGNIFIER_FULL);
  VerifyPrefFollowsUser(prefs::kScreenMagnifierEnabled,
                        base::FundamentalValue(true));
  VerifyPrefFollowsUser(prefs::kScreenMagnifierType,
                        base::FundamentalValue(ash::MAGNIFIER_FULL));
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled,
                                  base::FundamentalValue(false));
  VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType,
                                  base::FundamentalValue(0));

  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, RestoreOnSessionStart) {
  // Verifies that if recommended values have been set, user settings have
  // changed and a session is then started, the user settings are cleared
  // immediately and the timer that would have cleared them eventually on the
  // login screen is stopped.
  RegisterLoginProfilePrefs();
  SetRecommendedValues();

  CreateLoginProfile();
  SetUserSettings();

  NotifyOfSessionStart();
  VerifyPrefsFollowRecommendations();
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, DoNothingOnSessionStart) {
  // Verifies that if recommended values have not been set, user settings have
  // changed and a session is then started, the user settings are not cleared
  // immediately.
  RegisterLoginProfilePrefs();
  CreateLoginProfile();
  SetUserSettings();

  NotifyOfSessionStart();
  VerifyPrefsFollowUser();
  VerifyTimerIsStopped();
}

TEST_F(RecommendationRestorerTest, UserActivityResetsTimer) {
  // Verifies that user activity resets the timer which clears user settings.
  RegisterLoginProfilePrefs();

  recommended_prefs_->SetBoolean(prefs::kLargeCursorEnabled, false);

  CreateLoginProfile();

  prefs_->SetBoolean(prefs::kLargeCursorEnabled, true);
  VerifyTimerIsRunning();

  // Notify that there is user activity, then fast forward until the originally
  // set timer fires.
  NotifyOfUserActivity();
  runner_->RunPendingTasks();
  VerifyPrefFollowsUser(prefs::kLargeCursorEnabled,
                        base::FundamentalValue(true));

  // Fast forward until the reset timer fires.
  VerifyTimerIsRunning();
  runner_->RunUntilIdle();
  VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled,
                                  base::FundamentalValue(false));
  VerifyTimerIsStopped();
}

}  // namespace policy