Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / registry_dict_win_unittest.cc
blob4db7eadef9ddc8338fc4c903f9d55480f35cb385
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"
7 #include <string>
9 #include "base/values.h"
10 #include "components/policy/core/common/schema.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace policy {
14 namespace {
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", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(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()).Pass());
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", scoped_ptr<base::Value>(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) {
131 RegistryDict dict_a;
132 RegistryDict dict_b;
134 base::FundamentalValue int_value(42);
135 base::StringValue string_value("fortytwo");
137 dict_a.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy()));
138 scoped_ptr<RegistryDict> subdict(new RegistryDict());
139 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy()));
140 dict_a.SetKey("three", subdict.Pass());
142 dict_b.SetValue("four", scoped_ptr<base::Value>(string_value.DeepCopy()));
143 subdict.reset(new RegistryDict());
144 subdict->SetValue("two", scoped_ptr<base::Value>(int_value.DeepCopy()));
145 dict_b.SetKey("three", subdict.Pass());
146 subdict.reset(new RegistryDict());
147 subdict->SetValue("five", scoped_ptr<base::Value>(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) {
164 RegistryDict dict_a;
165 RegistryDict dict_b;
167 base::FundamentalValue int_value(42);
168 base::StringValue string_value("fortytwo");
170 dict_a.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy()));
171 dict_a.SetKey("two", make_scoped_ptr(new RegistryDict()).Pass());
172 dict_b.SetValue("three", scoped_ptr<base::Value>(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", scoped_ptr<base::Value>(int_value.DeepCopy()));
194 scoped_ptr<RegistryDict> subdict(new RegistryDict());
195 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy()));
196 test_dict.SetKey("three", subdict.Pass());
197 scoped_ptr<RegistryDict> list(new RegistryDict());
198 list->SetValue("1", scoped_ptr<base::Value>(string_value.DeepCopy()));
199 test_dict.SetKey("dict-to-list", list.Pass());
200 test_dict.SetValue("int-to-bool",
201 scoped_ptr<base::Value>(int_value.DeepCopy()));
202 test_dict.SetValue("int-to-double",
203 scoped_ptr<base::Value>(int_value.DeepCopy()));
204 test_dict.SetValue("string-to-bool",
205 scoped_ptr<base::Value>(string_zero.DeepCopy()));
206 test_dict.SetValue("string-to-double",
207 scoped_ptr<base::Value>(string_zero.DeepCopy()));
208 test_dict.SetValue("string-to-int",
209 scoped_ptr<base::Value>(string_zero.DeepCopy()));
210 test_dict.SetValue("string-to-dict",
211 scoped_ptr<base::Value>(string_dict.DeepCopy()));
213 std::string error;
214 Schema schema = Schema::Parse(
216 " \"type\": \"object\","
217 " \"properties\": {"
218 " \"dict-to-list\": {"
219 " \"type\": \"array\","
220 " \"items\": { \"type\": \"string\" }"
221 " },"
222 " \"int-to-bool\": { \"type\": \"boolean\" },"
223 " \"int-to-double\": { \"type\": \"number\" },"
224 " \"string-to-bool\": { \"type\": \"boolean\" },"
225 " \"string-to-double\": { \"type\": \"number\" },"
226 " \"string-to-int\": { \"type\": \"integer\" },"
227 " \"string-to-dict\": { \"type\": \"object\" }"
228 " }"
229 "}", &error);
230 ASSERT_TRUE(schema.valid()) << error;
232 scoped_ptr<base::Value> actual(test_dict.ConvertToJSON(schema));
234 base::DictionaryValue expected;
235 expected.Set("one", int_value.DeepCopy());
236 scoped_ptr<base::DictionaryValue> expected_subdict(
237 new base::DictionaryValue());
238 expected_subdict->Set("two", string_value.DeepCopy());
239 expected.Set("three", expected_subdict.release());
240 scoped_ptr<base::ListValue> expected_list(new base::ListValue());
241 expected_list->Append(string_value.DeepCopy());
242 expected.Set("dict-to-list", expected_list.release());
243 expected.Set("int-to-bool", new base::FundamentalValue(true));
244 expected.Set("int-to-double", new base::FundamentalValue(42.0));
245 expected.Set("string-to-bool", new base::FundamentalValue(false));
246 expected.Set("string-to-double", new base::FundamentalValue(0.0));
247 expected.Set("string-to-int",
248 new base::FundamentalValue(static_cast<int>(0)));
249 expected_list.reset(new base::ListValue());
250 expected_list->Append(new base::StringValue("value"));
251 expected_subdict.reset(new base::DictionaryValue());
252 expected_subdict->Set("key", expected_list.release());
253 expected.Set("string-to-dict", expected_subdict.release());
255 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected));
258 TEST(RegistryDictTest, KeyValueNameClashes) {
259 RegistryDict test_dict;
261 base::FundamentalValue int_value(42);
262 base::StringValue string_value("fortytwo");
264 test_dict.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy()));
265 scoped_ptr<RegistryDict> subdict(new RegistryDict());
266 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy()));
267 test_dict.SetKey("one", subdict.Pass());
269 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one")));
270 RegistryDict* actual_subdict = test_dict.GetKey("one");
271 ASSERT_TRUE(actual_subdict);
272 EXPECT_TRUE(base::Value::Equals(&string_value,
273 actual_subdict->GetValue("two")));
276 } // namespace
277 } // namespace policy