1// Copyright 2014 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 <set>
6#include <string>
7
8#include "base/memory/ref_counted.h"
9#include "base/prefs/testing_pref_store.h"
10#include "base/values.h"
11#include "chrome/browser/supervised_user/supervised_user_constants.h"
12#include "chrome/browser/supervised_user/supervised_user_pref_store.h"
13#include "chrome/browser/supervised_user/supervised_user_settings_service.h"
14#include "chrome/common/pref_names.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17using base::DictionaryValue;
18using base::Value;
19
20namespace {
21
22class SupervisedUserPrefStoreFixture : public PrefStore::Observer {
23 public:
24  explicit SupervisedUserPrefStoreFixture(
25      SupervisedUserSettingsService* settings_service);
26  virtual ~SupervisedUserPrefStoreFixture();
27
28  base::DictionaryValue* changed_prefs() {
29    return &changed_prefs_;
30  }
31
32  bool initialization_completed() const {
33    return initialization_completed_;
34  }
35
36  // PrefStore::Observer implementation:
37  virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
38  virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
39
40 private:
41  scoped_refptr<SupervisedUserPrefStore> pref_store_;
42  base::DictionaryValue changed_prefs_;
43  bool initialization_completed_;
44};
45
46SupervisedUserPrefStoreFixture::SupervisedUserPrefStoreFixture(
47    SupervisedUserSettingsService* settings_service)
48    : pref_store_(new SupervisedUserPrefStore(settings_service)),
49      initialization_completed_(pref_store_->IsInitializationComplete()) {
50  pref_store_->AddObserver(this);
51}
52
53SupervisedUserPrefStoreFixture::~SupervisedUserPrefStoreFixture() {
54  pref_store_->RemoveObserver(this);
55}
56
57void SupervisedUserPrefStoreFixture::OnPrefValueChanged(
58    const std::string& key) {
59  const base::Value* value = NULL;
60  ASSERT_TRUE(pref_store_->GetValue(key, &value));
61  changed_prefs_.Set(key, value->DeepCopy());
62}
63
64void SupervisedUserPrefStoreFixture::OnInitializationCompleted(bool succeeded) {
65  EXPECT_FALSE(initialization_completed_);
66  EXPECT_TRUE(succeeded);
67  EXPECT_TRUE(pref_store_->IsInitializationComplete());
68  initialization_completed_ = true;
69}
70
71}  // namespace
72
73class SupervisedUserPrefStoreTest : public ::testing::Test {
74 public:
75  virtual void SetUp() OVERRIDE;
76  virtual void TearDown() OVERRIDE;
77
78 protected:
79  SupervisedUserSettingsService service_;
80  scoped_refptr<TestingPrefStore> pref_store_;
81};
82
83void SupervisedUserPrefStoreTest::SetUp() {
84  pref_store_ = new TestingPrefStore();
85  service_.Init(pref_store_);
86}
87
88void SupervisedUserPrefStoreTest::TearDown() {
89  service_.Shutdown();
90}
91
92TEST_F(SupervisedUserPrefStoreTest, ConfigureSettings) {
93  SupervisedUserPrefStoreFixture fixture(&service_);
94  EXPECT_FALSE(fixture.initialization_completed());
95
96  // Prefs should not change yet when the service is ready, but not
97  // activated yet.
98  pref_store_->SetInitializationCompleted();
99  EXPECT_TRUE(fixture.initialization_completed());
100  EXPECT_EQ(0u, fixture.changed_prefs()->size());
101
102  service_.SetActive(true);
103
104  // kAllowDeletingBrowserHistory is hardcoded to false for supervised users.
105  bool allow_deleting_browser_history = true;
106  EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(
107      prefs::kAllowDeletingBrowserHistory, &allow_deleting_browser_history));
108  EXPECT_FALSE(allow_deleting_browser_history);
109
110  // kSupervisedModeManualHosts does not have a hardcoded value.
111  base::DictionaryValue* manual_hosts = NULL;
112  EXPECT_FALSE(fixture.changed_prefs()->GetDictionary(
113      prefs::kSupervisedUserManualHosts, &manual_hosts));
114
115  // kForceSafeSearch defaults to true for supervised users.
116  bool force_safesearch = false;
117  EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch,
118                                                  &force_safesearch));
119  EXPECT_TRUE(force_safesearch);
120
121  // Activating the service again should not change anything.
122  fixture.changed_prefs()->Clear();
123  service_.SetActive(true);
124  EXPECT_EQ(0u, fixture.changed_prefs()->size());
125
126  // kSupervisedModeManualHosts can be configured by the custodian.
127  scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
128  dict->SetBoolean("example.com", true);
129  dict->SetBoolean("moose.org", false);
130  service_.SetLocalSettingForTesting(
131      supervised_users::kContentPackManualBehaviorHosts,
132      scoped_ptr<base::Value>(dict->DeepCopy()));
133  EXPECT_EQ(1u, fixture.changed_prefs()->size());
134  ASSERT_TRUE(fixture.changed_prefs()->GetDictionary(
135      prefs::kSupervisedUserManualHosts, &manual_hosts));
136  EXPECT_TRUE(manual_hosts->Equals(dict.get()));
137
138  // kForceSafeSearch can be configured by the custodian, overriding the
139  // hardcoded default.
140  fixture.changed_prefs()->Clear();
141  service_.SetLocalSettingForTesting(
142      supervised_users::kForceSafeSearch,
143      scoped_ptr<base::Value>(new base::FundamentalValue(false)));
144  EXPECT_EQ(1u, fixture.changed_prefs()->size());
145  EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch,
146                                                  &force_safesearch));
147  EXPECT_FALSE(force_safesearch);
148}
149
150TEST_F(SupervisedUserPrefStoreTest, ActivateSettingsBeforeInitialization) {
151  SupervisedUserPrefStoreFixture fixture(&service_);
152  EXPECT_FALSE(fixture.initialization_completed());
153
154  service_.SetActive(true);
155  EXPECT_FALSE(fixture.initialization_completed());
156  EXPECT_EQ(0u, fixture.changed_prefs()->size());
157
158  pref_store_->SetInitializationCompleted();
159  EXPECT_TRUE(fixture.initialization_completed());
160  EXPECT_EQ(0u, fixture.changed_prefs()->size());
161}
162
163TEST_F(SupervisedUserPrefStoreTest, CreatePrefStoreAfterInitialization) {
164  pref_store_->SetInitializationCompleted();
165  service_.SetActive(true);
166
167  SupervisedUserPrefStoreFixture fixture(&service_);
168  EXPECT_TRUE(fixture.initialization_completed());
169  EXPECT_EQ(0u, fixture.changed_prefs()->size());
170}
171
172