OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/policy/configuration_policy_provider_test.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/policy/asynchronous_policy_loader.h" |
| 10 #include "chrome/browser/policy/asynchronous_policy_provider.h" |
| 11 #include "chrome/browser/policy/configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/policy_map.h" |
| 13 #include "policy/policy_constants.h" |
| 14 |
| 15 namespace policy { |
| 16 |
| 17 namespace test_policy_definitions { |
| 18 |
| 19 const char kKeyString[] = "StringPolicy"; |
| 20 const char kKeyBoolean[] = "BooleanPolicy"; |
| 21 const char kKeyInteger[] = "IntegerPolicy"; |
| 22 const char kKeyStringList[] = "StringListPolicy"; |
| 23 |
| 24 // In order to have correct enum values, we alias these to some actual policies. |
| 25 const ConfigurationPolicyType kPolicyString = kPolicyHomepageLocation; |
| 26 const ConfigurationPolicyType kPolicyBoolean = kPolicyHomepageIsNewTabPage; |
| 27 const ConfigurationPolicyType kPolicyInteger = kPolicyRestoreOnStartup; |
| 28 const ConfigurationPolicyType kPolicyStringList = kPolicyRestoreOnStartupURLs; |
| 29 |
| 30 static const PolicyDefinitionList::Entry kEntries[] = { |
| 31 { kPolicyString, base::Value::TYPE_STRING, kKeyString }, |
| 32 { kPolicyBoolean, base::Value::TYPE_BOOLEAN, kKeyBoolean }, |
| 33 { kPolicyInteger, base::Value::TYPE_INTEGER, kKeyInteger }, |
| 34 { kPolicyStringList, base::Value::TYPE_LIST, kKeyStringList }, |
| 35 }; |
| 36 |
| 37 const PolicyDefinitionList kList = { |
| 38 kEntries, kEntries + arraysize(kEntries) |
| 39 }; |
| 40 |
| 41 } // namespace test_policy_definitions |
| 42 |
| 43 PolicyProviderTestHarness::PolicyProviderTestHarness() {} |
| 44 |
| 45 PolicyProviderTestHarness::~PolicyProviderTestHarness() {} |
| 46 |
| 47 ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() {} |
| 48 |
| 49 ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} |
| 50 |
| 51 void ConfigurationPolicyProviderTest::SetUp() { |
| 52 AsynchronousPolicyTestBase::SetUp(); |
| 53 |
| 54 test_harness_.reset((*GetParam())()); |
| 55 test_harness_->SetUp(); |
| 56 |
| 57 provider_.reset( |
| 58 test_harness_->CreateProvider(&test_policy_definitions::kList)); |
| 59 |
| 60 PolicyMap policy_map; |
| 61 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 62 EXPECT_TRUE(policy_map.empty()); |
| 63 } |
| 64 |
| 65 void ConfigurationPolicyProviderTest::TearDown() { |
| 66 // Give providers the chance to clean up after themselves on the file thread. |
| 67 provider_.reset(); |
| 68 |
| 69 AsynchronousPolicyTestBase::TearDown(); |
| 70 } |
| 71 |
| 72 void ConfigurationPolicyProviderTest::CheckValue( |
| 73 const char* policy_name, |
| 74 ConfigurationPolicyType policy_type, |
| 75 const base::Value& expected_value, |
| 76 base::Closure install_value) { |
| 77 // Install the value, reload policy and check the provider for the value. |
| 78 install_value.Run(); |
| 79 provider_->ForceReload(); |
| 80 loop_.RunAllPending(); |
| 81 PolicyMap policy_map; |
| 82 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 83 EXPECT_EQ(1U, policy_map.size()); |
| 84 EXPECT_TRUE(base::Value::Equals(&expected_value, |
| 85 policy_map.Get(policy_type))); |
| 86 } |
| 87 |
| 88 TEST_P(ConfigurationPolicyProviderTest, Empty) { |
| 89 provider_->ForceReload(); |
| 90 loop_.RunAllPending(); |
| 91 PolicyMap policy_map; |
| 92 EXPECT_TRUE(provider_->Provide(&policy_map)); |
| 93 EXPECT_TRUE(policy_map.empty()); |
| 94 } |
| 95 |
| 96 TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
| 97 const char kTestString[] = "string_value"; |
| 98 StringValue expected_value(kTestString); |
| 99 CheckValue(test_policy_definitions::kKeyString, |
| 100 test_policy_definitions::kPolicyString, |
| 101 expected_value, |
| 102 base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
| 103 base::Unretained(test_harness_.get()), |
| 104 test_policy_definitions::kKeyString, |
| 105 kTestString)); |
| 106 } |
| 107 |
| 108 TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { |
| 109 base::FundamentalValue expected_value(true); |
| 110 CheckValue(test_policy_definitions::kKeyBoolean, |
| 111 test_policy_definitions::kPolicyBoolean, |
| 112 expected_value, |
| 113 base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, |
| 114 base::Unretained(test_harness_.get()), |
| 115 test_policy_definitions::kKeyBoolean, |
| 116 true)); |
| 117 } |
| 118 |
| 119 TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { |
| 120 base::FundamentalValue expected_value(42); |
| 121 CheckValue(test_policy_definitions::kKeyInteger, |
| 122 test_policy_definitions::kPolicyInteger, |
| 123 expected_value, |
| 124 base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, |
| 125 base::Unretained(test_harness_.get()), |
| 126 test_policy_definitions::kKeyInteger, |
| 127 42)); |
| 128 } |
| 129 |
| 130 TEST_P(ConfigurationPolicyProviderTest, StringListValue) { |
| 131 base::ListValue expected_value; |
| 132 expected_value.Set(0U, base::Value::CreateStringValue("first")); |
| 133 expected_value.Set(1U, base::Value::CreateStringValue("second")); |
| 134 CheckValue(test_policy_definitions::kKeyStringList, |
| 135 test_policy_definitions::kPolicyStringList, |
| 136 expected_value, |
| 137 base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
| 138 base::Unretained(test_harness_.get()), |
| 139 test_policy_definitions::kKeyStringList, |
| 140 &expected_value)); |
| 141 } |
| 142 |
| 143 } // namespace policy |
OLD | NEW |