Index: components/policy/core/common/policy_loader_mac_unittest.cc |
diff --git a/components/policy/core/common/policy_loader_mac_unittest.cc b/components/policy/core/common/policy_loader_mac_unittest.cc |
index a37c8e6e87471a3108634031612fa86f27185c84..a7e34019bbdd90f283f9fa5ec71038e21be011d9 100644 |
--- a/components/policy/core/common/policy_loader_mac_unittest.cc |
+++ b/components/policy/core/common/policy_loader_mac_unittest.cc |
@@ -16,6 +16,7 @@ |
#include "components/policy/core/common/policy_bundle.h" |
#include "components/policy/core/common/policy_loader_mac.h" |
#include "components/policy/core/common/policy_map.h" |
+#include "components/policy/core/common/policy_test_utils.h" |
#include "components/policy/core/common/preferences_mock_mac.h" |
#include "testing/gtest/include/gtest/gtest.h" |
@@ -25,99 +26,6 @@ namespace policy { |
namespace { |
-// Converts a base::Value to the equivalent CFPropertyListRef. |
-// The returned value is owned by the caller. |
-CFPropertyListRef CreatePropertyFromValue(const base::Value* value) { |
- switch (value->GetType()) { |
- case base::Value::TYPE_NULL: |
- return kCFNull; |
- |
- case base::Value::TYPE_BOOLEAN: { |
- bool bool_value; |
- if (value->GetAsBoolean(&bool_value)) |
- return bool_value ? kCFBooleanTrue : kCFBooleanFalse; |
- break; |
- } |
- |
- case base::Value::TYPE_INTEGER: { |
- int int_value; |
- if (value->GetAsInteger(&int_value)) { |
- return CFNumberCreate( |
- kCFAllocatorDefault, kCFNumberIntType, &int_value); |
- } |
- break; |
- } |
- |
- case base::Value::TYPE_DOUBLE: { |
- double double_value; |
- if (value->GetAsDouble(&double_value)) { |
- return CFNumberCreate( |
- kCFAllocatorDefault, kCFNumberDoubleType, &double_value); |
- } |
- break; |
- } |
- |
- case base::Value::TYPE_STRING: { |
- std::string string_value; |
- if (value->GetAsString(&string_value)) |
- return base::SysUTF8ToCFStringRef(string_value); |
- break; |
- } |
- |
- case base::Value::TYPE_DICTIONARY: { |
- const base::DictionaryValue* dict_value; |
- if (value->GetAsDictionary(&dict_value)) { |
- // |dict| is owned by the caller. |
- CFMutableDictionaryRef dict = |
- CFDictionaryCreateMutable(kCFAllocatorDefault, |
- dict_value->size(), |
- &kCFTypeDictionaryKeyCallBacks, |
- &kCFTypeDictionaryValueCallBacks); |
- for (base::DictionaryValue::Iterator iterator(*dict_value); |
- !iterator.IsAtEnd(); iterator.Advance()) { |
- // CFDictionaryAddValue() retains both |key| and |value|, so make sure |
- // the references are balanced. |
- ScopedCFTypeRef<CFStringRef> key( |
- base::SysUTF8ToCFStringRef(iterator.key())); |
- ScopedCFTypeRef<CFPropertyListRef> cf_value( |
- CreatePropertyFromValue(&iterator.value())); |
- if (cf_value) |
- CFDictionaryAddValue(dict, key, cf_value); |
- } |
- return dict; |
- } |
- break; |
- } |
- |
- case base::Value::TYPE_LIST: { |
- const base::ListValue* list; |
- if (value->GetAsList(&list)) { |
- CFMutableArrayRef array = |
- CFArrayCreateMutable(NULL, list->GetSize(), &kCFTypeArrayCallBacks); |
- for (base::ListValue::const_iterator it(list->begin()); |
- it != list->end(); ++it) { |
- // CFArrayAppendValue() retains |value|, so make sure the reference |
- // created by CreatePropertyFromValue() is released. |
- ScopedCFTypeRef<CFPropertyListRef> cf_value( |
- CreatePropertyFromValue(*it)); |
- if (cf_value) |
- CFArrayAppendValue(array, cf_value); |
- } |
- return array; |
- } |
- break; |
- } |
- |
- case base::Value::TYPE_BINARY: |
- // This type isn't converted (though it can be represented as CFData) |
- // because there's no equivalent JSON type, and policy values can only |
- // take valid JSON values. |
- break; |
- } |
- |
- return NULL; |
-} |
- |
class TestHarness : public PolicyProviderTestHarness { |
public: |
TestHarness(); |
@@ -195,8 +103,7 @@ void TestHarness::InstallBooleanPolicy(const std::string& policy_name, |
void TestHarness::InstallStringListPolicy(const std::string& policy_name, |
const base::ListValue* policy_value) { |
ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); |
- ScopedCFTypeRef<CFPropertyListRef> array( |
- CreatePropertyFromValue(policy_value)); |
+ ScopedCFTypeRef<CFPropertyListRef> array(ValueToProperty(policy_value)); |
ASSERT_TRUE(array); |
prefs_->AddTestItem(name, array, true); |
} |
@@ -205,8 +112,7 @@ void TestHarness::InstallDictionaryPolicy( |
const std::string& policy_name, |
const base::DictionaryValue* policy_value) { |
ScopedCFTypeRef<CFStringRef> name(base::SysUTF8ToCFStringRef(policy_name)); |
- ScopedCFTypeRef<CFPropertyListRef> dict( |
- CreatePropertyFromValue(policy_value)); |
+ ScopedCFTypeRef<CFPropertyListRef> dict(ValueToProperty(policy_value)); |
ASSERT_TRUE(dict); |
prefs_->AddTestItem(name, dict, true); |
} |
@@ -291,50 +197,4 @@ TEST_F(PolicyLoaderMacTest, TestNonForcedValue) { |
EXPECT_TRUE(provider_->policies().Equals(expected_bundle)); |
} |
-TEST_F(PolicyLoaderMacTest, TestConversions) { |
- base::DictionaryValue root; |
- |
- // base::Value::TYPE_NULL |
- root.Set("null", base::Value::CreateNullValue()); |
- |
- // base::Value::TYPE_BOOLEAN |
- root.SetBoolean("false", false); |
- root.SetBoolean("true", true); |
- |
- // base::Value::TYPE_INTEGER |
- root.SetInteger("int", 123); |
- root.SetInteger("zero", 0); |
- |
- // base::Value::TYPE_DOUBLE |
- root.SetDouble("double", 123.456); |
- root.SetDouble("zerod", 0.0); |
- |
- // base::Value::TYPE_STRING |
- root.SetString("string", "the fox jumps over something"); |
- root.SetString("empty", ""); |
- |
- // base::Value::TYPE_LIST |
- base::ListValue list; |
- root.Set("emptyl", list.DeepCopy()); |
- for (base::DictionaryValue::Iterator it(root); !it.IsAtEnd(); it.Advance()) |
- list.Append(it.value().DeepCopy()); |
- EXPECT_EQ(root.size(), list.GetSize()); |
- list.Append(root.DeepCopy()); |
- root.Set("list", list.DeepCopy()); |
- |
- // base::Value::TYPE_DICTIONARY |
- base::DictionaryValue dict; |
- root.Set("emptyd", dict.DeepCopy()); |
- // Very meta. |
- root.Set("dict", root.DeepCopy()); |
- |
- ScopedCFTypeRef<CFPropertyListRef> property(CreatePropertyFromValue(&root)); |
- ASSERT_TRUE(property); |
- scoped_ptr<base::Value> value( |
- PolicyLoaderMac::CreateValueFromProperty(property)); |
- ASSERT_TRUE(value.get()); |
- |
- EXPECT_TRUE(root.Equals(value.get())); |
-} |
- |
} // namespace policy |