Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Unified Diff: chrome/browser/policy/configuration_policy_reader_unittest.cc

Issue 7585036: First CL for the about:policy page. This only implements the policy section of the page. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created MockConfigurationPolicyReader and fixed other nits. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/policy/configuration_policy_reader_unittest.cc
diff --git a/chrome/browser/policy/configuration_policy_reader_unittest.cc b/chrome/browser/policy/configuration_policy_reader_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e1ca234f8d574520b1aa9b0560dde1b213a12b49
--- /dev/null
+++ b/chrome/browser/policy/configuration_policy_reader_unittest.cc
@@ -0,0 +1,317 @@
+// Copyright (c) 2011 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 "base/scoped_ptr.h"
+#include "base/string16.h"
+#include "base/utf_string_conversions.h"
+#include "chrome/browser/policy/configuration_policy_pref_store.h"
+#include "chrome/browser/policy/configuration_policy_reader.h"
+#include "chrome/browser/policy/mock_configuration_policy_provider.h"
+#include "chrome/browser/policy/mock_configuration_policy_reader.h"
+#include "policy/policy_constants.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace policy {
+
+class ConfigurationPolicyReaderTest : public testing::Test {
+ protected:
+ ConfigurationPolicyReaderTest() : provider_() {
+ managed_reader_.reset(new ConfigurationPolicyReader(&provider_,
+ PolicyStatusInfo::MANDATORY));
Mattias Nissler (ping if slow) 2011/08/29 11:24:40 indentation
+ recommended_reader_.reset(new ConfigurationPolicyReader(&provider_,
+ PolicyStatusInfo::RECOMMENDED));
Mattias Nissler (ping if slow) 2011/08/29 11:24:40 indentation
+ status_ok_ = ASCIIToUTF16("ok");
+ }
+
+ DictionaryValue* CreateDictionary(const char* policy_name,
+ Value* policy_value) {
+ DictionaryValue* dict = new DictionaryValue();
+ dict->SetString(
+ PolicyStatusInfo::name_dict_path, ASCIIToUTF16(policy_name));
+ dict->SetString(PolicyStatusInfo::level_dict_path,
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY));
+ dict->SetString(PolicyStatusInfo::source_type_dict_path,
+ PolicyStatusInfo::GetSourceTypeString(PolicyStatusInfo::USER));
+ dict->Set(PolicyStatusInfo::value_dict_path, policy_value);
+ dict->SetBoolean(PolicyStatusInfo::set_dict_path, true);
+ dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_);
+
+ return dict;
+ }
+
+ MockConfigurationPolicyProvider provider_;
+ scoped_ptr<ConfigurationPolicyReader> managed_reader_;
+ scoped_ptr<ConfigurationPolicyReader> recommended_reader_;
+ string16 status_ok_;
+};
+
+TEST_F(ConfigurationPolicyReaderTest, GetDefault) {
+ EXPECT_EQ(NULL, managed_reader_->GetPolicyStatus(kPolicyHomepageLocation));
+}
+
+// Test for list-valued policy settings.
+TEST_F(ConfigurationPolicyReaderTest, SetListValue) {
+ ListValue* in_value = new ListValue();
+ in_value->Append(Value::CreateStringValue("test1"));
+ in_value->Append(Value::CreateStringValue("test2"));
+ provider_.AddPolicy(kPolicyRestoreOnStartupURLs, in_value);
+ managed_reader_->OnUpdatePolicy();
+
+ scoped_ptr<DictionaryValue>
+ dict(CreateDictionary(key::kRestoreOnStartupURLs, in_value->DeepCopy()));
+ EXPECT_TRUE(
+ dict->Equals(managed_reader_->
+ GetPolicyStatus(kPolicyRestoreOnStartupURLs)));
Mattias Nissler (ping if slow) 2011/08/29 11:24:40 another 4 spaces indentation (or break after the o
+
+ recommended_reader_->OnUpdatePolicy();
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED));
+ EXPECT_TRUE(dict->Equals(
+ recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartupURLs)));
+}
+
+// Test for string-valued policy settings.
+TEST_F(ConfigurationPolicyReaderTest, SetStringValue) {
+ provider_.AddPolicy(kPolicyHomepageLocation,
+ Value::CreateStringValue("http://chromium.org"));
+ managed_reader_->OnUpdatePolicy();
+ scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kHomepageLocation,
+ Value::CreateStringValue("http://chromium.org")));
+ EXPECT_TRUE(
+ dict->Equals(managed_reader_->GetPolicyStatus(kPolicyHomepageLocation)));
+
+ recommended_reader_->OnUpdatePolicy();
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED));
+ EXPECT_TRUE(dict->Equals(
+ recommended_reader_->GetPolicyStatus(kPolicyHomepageLocation)));
+}
+
+// Test for boolean-valued policy settings.
+TEST_F(ConfigurationPolicyReaderTest, SetBooleanValue) {
+ provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(true));
+ managed_reader_->OnUpdatePolicy();
+ scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kShowHomeButton,
+ Value::CreateBooleanValue(true)));
+ EXPECT_TRUE(dict->Equals(
+ managed_reader_->GetPolicyStatus(kPolicyShowHomeButton)));
+
+ recommended_reader_->OnUpdatePolicy();
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED));
+ EXPECT_TRUE(dict->Equals(
+ recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton)));
+
+ provider_.AddPolicy(kPolicyShowHomeButton, Value::CreateBooleanValue(false));
+ managed_reader_->OnUpdatePolicy();
+ dict->Set(
+ PolicyStatusInfo::value_dict_path, Value::CreateBooleanValue(false));
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::MANDATORY));
+ EXPECT_TRUE(
+ dict->Equals(managed_reader_->GetPolicyStatus(kPolicyShowHomeButton)));
+
+ recommended_reader_->OnUpdatePolicy();
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED));
+ EXPECT_TRUE(dict->Equals(
+ recommended_reader_->GetPolicyStatus(kPolicyShowHomeButton)));
+}
+
+// Test for integer-valued policy settings.
+TEST_F(ConfigurationPolicyReaderTest, SetValue) {
+ provider_.AddPolicy(kPolicyRestoreOnStartup, Value::CreateIntegerValue(3));
+ managed_reader_->OnUpdatePolicy();
+ scoped_ptr<DictionaryValue> dict(CreateDictionary(key::kRestoreOnStartup,
+ Value::CreateIntegerValue(3)));
+ EXPECT_TRUE(dict->Equals(
+ managed_reader_->GetPolicyStatus(kPolicyRestoreOnStartup)));
+
+ recommended_reader_->OnUpdatePolicy();
+ dict->SetString("level",
+ PolicyStatusInfo::GetPolicyLevelString(PolicyStatusInfo::RECOMMENDED));
+ EXPECT_TRUE(dict->Equals(
+ recommended_reader_->GetPolicyStatus(kPolicyRestoreOnStartup)));
+}
+
+class PolicyStatusTest : public testing::Test {
+ protected:
+ typedef ConfigurationPolicyProvider::PolicyDefinitionList
+ PolicyDefinitionList;
+
+ PolicyStatusTest()
+ : managed_platform_provider_(),
+ managed_cloud_provider_(),
+ recommended_platform_provider_(),
+ recommended_cloud_provider_() {
+ managed_platform_ =
+ new MockConfigurationPolicyReader(&managed_platform_provider_,
+ PolicyStatusInfo::MANDATORY);
+ managed_cloud_ =
+ new MockConfigurationPolicyReader(&managed_cloud_provider_,
+ PolicyStatusInfo::MANDATORY);
+ recommended_platform_ =
+ new MockConfigurationPolicyReader(&recommended_platform_provider_,
+ PolicyStatusInfo::RECOMMENDED);
+ recommended_cloud_ =
+ new MockConfigurationPolicyReader(&recommended_cloud_provider_,
+ PolicyStatusInfo::RECOMMENDED);
+
+ policy_status_.reset(new PolicyStatus(managed_platform_,
+ managed_cloud_,
+ recommended_platform_,
+ recommended_cloud_));
+ policy_list_ =
+ ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList();
+ status_ok_ = ASCIIToUTF16("ok");
+ }
+
+ void UpdatePolicyReaders() {
+ managed_platform_->OnUpdatePolicy();
+ managed_cloud_->OnUpdatePolicy();
+ recommended_platform_->OnUpdatePolicy();
+ recommended_cloud_->OnUpdatePolicy();
+ }
+
+ void SetDictionaryPaths(DictionaryValue* dict,
+ const char* policy_name,
+ bool defined,
+ PolicyStatusInfo::PolicyLevel level) {
+ dict->SetString(PolicyStatusInfo::name_dict_path,
+ ASCIIToUTF16(policy_name));
+ if (defined) {
+ dict->SetString(PolicyStatusInfo::level_dict_path,
+ PolicyStatusInfo::GetPolicyLevelString(level));
+ }
+ }
+
+ MockConfigurationPolicyProvider managed_platform_provider_;
Mattias Nissler (ping if slow) 2011/08/29 11:24:40 Why do you still need providers? I think you can j
+ MockConfigurationPolicyProvider managed_cloud_provider_;
+ MockConfigurationPolicyProvider recommended_platform_provider_;
+ MockConfigurationPolicyProvider recommended_cloud_provider_;
+ MockConfigurationPolicyReader* managed_platform_;
+ MockConfigurationPolicyReader* managed_cloud_;
+ MockConfigurationPolicyReader* recommended_platform_;
+ MockConfigurationPolicyReader* recommended_cloud_;
+ scoped_ptr<PolicyStatus> policy_status_;
+ const PolicyDefinitionList* policy_list_;
+ string16 status_ok_;
+};
+
+TEST_F(PolicyStatusTest, GetPolicyStatusList) {
+ bool any_policies_sent;
+ scoped_ptr<ListValue> status_list(
+ policy_status_->GetPolicyStatusList(&any_policies_sent));
+
+ EXPECT_FALSE(any_policies_sent);
+
+ size_t policy_list_size =
+ static_cast<size_t>(policy_list_->end - policy_list_->begin);
+
+ EXPECT_EQ(policy_list_size, status_list->GetSize());
+
+ managed_platform_provider_.AddPolicy(kPolicyInstantEnabled,
+ Value::CreateBooleanValue(true));
+ managed_cloud_provider_.AddPolicy(kPolicyDisablePluginFinder,
+ Value::CreateBooleanValue(true));
+ recommended_platform_provider_.AddPolicy(kPolicySyncDisabled,
+ Value::CreateBooleanValue(true));
+ recommended_cloud_provider_.AddPolicy(kPolicyTranslateEnabled,
+ Value::CreateBooleanValue(true));
+ UpdatePolicyReaders();
+
+ status_list.reset(policy_status_->GetPolicyStatusList(&any_policies_sent));
+ EXPECT_TRUE(any_policies_sent);
+ EXPECT_EQ(policy_list_size, status_list->GetSize());
+
+ scoped_ptr<DictionaryValue> undefined_dict(new DictionaryValue());
+ undefined_dict->SetString(PolicyStatusInfo::level_dict_path,
+ PolicyStatusInfo::GetPolicyLevelString(
+ PolicyStatusInfo::LEVEL_UNDEFINED));
+ undefined_dict->SetString(PolicyStatusInfo::source_type_dict_path,
+ PolicyStatusInfo::GetSourceTypeString(
+ PolicyStatusInfo::SOURCE_TYPE_UNDEFINED));
+ undefined_dict->Set(PolicyStatusInfo::value_dict_path,
+ Value::CreateNullValue());
+ undefined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, false);
+ undefined_dict->SetString(PolicyStatusInfo::status_dict_path, string16());
+
+ scoped_ptr<DictionaryValue> defined_dict(new DictionaryValue());
+ defined_dict->SetString(PolicyStatusInfo::source_type_dict_path,
+ PolicyStatusInfo::GetSourceTypeString(
+ PolicyStatusInfo::USER));
+ defined_dict->Set(PolicyStatusInfo::value_dict_path,
+ Value::CreateBooleanValue(true));
+ defined_dict->SetBoolean(PolicyStatusInfo::set_dict_path, true);
+ defined_dict->SetString(PolicyStatusInfo::status_dict_path, status_ok_);
+
+ for (const PolicyDefinitionList::Entry* entry = policy_list_->begin;
+ entry != policy_list_->end; ++entry) {
+ Value* status_dict = NULL;
+
+ // Every policy in |policy_list_| has to appear in the returned ListValue.
+ string16 name = ASCIIToUTF16(entry->name);
+ for (ListValue::const_iterator status_entry = status_list->begin();
+ status_entry != status_list->end();
+ ++status_entry) {
+ string16 value;
+ ASSERT_TRUE((*status_entry)->IsType(Value::TYPE_DICTIONARY));
+ DictionaryValue* dict = static_cast<DictionaryValue*>(*status_entry);
+ ASSERT_TRUE(dict->GetString(PolicyStatusInfo::name_dict_path, &value));
+
+ if (value == name)
+ status_dict = *status_entry;
+ }
+
+ ASSERT_FALSE(status_dict == NULL);
+
+ switch (entry->policy_type) {
+ case kPolicyInstantEnabled:
+ SetDictionaryPaths(defined_dict.get(),
+ entry->name,
+ true,
+ PolicyStatusInfo::MANDATORY);
+ EXPECT_TRUE(defined_dict->Equals(status_dict));
+ break;
+ case kPolicyDisablePluginFinder:
+ SetDictionaryPaths(defined_dict.get(),
+ entry->name,
+ true,
+ PolicyStatusInfo::MANDATORY);
+ EXPECT_TRUE(defined_dict->Equals(status_dict));
+ break;
+ case kPolicySyncDisabled:
+ SetDictionaryPaths(defined_dict.get(),
+ entry->name,
+ true,
+ PolicyStatusInfo::RECOMMENDED);
+ EXPECT_TRUE(defined_dict->Equals(status_dict));
+ break;
+ case kPolicyTranslateEnabled:
+ SetDictionaryPaths(defined_dict.get(),
+ entry->name,
+ true,
+ PolicyStatusInfo::RECOMMENDED);
+ EXPECT_TRUE(defined_dict->Equals(status_dict));
+ break;
+ default:
+ SetDictionaryPaths(undefined_dict.get(),
+ entry->name,
+ false,
+ PolicyStatusInfo::LEVEL_UNDEFINED);
+ EXPECT_TRUE(undefined_dict->Equals(status_dict));
+ break;
+ }
+ }
+}
+
+TEST_F(PolicyStatusTest, GetPolicyName) {
+ for (const PolicyDefinitionList::Entry* entry = policy_list_->begin;
+ entry != policy_list_->end; ++entry) {
+ EXPECT_EQ(ASCIIToUTF16(entry->name),
+ PolicyStatus::GetPolicyName(entry->policy_type));
+ }
+}
+
+} // namespace policy

Powered by Google App Engine
This is Rietveld 408576698