1 // Copyright 2013 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.
5 #include "components/policy/core/common/registry_dict_win.h"
9 #include "base/values.h"
10 #include "components/policy/core/common/schema.h"
11 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(RegistryDictTest
, SetAndGetValue
) {
17 RegistryDict test_dict
;
19 base::FundamentalValue
int_value(42);
20 base::StringValue
string_value("fortytwo");
22 test_dict
.SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
23 EXPECT_EQ(1, test_dict
.values().size());
24 EXPECT_TRUE(base::Value::Equals(&int_value
, test_dict
.GetValue("one")));
25 EXPECT_FALSE(test_dict
.GetValue("two"));
27 test_dict
.SetValue("two", make_scoped_ptr(string_value
.DeepCopy()));
28 EXPECT_EQ(2, test_dict
.values().size());
29 EXPECT_TRUE(base::Value::Equals(&int_value
, test_dict
.GetValue("one")));
30 EXPECT_TRUE(base::Value::Equals(&string_value
, test_dict
.GetValue("two")));
32 scoped_ptr
<base::Value
> one(test_dict
.RemoveValue("one"));
33 EXPECT_EQ(1, test_dict
.values().size());
34 EXPECT_TRUE(base::Value::Equals(&int_value
, one
.get()));
35 EXPECT_FALSE(test_dict
.GetValue("one"));
36 EXPECT_TRUE(base::Value::Equals(&string_value
, test_dict
.GetValue("two")));
38 test_dict
.ClearValues();
39 EXPECT_FALSE(test_dict
.GetValue("one"));
40 EXPECT_FALSE(test_dict
.GetValue("two"));
41 EXPECT_TRUE(test_dict
.values().empty());
44 TEST(RegistryDictTest
, CaseInsensitiveButPreservingValueNames
) {
45 RegistryDict test_dict
;
47 base::FundamentalValue
int_value(42);
48 base::StringValue
string_value("fortytwo");
50 test_dict
.SetValue("One", make_scoped_ptr(int_value
.DeepCopy()));
51 EXPECT_EQ(1, test_dict
.values().size());
52 EXPECT_TRUE(base::Value::Equals(&int_value
, test_dict
.GetValue("oNe")));
54 RegistryDict::ValueMap::const_iterator entry
= test_dict
.values().begin();
55 ASSERT_NE(entry
, test_dict
.values().end());
56 EXPECT_EQ("One", entry
->first
);
58 test_dict
.SetValue("ONE", make_scoped_ptr(string_value
.DeepCopy()));
59 EXPECT_EQ(1, test_dict
.values().size());
60 EXPECT_TRUE(base::Value::Equals(&string_value
, test_dict
.GetValue("one")));
62 scoped_ptr
<base::Value
> removed_value(test_dict
.RemoveValue("onE"));
63 EXPECT_TRUE(base::Value::Equals(&string_value
, removed_value
.get()));
64 EXPECT_TRUE(test_dict
.values().empty());
67 TEST(RegistryDictTest
, SetAndGetKeys
) {
68 RegistryDict test_dict
;
70 base::FundamentalValue
int_value(42);
71 base::StringValue
string_value("fortytwo");
73 scoped_ptr
<RegistryDict
> subdict(new RegistryDict());
74 subdict
->SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
75 test_dict
.SetKey("two", subdict
.Pass());
76 EXPECT_EQ(1, test_dict
.keys().size());
77 RegistryDict
* actual_subdict
= test_dict
.GetKey("two");
78 ASSERT_TRUE(actual_subdict
);
79 EXPECT_TRUE(base::Value::Equals(&int_value
, actual_subdict
->GetValue("one")));
81 subdict
.reset(new RegistryDict());
82 subdict
->SetValue("three", make_scoped_ptr(string_value
.DeepCopy()));
83 test_dict
.SetKey("four", subdict
.Pass());
84 EXPECT_EQ(2, test_dict
.keys().size());
85 actual_subdict
= test_dict
.GetKey("two");
86 ASSERT_TRUE(actual_subdict
);
87 EXPECT_TRUE(base::Value::Equals(&int_value
, actual_subdict
->GetValue("one")));
88 actual_subdict
= test_dict
.GetKey("four");
89 ASSERT_TRUE(actual_subdict
);
90 EXPECT_TRUE(base::Value::Equals(&string_value
,
91 actual_subdict
->GetValue("three")));
93 test_dict
.ClearKeys();
94 EXPECT_FALSE(test_dict
.GetKey("one"));
95 EXPECT_FALSE(test_dict
.GetKey("three"));
96 EXPECT_TRUE(test_dict
.keys().empty());
99 TEST(RegistryDictTest
, CaseInsensitiveButPreservingKeyNames
) {
100 RegistryDict test_dict
;
102 base::FundamentalValue
int_value(42);
104 test_dict
.SetKey("One", make_scoped_ptr(new RegistryDict()));
105 EXPECT_EQ(1, test_dict
.keys().size());
106 RegistryDict
* actual_subdict
= test_dict
.GetKey("One");
107 ASSERT_TRUE(actual_subdict
);
108 EXPECT_TRUE(actual_subdict
->values().empty());
110 RegistryDict::KeyMap::const_iterator entry
= test_dict
.keys().begin();
111 ASSERT_NE(entry
, test_dict
.keys().end());
112 EXPECT_EQ("One", entry
->first
);
114 scoped_ptr
<RegistryDict
> subdict(new RegistryDict());
115 subdict
->SetValue("two", make_scoped_ptr(int_value
.DeepCopy()));
116 test_dict
.SetKey("ONE", subdict
.Pass());
117 EXPECT_EQ(1, test_dict
.keys().size());
118 actual_subdict
= test_dict
.GetKey("One");
119 ASSERT_TRUE(actual_subdict
);
120 EXPECT_TRUE(base::Value::Equals(&int_value
,
121 actual_subdict
->GetValue("two")));
123 scoped_ptr
<RegistryDict
> removed_key(test_dict
.RemoveKey("one"));
124 ASSERT_TRUE(removed_key
);
125 EXPECT_TRUE(base::Value::Equals(&int_value
,
126 removed_key
->GetValue("two")));
127 EXPECT_TRUE(test_dict
.keys().empty());
130 TEST(RegistryDictTest
, Merge
) {
134 base::FundamentalValue
int_value(42);
135 base::StringValue
string_value("fortytwo");
137 dict_a
.SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
138 scoped_ptr
<RegistryDict
> subdict(new RegistryDict());
139 subdict
->SetValue("two", make_scoped_ptr(string_value
.DeepCopy()));
140 dict_a
.SetKey("three", subdict
.Pass());
142 dict_b
.SetValue("four", make_scoped_ptr(string_value
.DeepCopy()));
143 subdict
.reset(new RegistryDict());
144 subdict
->SetValue("two", make_scoped_ptr(int_value
.DeepCopy()));
145 dict_b
.SetKey("three", subdict
.Pass());
146 subdict
.reset(new RegistryDict());
147 subdict
->SetValue("five", make_scoped_ptr(int_value
.DeepCopy()));
148 dict_b
.SetKey("six", subdict
.Pass());
150 dict_a
.Merge(dict_b
);
152 EXPECT_TRUE(base::Value::Equals(&int_value
, dict_a
.GetValue("one")));
153 EXPECT_TRUE(base::Value::Equals(&string_value
, dict_b
.GetValue("four")));
154 RegistryDict
* actual_subdict
= dict_a
.GetKey("three");
155 ASSERT_TRUE(actual_subdict
);
156 EXPECT_TRUE(base::Value::Equals(&int_value
, actual_subdict
->GetValue("two")));
157 actual_subdict
= dict_a
.GetKey("six");
158 ASSERT_TRUE(actual_subdict
);
159 EXPECT_TRUE(base::Value::Equals(&int_value
,
160 actual_subdict
->GetValue("five")));
163 TEST(RegistryDictTest
, Swap
) {
167 base::FundamentalValue
int_value(42);
168 base::StringValue
string_value("fortytwo");
170 dict_a
.SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
171 dict_a
.SetKey("two", make_scoped_ptr(new RegistryDict()));
172 dict_b
.SetValue("three", make_scoped_ptr(string_value
.DeepCopy()));
174 dict_a
.Swap(&dict_b
);
176 EXPECT_TRUE(base::Value::Equals(&int_value
, dict_b
.GetValue("one")));
177 EXPECT_TRUE(dict_b
.GetKey("two"));
178 EXPECT_FALSE(dict_b
.GetValue("two"));
180 EXPECT_TRUE(base::Value::Equals(&string_value
, dict_a
.GetValue("three")));
181 EXPECT_FALSE(dict_a
.GetValue("one"));
182 EXPECT_FALSE(dict_a
.GetKey("two"));
185 TEST(RegistryDictTest
, ConvertToJSON
) {
186 RegistryDict test_dict
;
188 base::FundamentalValue
int_value(42);
189 base::StringValue
string_value("fortytwo");
190 base::StringValue
string_zero("0");
191 base::StringValue
string_dict("{ \"key\": [ \"value\" ] }");
193 test_dict
.SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
194 scoped_ptr
<RegistryDict
> subdict(new RegistryDict());
195 subdict
->SetValue("two", make_scoped_ptr(string_value
.DeepCopy()));
196 test_dict
.SetKey("three", subdict
.Pass());
197 scoped_ptr
<RegistryDict
> list(new RegistryDict());
198 list
->SetValue("1", make_scoped_ptr(string_value
.DeepCopy()));
199 test_dict
.SetKey("dict-to-list", list
.Pass());
200 test_dict
.SetValue("int-to-bool", make_scoped_ptr(int_value
.DeepCopy()));
201 test_dict
.SetValue("int-to-double", make_scoped_ptr(int_value
.DeepCopy()));
202 test_dict
.SetValue("string-to-bool", make_scoped_ptr(string_zero
.DeepCopy()));
203 test_dict
.SetValue("string-to-double",
204 make_scoped_ptr(string_zero
.DeepCopy()));
205 test_dict
.SetValue("string-to-int", make_scoped_ptr(string_zero
.DeepCopy()));
206 test_dict
.SetValue("string-to-dict", make_scoped_ptr(string_dict
.DeepCopy()));
209 Schema schema
= Schema::Parse(
211 " \"type\": \"object\","
213 " \"dict-to-list\": {"
214 " \"type\": \"array\","
215 " \"items\": { \"type\": \"string\" }"
217 " \"int-to-bool\": { \"type\": \"boolean\" },"
218 " \"int-to-double\": { \"type\": \"number\" },"
219 " \"string-to-bool\": { \"type\": \"boolean\" },"
220 " \"string-to-double\": { \"type\": \"number\" },"
221 " \"string-to-int\": { \"type\": \"integer\" },"
222 " \"string-to-dict\": { \"type\": \"object\" }"
225 ASSERT_TRUE(schema
.valid()) << error
;
227 scoped_ptr
<base::Value
> actual(test_dict
.ConvertToJSON(schema
));
229 base::DictionaryValue expected
;
230 expected
.Set("one", int_value
.DeepCopy());
231 scoped_ptr
<base::DictionaryValue
> expected_subdict(
232 new base::DictionaryValue());
233 expected_subdict
->Set("two", string_value
.DeepCopy());
234 expected
.Set("three", expected_subdict
.release());
235 scoped_ptr
<base::ListValue
> expected_list(new base::ListValue());
236 expected_list
->Append(string_value
.DeepCopy());
237 expected
.Set("dict-to-list", expected_list
.release());
238 expected
.Set("int-to-bool", new base::FundamentalValue(true));
239 expected
.Set("int-to-double", new base::FundamentalValue(42.0));
240 expected
.Set("string-to-bool", new base::FundamentalValue(false));
241 expected
.Set("string-to-double", new base::FundamentalValue(0.0));
242 expected
.Set("string-to-int", new base::FundamentalValue((int) 0));
243 expected_list
.reset(new base::ListValue());
244 expected_list
->Append(new base::StringValue("value"));
245 expected_subdict
.reset(new base::DictionaryValue());
246 expected_subdict
->Set("key", expected_list
.release());
247 expected
.Set("string-to-dict", expected_subdict
.release());
249 EXPECT_TRUE(base::Value::Equals(actual
.get(), &expected
));
252 TEST(RegistryDictTest
, KeyValueNameClashes
) {
253 RegistryDict test_dict
;
255 base::FundamentalValue
int_value(42);
256 base::StringValue
string_value("fortytwo");
258 test_dict
.SetValue("one", make_scoped_ptr(int_value
.DeepCopy()));
259 scoped_ptr
<RegistryDict
> subdict(new RegistryDict());
260 subdict
->SetValue("two", make_scoped_ptr(string_value
.DeepCopy()));
261 test_dict
.SetKey("one", subdict
.Pass());
263 EXPECT_TRUE(base::Value::Equals(&int_value
, test_dict
.GetValue("one")));
264 RegistryDict
* actual_subdict
= test_dict
.GetKey("one");
265 ASSERT_TRUE(actual_subdict
);
266 EXPECT_TRUE(base::Value::Equals(&string_value
,
267 actual_subdict
->GetValue("two")));
271 } // namespace policy