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.
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
;
22 class SupervisedUserPrefStoreFixture
: public PrefStore::Observer
{
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
;
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;
73 class SupervisedUserPrefStoreTest
: public ::testing::Test
{
75 void SetUp() override
;
76 void TearDown() override
;
79 SupervisedUserSettingsService service_
;
80 scoped_refptr
<TestingPrefStore
> pref_store_
;
83 void SupervisedUserPrefStoreTest::SetUp() {
84 pref_store_
= new TestingPrefStore();
85 service_
.Init(pref_store_
);
88 void SupervisedUserPrefStoreTest::TearDown() {
92 TEST_F(SupervisedUserPrefStoreTest
, ConfigureSettings
) {
93 SupervisedUserPrefStoreFixture
fixture(&service_
);
94 EXPECT_FALSE(fixture
.initialization_completed());
96 // Prefs should not change yet when the service is ready, but not
98 pref_store_
->SetInitializationCompleted();
99 EXPECT_TRUE(fixture
.initialization_completed());
100 EXPECT_EQ(0u, fixture
.changed_prefs()->size());
102 service_
.SetActive(true);
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
);
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
));
115 // kForceGoogleSafeSearch and kForceYouTubeSafetyMode default to true for
117 bool force_google_safesearch
= false;
118 bool force_youtube_safety_mode
= false;
119 EXPECT_TRUE(fixture
.changed_prefs()->GetBoolean(prefs::kForceGoogleSafeSearch
,
120 &force_google_safesearch
));
121 EXPECT_TRUE(fixture
.changed_prefs()->GetBoolean(
122 prefs::kForceYouTubeSafetyMode
, &force_youtube_safety_mode
));
123 EXPECT_TRUE(force_google_safesearch
);
124 EXPECT_TRUE(force_youtube_safety_mode
);
126 // Activating the service again should not change anything.
127 fixture
.changed_prefs()->Clear();
128 service_
.SetActive(true);
129 EXPECT_EQ(0u, fixture
.changed_prefs()->size());
131 // kSupervisedModeManualHosts can be configured by the custodian.
132 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
133 dict
->SetBoolean("example.com", true);
134 dict
->SetBoolean("moose.org", false);
135 service_
.SetLocalSetting(
136 supervised_users::kContentPackManualBehaviorHosts
,
137 scoped_ptr
<base::Value
>(dict
->DeepCopy()));
138 EXPECT_EQ(1u, fixture
.changed_prefs()->size());
139 ASSERT_TRUE(fixture
.changed_prefs()->GetDictionary(
140 prefs::kSupervisedUserManualHosts
, &manual_hosts
));
141 EXPECT_TRUE(manual_hosts
->Equals(dict
.get()));
143 // kForceGoogleSafeSearch and kForceYouTubeSafetyMode can be configured by the
144 // custodian, overriding the hardcoded default.
145 fixture
.changed_prefs()->Clear();
146 service_
.SetLocalSetting(
147 supervised_users::kForceSafeSearch
,
148 scoped_ptr
<base::Value
>(new base::FundamentalValue(false)));
149 EXPECT_EQ(1u, fixture
.changed_prefs()->size());
150 EXPECT_TRUE(fixture
.changed_prefs()->GetBoolean(prefs::kForceGoogleSafeSearch
,
151 &force_google_safesearch
));
152 EXPECT_TRUE(fixture
.changed_prefs()->GetBoolean(
153 prefs::kForceYouTubeSafetyMode
, &force_youtube_safety_mode
));
154 EXPECT_FALSE(force_youtube_safety_mode
);
155 EXPECT_FALSE(force_google_safesearch
);
158 TEST_F(SupervisedUserPrefStoreTest
, ActivateSettingsBeforeInitialization
) {
159 SupervisedUserPrefStoreFixture
fixture(&service_
);
160 EXPECT_FALSE(fixture
.initialization_completed());
162 service_
.SetActive(true);
163 EXPECT_FALSE(fixture
.initialization_completed());
164 EXPECT_EQ(0u, fixture
.changed_prefs()->size());
166 pref_store_
->SetInitializationCompleted();
167 EXPECT_TRUE(fixture
.initialization_completed());
168 EXPECT_EQ(0u, fixture
.changed_prefs()->size());
171 TEST_F(SupervisedUserPrefStoreTest
, CreatePrefStoreAfterInitialization
) {
172 pref_store_
->SetInitializationCompleted();
173 service_
.SetActive(true);
175 SupervisedUserPrefStoreFixture
fixture(&service_
);
176 EXPECT_TRUE(fixture
.initialization_completed());
177 EXPECT_EQ(0u, fixture
.changed_prefs()->size());