Chromium Code Reviews| Index: chrome/browser/policy/configuration_policy_provider_test.cc |
| diff --git a/chrome/browser/policy/configuration_policy_provider_test.cc b/chrome/browser/policy/configuration_policy_provider_test.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d2b73c4754dd737fb80f6b99ba1c124ee8680a3f |
| --- /dev/null |
| +++ b/chrome/browser/policy/configuration_policy_provider_test.cc |
| @@ -0,0 +1,146 @@ |
| +// 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 "chrome/browser/policy/configuration_policy_provider_test.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/policy/asynchronous_policy_loader.h" |
| +#include "chrome/browser/policy/asynchronous_policy_provider.h" |
| +#include "chrome/browser/policy/configuration_policy_provider.h" |
| +#include "chrome/browser/policy/policy_map.h" |
| +#include "policy/policy_constants.h" |
| + |
| +namespace policy { |
| + |
| +namespace test_policy_definitions { |
| + |
| +const char kKeyString[] = "StringPolicy"; |
| +const char kKeyBoolean[] = "BooleanPolicy"; |
| +const char kKeyInteger[] = "IntegerPolicy"; |
| +const char kKeyStringList[] = "StringListPolicy"; |
| + |
| +// In order to have correct enum values, we alias these to some actual policies. |
| +extern const ConfigurationPolicyType kPolicyString = |
|
Joao da Silva
2011/11/09 15:37:47
Nit: remove extern here and on the 3 next too
Mattias Nissler (ping if slow)
2011/11/09 17:04:11
Done.
|
| + kPolicyHomepageLocation; |
| +extern const ConfigurationPolicyType kPolicyBoolean = |
| + kPolicyHomepageIsNewTabPage; |
| +extern const ConfigurationPolicyType kPolicyInteger = |
| + kPolicyRestoreOnStartup; |
| +extern const ConfigurationPolicyType kPolicyStringList = |
| + kPolicyRestoreOnStartupURLs; |
| + |
| +static const PolicyDefinitionList::Entry kEntries[] = { |
|
Joao da Silva
2011/11/09 15:37:47
Nit: not sure if we prefer an anonymous namespace
Mattias Nissler (ping if slow)
2011/11/09 17:04:11
I think the additional noise generated by the anon
|
| + { kPolicyString, base::Value::TYPE_STRING, kKeyString }, |
| + { kPolicyBoolean, base::Value::TYPE_BOOLEAN, kKeyBoolean }, |
| + { kPolicyInteger, base::Value::TYPE_INTEGER, kKeyInteger }, |
| + { kPolicyStringList, base::Value::TYPE_LIST, kKeyStringList }, |
| +}; |
| + |
| +const PolicyDefinitionList kList = { |
| + kEntries, kEntries + arraysize(kEntries) |
| +}; |
| + |
| +} // namespace test_policy_definitions |
| + |
| +PolicyProviderTestHarness::PolicyProviderTestHarness() {} |
| + |
| +PolicyProviderTestHarness::~PolicyProviderTestHarness() {} |
| + |
| +ConfigurationPolicyProviderTest::ConfigurationPolicyProviderTest() |
| + : ui_thread_(content::BrowserThread::UI, &loop_), |
| + file_thread_(content::BrowserThread::FILE, &loop_) {} |
| + |
| +ConfigurationPolicyProviderTest::~ConfigurationPolicyProviderTest() {} |
| + |
| +void ConfigurationPolicyProviderTest::SetUp() { |
| + test_harness_.reset((*GetParam())()); |
| + test_harness_->SetUp(); |
| + |
| + provider_.reset( |
| + test_harness_->CreateProvider(&test_policy_definitions::kList)); |
| + |
| + PolicyMap policy_map; |
| + EXPECT_TRUE(provider_->Provide(&policy_map)); |
| + EXPECT_TRUE(policy_map.empty()); |
| +} |
| + |
| +void ConfigurationPolicyProviderTest::TearDown() { |
| + // Give providers the chance to clean up after themselves on the file thread. |
| + provider_.reset(); |
| + loop_.RunAllPending(); |
| +} |
| + |
| +void ConfigurationPolicyProviderTest::CheckValue( |
| + const char* policy_name, |
| + ConfigurationPolicyType policy_type, |
| + const base::Value& expected_value, |
| + base::Closure install_value) { |
| + // Install the value, reload policy and check the provider for the value. |
| + install_value.Run(); |
| + provider_->ForceReload(); |
| + loop_.RunAllPending(); |
| + PolicyMap policy_map; |
| + EXPECT_TRUE(provider_->Provide(&policy_map)); |
| + EXPECT_EQ(1U, policy_map.size()); |
| + EXPECT_TRUE(base::Value::Equals(&expected_value, |
| + policy_map.Get(policy_type))); |
| +} |
| + |
| +TEST_P(ConfigurationPolicyProviderTest, Empty) { |
| + provider_->ForceReload(); |
| + loop_.RunAllPending(); |
| + PolicyMap policy_map; |
| + EXPECT_TRUE(provider_->Provide(&policy_map)); |
| + EXPECT_TRUE(policy_map.empty()); |
| +} |
| + |
| +TEST_P(ConfigurationPolicyProviderTest, StringValue) { |
| + const char kTestString[] = "string_value"; |
| + StringValue expected_value(kTestString); |
| + CheckValue(test_policy_definitions::kKeyString, |
| + test_policy_definitions::kPolicyString, |
| + expected_value, |
| + base::Bind(&PolicyProviderTestHarness::InstallStringPolicy, |
| + base::Unretained(test_harness_.get()), |
| + test_policy_definitions::kKeyString, |
| + kTestString)); |
| +} |
| + |
| +TEST_P(ConfigurationPolicyProviderTest, BooleanValue) { |
| + base::FundamentalValue expected_value(true); |
| + CheckValue(test_policy_definitions::kKeyBoolean, |
| + test_policy_definitions::kPolicyBoolean, |
| + expected_value, |
| + base::Bind(&PolicyProviderTestHarness::InstallBooleanPolicy, |
| + base::Unretained(test_harness_.get()), |
| + test_policy_definitions::kKeyBoolean, |
| + true)); |
| +} |
| + |
| +TEST_P(ConfigurationPolicyProviderTest, IntegerValue) { |
| + base::FundamentalValue expected_value(42); |
| + CheckValue(test_policy_definitions::kKeyInteger, |
| + test_policy_definitions::kPolicyInteger, |
| + expected_value, |
| + base::Bind(&PolicyProviderTestHarness::InstallIntegerPolicy, |
| + base::Unretained(test_harness_.get()), |
| + test_policy_definitions::kKeyInteger, |
| + 42)); |
| +} |
| + |
| +TEST_P(ConfigurationPolicyProviderTest, StringListValue) { |
| + base::ListValue expected_value; |
| + expected_value.Set(0U, base::Value::CreateStringValue("first")); |
| + expected_value.Set(1U, base::Value::CreateStringValue("second")); |
| + CheckValue(test_policy_definitions::kKeyStringList, |
| + test_policy_definitions::kPolicyStringList, |
| + expected_value, |
| + base::Bind(&PolicyProviderTestHarness::InstallStringListPolicy, |
| + base::Unretained(test_harness_.get()), |
| + test_policy_definitions::kKeyStringList, |
| + &expected_value)); |
| +} |
| + |
| +} // namespace policy |