Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(575)

Side by Side Diff: components/policy/core/common/registry_dict_win_unittest.cc

Issue 2481753002: Push preg_parser and registry_dict changes upstream (Closed)
Patch Set: Minor formatting fixes Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/policy/core/common/registry_dict_win.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "components/policy/core/common/registry_dict_win.h"
6
7 #include <string>
8 #include <utility>
9
10 #include "base/memory/ptr_util.h"
11 #include "base/values.h"
12 #include "components/policy/core/common/schema.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace policy {
16 namespace {
17
18 TEST(RegistryDictTest, SetAndGetValue) {
19 RegistryDict test_dict;
20
21 base::FundamentalValue int_value(42);
22 base::StringValue string_value("fortytwo");
23
24 test_dict.SetValue("one", int_value.CreateDeepCopy());
25 EXPECT_EQ(1u, test_dict.values().size());
26 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one")));
27 EXPECT_FALSE(test_dict.GetValue("two"));
28
29 test_dict.SetValue("two", string_value.CreateDeepCopy());
30 EXPECT_EQ(2u, test_dict.values().size());
31 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one")));
32 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two")));
33
34 std::unique_ptr<base::Value> one(test_dict.RemoveValue("one"));
35 EXPECT_EQ(1u, test_dict.values().size());
36 EXPECT_TRUE(base::Value::Equals(&int_value, one.get()));
37 EXPECT_FALSE(test_dict.GetValue("one"));
38 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two")));
39
40 test_dict.ClearValues();
41 EXPECT_FALSE(test_dict.GetValue("one"));
42 EXPECT_FALSE(test_dict.GetValue("two"));
43 EXPECT_TRUE(test_dict.values().empty());
44 }
45
46 TEST(RegistryDictTest, CaseInsensitiveButPreservingValueNames) {
47 RegistryDict test_dict;
48
49 base::FundamentalValue int_value(42);
50 base::StringValue string_value("fortytwo");
51
52 test_dict.SetValue("One", int_value.CreateDeepCopy());
53 EXPECT_EQ(1u, test_dict.values().size());
54 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("oNe")));
55
56 RegistryDict::ValueMap::const_iterator entry = test_dict.values().begin();
57 ASSERT_NE(entry, test_dict.values().end());
58 EXPECT_EQ("One", entry->first);
59
60 test_dict.SetValue("ONE", string_value.CreateDeepCopy());
61 EXPECT_EQ(1u, test_dict.values().size());
62 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("one")));
63
64 std::unique_ptr<base::Value> removed_value(test_dict.RemoveValue("onE"));
65 EXPECT_TRUE(base::Value::Equals(&string_value, removed_value.get()));
66 EXPECT_TRUE(test_dict.values().empty());
67 }
68
69 TEST(RegistryDictTest, SetAndGetKeys) {
70 RegistryDict test_dict;
71
72 base::FundamentalValue int_value(42);
73 base::StringValue string_value("fortytwo");
74
75 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
76 subdict->SetValue("one", int_value.CreateDeepCopy());
77 test_dict.SetKey("two", std::move(subdict));
78 EXPECT_EQ(1u, test_dict.keys().size());
79 RegistryDict* actual_subdict = test_dict.GetKey("two");
80 ASSERT_TRUE(actual_subdict);
81 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one")));
82
83 subdict.reset(new RegistryDict());
84 subdict->SetValue("three", string_value.CreateDeepCopy());
85 test_dict.SetKey("four", std::move(subdict));
86 EXPECT_EQ(2u, test_dict.keys().size());
87 actual_subdict = test_dict.GetKey("two");
88 ASSERT_TRUE(actual_subdict);
89 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one")));
90 actual_subdict = test_dict.GetKey("four");
91 ASSERT_TRUE(actual_subdict);
92 EXPECT_TRUE(base::Value::Equals(&string_value,
93 actual_subdict->GetValue("three")));
94
95 test_dict.ClearKeys();
96 EXPECT_FALSE(test_dict.GetKey("one"));
97 EXPECT_FALSE(test_dict.GetKey("three"));
98 EXPECT_TRUE(test_dict.keys().empty());
99 }
100
101 TEST(RegistryDictTest, CaseInsensitiveButPreservingKeyNames) {
102 RegistryDict test_dict;
103
104 base::FundamentalValue int_value(42);
105
106 test_dict.SetKey("One", base::MakeUnique<RegistryDict>());
107 EXPECT_EQ(1u, test_dict.keys().size());
108 RegistryDict* actual_subdict = test_dict.GetKey("One");
109 ASSERT_TRUE(actual_subdict);
110 EXPECT_TRUE(actual_subdict->values().empty());
111
112 RegistryDict::KeyMap::const_iterator entry = test_dict.keys().begin();
113 ASSERT_NE(entry, test_dict.keys().end());
114 EXPECT_EQ("One", entry->first);
115
116 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
117 subdict->SetValue("two", int_value.CreateDeepCopy());
118 test_dict.SetKey("ONE", std::move(subdict));
119 EXPECT_EQ(1u, test_dict.keys().size());
120 actual_subdict = test_dict.GetKey("One");
121 ASSERT_TRUE(actual_subdict);
122 EXPECT_TRUE(base::Value::Equals(&int_value,
123 actual_subdict->GetValue("two")));
124
125 std::unique_ptr<RegistryDict> removed_key(test_dict.RemoveKey("one"));
126 ASSERT_TRUE(removed_key);
127 EXPECT_TRUE(base::Value::Equals(&int_value,
128 removed_key->GetValue("two")));
129 EXPECT_TRUE(test_dict.keys().empty());
130 }
131
132 TEST(RegistryDictTest, Merge) {
133 RegistryDict dict_a;
134 RegistryDict dict_b;
135
136 base::FundamentalValue int_value(42);
137 base::StringValue string_value("fortytwo");
138
139 dict_a.SetValue("one", int_value.CreateDeepCopy());
140 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
141 subdict->SetValue("two", string_value.CreateDeepCopy());
142 dict_a.SetKey("three", std::move(subdict));
143
144 dict_b.SetValue("four", string_value.CreateDeepCopy());
145 subdict.reset(new RegistryDict());
146 subdict->SetValue("two", int_value.CreateDeepCopy());
147 dict_b.SetKey("three", std::move(subdict));
148 subdict.reset(new RegistryDict());
149 subdict->SetValue("five", int_value.CreateDeepCopy());
150 dict_b.SetKey("six", std::move(subdict));
151
152 dict_a.Merge(dict_b);
153
154 EXPECT_TRUE(base::Value::Equals(&int_value, dict_a.GetValue("one")));
155 EXPECT_TRUE(base::Value::Equals(&string_value, dict_b.GetValue("four")));
156 RegistryDict* actual_subdict = dict_a.GetKey("three");
157 ASSERT_TRUE(actual_subdict);
158 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("two")));
159 actual_subdict = dict_a.GetKey("six");
160 ASSERT_TRUE(actual_subdict);
161 EXPECT_TRUE(base::Value::Equals(&int_value,
162 actual_subdict->GetValue("five")));
163 }
164
165 TEST(RegistryDictTest, Swap) {
166 RegistryDict dict_a;
167 RegistryDict dict_b;
168
169 base::FundamentalValue int_value(42);
170 base::StringValue string_value("fortytwo");
171
172 dict_a.SetValue("one", int_value.CreateDeepCopy());
173 dict_a.SetKey("two", base::MakeUnique<RegistryDict>());
174 dict_b.SetValue("three", string_value.CreateDeepCopy());
175
176 dict_a.Swap(&dict_b);
177
178 EXPECT_TRUE(base::Value::Equals(&int_value, dict_b.GetValue("one")));
179 EXPECT_TRUE(dict_b.GetKey("two"));
180 EXPECT_FALSE(dict_b.GetValue("two"));
181
182 EXPECT_TRUE(base::Value::Equals(&string_value, dict_a.GetValue("three")));
183 EXPECT_FALSE(dict_a.GetValue("one"));
184 EXPECT_FALSE(dict_a.GetKey("two"));
185 }
186
187 TEST(RegistryDictTest, ConvertToJSON) {
188 RegistryDict test_dict;
189
190 base::FundamentalValue int_value(42);
191 base::StringValue string_value("fortytwo");
192 base::StringValue string_zero("0");
193 base::StringValue string_dict("{ \"key\": [ \"value\" ] }");
194
195 test_dict.SetValue("one", int_value.CreateDeepCopy());
196 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
197 subdict->SetValue("two", string_value.CreateDeepCopy());
198 test_dict.SetKey("three", std::move(subdict));
199 std::unique_ptr<RegistryDict> list(new RegistryDict());
200 list->SetValue("1", string_value.CreateDeepCopy());
201 test_dict.SetKey("dict-to-list", std::move(list));
202 test_dict.SetValue("int-to-bool", int_value.CreateDeepCopy());
203 test_dict.SetValue("int-to-double", int_value.CreateDeepCopy());
204 test_dict.SetValue("string-to-bool", string_zero.CreateDeepCopy());
205 test_dict.SetValue("string-to-double", string_zero.CreateDeepCopy());
206 test_dict.SetValue("string-to-int", string_zero.CreateDeepCopy());
207 test_dict.SetValue("string-to-dict", string_dict.CreateDeepCopy());
208
209 std::string error;
210 Schema schema = Schema::Parse(
211 "{"
212 " \"type\": \"object\","
213 " \"properties\": {"
214 " \"dict-to-list\": {"
215 " \"type\": \"array\","
216 " \"items\": { \"type\": \"string\" }"
217 " },"
218 " \"int-to-bool\": { \"type\": \"boolean\" },"
219 " \"int-to-double\": { \"type\": \"number\" },"
220 " \"string-to-bool\": { \"type\": \"boolean\" },"
221 " \"string-to-double\": { \"type\": \"number\" },"
222 " \"string-to-int\": { \"type\": \"integer\" },"
223 " \"string-to-dict\": { \"type\": \"object\" }"
224 " }"
225 "}",
226 &error);
227 ASSERT_TRUE(schema.valid()) << error;
228
229 std::unique_ptr<base::Value> actual(test_dict.ConvertToJSON(schema));
230
231 base::DictionaryValue expected;
232 expected.Set("one", int_value.CreateDeepCopy());
233 std::unique_ptr<base::DictionaryValue> expected_subdict(
234 new base::DictionaryValue());
235 expected_subdict->Set("two", string_value.CreateDeepCopy());
236 expected.Set("three", std::move(expected_subdict));
237 std::unique_ptr<base::ListValue> expected_list(new base::ListValue());
238 expected_list->Append(string_value.CreateDeepCopy());
239 expected.Set("dict-to-list", std::move(expected_list));
240 expected.Set("int-to-bool", new base::FundamentalValue(true));
241 expected.Set("int-to-double", new base::FundamentalValue(42.0));
242 expected.Set("string-to-bool", new base::FundamentalValue(false));
243 expected.Set("string-to-double", new base::FundamentalValue(0.0));
244 expected.Set("string-to-int",
245 new base::FundamentalValue(static_cast<int>(0)));
246 expected_list.reset(new base::ListValue());
247 expected_list->Append(new base::StringValue("value"));
248 expected_subdict.reset(new base::DictionaryValue());
249 expected_subdict->Set("key", std::move(expected_list));
250 expected.Set("string-to-dict", std::move(expected_subdict));
251
252 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected));
253 }
254
255 TEST(RegistryDictTest, NonSequentialConvertToJSON) {
256 RegistryDict test_dict;
257
258 std::unique_ptr<RegistryDict> list(new RegistryDict());
259 list->SetValue("1", base::StringValue("1").CreateDeepCopy());
260 list->SetValue("2", base::StringValue("2").CreateDeepCopy());
261 list->SetValue("THREE", base::StringValue("3").CreateDeepCopy());
262 list->SetValue("4", base::StringValue("4").CreateDeepCopy());
263 test_dict.SetKey("dict-to-list", std::move(list));
264
265 std::string error;
266 Schema schema = Schema::Parse(
267 "{"
268 " \"type\": \"object\","
269 " \"properties\": {"
270 " \"dict-to-list\": {"
271 " \"type\": \"array\","
272 " \"items\": { \"type\": \"string\" }"
273 " }"
274 " }"
275 "}",
276 &error);
277 ASSERT_TRUE(schema.valid()) << error;
278
279 std::unique_ptr<base::Value> actual(test_dict.ConvertToJSON(schema));
280
281 base::DictionaryValue expected;
282 std::unique_ptr<base::ListValue> expected_list(new base::ListValue());
283 expected_list->Append(base::StringValue("1").CreateDeepCopy());
284 expected_list->Append(base::StringValue("2").CreateDeepCopy());
285 expected_list->Append(base::StringValue("4").CreateDeepCopy());
286 expected.Set("dict-to-list", std::move(expected_list));
287
288 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected));
289 }
290
291 TEST(RegistryDictTest, KeyValueNameClashes) {
292 RegistryDict test_dict;
293
294 base::FundamentalValue int_value(42);
295 base::StringValue string_value("fortytwo");
296
297 test_dict.SetValue("one", int_value.CreateDeepCopy());
298 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
299 subdict->SetValue("two", string_value.CreateDeepCopy());
300 test_dict.SetKey("one", std::move(subdict));
301
302 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one")));
303 RegistryDict* actual_subdict = test_dict.GetKey("one");
304 ASSERT_TRUE(actual_subdict);
305 EXPECT_TRUE(base::Value::Equals(&string_value,
306 actual_subdict->GetValue("two")));
307 }
308
309 } // namespace
310 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/registry_dict_win.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698