Index: chrome/browser/managed_mode/supervised_user_pref_store_unittest.cc |
diff --git a/chrome/browser/managed_mode/supervised_user_pref_store_unittest.cc b/chrome/browser/managed_mode/supervised_user_pref_store_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..507d8257c3ba0f3adc9a34644e8167aca3e9f530 |
--- /dev/null |
+++ b/chrome/browser/managed_mode/supervised_user_pref_store_unittest.cc |
@@ -0,0 +1,207 @@ |
+// 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 <set> |
+#include <string> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/prefs/pref_store.h" |
+#include "base/values.h" |
+#include "chrome/browser/managed_mode/managed_user_constants.h" |
+#include "chrome/browser/managed_mode/managed_user_settings_service.h" |
+#include "chrome/browser/managed_mode/supervised_user_pref_store.h" |
+#include "chrome/common/pref_names.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+using base::DictionaryValue; |
+using base::Value; |
+ |
+namespace { |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// |
+// FakeManagedUserSettingsService |
+// |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+class FakeManagedUserSettingsService : public ManagedUserSettingsService { |
+ public: |
+ FakeManagedUserSettingsService(); |
+ virtual ~FakeManagedUserSettingsService(); |
+ |
+ // Takes ownership of |value|. |
+ void SetValueForSetting(const std::string& name, Value* value); |
+ |
+ void SetReady(); |
+ |
+ // ManagedUserSettingsService implementation: |
+ virtual scoped_ptr<DictionaryValue> GetSettings() const OVERRIDE; |
+ virtual bool IsReady() const OVERRIDE; |
+ |
+ private: |
+ bool is_ready_; |
+ DictionaryValue settings_; |
+}; |
+ |
+FakeManagedUserSettingsService::FakeManagedUserSettingsService() |
+ : is_ready_(false) {} |
+FakeManagedUserSettingsService::~FakeManagedUserSettingsService() {} |
+ |
+void FakeManagedUserSettingsService::SetValueForSetting(const std::string& name, |
+ Value* value) { |
+ settings_.SetWithoutPathExpansion(name, value); |
+ InformSubscribers(); |
+} |
+ |
+void FakeManagedUserSettingsService::SetReady() { |
+ is_ready_ = true; |
+ InformSubscribers(); |
+} |
+ |
+scoped_ptr<DictionaryValue> |
+FakeManagedUserSettingsService::GetSettings() const { |
+ return make_scoped_ptr(settings_.DeepCopy()); |
+} |
+ |
+bool FakeManagedUserSettingsService::IsReady() const { |
+ return is_ready_; |
+} |
+ |
+/////////////////////////////////////////////////////////////////////////////// |
+// |
+// SupervisedUserPrefStoreFixture |
+// |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+class SupervisedUserPrefStoreFixture : public PrefStore::Observer { |
+ public: |
+ explicit SupervisedUserPrefStoreFixture( |
+ ManagedUserSettingsService* settings_service); |
+ virtual ~SupervisedUserPrefStoreFixture(); |
+ |
+ DictionaryValue* changed_prefs() { |
+ return &changed_prefs_; |
+ } |
+ |
+ bool initialization_completed() const { |
+ return initialization_completed_; |
+ } |
+ |
+ // PrefStore::Observer implementation: |
+ virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
+ virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
+ |
+private: |
+ scoped_refptr<SupervisedUserPrefStore> pref_store_; |
+ DictionaryValue changed_prefs_; |
+ bool initialization_completed_; |
+}; |
+ |
+SupervisedUserPrefStoreFixture::SupervisedUserPrefStoreFixture( |
+ ManagedUserSettingsService* settings_service) |
+ : pref_store_(new SupervisedUserPrefStore(settings_service)), |
+ initialization_completed_(pref_store_->IsInitializationComplete()) { |
+ pref_store_->AddObserver(this); |
+} |
+ |
+SupervisedUserPrefStoreFixture::~SupervisedUserPrefStoreFixture() { |
+ pref_store_->RemoveObserver(this); |
+} |
+ |
+void SupervisedUserPrefStoreFixture::OnPrefValueChanged( |
+ const std::string& key) { |
+ const Value* value = NULL; |
+ ASSERT_TRUE(pref_store_->GetValue(key, &value)); |
+ changed_prefs_.Set(key, value->DeepCopy()); |
+} |
+ |
+void SupervisedUserPrefStoreFixture::OnInitializationCompleted(bool succeeded) { |
+ EXPECT_FALSE(initialization_completed_); |
+ EXPECT_TRUE(succeeded); |
+ EXPECT_TRUE(pref_store_->IsInitializationComplete()); |
+ initialization_completed_ = true; |
+} |
+ |
+} // namespace |
+ |
+TEST(SupervisedUserPrefStoreTest, ConfigureSettings) { |
+ FakeManagedUserSettingsService service; |
+ SupervisedUserPrefStoreFixture fixture(&service); |
+ EXPECT_FALSE(fixture.initialization_completed()); |
+ |
+ // Prefs should not change yet when the service is ready, but not |
+ // activated yet. |
+ service.SetReady(); |
+ EXPECT_TRUE(fixture.initialization_completed()); |
+ EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
+ |
+ service.Activate(); |
+ |
+ // kAllowDeletingBrowserHistory is hardcoded to false for managed users. |
+ bool allow_deleting_browser_history = true; |
+ EXPECT_TRUE(fixture.changed_prefs()->GetBoolean( |
+ prefs::kAllowDeletingBrowserHistory, &allow_deleting_browser_history)); |
+ EXPECT_FALSE(allow_deleting_browser_history); |
+ |
+ // kManagedModeManualHosts does not have a hardcoded value. |
+ DictionaryValue* manual_hosts = NULL; |
+ EXPECT_FALSE(fixture.changed_prefs()->GetDictionary( |
+ prefs::kManagedModeManualHosts, &manual_hosts)); |
+ |
+ // kForceSafeSearch defaults to true for managed users. |
+ bool force_safesearch = false; |
+ EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch, |
+ &force_safesearch)); |
+ EXPECT_TRUE(force_safesearch); |
+ |
+ // Activating the service again should not change anything. |
+ fixture.changed_prefs()->Clear(); |
+ service.Activate(); |
+ EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
+ |
+ // kManagedModeManualHosts can be configured by the custodian. |
+ scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
+ dict->SetBoolean("example.com", true); |
+ dict->SetBoolean("moose.org", false); |
+ service.SetValueForSetting(managed_users::kContentPackManualBehaviorHosts, |
+ dict->DeepCopy()); |
+ EXPECT_EQ(1u, fixture.changed_prefs()->size()); |
+ ASSERT_TRUE(fixture.changed_prefs()->GetDictionary( |
+ prefs::kManagedModeManualHosts, &manual_hosts)); |
+ EXPECT_TRUE(manual_hosts->Equals(dict.get())); |
+ |
+ // kForceSafeSearch can be configured by the custodian, overriding the |
+ // hardcoded default. |
+ fixture.changed_prefs()->Clear(); |
+ service.SetValueForSetting(managed_users::kForceSafeSearch, |
+ new base::FundamentalValue(false)); |
+ EXPECT_EQ(1u, fixture.changed_prefs()->size()); |
+ EXPECT_TRUE(fixture.changed_prefs()->GetBoolean(prefs::kForceSafeSearch, |
+ &force_safesearch)); |
+ EXPECT_FALSE(force_safesearch); |
+} |
+ |
+TEST(SupervisedUserPrefStoreTest, ActivateSettingsBeforeInitialization) { |
+ FakeManagedUserSettingsService service; |
+ SupervisedUserPrefStoreFixture fixture(&service); |
+ EXPECT_FALSE(fixture.initialization_completed()); |
+ |
+ service.Activate(); |
+ EXPECT_FALSE(fixture.initialization_completed()); |
+ EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
+ |
+ service.SetReady(); |
+ EXPECT_TRUE(fixture.initialization_completed()); |
+ EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
+} |
+ |
+TEST(SupervisedUserPrefStoreTest, CreatePrefStoreAfterInitialization) { |
+ FakeManagedUserSettingsService service; |
+ service.SetReady(); |
+ service.Activate(); |
+ |
+ SupervisedUserPrefStoreFixture fixture(&service); |
+ EXPECT_TRUE(fixture.initialization_completed()); |
+ EXPECT_EQ(0u, fixture.changed_prefs()->size()); |
+} |