Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_pref_store_unittest.cc
blobb88cb94c387ed24f0e5556cce8116aa44d007acc
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.
5 #include <set>
6 #include <string>
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"
17 using base::DictionaryValue;
18 using base::Value;
20 namespace {
22 class SupervisedUserPrefStoreFixture : public PrefStore::Observer {
23 public:
24 explicit SupervisedUserPrefStoreFixture(
25 SupervisedUserSettingsService* settings_service);
26 ~SupervisedUserPrefStoreFixture() override;
28 base::DictionaryValue* changed_prefs() {
29 return &changed_prefs_;
32 bool initialization_completed() const {
33 return initialization_completed_;
36 // PrefStore::Observer implementation:
37 void OnPrefValueChanged(const std::string& key) override;
38 void OnInitializationCompleted(bool succeeded) override;
40 private:
41 scoped_refptr<SupervisedUserPrefStore> pref_store_;
42 base::DictionaryValue changed_prefs_;
43 bool initialization_completed_;
46 SupervisedUserPrefStoreFixture::SupervisedUserPrefStoreFixture(
47 SupervisedUserSettingsService* settings_service)
48 : pref_store_(new SupervisedUserPrefStore(settings_service)),
49 initialization_completed_(pref_store_->IsInitializationComplete()) {
50 pref_store_->AddObserver(this);
53 SupervisedUserPrefStoreFixture::~SupervisedUserPrefStoreFixture() {
54 pref_store_->RemoveObserver(this);
57 void 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());
64 void SupervisedUserPrefStoreFixture::OnInitializationCompleted(bool succeeded) {
65 EXPECT_FALSE(initialization_completed_);
66 EXPECT_TRUE(succeeded);
67 EXPECT_TRUE(pref_store_->IsInitializationComplete());
68 initialization_completed_ = true;
71 } // namespace
73 class SupervisedUserPrefStoreTest : public ::testing::Test {
74 public:
75 SupervisedUserPrefStoreTest() : service_(nullptr) {}
76 void SetUp() override;
77 void TearDown() override;
79 protected:
80 SupervisedUserSettingsService service_;
81 scoped_refptr<TestingPrefStore> pref_store_;
84 void SupervisedUserPrefStoreTest::SetUp() {
85 pref_store_ = new TestingPrefStore();
86 service_.Init(pref_store_);
89 void SupervisedUserPrefStoreTest::TearDown() {
90 service_.Shutdown();
93 TEST_F(SupervisedUserPrefStoreTest, ConfigureSettings) {
94 SupervisedUserPrefStoreFixture fixture(&service_);
95 EXPECT_FALSE(fixture.initialization_completed());
97 // Prefs should not change yet when the service is ready, but not
98 // activated yet.
99 pref_store_->SetInitializationCompleted();
100 EXPECT_TRUE(fixture.initialization_completed());
101 EXPECT_EQ(0u, fixture.changed_prefs()->size());
103 service_.SetActive(true);
105 // kAllowDeletingBrowserHistory is hardcoded to false for supervised users.
106 bool allow_deleting_browser_history = true;
107 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(
108 prefs::kAllowDeletingBrowserHistory, &allow_deleting_browser_history));
109 EXPECT_FALSE(allow_deleting_browser_history);
111 // kSupervisedModeManualHosts does not have a hardcoded value.
112 base::DictionaryValue* manual_hosts = NULL;
113 EXPECT_FALSE(fixture.changed_prefs()->GetDictionary(
114 prefs::kSupervisedUserManualHosts, &manual_hosts));
116 // kForceGoogleSafeSearch and kForceYouTubeSafetyMode default to true for
117 // supervised users.
118 bool force_google_safesearch = false;
119 bool force_youtube_safety_mode = false;
120 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceGoogleSafeSearch,
121 &force_google_safesearch));
122 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(
123 prefs::kForceYouTubeSafetyMode, &force_youtube_safety_mode));
124 EXPECT_TRUE(force_google_safesearch);
125 EXPECT_TRUE(force_youtube_safety_mode);
127 // Activating the service again should not change anything.
128 fixture.changed_prefs()->Clear();
129 service_.SetActive(true);
130 EXPECT_EQ(0u, fixture.changed_prefs()->size());
132 // kSupervisedModeManualHosts can be configured by the custodian.
133 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
134 dict->SetBoolean("example.com", true);
135 dict->SetBoolean("moose.org", false);
136 service_.SetLocalSetting(
137 supervised_users::kContentPackManualBehaviorHosts,
138 scoped_ptr<base::Value>(dict->DeepCopy()));
139 EXPECT_EQ(1u, fixture.changed_prefs()->size());
140 ASSERT_TRUE(fixture.changed_prefs()->GetDictionary(
141 prefs::kSupervisedUserManualHosts, &manual_hosts));
142 EXPECT_TRUE(manual_hosts->Equals(dict.get()));
144 // kForceGoogleSafeSearch and kForceYouTubeSafetyMode can be configured by the
145 // custodian, overriding the hardcoded default.
146 fixture.changed_prefs()->Clear();
147 service_.SetLocalSetting(
148 supervised_users::kForceSafeSearch,
149 scoped_ptr<base::Value>(new base::FundamentalValue(false)));
150 EXPECT_EQ(1u, fixture.changed_prefs()->size());
151 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceGoogleSafeSearch,
152 &force_google_safesearch));
153 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(
154 prefs::kForceYouTubeSafetyMode, &force_youtube_safety_mode));
155 EXPECT_FALSE(force_youtube_safety_mode);
156 EXPECT_FALSE(force_google_safesearch);
159 TEST_F(SupervisedUserPrefStoreTest, ActivateSettingsBeforeInitialization) {
160 SupervisedUserPrefStoreFixture fixture(&service_);
161 EXPECT_FALSE(fixture.initialization_completed());
163 service_.SetActive(true);
164 EXPECT_FALSE(fixture.initialization_completed());
165 EXPECT_EQ(0u, fixture.changed_prefs()->size());
167 pref_store_->SetInitializationCompleted();
168 EXPECT_TRUE(fixture.initialization_completed());
169 EXPECT_EQ(0u, fixture.changed_prefs()->size());
172 TEST_F(SupervisedUserPrefStoreTest, CreatePrefStoreAfterInitialization) {
173 pref_store_->SetInitializationCompleted();
174 service_.SetActive(true);
176 SupervisedUserPrefStoreFixture fixture(&service_);
177 EXPECT_TRUE(fixture.initialization_completed());
178 EXPECT_EQ(0u, fixture.changed_prefs()->size());