OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <set> |
| 6 #include <string> |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/prefs/pref_store.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/managed_mode/managed_user_constants.h" |
| 12 #include "chrome/browser/managed_mode/managed_user_settings_service.h" |
| 13 #include "chrome/browser/managed_mode/supervised_user_pref_store.h" |
| 14 #include "chrome/common/pref_names.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 using base::DictionaryValue; |
| 18 using base::Value; |
| 19 |
| 20 namespace { |
| 21 |
| 22 /////////////////////////////////////////////////////////////////////////////// |
| 23 // |
| 24 // FakeManagedUserSettingsService |
| 25 // |
| 26 /////////////////////////////////////////////////////////////////////////////// |
| 27 |
| 28 class FakeManagedUserSettingsService : public ManagedUserSettingsService { |
| 29 public: |
| 30 FakeManagedUserSettingsService(); |
| 31 virtual ~FakeManagedUserSettingsService(); |
| 32 |
| 33 // Takes ownership of |value|. |
| 34 void SetValueForSetting(const std::string& name, Value* value); |
| 35 |
| 36 void SetReady(); |
| 37 |
| 38 // ManagedUserSettingsService implementation: |
| 39 virtual scoped_ptr<DictionaryValue> GetSettings() const OVERRIDE; |
| 40 virtual bool IsReady() const OVERRIDE; |
| 41 |
| 42 private: |
| 43 bool is_ready_; |
| 44 DictionaryValue settings_; |
| 45 }; |
| 46 |
| 47 FakeManagedUserSettingsService::FakeManagedUserSettingsService() |
| 48 : is_ready_(false) {} |
| 49 FakeManagedUserSettingsService::~FakeManagedUserSettingsService() {} |
| 50 |
| 51 void FakeManagedUserSettingsService::SetValueForSetting(const std::string& name, |
| 52 Value* value) { |
| 53 settings_.SetWithoutPathExpansion(name, value); |
| 54 InformSubscribers(); |
| 55 } |
| 56 |
| 57 void FakeManagedUserSettingsService::SetReady() { |
| 58 is_ready_ = true; |
| 59 InformSubscribers(); |
| 60 } |
| 61 |
| 62 scoped_ptr<DictionaryValue> |
| 63 FakeManagedUserSettingsService::GetSettings() const { |
| 64 return make_scoped_ptr(settings_.DeepCopy()); |
| 65 } |
| 66 |
| 67 bool FakeManagedUserSettingsService::IsReady() const { |
| 68 return is_ready_; |
| 69 } |
| 70 |
| 71 /////////////////////////////////////////////////////////////////////////////// |
| 72 // |
| 73 // SupervisedUserPrefStoreFixture |
| 74 // |
| 75 /////////////////////////////////////////////////////////////////////////////// |
| 76 |
| 77 class SupervisedUserPrefStoreFixture : public PrefStore::Observer { |
| 78 public: |
| 79 explicit SupervisedUserPrefStoreFixture( |
| 80 ManagedUserSettingsService* settings_service); |
| 81 virtual ~SupervisedUserPrefStoreFixture(); |
| 82 |
| 83 DictionaryValue* changed_prefs() { |
| 84 return &changed_prefs_; |
| 85 } |
| 86 |
| 87 bool initialization_completed() const { |
| 88 return initialization_completed_; |
| 89 } |
| 90 |
| 91 // PrefStore::Observer implementation: |
| 92 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 93 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 94 |
| 95 private: |
| 96 scoped_refptr<SupervisedUserPrefStore> pref_store_; |
| 97 DictionaryValue changed_prefs_; |
| 98 bool initialization_completed_; |
| 99 }; |
| 100 |
| 101 SupervisedUserPrefStoreFixture::SupervisedUserPrefStoreFixture( |
| 102 ManagedUserSettingsService* settings_service) |
| 103 : pref_store_(new SupervisedUserPrefStore(settings_service)), |
| 104 initialization_completed_(pref_store_->IsInitializationComplete()) { |
| 105 pref_store_->AddObserver(this); |
| 106 } |
| 107 |
| 108 SupervisedUserPrefStoreFixture::~SupervisedUserPrefStoreFixture() { |
| 109 pref_store_->RemoveObserver(this); |
| 110 } |
| 111 |
| 112 void SupervisedUserPrefStoreFixture::OnPrefValueChanged( |
| 113 const std::string& key) { |
| 114 const Value* value = NULL; |
| 115 ASSERT_TRUE(pref_store_->GetValue(key, &value)); |
| 116 changed_prefs_.Set(key, value->DeepCopy()); |
| 117 } |
| 118 |
| 119 void SupervisedUserPrefStoreFixture::OnInitializationCompleted(bool succeeded) { |
| 120 EXPECT_FALSE(initialization_completed_); |
| 121 EXPECT_TRUE(succeeded); |
| 122 EXPECT_TRUE(pref_store_->IsInitializationComplete()); |
| 123 initialization_completed_ = true; |
| 124 } |
| 125 |
| 126 } // namespace |
| 127 |
| 128 TEST(SupervisedUserPrefStoreTest, ConfigureSettings) { |
| 129 FakeManagedUserSettingsService service; |
| 130 SupervisedUserPrefStoreFixture fixture(&service); |
| 131 EXPECT_FALSE(fixture.initialization_completed()); |
| 132 |
| 133 // Prefs should not change yet when the service is ready, but not |
| 134 // activated yet. |
| 135 service.SetReady(); |
| 136 EXPECT_TRUE(fixture.initialization_completed()); |
| 137 EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
| 138 |
| 139 service.Activate(); |
| 140 |
| 141 // kAllowDeletingBrowserHistory is hardcoded to false for managed users. |
| 142 bool allow_deleting_browser_history = true; |
| 143 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean( |
| 144 prefs::kAllowDeletingBrowserHistory, &allow_deleting_browser_history)); |
| 145 EXPECT_FALSE(allow_deleting_browser_history); |
| 146 |
| 147 // kManagedModeManualHosts does not have a hardcoded value. |
| 148 DictionaryValue* manual_hosts = NULL; |
| 149 EXPECT_FALSE(fixture.changed_prefs()->GetDictionary( |
| 150 prefs::kManagedModeManualHosts, &manual_hosts)); |
| 151 |
| 152 // kForceSafeSearch defaults to true for managed users. |
| 153 bool force_safesearch = false; |
| 154 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch, |
| 155 &force_safesearch)); |
| 156 EXPECT_TRUE(force_safesearch); |
| 157 |
| 158 // Activating the service again should not change anything. |
| 159 fixture.changed_prefs()->Clear(); |
| 160 service.Activate(); |
| 161 EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
| 162 |
| 163 // kManagedModeManualHosts can be configured by the custodian. |
| 164 scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
| 165 dict->SetBoolean("example.com", true); |
| 166 dict->SetBoolean("moose.org", false); |
| 167 service.SetValueForSetting(managed_users::kContentPackManualBehaviorHosts, |
| 168 dict->DeepCopy()); |
| 169 EXPECT_EQ(1u, fixture.changed_prefs()->size()); |
| 170 ASSERT_TRUE(fixture.changed_prefs()->GetDictionary( |
| 171 prefs::kManagedModeManualHosts, &manual_hosts)); |
| 172 EXPECT_TRUE(manual_hosts->Equals(dict.get())); |
| 173 |
| 174 // kForceSafeSearch can be configured by the custodian, overriding the |
| 175 // hardcoded default. |
| 176 fixture.changed_prefs()->Clear(); |
| 177 service.SetValueForSetting(managed_users::kForceSafeSearch, |
| 178 new base::FundamentalValue(false)); |
| 179 EXPECT_EQ(1u, fixture.changed_prefs()->size()); |
| 180 EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch, |
| 181 &force_safesearch)); |
| 182 EXPECT_FALSE(force_safesearch); |
| 183 } |
| 184 |
| 185 TEST(SupervisedUserPrefStoreTest, ActivateSettingsBeforeInitialization) { |
| 186 FakeManagedUserSettingsService service; |
| 187 SupervisedUserPrefStoreFixture fixture(&service); |
| 188 EXPECT_FALSE(fixture.initialization_completed()); |
| 189 |
| 190 service.Activate(); |
| 191 EXPECT_FALSE(fixture.initialization_completed()); |
| 192 EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
| 193 |
| 194 service.SetReady(); |
| 195 EXPECT_TRUE(fixture.initialization_completed()); |
| 196 EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
| 197 } |
| 198 |
| 199 TEST(SupervisedUserPrefStoreTest, CreatePrefStoreAfterInitialization) { |
| 200 FakeManagedUserSettingsService service; |
| 201 service.SetReady(); |
| 202 service.Activate(); |
| 203 |
| 204 SupervisedUserPrefStoreFixture fixture(&service); |
| 205 EXPECT_TRUE(fixture.initialization_completed()); |
| 206 EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
| 207 } |
OLD | NEW |