1 // Copyright (c) 2012 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 "base/values.h"
6
7 #include <stddef.h>
8
9 #include <functional>
10 #include <limits>
11 #include <memory>
12 #include <string>
13 #include <type_traits>
14 #include <utility>
15 #include <vector>
16
17 #include "base/containers/adapters.h"
18 #include "base/memory/ptr_util.h"
19 #include "base/strings/string16.h"
20 #include "base/strings/string_piece.h"
21 #include "base/strings/utf_string_conversions.h"
22 #include "testing/gmock/include/gmock/gmock.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace base {
26
TEST(ValuesTest,TestNothrow)27 TEST(ValuesTest, TestNothrow) {
28 static_assert(std::is_nothrow_move_constructible<Value>::value,
29 "IsNothrowMoveConstructible");
30 static_assert(std::is_nothrow_default_constructible<Value>::value,
31 "IsNothrowDefaultConstructible");
32 static_assert(std::is_nothrow_constructible<Value, std::string&&>::value,
33 "IsNothrowMoveConstructibleFromString");
34 static_assert(
35 std::is_nothrow_constructible<Value, Value::BlobStorage&&>::value,
36 "IsNothrowMoveConstructibleFromBlob");
37 static_assert(
38 std::is_nothrow_constructible<Value, Value::ListStorage&&>::value,
39 "IsNothrowMoveConstructibleFromList");
40 static_assert(std::is_nothrow_move_assignable<Value>::value,
41 "IsNothrowMoveAssignable");
42 static_assert(
43 std::is_nothrow_constructible<ListValue, Value::ListStorage&&>::value,
44 "ListIsNothrowMoveConstructibleFromList");
45 }
46
47 // Group of tests for the value constructors.
TEST(ValuesTest,ConstructBool)48 TEST(ValuesTest, ConstructBool) {
49 Value true_value(true);
50 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type());
51 EXPECT_TRUE(true_value.GetBool());
52
53 Value false_value(false);
54 EXPECT_EQ(Value::Type::BOOLEAN, false_value.type());
55 EXPECT_FALSE(false_value.GetBool());
56 }
57
TEST(ValuesTest,ConstructInt)58 TEST(ValuesTest, ConstructInt) {
59 Value value(-37);
60 EXPECT_EQ(Value::Type::INTEGER, value.type());
61 EXPECT_EQ(-37, value.GetInt());
62 }
63
TEST(ValuesTest,ConstructDouble)64 TEST(ValuesTest, ConstructDouble) {
65 Value value(-4.655);
66 EXPECT_EQ(Value::Type::DOUBLE, value.type());
67 EXPECT_EQ(-4.655, value.GetDouble());
68 }
69
TEST(ValuesTest,ConstructStringFromConstCharPtr)70 TEST(ValuesTest, ConstructStringFromConstCharPtr) {
71 const char* str = "foobar";
72 Value value(str);
73 EXPECT_EQ(Value::Type::STRING, value.type());
74 EXPECT_EQ("foobar", value.GetString());
75 }
76
TEST(ValuesTest,ConstructStringFromStringPiece)77 TEST(ValuesTest, ConstructStringFromStringPiece) {
78 std::string str = "foobar";
79 Value value{StringPiece(str)};
80 EXPECT_EQ(Value::Type::STRING, value.type());
81 EXPECT_EQ("foobar", value.GetString());
82 }
83
TEST(ValuesTest,ConstructStringFromStdStringRRef)84 TEST(ValuesTest, ConstructStringFromStdStringRRef) {
85 std::string str = "foobar";
86 Value value(std::move(str));
87 EXPECT_EQ(Value::Type::STRING, value.type());
88 EXPECT_EQ("foobar", value.GetString());
89 }
90
TEST(ValuesTest,ConstructStringFromConstChar16Ptr)91 TEST(ValuesTest, ConstructStringFromConstChar16Ptr) {
92 string16 str = ASCIIToUTF16("foobar");
93 Value value(str.c_str());
94 EXPECT_EQ(Value::Type::STRING, value.type());
95 EXPECT_EQ("foobar", value.GetString());
96 }
97
TEST(ValuesTest,ConstructStringFromStringPiece16)98 TEST(ValuesTest, ConstructStringFromStringPiece16) {
99 string16 str = ASCIIToUTF16("foobar");
100 Value value{StringPiece16(str)};
101 EXPECT_EQ(Value::Type::STRING, value.type());
102 EXPECT_EQ("foobar", value.GetString());
103 }
104
TEST(ValuesTest,ConstructBinary)105 TEST(ValuesTest, ConstructBinary) {
106 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
107 EXPECT_EQ(Value::Type::BINARY, value.type());
108 EXPECT_EQ(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}),
109 value.GetBlob());
110 }
111
TEST(ValuesTest,ConstructDict)112 TEST(ValuesTest, ConstructDict) {
113 DictionaryValue value;
114 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
115 }
116
TEST(ValuesTest,ConstructDictFromStorage)117 TEST(ValuesTest, ConstructDictFromStorage) {
118 Value::DictStorage storage;
119 storage.emplace("foo", std::make_unique<Value>("bar"));
120 {
121 DictionaryValue value(storage);
122 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
123 EXPECT_EQ(Value::Type::STRING, value.FindKey("foo")->type());
124 EXPECT_EQ("bar", value.FindKey("foo")->GetString());
125 }
126
127 *storage["foo"] = base::Value("baz");
128 {
129 DictionaryValue value(std::move(storage));
130 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
131 EXPECT_EQ(Value::Type::STRING, value.FindKey("foo")->type());
132 EXPECT_EQ("baz", value.FindKey("foo")->GetString());
133 }
134 }
135
TEST(ValuesTest,ConstructList)136 TEST(ValuesTest, ConstructList) {
137 ListValue value;
138 EXPECT_EQ(Value::Type::LIST, value.type());
139 }
140
TEST(ValuesTest,ConstructListFromStorage)141 TEST(ValuesTest, ConstructListFromStorage) {
142 Value::ListStorage storage;
143 storage.emplace_back("foo");
144 {
145 ListValue value(storage);
146 EXPECT_EQ(Value::Type::LIST, value.type());
147 EXPECT_EQ(1u, value.GetList().size());
148 EXPECT_EQ(Value::Type::STRING, value.GetList()[0].type());
149 EXPECT_EQ("foo", value.GetList()[0].GetString());
150 }
151
152 storage.back() = base::Value("bar");
153 {
154 ListValue value(std::move(storage));
155 EXPECT_EQ(Value::Type::LIST, value.type());
156 EXPECT_EQ(1u, value.GetList().size());
157 EXPECT_EQ(Value::Type::STRING, value.GetList()[0].type());
158 EXPECT_EQ("bar", value.GetList()[0].GetString());
159 }
160 }
161
162 // Group of tests for the copy constructors and copy-assigmnent. For equality
163 // checks comparisons of the interesting fields are done instead of relying on
164 // Equals being correct.
TEST(ValuesTest,CopyBool)165 TEST(ValuesTest, CopyBool) {
166 Value true_value(true);
167 Value copied_true_value(true_value.Clone());
168 EXPECT_EQ(true_value.type(), copied_true_value.type());
169 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool());
170
171 Value false_value(false);
172 Value copied_false_value(false_value.Clone());
173 EXPECT_EQ(false_value.type(), copied_false_value.type());
174 EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool());
175
176 Value blank;
177
178 blank = true_value.Clone();
179 EXPECT_EQ(true_value.type(), blank.type());
180 EXPECT_EQ(true_value.GetBool(), blank.GetBool());
181
182 blank = false_value.Clone();
183 EXPECT_EQ(false_value.type(), blank.type());
184 EXPECT_EQ(false_value.GetBool(), blank.GetBool());
185 }
186
TEST(ValuesTest,CopyInt)187 TEST(ValuesTest, CopyInt) {
188 Value value(74);
189 Value copied_value(value.Clone());
190 EXPECT_EQ(value.type(), copied_value.type());
191 EXPECT_EQ(value.GetInt(), copied_value.GetInt());
192
193 Value blank;
194
195 blank = value.Clone();
196 EXPECT_EQ(value.type(), blank.type());
197 EXPECT_EQ(value.GetInt(), blank.GetInt());
198 }
199
TEST(ValuesTest,CopyDouble)200 TEST(ValuesTest, CopyDouble) {
201 Value value(74.896);
202 Value copied_value(value.Clone());
203 EXPECT_EQ(value.type(), copied_value.type());
204 EXPECT_EQ(value.GetDouble(), copied_value.GetDouble());
205
206 Value blank;
207
208 blank = value.Clone();
209 EXPECT_EQ(value.type(), blank.type());
210 EXPECT_EQ(value.GetDouble(), blank.GetDouble());
211 }
212
TEST(ValuesTest,CopyString)213 TEST(ValuesTest, CopyString) {
214 Value value("foobar");
215 Value copied_value(value.Clone());
216 EXPECT_EQ(value.type(), copied_value.type());
217 EXPECT_EQ(value.GetString(), copied_value.GetString());
218
219 Value blank;
220
221 blank = value.Clone();
222 EXPECT_EQ(value.type(), blank.type());
223 EXPECT_EQ(value.GetString(), blank.GetString());
224 }
225
TEST(ValuesTest,CopyBinary)226 TEST(ValuesTest, CopyBinary) {
227 Value value(Value::BlobStorage({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
228 Value copied_value(value.Clone());
229 EXPECT_EQ(value.type(), copied_value.type());
230 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob());
231
232 Value blank;
233
234 blank = value.Clone();
235 EXPECT_EQ(value.type(), blank.type());
236 EXPECT_EQ(value.GetBlob(), blank.GetBlob());
237 }
238
TEST(ValuesTest,CopyDictionary)239 TEST(ValuesTest, CopyDictionary) {
240 Value::DictStorage storage;
241 storage.emplace("Int", std::make_unique<Value>(123));
242 Value value(std::move(storage));
243
244 Value copied_value(value.Clone());
245 EXPECT_EQ(value, copied_value);
246
247 Value blank;
248 blank = value.Clone();
249 EXPECT_EQ(value, blank);
250 }
251
TEST(ValuesTest,CopyList)252 TEST(ValuesTest, CopyList) {
253 Value::ListStorage storage;
254 storage.emplace_back(123);
255 Value value(std::move(storage));
256
257 Value copied_value(value.Clone());
258 EXPECT_EQ(value, copied_value);
259
260 Value blank;
261 blank = value.Clone();
262 EXPECT_EQ(value, blank);
263 }
264
265 // Group of tests for the move constructors and move-assigmnent.
TEST(ValuesTest,MoveBool)266 TEST(ValuesTest, MoveBool) {
267 Value true_value(true);
268 Value moved_true_value(std::move(true_value));
269 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type());
270 EXPECT_TRUE(moved_true_value.GetBool());
271
272 Value false_value(false);
273 Value moved_false_value(std::move(false_value));
274 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type());
275 EXPECT_FALSE(moved_false_value.GetBool());
276
277 Value blank;
278
279 blank = Value(true);
280 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
281 EXPECT_TRUE(blank.GetBool());
282
283 blank = Value(false);
284 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
285 EXPECT_FALSE(blank.GetBool());
286 }
287
TEST(ValuesTest,MoveInt)288 TEST(ValuesTest, MoveInt) {
289 Value value(74);
290 Value moved_value(std::move(value));
291 EXPECT_EQ(Value::Type::INTEGER, moved_value.type());
292 EXPECT_EQ(74, moved_value.GetInt());
293
294 Value blank;
295
296 blank = Value(47);
297 EXPECT_EQ(Value::Type::INTEGER, blank.type());
298 EXPECT_EQ(47, blank.GetInt());
299 }
300
TEST(ValuesTest,MoveDouble)301 TEST(ValuesTest, MoveDouble) {
302 Value value(74.896);
303 Value moved_value(std::move(value));
304 EXPECT_EQ(Value::Type::DOUBLE, moved_value.type());
305 EXPECT_EQ(74.896, moved_value.GetDouble());
306
307 Value blank;
308
309 blank = Value(654.38);
310 EXPECT_EQ(Value::Type::DOUBLE, blank.type());
311 EXPECT_EQ(654.38, blank.GetDouble());
312 }
313
TEST(ValuesTest,MoveString)314 TEST(ValuesTest, MoveString) {
315 Value value("foobar");
316 Value moved_value(std::move(value));
317 EXPECT_EQ(Value::Type::STRING, moved_value.type());
318 EXPECT_EQ("foobar", moved_value.GetString());
319
320 Value blank;
321
322 blank = Value("foobar");
323 EXPECT_EQ(Value::Type::STRING, blank.type());
324 EXPECT_EQ("foobar", blank.GetString());
325 }
326
TEST(ValuesTest,MoveBinary)327 TEST(ValuesTest, MoveBinary) {
328 const Value::BlobStorage buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2};
329 Value value(buffer);
330 Value moved_value(std::move(value));
331 EXPECT_EQ(Value::Type::BINARY, moved_value.type());
332 EXPECT_EQ(buffer, moved_value.GetBlob());
333
334 Value blank;
335
336 blank = Value(buffer);
337 EXPECT_EQ(Value::Type::BINARY, blank.type());
338 EXPECT_EQ(buffer, blank.GetBlob());
339 }
340
TEST(ValuesTest,MoveConstructDictionary)341 TEST(ValuesTest, MoveConstructDictionary) {
342 Value::DictStorage storage;
343 storage.emplace("Int", std::make_unique<Value>(123));
344
345 Value value(std::move(storage));
346 Value moved_value(std::move(value));
347 EXPECT_EQ(Value::Type::DICTIONARY, moved_value.type());
348 EXPECT_EQ(123, moved_value.FindKey("Int")->GetInt());
349 }
350
TEST(ValuesTest,MoveAssignDictionary)351 TEST(ValuesTest, MoveAssignDictionary) {
352 Value::DictStorage storage;
353 storage.emplace("Int", std::make_unique<Value>(123));
354
355 Value blank;
356 blank = Value(std::move(storage));
357 EXPECT_EQ(Value::Type::DICTIONARY, blank.type());
358 EXPECT_EQ(123, blank.FindKey("Int")->GetInt());
359 }
360
TEST(ValuesTest,MoveList)361 TEST(ValuesTest, MoveList) {
362 Value::ListStorage storage;
363 storage.emplace_back(123);
364 Value value(storage);
365 Value moved_value(std::move(value));
366 EXPECT_EQ(Value::Type::LIST, moved_value.type());
367 EXPECT_EQ(123, moved_value.GetList().back().GetInt());
368
369 Value blank;
370 blank = Value(std::move(storage));
371 EXPECT_EQ(Value::Type::LIST, blank.type());
372 EXPECT_EQ(123, blank.GetList().back().GetInt());
373 }
374
TEST(ValuesTest,FindKey)375 TEST(ValuesTest, FindKey) {
376 Value::DictStorage storage;
377 storage.emplace("foo", std::make_unique<Value>("bar"));
378 Value dict(std::move(storage));
379 EXPECT_NE(nullptr, dict.FindKey("foo"));
380 EXPECT_EQ(nullptr, dict.FindKey("baz"));
381
382 // Single not found key.
383 bool found = dict.FindKey("notfound");
384 EXPECT_FALSE(found);
385 }
386
TEST(ValuesTest,FindKeyChangeValue)387 TEST(ValuesTest, FindKeyChangeValue) {
388 Value::DictStorage storage;
389 storage.emplace("foo", std::make_unique<Value>("bar"));
390 Value dict(std::move(storage));
391 Value* found = dict.FindKey("foo");
392 EXPECT_NE(nullptr, found);
393 EXPECT_EQ("bar", found->GetString());
394
395 *found = Value(123);
396 EXPECT_EQ(123, dict.FindKey("foo")->GetInt());
397 }
398
TEST(ValuesTest,FindKeyConst)399 TEST(ValuesTest, FindKeyConst) {
400 Value::DictStorage storage;
401 storage.emplace("foo", std::make_unique<Value>("bar"));
402 const Value dict(std::move(storage));
403 EXPECT_NE(nullptr, dict.FindKey("foo"));
404 EXPECT_EQ(nullptr, dict.FindKey("baz"));
405 }
406
TEST(ValuesTest,FindKeyOfType)407 TEST(ValuesTest, FindKeyOfType) {
408 Value::DictStorage storage;
409 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
410 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
411 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
412 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
413 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
414 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
415 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
416 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
417
418 Value dict(std::move(storage));
419 EXPECT_NE(nullptr, dict.FindKeyOfType("null", Value::Type::NONE));
420 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BOOLEAN));
421 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::INTEGER));
422 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DOUBLE));
423 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::STRING));
424 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BINARY));
425 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::LIST));
426 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DICTIONARY));
427
428 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::NONE));
429 EXPECT_NE(nullptr, dict.FindKeyOfType("bool", Value::Type::BOOLEAN));
430 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::INTEGER));
431 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DOUBLE));
432 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::STRING));
433 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::BINARY));
434 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::LIST));
435 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DICTIONARY));
436
437 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::NONE));
438 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BOOLEAN));
439 EXPECT_NE(nullptr, dict.FindKeyOfType("int", Value::Type::INTEGER));
440 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DOUBLE));
441 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::STRING));
442 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BINARY));
443 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::LIST));
444 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DICTIONARY));
445
446 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::NONE));
447 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BOOLEAN));
448 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::INTEGER));
449 EXPECT_NE(nullptr, dict.FindKeyOfType("double", Value::Type::DOUBLE));
450 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::STRING));
451 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BINARY));
452 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::LIST));
453 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::DICTIONARY));
454
455 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::NONE));
456 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BOOLEAN));
457 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::INTEGER));
458 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DOUBLE));
459 EXPECT_NE(nullptr, dict.FindKeyOfType("string", Value::Type::STRING));
460 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BINARY));
461 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::LIST));
462 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DICTIONARY));
463
464 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::NONE));
465 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::BOOLEAN));
466 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::INTEGER));
467 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DOUBLE));
468 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::STRING));
469 EXPECT_NE(nullptr, dict.FindKeyOfType("blob", Value::Type::BINARY));
470 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::LIST));
471 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DICTIONARY));
472
473 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::NONE));
474 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BOOLEAN));
475 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::INTEGER));
476 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DOUBLE));
477 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::STRING));
478 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BINARY));
479 EXPECT_NE(nullptr, dict.FindKeyOfType("list", Value::Type::LIST));
480 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DICTIONARY));
481
482 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::NONE));
483 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BOOLEAN));
484 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::INTEGER));
485 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::DOUBLE));
486 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::STRING));
487 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BINARY));
488 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::LIST));
489 EXPECT_NE(nullptr, dict.FindKeyOfType("dict", Value::Type::DICTIONARY));
490 }
491
TEST(ValuesTest,FindKeyOfTypeConst)492 TEST(ValuesTest, FindKeyOfTypeConst) {
493 Value::DictStorage storage;
494 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
495 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
496 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
497 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
498 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
499 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
500 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
501 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
502
503 const Value dict(std::move(storage));
504 EXPECT_NE(nullptr, dict.FindKeyOfType("null", Value::Type::NONE));
505 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BOOLEAN));
506 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::INTEGER));
507 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DOUBLE));
508 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::STRING));
509 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::BINARY));
510 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::LIST));
511 EXPECT_EQ(nullptr, dict.FindKeyOfType("null", Value::Type::DICTIONARY));
512
513 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::NONE));
514 EXPECT_NE(nullptr, dict.FindKeyOfType("bool", Value::Type::BOOLEAN));
515 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::INTEGER));
516 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DOUBLE));
517 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::STRING));
518 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::BINARY));
519 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::LIST));
520 EXPECT_EQ(nullptr, dict.FindKeyOfType("bool", Value::Type::DICTIONARY));
521
522 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::NONE));
523 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BOOLEAN));
524 EXPECT_NE(nullptr, dict.FindKeyOfType("int", Value::Type::INTEGER));
525 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DOUBLE));
526 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::STRING));
527 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::BINARY));
528 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::LIST));
529 EXPECT_EQ(nullptr, dict.FindKeyOfType("int", Value::Type::DICTIONARY));
530
531 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::NONE));
532 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BOOLEAN));
533 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::INTEGER));
534 EXPECT_NE(nullptr, dict.FindKeyOfType("double", Value::Type::DOUBLE));
535 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::STRING));
536 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::BINARY));
537 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::LIST));
538 EXPECT_EQ(nullptr, dict.FindKeyOfType("double", Value::Type::DICTIONARY));
539
540 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::NONE));
541 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BOOLEAN));
542 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::INTEGER));
543 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DOUBLE));
544 EXPECT_NE(nullptr, dict.FindKeyOfType("string", Value::Type::STRING));
545 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::BINARY));
546 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::LIST));
547 EXPECT_EQ(nullptr, dict.FindKeyOfType("string", Value::Type::DICTIONARY));
548
549 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::NONE));
550 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::BOOLEAN));
551 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::INTEGER));
552 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DOUBLE));
553 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::STRING));
554 EXPECT_NE(nullptr, dict.FindKeyOfType("blob", Value::Type::BINARY));
555 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::LIST));
556 EXPECT_EQ(nullptr, dict.FindKeyOfType("blob", Value::Type::DICTIONARY));
557
558 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::NONE));
559 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BOOLEAN));
560 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::INTEGER));
561 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DOUBLE));
562 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::STRING));
563 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::BINARY));
564 EXPECT_NE(nullptr, dict.FindKeyOfType("list", Value::Type::LIST));
565 EXPECT_EQ(nullptr, dict.FindKeyOfType("list", Value::Type::DICTIONARY));
566
567 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::NONE));
568 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BOOLEAN));
569 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::INTEGER));
570 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::DOUBLE));
571 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::STRING));
572 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::BINARY));
573 EXPECT_EQ(nullptr, dict.FindKeyOfType("dict", Value::Type::LIST));
574 EXPECT_NE(nullptr, dict.FindKeyOfType("dict", Value::Type::DICTIONARY));
575 }
576
TEST(ValuesTest,SetKey)577 TEST(ValuesTest, SetKey) {
578 Value::DictStorage storage;
579 storage.emplace("null", std::make_unique<Value>(Value::Type::NONE));
580 storage.emplace("bool", std::make_unique<Value>(Value::Type::BOOLEAN));
581 storage.emplace("int", std::make_unique<Value>(Value::Type::INTEGER));
582 storage.emplace("double", std::make_unique<Value>(Value::Type::DOUBLE));
583 storage.emplace("string", std::make_unique<Value>(Value::Type::STRING));
584 storage.emplace("blob", std::make_unique<Value>(Value::Type::BINARY));
585 storage.emplace("list", std::make_unique<Value>(Value::Type::LIST));
586 storage.emplace("dict", std::make_unique<Value>(Value::Type::DICTIONARY));
587
588 Value dict(Value::Type::DICTIONARY);
589 dict.SetKey(StringPiece("null"), Value(Value::Type::NONE));
590 dict.SetKey(StringPiece("bool"), Value(Value::Type::BOOLEAN));
591 dict.SetKey(std::string("int"), Value(Value::Type::INTEGER));
592 dict.SetKey(std::string("double"), Value(Value::Type::DOUBLE));
593 dict.SetKey(std::string("string"), Value(Value::Type::STRING));
594 dict.SetKey("blob", Value(Value::Type::BINARY));
595 dict.SetKey("list", Value(Value::Type::LIST));
596 dict.SetKey("dict", Value(Value::Type::DICTIONARY));
597
598 EXPECT_EQ(Value(std::move(storage)), dict);
599 }
600
TEST(ValuesTest,FindPath)601 TEST(ValuesTest, FindPath) {
602 // Construct a dictionary path {root}.foo.bar = 123
603 Value foo(Value::Type::DICTIONARY);
604 foo.SetKey("bar", Value(123));
605
606 Value root(Value::Type::DICTIONARY);
607 root.SetKey("foo", std::move(foo));
608
609 // No key (stupid but well-defined and takes work to prevent).
610 Value* found = root.FindPath(std::vector<StringPiece>{});
611 EXPECT_EQ(&root, found);
612
613 // Double key, second not found.
614 found = root.FindPath(std::vector<StringPiece>{"foo", "notfound"});
615 EXPECT_FALSE(found);
616
617 // Double key, found.
618 found = root.FindPath(std::vector<StringPiece>{"foo", "bar"});
619 EXPECT_TRUE(found);
620 EXPECT_TRUE(found->is_int());
621 EXPECT_EQ(123, found->GetInt());
622 }
623
TEST(ValuesTest,SetPath)624 TEST(ValuesTest, SetPath) {
625 Value root(Value::Type::DICTIONARY);
626
627 Value* inserted = root.SetPath({"one", "two"}, Value(123));
628 Value* found = root.FindPathOfType({"one", "two"}, Value::Type::INTEGER);
629 ASSERT_TRUE(found);
630 EXPECT_EQ(inserted, found);
631 EXPECT_EQ(123, found->GetInt());
632
633 inserted = root.SetPath(std::vector<StringPiece>{"foo", "bar"}, Value(123));
634 found = root.FindPathOfType({"foo", "bar"}, Value::Type::INTEGER);
635 ASSERT_TRUE(found);
636 EXPECT_EQ(inserted, found);
637 EXPECT_EQ(123, found->GetInt());
638
639 // Overwrite with a different value.
640 root.SetPath({"foo", "bar"}, Value("hello"));
641 found = root.FindPathOfType(std::vector<StringPiece>{"foo", "bar"},
642 Value::Type::STRING);
643 ASSERT_TRUE(found);
644 EXPECT_EQ("hello", found->GetString());
645
646 // Can't change existing non-dictionary keys to dictionaries.
647 found =
648 root.SetPath(std::vector<StringPiece>{"foo", "bar", "baz"}, Value(123));
649 EXPECT_FALSE(found);
650 }
651
TEST(ValuesTest,RemoveKey)652 TEST(ValuesTest, RemoveKey) {
653 Value root(Value::Type::DICTIONARY);
654 root.SetKey("one", Value(123));
655
656 // Removal of missing key should fail.
657 EXPECT_FALSE(root.RemoveKey("two"));
658
659 // Removal of existing key should succeed.
660 EXPECT_TRUE(root.RemoveKey("one"));
661
662 // Second removal of previously existing key should fail.
663 EXPECT_FALSE(root.RemoveKey("one"));
664 }
665
TEST(ValuesTest,RemovePath)666 TEST(ValuesTest, RemovePath) {
667 Value root(Value::Type::DICTIONARY);
668 root.SetPath({"one", "two", "three"}, Value(123));
669
670 // Removal of missing key should fail.
671 EXPECT_FALSE(root.RemovePath({"one", "two", "four"}));
672
673 // Removal of existing key should succeed.
674 EXPECT_TRUE(root.RemovePath({"one", "two", "three"}));
675
676 // Second removal of previously existing key should fail.
677 EXPECT_FALSE(root.RemovePath({"one", "two", "three"}));
678
679 // Intermediate empty dictionaries should be cleared.
680 EXPECT_FALSE(root.FindKey("one"));
681
682 root.SetPath({"one", "two", "three"}, Value(123));
683 root.SetPath({"one", "two", "four"}, Value(124));
684
685 EXPECT_TRUE(root.RemovePath(std::vector<StringPiece>{"one", "two", "three"}));
686 // Intermediate non-empty dictionaries should be kept.
687 EXPECT_TRUE(root.FindKey("one"));
688 EXPECT_TRUE(root.FindPath({"one", "two"}));
689 EXPECT_TRUE(root.FindPath({"one", "two", "four"}));
690 }
691
TEST(ValuesTest,Basic)692 TEST(ValuesTest, Basic) {
693 // Test basic dictionary getting/setting
694 DictionaryValue settings;
695 std::string homepage = "http://google.com";
696 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
697 ASSERT_EQ(std::string("http://google.com"), homepage);
698
699 ASSERT_FALSE(settings.Get("global", nullptr));
700 settings.SetBoolean("global", true);
701 ASSERT_TRUE(settings.Get("global", nullptr));
702 settings.SetString("global.homepage", "http://scurvy.com");
703 ASSERT_TRUE(settings.Get("global", nullptr));
704 homepage = "http://google.com";
705 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
706 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
707
708 // Test storing a dictionary in a list.
709 ListValue* toolbar_bookmarks;
710 ASSERT_FALSE(
711 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
712
713 std::unique_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
714 settings.Set("global.toolbar.bookmarks", std::move(new_toolbar_bookmarks));
715 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
716
717 std::unique_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
718 new_bookmark->SetString("name", "Froogle");
719 new_bookmark->SetString("url", "http://froogle.com");
720 toolbar_bookmarks->Append(std::move(new_bookmark));
721
722 ListValue* bookmark_list;
723 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
724 DictionaryValue* bookmark;
725 ASSERT_EQ(1U, bookmark_list->GetSize());
726 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
727 std::string bookmark_name = "Unnamed";
728 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
729 ASSERT_EQ(std::string("Froogle"), bookmark_name);
730 std::string bookmark_url;
731 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
732 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
733 }
734
TEST(ValuesTest,List)735 TEST(ValuesTest, List) {
736 std::unique_ptr<ListValue> mixed_list(new ListValue());
737 mixed_list->Set(0, std::make_unique<Value>(true));
738 mixed_list->Set(1, std::make_unique<Value>(42));
739 mixed_list->Set(2, std::make_unique<Value>(88.8));
740 mixed_list->Set(3, std::make_unique<Value>("foo"));
741 ASSERT_EQ(4u, mixed_list->GetSize());
742
743 Value* value = nullptr;
744 bool bool_value = false;
745 int int_value = 0;
746 double double_value = 0.0;
747 std::string string_value;
748
749 ASSERT_FALSE(mixed_list->Get(4, &value));
750
751 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
752 ASSERT_EQ(0, int_value);
753 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
754 ASSERT_FALSE(bool_value);
755 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
756 ASSERT_EQ("", string_value);
757 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
758 ASSERT_EQ(0, int_value);
759 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
760 ASSERT_FALSE(bool_value);
761
762 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
763 ASSERT_TRUE(bool_value);
764 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
765 ASSERT_EQ(42, int_value);
766 // implicit conversion from Integer to Double should be possible.
767 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
768 ASSERT_EQ(42, double_value);
769 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
770 ASSERT_EQ(88.8, double_value);
771 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
772 ASSERT_EQ("foo", string_value);
773
774 // Try searching in the mixed list.
775 base::Value sought_value(42);
776 base::Value not_found_value(false);
777
778 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
779 ASSERT_TRUE((*mixed_list->Find(sought_value)).GetAsInteger(&int_value));
780 ASSERT_EQ(42, int_value);
781 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
782 }
783
TEST(ValuesTest,BinaryValue)784 TEST(ValuesTest, BinaryValue) {
785 // Default constructor creates a BinaryValue with a buffer of size 0.
786 auto binary = std::make_unique<Value>(Value::Type::BINARY);
787 ASSERT_TRUE(binary.get());
788 ASSERT_TRUE(binary->GetBlob().empty());
789
790 // Test the common case of a non-empty buffer
791 Value::BlobStorage buffer(15);
792 char* original_buffer = buffer.data();
793 binary.reset(new Value(std::move(buffer)));
794 ASSERT_TRUE(binary.get());
795 ASSERT_TRUE(binary->GetBlob().data());
796 ASSERT_EQ(original_buffer, binary->GetBlob().data());
797 ASSERT_EQ(15U, binary->GetBlob().size());
798
799 char stack_buffer[42];
800 memset(stack_buffer, '!', 42);
801 binary = Value::CreateWithCopiedBuffer(stack_buffer, 42);
802 ASSERT_TRUE(binary.get());
803 ASSERT_TRUE(binary->GetBlob().data());
804 ASSERT_NE(stack_buffer, binary->GetBlob().data());
805 ASSERT_EQ(42U, binary->GetBlob().size());
806 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBlob().data(),
807 binary->GetBlob().size()));
808 }
809
TEST(ValuesTest,StringValue)810 TEST(ValuesTest, StringValue) {
811 // Test overloaded StringValue constructor.
812 std::unique_ptr<Value> narrow_value(new Value("narrow"));
813 ASSERT_TRUE(narrow_value.get());
814 ASSERT_TRUE(narrow_value->is_string());
815 std::unique_ptr<Value> utf16_value(new Value(ASCIIToUTF16("utf16")));
816 ASSERT_TRUE(utf16_value.get());
817 ASSERT_TRUE(utf16_value->is_string());
818
819 // Test overloaded GetAsString.
820 std::string narrow = "http://google.com";
821 string16 utf16 = ASCIIToUTF16("http://google.com");
822 const Value* string_value = nullptr;
823 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
824 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
825 ASSERT_TRUE(narrow_value->GetAsString(&string_value));
826 ASSERT_EQ(std::string("narrow"), narrow);
827 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
828 ASSERT_EQ(string_value->GetString(), narrow);
829
830 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
831 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
832 ASSERT_TRUE(utf16_value->GetAsString(&string_value));
833 ASSERT_EQ(std::string("utf16"), narrow);
834 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
835 ASSERT_EQ(string_value->GetString(), narrow);
836
837 // Don't choke on NULL values.
838 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(nullptr)));
839 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(nullptr)));
840 ASSERT_TRUE(narrow_value->GetAsString(static_cast<const Value**>(nullptr)));
841 }
842
TEST(ValuesTest,ListDeletion)843 TEST(ValuesTest, ListDeletion) {
844 ListValue list;
845 list.Append(std::make_unique<Value>());
846 EXPECT_FALSE(list.empty());
847 list.Clear();
848 EXPECT_TRUE(list.empty());
849 }
850
TEST(ValuesTest,ListRemoval)851 TEST(ValuesTest, ListRemoval) {
852 std::unique_ptr<Value> removed_item;
853
854 {
855 ListValue list;
856 list.Append(std::make_unique<Value>());
857 EXPECT_EQ(1U, list.GetSize());
858 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
859 &removed_item));
860 EXPECT_FALSE(list.Remove(1, &removed_item));
861 EXPECT_TRUE(list.Remove(0, &removed_item));
862 ASSERT_TRUE(removed_item);
863 EXPECT_EQ(0U, list.GetSize());
864 }
865 removed_item.reset();
866
867 {
868 ListValue list;
869 list.Append(std::make_unique<Value>());
870 EXPECT_TRUE(list.Remove(0, nullptr));
871 EXPECT_EQ(0U, list.GetSize());
872 }
873
874 {
875 ListValue list;
876 auto value = std::make_unique<Value>();
877 Value original_value = value->Clone();
878 list.Append(std::move(value));
879 size_t index = 0;
880 list.Remove(original_value, &index);
881 EXPECT_EQ(0U, index);
882 EXPECT_EQ(0U, list.GetSize());
883 }
884 }
885
TEST(ValuesTest,DictionaryDeletion)886 TEST(ValuesTest, DictionaryDeletion) {
887 std::string key = "test";
888 DictionaryValue dict;
889 dict.Set(key, std::make_unique<Value>());
890 EXPECT_FALSE(dict.empty());
891 EXPECT_FALSE(dict.DictEmpty());
892 EXPECT_EQ(1U, dict.DictSize());
893 dict.Clear();
894 EXPECT_TRUE(dict.empty());
895 EXPECT_TRUE(dict.DictEmpty());
896 EXPECT_EQ(0U, dict.DictSize());
897 }
898
TEST(ValuesTest,DictionarySetReturnsPointer)899 TEST(ValuesTest, DictionarySetReturnsPointer) {
900 {
901 DictionaryValue dict;
902 Value* blank_ptr = dict.Set("foo.bar", std::make_unique<base::Value>());
903 EXPECT_EQ(Value::Type::NONE, blank_ptr->type());
904 }
905
906 {
907 DictionaryValue dict;
908 Value* blank_ptr = dict.SetWithoutPathExpansion(
909 "foo.bar", std::make_unique<base::Value>());
910 EXPECT_EQ(Value::Type::NONE, blank_ptr->type());
911 }
912
913 {
914 DictionaryValue dict;
915 Value* int_ptr = dict.SetInteger("foo.bar", 42);
916 EXPECT_EQ(Value::Type::INTEGER, int_ptr->type());
917 EXPECT_EQ(42, int_ptr->GetInt());
918 }
919
920 {
921 DictionaryValue dict;
922 Value* double_ptr = dict.SetDouble("foo.bar", 3.142);
923 EXPECT_EQ(Value::Type::DOUBLE, double_ptr->type());
924 EXPECT_EQ(3.142, double_ptr->GetDouble());
925 }
926
927 {
928 DictionaryValue dict;
929 Value* string_ptr = dict.SetString("foo.bar", "foo");
930 EXPECT_EQ(Value::Type::STRING, string_ptr->type());
931 EXPECT_EQ("foo", string_ptr->GetString());
932 }
933
934 {
935 DictionaryValue dict;
936 Value* string16_ptr = dict.SetString("foo.bar", ASCIIToUTF16("baz"));
937 EXPECT_EQ(Value::Type::STRING, string16_ptr->type());
938 EXPECT_EQ("baz", string16_ptr->GetString());
939 }
940
941 {
942 DictionaryValue dict;
943 DictionaryValue* dict_ptr = dict.SetDictionary(
944 "foo.bar", std::make_unique<base::DictionaryValue>());
945 EXPECT_EQ(Value::Type::DICTIONARY, dict_ptr->type());
946 }
947
948 {
949 DictionaryValue dict;
950 ListValue* list_ptr =
951 dict.SetList("foo.bar", std::make_unique<base::ListValue>());
952 EXPECT_EQ(Value::Type::LIST, list_ptr->type());
953 }
954 }
955
TEST(ValuesTest,DictionaryRemoval)956 TEST(ValuesTest, DictionaryRemoval) {
957 std::string key = "test";
958 std::unique_ptr<Value> removed_item;
959
960 {
961 DictionaryValue dict;
962 EXPECT_EQ(0U, dict.DictSize());
963 EXPECT_TRUE(dict.DictEmpty());
964 dict.Set(key, std::make_unique<Value>());
965 EXPECT_TRUE(dict.HasKey(key));
966 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
967 EXPECT_EQ(1U, dict.DictSize());
968 EXPECT_FALSE(dict.DictEmpty());
969
970 EXPECT_TRUE(dict.Remove(key, &removed_item));
971 EXPECT_FALSE(dict.HasKey(key));
972 ASSERT_TRUE(removed_item);
973 EXPECT_EQ(0U, dict.DictSize());
974 EXPECT_TRUE(dict.DictEmpty());
975 }
976
977 {
978 DictionaryValue dict;
979 dict.Set(key, std::make_unique<Value>());
980 EXPECT_TRUE(dict.HasKey(key));
981 EXPECT_TRUE(dict.Remove(key, nullptr));
982 EXPECT_FALSE(dict.HasKey(key));
983 }
984 }
985
TEST(ValuesTest,DictionaryWithoutPathExpansion)986 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
987 DictionaryValue dict;
988 dict.Set("this.is.expanded", std::make_unique<Value>());
989 dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>());
990
991 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
992 EXPECT_TRUE(dict.HasKey("this"));
993 Value* value1;
994 EXPECT_TRUE(dict.Get("this", &value1));
995 DictionaryValue* value2;
996 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
997 EXPECT_EQ(value1, value2);
998 EXPECT_EQ(1U, value2->size());
999
1000 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
1001 Value* value3;
1002 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
1003 Value* value4;
1004 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
1005 EXPECT_EQ(Value::Type::NONE, value4->type());
1006 }
1007
1008 // Tests the deprecated version of SetWithoutPathExpansion.
1009 // TODO(estade): remove.
TEST(ValuesTest,DictionaryWithoutPathExpansionDeprecated)1010 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
1011 DictionaryValue dict;
1012 dict.Set("this.is.expanded", std::make_unique<Value>());
1013 dict.SetWithoutPathExpansion("this.isnt.expanded", std::make_unique<Value>());
1014
1015 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
1016 EXPECT_TRUE(dict.HasKey("this"));
1017 Value* value1;
1018 EXPECT_TRUE(dict.Get("this", &value1));
1019 DictionaryValue* value2;
1020 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
1021 EXPECT_EQ(value1, value2);
1022 EXPECT_EQ(1U, value2->size());
1023
1024 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
1025 Value* value3;
1026 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
1027 Value* value4;
1028 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
1029 EXPECT_EQ(Value::Type::NONE, value4->type());
1030 }
1031
TEST(ValuesTest,DictionaryRemovePath)1032 TEST(ValuesTest, DictionaryRemovePath) {
1033 DictionaryValue dict;
1034 dict.SetInteger("a.long.way.down", 1);
1035 dict.SetBoolean("a.long.key.path", true);
1036
1037 std::unique_ptr<Value> removed_item;
1038 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item));
1039 ASSERT_TRUE(removed_item);
1040 EXPECT_TRUE(removed_item->is_int());
1041 EXPECT_FALSE(dict.HasKey("a.long.way.down"));
1042 EXPECT_FALSE(dict.HasKey("a.long.way"));
1043 EXPECT_TRUE(dict.Get("a.long.key.path", nullptr));
1044
1045 removed_item.reset();
1046 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item));
1047 EXPECT_FALSE(removed_item);
1048 EXPECT_TRUE(dict.Get("a.long.key.path", nullptr));
1049
1050 removed_item.reset();
1051 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
1052 ASSERT_TRUE(removed_item);
1053 EXPECT_TRUE(removed_item->is_bool());
1054 EXPECT_TRUE(dict.empty());
1055 }
1056
TEST(ValuesTest,DeepCopy)1057 TEST(ValuesTest, DeepCopy) {
1058 DictionaryValue original_dict;
1059 Value* null_weak = original_dict.Set("null", std::make_unique<Value>());
1060 Value* bool_weak = original_dict.Set("bool", std::make_unique<Value>(true));
1061 Value* int_weak = original_dict.Set("int", std::make_unique<Value>(42));
1062 Value* double_weak =
1063 original_dict.Set("double", std::make_unique<Value>(3.14));
1064 Value* string_weak =
1065 original_dict.Set("string", std::make_unique<Value>("hello"));
1066 Value* string16_weak = original_dict.Set(
1067 "string16", std::make_unique<Value>(ASCIIToUTF16("hello16")));
1068
1069 Value* binary_weak = original_dict.Set(
1070 "binary", std::make_unique<Value>(Value::BlobStorage(42, '!')));
1071
1072 Value::ListStorage storage;
1073 storage.emplace_back(0);
1074 storage.emplace_back(1);
1075 Value* list_weak =
1076 original_dict.Set("list", std::make_unique<Value>(std::move(storage)));
1077 Value* list_element_0_weak = &list_weak->GetList()[0];
1078 Value* list_element_1_weak = &list_weak->GetList()[1];
1079
1080 DictionaryValue* dict_weak = original_dict.SetDictionary(
1081 "dictionary", std::make_unique<DictionaryValue>());
1082 dict_weak->SetString("key", "value");
1083
1084 auto copy_dict = original_dict.CreateDeepCopy();
1085 ASSERT_TRUE(copy_dict.get());
1086 ASSERT_NE(copy_dict.get(), &original_dict);
1087
1088 Value* copy_null = nullptr;
1089 ASSERT_TRUE(copy_dict->Get("null", ©_null));
1090 ASSERT_TRUE(copy_null);
1091 ASSERT_NE(copy_null, null_weak);
1092 ASSERT_TRUE(copy_null->is_none());
1093
1094 Value* copy_bool = nullptr;
1095 ASSERT_TRUE(copy_dict->Get("bool", ©_bool));
1096 ASSERT_TRUE(copy_bool);
1097 ASSERT_NE(copy_bool, bool_weak);
1098 ASSERT_TRUE(copy_bool->is_bool());
1099 bool copy_bool_value = false;
1100 ASSERT_TRUE(copy_bool->GetAsBoolean(©_bool_value));
1101 ASSERT_TRUE(copy_bool_value);
1102
1103 Value* copy_int = nullptr;
1104 ASSERT_TRUE(copy_dict->Get("int", ©_int));
1105 ASSERT_TRUE(copy_int);
1106 ASSERT_NE(copy_int, int_weak);
1107 ASSERT_TRUE(copy_int->is_int());
1108 int copy_int_value = 0;
1109 ASSERT_TRUE(copy_int->GetAsInteger(©_int_value));
1110 ASSERT_EQ(42, copy_int_value);
1111
1112 Value* copy_double = nullptr;
1113 ASSERT_TRUE(copy_dict->Get("double", ©_double));
1114 ASSERT_TRUE(copy_double);
1115 ASSERT_NE(copy_double, double_weak);
1116 ASSERT_TRUE(copy_double->is_double());
1117 double copy_double_value = 0;
1118 ASSERT_TRUE(copy_double->GetAsDouble(©_double_value));
1119 ASSERT_EQ(3.14, copy_double_value);
1120
1121 Value* copy_string = nullptr;
1122 ASSERT_TRUE(copy_dict->Get("string", ©_string));
1123 ASSERT_TRUE(copy_string);
1124 ASSERT_NE(copy_string, string_weak);
1125 ASSERT_TRUE(copy_string->is_string());
1126 std::string copy_string_value;
1127 string16 copy_string16_value;
1128 ASSERT_TRUE(copy_string->GetAsString(©_string_value));
1129 ASSERT_TRUE(copy_string->GetAsString(©_string16_value));
1130 ASSERT_EQ(std::string("hello"), copy_string_value);
1131 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
1132
1133 Value* copy_string16 = nullptr;
1134 ASSERT_TRUE(copy_dict->Get("string16", ©_string16));
1135 ASSERT_TRUE(copy_string16);
1136 ASSERT_NE(copy_string16, string16_weak);
1137 ASSERT_TRUE(copy_string16->is_string());
1138 ASSERT_TRUE(copy_string16->GetAsString(©_string_value));
1139 ASSERT_TRUE(copy_string16->GetAsString(©_string16_value));
1140 ASSERT_EQ(std::string("hello16"), copy_string_value);
1141 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
1142
1143 Value* copy_binary = nullptr;
1144 ASSERT_TRUE(copy_dict->Get("binary", ©_binary));
1145 ASSERT_TRUE(copy_binary);
1146 ASSERT_NE(copy_binary, binary_weak);
1147 ASSERT_TRUE(copy_binary->is_blob());
1148 ASSERT_NE(binary_weak->GetBlob().data(), copy_binary->GetBlob().data());
1149 ASSERT_EQ(binary_weak->GetBlob(), copy_binary->GetBlob());
1150
1151 Value* copy_value = nullptr;
1152 ASSERT_TRUE(copy_dict->Get("list", ©_value));
1153 ASSERT_TRUE(copy_value);
1154 ASSERT_NE(copy_value, list_weak);
1155 ASSERT_TRUE(copy_value->is_list());
1156 ListValue* copy_list = nullptr;
1157 ASSERT_TRUE(copy_value->GetAsList(©_list));
1158 ASSERT_TRUE(copy_list);
1159 ASSERT_EQ(2U, copy_list->GetSize());
1160
1161 Value* copy_list_element_0;
1162 ASSERT_TRUE(copy_list->Get(0, ©_list_element_0));
1163 ASSERT_TRUE(copy_list_element_0);
1164 ASSERT_NE(copy_list_element_0, list_element_0_weak);
1165 int copy_list_element_0_value;
1166 ASSERT_TRUE(copy_list_element_0->GetAsInteger(©_list_element_0_value));
1167 ASSERT_EQ(0, copy_list_element_0_value);
1168
1169 Value* copy_list_element_1;
1170 ASSERT_TRUE(copy_list->Get(1, ©_list_element_1));
1171 ASSERT_TRUE(copy_list_element_1);
1172 ASSERT_NE(copy_list_element_1, list_element_1_weak);
1173 int copy_list_element_1_value;
1174 ASSERT_TRUE(copy_list_element_1->GetAsInteger(©_list_element_1_value));
1175 ASSERT_EQ(1, copy_list_element_1_value);
1176
1177 copy_value = nullptr;
1178 ASSERT_TRUE(copy_dict->Get("dictionary", ©_value));
1179 ASSERT_TRUE(copy_value);
1180 ASSERT_NE(copy_value, dict_weak);
1181 ASSERT_TRUE(copy_value->is_dict());
1182 DictionaryValue* copy_nested_dictionary = nullptr;
1183 ASSERT_TRUE(copy_value->GetAsDictionary(©_nested_dictionary));
1184 ASSERT_TRUE(copy_nested_dictionary);
1185 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
1186 }
1187
TEST(ValuesTest,Equals)1188 TEST(ValuesTest, Equals) {
1189 auto null1 = std::make_unique<Value>();
1190 auto null2 = std::make_unique<Value>();
1191 EXPECT_NE(null1.get(), null2.get());
1192 EXPECT_EQ(*null1, *null2);
1193
1194 Value boolean(false);
1195 EXPECT_NE(*null1, boolean);
1196
1197 DictionaryValue dv;
1198 dv.SetBoolean("a", false);
1199 dv.SetInteger("b", 2);
1200 dv.SetDouble("c", 2.5);
1201 dv.SetString("d1", "string");
1202 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
1203 dv.Set("e", std::make_unique<Value>());
1204
1205 auto copy = dv.CreateDeepCopy();
1206 EXPECT_EQ(dv, *copy);
1207
1208 std::unique_ptr<ListValue> list(new ListValue);
1209 list->Append(std::make_unique<Value>());
1210 list->Append(WrapUnique(new DictionaryValue));
1211 auto list_copy = std::make_unique<Value>(list->Clone());
1212
1213 ListValue* list_weak = dv.SetList("f", std::move(list));
1214 EXPECT_NE(dv, *copy);
1215 copy->Set("f", std::move(list_copy));
1216 EXPECT_EQ(dv, *copy);
1217
1218 list_weak->Append(std::make_unique<Value>(true));
1219 EXPECT_NE(dv, *copy);
1220
1221 // Check if Equals detects differences in only the keys.
1222 copy = dv.CreateDeepCopy();
1223 EXPECT_EQ(dv, *copy);
1224 copy->Remove("a", nullptr);
1225 copy->SetBoolean("aa", false);
1226 EXPECT_NE(dv, *copy);
1227 }
1228
TEST(ValuesTest,Comparisons)1229 TEST(ValuesTest, Comparisons) {
1230 // Test None Values.
1231 Value null1;
1232 Value null2;
1233 EXPECT_EQ(null1, null2);
1234 EXPECT_FALSE(null1 != null2);
1235 EXPECT_FALSE(null1 < null2);
1236 EXPECT_FALSE(null1 > null2);
1237 EXPECT_LE(null1, null2);
1238 EXPECT_GE(null1, null2);
1239
1240 // Test Bool Values.
1241 Value bool1(false);
1242 Value bool2(true);
1243 EXPECT_FALSE(bool1 == bool2);
1244 EXPECT_NE(bool1, bool2);
1245 EXPECT_LT(bool1, bool2);
1246 EXPECT_FALSE(bool1 > bool2);
1247 EXPECT_LE(bool1, bool2);
1248 EXPECT_FALSE(bool1 >= bool2);
1249
1250 // Test Int Values.
1251 Value int1(1);
1252 Value int2(2);
1253 EXPECT_FALSE(int1 == int2);
1254 EXPECT_NE(int1, int2);
1255 EXPECT_LT(int1, int2);
1256 EXPECT_FALSE(int1 > int2);
1257 EXPECT_LE(int1, int2);
1258 EXPECT_FALSE(int1 >= int2);
1259
1260 // Test Double Values.
1261 Value double1(1.0);
1262 Value double2(2.0);
1263 EXPECT_FALSE(double1 == double2);
1264 EXPECT_NE(double1, double2);
1265 EXPECT_LT(double1, double2);
1266 EXPECT_FALSE(double1 > double2);
1267 EXPECT_LE(double1, double2);
1268 EXPECT_FALSE(double1 >= double2);
1269
1270 // Test String Values.
1271 Value string1("1");
1272 Value string2("2");
1273 EXPECT_FALSE(string1 == string2);
1274 EXPECT_NE(string1, string2);
1275 EXPECT_LT(string1, string2);
1276 EXPECT_FALSE(string1 > string2);
1277 EXPECT_LE(string1, string2);
1278 EXPECT_FALSE(string1 >= string2);
1279
1280 // Test Binary Values.
1281 Value binary1(Value::BlobStorage{0x01});
1282 Value binary2(Value::BlobStorage{0x02});
1283 EXPECT_FALSE(binary1 == binary2);
1284 EXPECT_NE(binary1, binary2);
1285 EXPECT_LT(binary1, binary2);
1286 EXPECT_FALSE(binary1 > binary2);
1287 EXPECT_LE(binary1, binary2);
1288 EXPECT_FALSE(binary1 >= binary2);
1289
1290 // Test Empty List Values.
1291 ListValue null_list1;
1292 ListValue null_list2;
1293 EXPECT_EQ(null_list1, null_list2);
1294 EXPECT_FALSE(null_list1 != null_list2);
1295 EXPECT_FALSE(null_list1 < null_list2);
1296 EXPECT_FALSE(null_list1 > null_list2);
1297 EXPECT_LE(null_list1, null_list2);
1298 EXPECT_GE(null_list1, null_list2);
1299
1300 // Test Non Empty List Values.
1301 ListValue int_list1;
1302 ListValue int_list2;
1303 int_list1.AppendInteger(1);
1304 int_list2.AppendInteger(2);
1305 EXPECT_FALSE(int_list1 == int_list2);
1306 EXPECT_NE(int_list1, int_list2);
1307 EXPECT_LT(int_list1, int_list2);
1308 EXPECT_FALSE(int_list1 > int_list2);
1309 EXPECT_LE(int_list1, int_list2);
1310 EXPECT_FALSE(int_list1 >= int_list2);
1311
1312 // Test Empty Dict Values.
1313 DictionaryValue null_dict1;
1314 DictionaryValue null_dict2;
1315 EXPECT_EQ(null_dict1, null_dict2);
1316 EXPECT_FALSE(null_dict1 != null_dict2);
1317 EXPECT_FALSE(null_dict1 < null_dict2);
1318 EXPECT_FALSE(null_dict1 > null_dict2);
1319 EXPECT_LE(null_dict1, null_dict2);
1320 EXPECT_GE(null_dict1, null_dict2);
1321
1322 // Test Non Empty Dict Values.
1323 DictionaryValue int_dict1;
1324 DictionaryValue int_dict2;
1325 int_dict1.SetInteger("key", 1);
1326 int_dict2.SetInteger("key", 2);
1327 EXPECT_FALSE(int_dict1 == int_dict2);
1328 EXPECT_NE(int_dict1, int_dict2);
1329 EXPECT_LT(int_dict1, int_dict2);
1330 EXPECT_FALSE(int_dict1 > int_dict2);
1331 EXPECT_LE(int_dict1, int_dict2);
1332 EXPECT_FALSE(int_dict1 >= int_dict2);
1333
1334 // Test Values of different types.
1335 std::vector<Value> values;
1336 values.emplace_back(std::move(null1));
1337 values.emplace_back(std::move(bool1));
1338 values.emplace_back(std::move(int1));
1339 values.emplace_back(std::move(double1));
1340 values.emplace_back(std::move(string1));
1341 values.emplace_back(std::move(binary1));
1342 values.emplace_back(std::move(int_dict1));
1343 values.emplace_back(std::move(int_list1));
1344 for (size_t i = 0; i < values.size(); ++i) {
1345 for (size_t j = i + 1; j < values.size(); ++j) {
1346 EXPECT_FALSE(values[i] == values[j]);
1347 EXPECT_NE(values[i], values[j]);
1348 EXPECT_LT(values[i], values[j]);
1349 EXPECT_FALSE(values[i] > values[j]);
1350 EXPECT_LE(values[i], values[j]);
1351 EXPECT_FALSE(values[i] >= values[j]);
1352 }
1353 }
1354 }
1355
TEST(ValuesTest,DeepCopyCovariantReturnTypes)1356 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
1357 DictionaryValue original_dict;
1358 Value* null_weak = original_dict.SetKey("null", Value());
1359 Value* bool_weak = original_dict.SetKey("bool", Value(true));
1360 Value* int_weak = original_dict.SetKey("int", Value(42));
1361 Value* double_weak = original_dict.SetKey("double", Value(3.14));
1362 Value* string_weak = original_dict.SetKey("string", Value("hello"));
1363 Value* string16_weak =
1364 original_dict.SetKey("string16", Value(ASCIIToUTF16("hello16")));
1365 Value* binary_weak =
1366 original_dict.SetKey("binary", Value(Value::BlobStorage(42, '!')));
1367
1368 Value::ListStorage storage;
1369 storage.emplace_back(0);
1370 storage.emplace_back(1);
1371 Value* list_weak = original_dict.SetKey("list", Value(std::move(storage)));
1372
1373 auto copy_dict = std::make_unique<Value>(original_dict.Clone());
1374 auto copy_null = std::make_unique<Value>(null_weak->Clone());
1375 auto copy_bool = std::make_unique<Value>(bool_weak->Clone());
1376 auto copy_int = std::make_unique<Value>(int_weak->Clone());
1377 auto copy_double = std::make_unique<Value>(double_weak->Clone());
1378 auto copy_string = std::make_unique<Value>(string_weak->Clone());
1379 auto copy_string16 = std::make_unique<Value>(string16_weak->Clone());
1380 auto copy_binary = std::make_unique<Value>(binary_weak->Clone());
1381 auto copy_list = std::make_unique<Value>(list_weak->Clone());
1382
1383 EXPECT_EQ(original_dict, *copy_dict);
1384 EXPECT_EQ(*null_weak, *copy_null);
1385 EXPECT_EQ(*bool_weak, *copy_bool);
1386 EXPECT_EQ(*int_weak, *copy_int);
1387 EXPECT_EQ(*double_weak, *copy_double);
1388 EXPECT_EQ(*string_weak, *copy_string);
1389 EXPECT_EQ(*string16_weak, *copy_string16);
1390 EXPECT_EQ(*binary_weak, *copy_binary);
1391 EXPECT_EQ(*list_weak, *copy_list);
1392 }
1393
TEST(ValuesTest,RemoveEmptyChildren)1394 TEST(ValuesTest, RemoveEmptyChildren) {
1395 auto root = std::make_unique<DictionaryValue>();
1396 // Remove empty lists and dictionaries.
1397 root->Set("empty_dict", std::make_unique<DictionaryValue>());
1398 root->Set("empty_list", std::make_unique<ListValue>());
1399 root->SetWithoutPathExpansion("a.b.c.d.e",
1400 std::make_unique<DictionaryValue>());
1401 root = root->DeepCopyWithoutEmptyChildren();
1402 EXPECT_TRUE(root->empty());
1403
1404 // Make sure we don't prune too much.
1405 root->SetBoolean("bool", true);
1406 root->Set("empty_dict", std::make_unique<DictionaryValue>());
1407 root->SetString("empty_string", std::string());
1408 root = root->DeepCopyWithoutEmptyChildren();
1409 EXPECT_EQ(2U, root->size());
1410
1411 // Should do nothing.
1412 root = root->DeepCopyWithoutEmptyChildren();
1413 EXPECT_EQ(2U, root->size());
1414
1415 // Nested test cases. These should all reduce back to the bool and string
1416 // set above.
1417 {
1418 root->Set("a.b.c.d.e", std::make_unique<DictionaryValue>());
1419 root = root->DeepCopyWithoutEmptyChildren();
1420 EXPECT_EQ(2U, root->size());
1421 }
1422 {
1423 auto inner = std::make_unique<DictionaryValue>();
1424 inner->Set("empty_dict", std::make_unique<DictionaryValue>());
1425 inner->Set("empty_list", std::make_unique<ListValue>());
1426 root->Set("dict_with_empty_children", std::move(inner));
1427 root = root->DeepCopyWithoutEmptyChildren();
1428 EXPECT_EQ(2U, root->size());
1429 }
1430 {
1431 auto inner = std::make_unique<ListValue>();
1432 inner->Append(std::make_unique<DictionaryValue>());
1433 inner->Append(std::make_unique<ListValue>());
1434 root->Set("list_with_empty_children", std::move(inner));
1435 root = root->DeepCopyWithoutEmptyChildren();
1436 EXPECT_EQ(2U, root->size());
1437 }
1438
1439 // Nested with siblings.
1440 {
1441 auto inner = std::make_unique<ListValue>();
1442 inner->Append(std::make_unique<DictionaryValue>());
1443 inner->Append(std::make_unique<ListValue>());
1444 root->Set("list_with_empty_children", std::move(inner));
1445 auto inner2 = std::make_unique<DictionaryValue>();
1446 inner2->Set("empty_dict", std::make_unique<DictionaryValue>());
1447 inner2->Set("empty_list", std::make_unique<ListValue>());
1448 root->Set("dict_with_empty_children", std::move(inner2));
1449 root = root->DeepCopyWithoutEmptyChildren();
1450 EXPECT_EQ(2U, root->size());
1451 }
1452
1453 // Make sure nested values don't get pruned.
1454 {
1455 auto inner = std::make_unique<ListValue>();
1456 auto inner2 = std::make_unique<ListValue>();
1457 inner2->Append(std::make_unique<Value>("hello"));
1458 inner->Append(std::make_unique<DictionaryValue>());
1459 inner->Append(std::move(inner2));
1460 root->Set("list_with_empty_children", std::move(inner));
1461 root = root->DeepCopyWithoutEmptyChildren();
1462 EXPECT_EQ(3U, root->size());
1463
1464 ListValue* inner_value, *inner_value2;
1465 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
1466 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
1467 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
1468 EXPECT_EQ(1U, inner_value2->GetSize());
1469 }
1470 }
1471
TEST(ValuesTest,MergeDictionary)1472 TEST(ValuesTest, MergeDictionary) {
1473 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
1474 base->SetString("base_key", "base_key_value_base");
1475 base->SetString("collide_key", "collide_key_value_base");
1476 std::unique_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
1477 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
1478 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
1479 base->Set("sub_dict_key", std::move(base_sub_dict));
1480
1481 std::unique_ptr<DictionaryValue> merge(new DictionaryValue);
1482 merge->SetString("merge_key", "merge_key_value_merge");
1483 merge->SetString("collide_key", "collide_key_value_merge");
1484 std::unique_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
1485 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
1486 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
1487 merge->Set("sub_dict_key", std::move(merge_sub_dict));
1488
1489 base->MergeDictionary(merge.get());
1490
1491 EXPECT_EQ(4U, base->size());
1492 std::string base_key_value;
1493 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
1494 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
1495 std::string collide_key_value;
1496 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
1497 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
1498 std::string merge_key_value;
1499 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
1500 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
1501
1502 DictionaryValue* res_sub_dict;
1503 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
1504 EXPECT_EQ(3U, res_sub_dict->size());
1505 std::string sub_base_key_value;
1506 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
1507 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1508 std::string sub_collide_key_value;
1509 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
1510 &sub_collide_key_value));
1511 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1512 std::string sub_merge_key_value;
1513 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
1514 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1515 }
1516
TEST(ValuesTest,MergeDictionaryDeepCopy)1517 TEST(ValuesTest, MergeDictionaryDeepCopy) {
1518 std::unique_ptr<DictionaryValue> child(new DictionaryValue);
1519 DictionaryValue* original_child = child.get();
1520 child->SetString("test", "value");
1521 EXPECT_EQ(1U, child->size());
1522
1523 std::string value;
1524 EXPECT_TRUE(child->GetString("test", &value));
1525 EXPECT_EQ("value", value);
1526
1527 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
1528 base->Set("dict", std::move(child));
1529 EXPECT_EQ(1U, base->size());
1530
1531 DictionaryValue* ptr;
1532 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
1533 EXPECT_EQ(original_child, ptr);
1534
1535 std::unique_ptr<DictionaryValue> merged(new DictionaryValue);
1536 merged->MergeDictionary(base.get());
1537 EXPECT_EQ(1U, merged->size());
1538 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
1539 EXPECT_NE(original_child, ptr);
1540 EXPECT_TRUE(ptr->GetString("test", &value));
1541 EXPECT_EQ("value", value);
1542
1543 original_child->SetString("test", "overwrite");
1544 base.reset();
1545 EXPECT_TRUE(ptr->GetString("test", &value));
1546 EXPECT_EQ("value", value);
1547 }
1548
TEST(ValuesTest,DictionaryIterator)1549 TEST(ValuesTest, DictionaryIterator) {
1550 DictionaryValue dict;
1551 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1552 ADD_FAILURE();
1553 }
1554
1555 Value value1("value1");
1556 dict.SetKey("key1", value1.Clone());
1557 bool seen1 = false;
1558 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1559 EXPECT_FALSE(seen1);
1560 EXPECT_EQ("key1", it.key());
1561 EXPECT_EQ(value1, it.value());
1562 seen1 = true;
1563 }
1564 EXPECT_TRUE(seen1);
1565
1566 Value value2("value2");
1567 dict.SetKey("key2", value2.Clone());
1568 bool seen2 = seen1 = false;
1569 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
1570 if (it.key() == "key1") {
1571 EXPECT_FALSE(seen1);
1572 EXPECT_EQ(value1, it.value());
1573 seen1 = true;
1574 } else if (it.key() == "key2") {
1575 EXPECT_FALSE(seen2);
1576 EXPECT_EQ(value2, it.value());
1577 seen2 = true;
1578 } else {
1579 ADD_FAILURE();
1580 }
1581 }
1582 EXPECT_TRUE(seen1);
1583 EXPECT_TRUE(seen2);
1584 }
1585
TEST(ValuesTest,StdDictionaryIterator)1586 TEST(ValuesTest, StdDictionaryIterator) {
1587 DictionaryValue dict;
1588 for (auto it = dict.begin(); it != dict.end(); ++it) {
1589 ADD_FAILURE();
1590 }
1591
1592 Value value1("value1");
1593 dict.SetKey("key1", value1.Clone());
1594 bool seen1 = false;
1595 for (const auto& it : dict) {
1596 EXPECT_FALSE(seen1);
1597 EXPECT_EQ("key1", it.first);
1598 EXPECT_EQ(value1, *it.second);
1599 seen1 = true;
1600 }
1601 EXPECT_TRUE(seen1);
1602
1603 Value value2("value2");
1604 dict.SetKey("key2", value2.Clone());
1605 bool seen2 = seen1 = false;
1606 for (const auto& it : dict) {
1607 if (it.first == "key1") {
1608 EXPECT_FALSE(seen1);
1609 EXPECT_EQ(value1, *it.second);
1610 seen1 = true;
1611 } else if (it.first == "key2") {
1612 EXPECT_FALSE(seen2);
1613 EXPECT_EQ(value2, *it.second);
1614 seen2 = true;
1615 } else {
1616 ADD_FAILURE();
1617 }
1618 }
1619 EXPECT_TRUE(seen1);
1620 EXPECT_TRUE(seen2);
1621 }
1622
1623 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
1624 // and still return true/false based on success.
TEST(ValuesTest,GetWithNullOutValue)1625 TEST(ValuesTest, GetWithNullOutValue) {
1626 DictionaryValue main_dict;
1627 ListValue main_list;
1628
1629 Value bool_value(false);
1630 Value int_value(1234);
1631 Value double_value(12.34567);
1632 Value string_value("foo");
1633 Value binary_value(Value::Type::BINARY);
1634 DictionaryValue dict_value;
1635 ListValue list_value;
1636
1637 main_dict.SetKey("bool", bool_value.Clone());
1638 main_dict.SetKey("int", int_value.Clone());
1639 main_dict.SetKey("double", double_value.Clone());
1640 main_dict.SetKey("string", string_value.Clone());
1641 main_dict.SetKey("binary", binary_value.Clone());
1642 main_dict.SetKey("dict", dict_value.Clone());
1643 main_dict.SetKey("list", list_value.Clone());
1644
1645 main_list.Append(std::make_unique<Value>(bool_value.Clone()));
1646 main_list.Append(std::make_unique<Value>(int_value.Clone()));
1647 main_list.Append(std::make_unique<Value>(double_value.Clone()));
1648 main_list.Append(std::make_unique<Value>(string_value.Clone()));
1649 main_list.Append(std::make_unique<Value>(binary_value.Clone()));
1650 main_list.Append(std::make_unique<Value>(dict_value.Clone()));
1651 main_list.Append(std::make_unique<Value>(list_value.Clone()));
1652
1653 EXPECT_TRUE(main_dict.Get("bool", nullptr));
1654 EXPECT_TRUE(main_dict.Get("int", nullptr));
1655 EXPECT_TRUE(main_dict.Get("double", nullptr));
1656 EXPECT_TRUE(main_dict.Get("string", nullptr));
1657 EXPECT_TRUE(main_dict.Get("binary", nullptr));
1658 EXPECT_TRUE(main_dict.Get("dict", nullptr));
1659 EXPECT_TRUE(main_dict.Get("list", nullptr));
1660 EXPECT_FALSE(main_dict.Get("DNE", nullptr));
1661
1662 EXPECT_TRUE(main_dict.GetBoolean("bool", nullptr));
1663 EXPECT_FALSE(main_dict.GetBoolean("int", nullptr));
1664 EXPECT_FALSE(main_dict.GetBoolean("double", nullptr));
1665 EXPECT_FALSE(main_dict.GetBoolean("string", nullptr));
1666 EXPECT_FALSE(main_dict.GetBoolean("binary", nullptr));
1667 EXPECT_FALSE(main_dict.GetBoolean("dict", nullptr));
1668 EXPECT_FALSE(main_dict.GetBoolean("list", nullptr));
1669 EXPECT_FALSE(main_dict.GetBoolean("DNE", nullptr));
1670
1671 EXPECT_FALSE(main_dict.GetInteger("bool", nullptr));
1672 EXPECT_TRUE(main_dict.GetInteger("int", nullptr));
1673 EXPECT_FALSE(main_dict.GetInteger("double", nullptr));
1674 EXPECT_FALSE(main_dict.GetInteger("string", nullptr));
1675 EXPECT_FALSE(main_dict.GetInteger("binary", nullptr));
1676 EXPECT_FALSE(main_dict.GetInteger("dict", nullptr));
1677 EXPECT_FALSE(main_dict.GetInteger("list", nullptr));
1678 EXPECT_FALSE(main_dict.GetInteger("DNE", nullptr));
1679
1680 // Both int and double values can be obtained from GetDouble.
1681 EXPECT_FALSE(main_dict.GetDouble("bool", nullptr));
1682 EXPECT_TRUE(main_dict.GetDouble("int", nullptr));
1683 EXPECT_TRUE(main_dict.GetDouble("double", nullptr));
1684 EXPECT_FALSE(main_dict.GetDouble("string", nullptr));
1685 EXPECT_FALSE(main_dict.GetDouble("binary", nullptr));
1686 EXPECT_FALSE(main_dict.GetDouble("dict", nullptr));
1687 EXPECT_FALSE(main_dict.GetDouble("list", nullptr));
1688 EXPECT_FALSE(main_dict.GetDouble("DNE", nullptr));
1689
1690 EXPECT_FALSE(main_dict.GetString("bool", static_cast<std::string*>(nullptr)));
1691 EXPECT_FALSE(main_dict.GetString("int", static_cast<std::string*>(nullptr)));
1692 EXPECT_FALSE(
1693 main_dict.GetString("double", static_cast<std::string*>(nullptr)));
1694 EXPECT_TRUE(
1695 main_dict.GetString("string", static_cast<std::string*>(nullptr)));
1696 EXPECT_FALSE(
1697 main_dict.GetString("binary", static_cast<std::string*>(nullptr)));
1698 EXPECT_FALSE(main_dict.GetString("dict", static_cast<std::string*>(nullptr)));
1699 EXPECT_FALSE(main_dict.GetString("list", static_cast<std::string*>(nullptr)));
1700 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<std::string*>(nullptr)));
1701
1702 EXPECT_FALSE(main_dict.GetString("bool", static_cast<string16*>(nullptr)));
1703 EXPECT_FALSE(main_dict.GetString("int", static_cast<string16*>(nullptr)));
1704 EXPECT_FALSE(main_dict.GetString("double", static_cast<string16*>(nullptr)));
1705 EXPECT_TRUE(main_dict.GetString("string", static_cast<string16*>(nullptr)));
1706 EXPECT_FALSE(main_dict.GetString("binary", static_cast<string16*>(nullptr)));
1707 EXPECT_FALSE(main_dict.GetString("dict", static_cast<string16*>(nullptr)));
1708 EXPECT_FALSE(main_dict.GetString("list", static_cast<string16*>(nullptr)));
1709 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<string16*>(nullptr)));
1710
1711 EXPECT_FALSE(main_dict.GetBinary("bool", nullptr));
1712 EXPECT_FALSE(main_dict.GetBinary("int", nullptr));
1713 EXPECT_FALSE(main_dict.GetBinary("double", nullptr));
1714 EXPECT_FALSE(main_dict.GetBinary("string", nullptr));
1715 EXPECT_TRUE(main_dict.GetBinary("binary", nullptr));
1716 EXPECT_FALSE(main_dict.GetBinary("dict", nullptr));
1717 EXPECT_FALSE(main_dict.GetBinary("list", nullptr));
1718 EXPECT_FALSE(main_dict.GetBinary("DNE", nullptr));
1719
1720 EXPECT_FALSE(main_dict.GetDictionary("bool", nullptr));
1721 EXPECT_FALSE(main_dict.GetDictionary("int", nullptr));
1722 EXPECT_FALSE(main_dict.GetDictionary("double", nullptr));
1723 EXPECT_FALSE(main_dict.GetDictionary("string", nullptr));
1724 EXPECT_FALSE(main_dict.GetDictionary("binary", nullptr));
1725 EXPECT_TRUE(main_dict.GetDictionary("dict", nullptr));
1726 EXPECT_FALSE(main_dict.GetDictionary("list", nullptr));
1727 EXPECT_FALSE(main_dict.GetDictionary("DNE", nullptr));
1728
1729 EXPECT_FALSE(main_dict.GetList("bool", nullptr));
1730 EXPECT_FALSE(main_dict.GetList("int", nullptr));
1731 EXPECT_FALSE(main_dict.GetList("double", nullptr));
1732 EXPECT_FALSE(main_dict.GetList("string", nullptr));
1733 EXPECT_FALSE(main_dict.GetList("binary", nullptr));
1734 EXPECT_FALSE(main_dict.GetList("dict", nullptr));
1735 EXPECT_TRUE(main_dict.GetList("list", nullptr));
1736 EXPECT_FALSE(main_dict.GetList("DNE", nullptr));
1737
1738 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("bool", nullptr));
1739 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("int", nullptr));
1740 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("double", nullptr));
1741 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("string", nullptr));
1742 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("binary", nullptr));
1743 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("dict", nullptr));
1744 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("list", nullptr));
1745 EXPECT_FALSE(main_dict.GetWithoutPathExpansion("DNE", nullptr));
1746
1747 EXPECT_TRUE(main_dict.GetBooleanWithoutPathExpansion("bool", nullptr));
1748 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("int", nullptr));
1749 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("double", nullptr));
1750 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("string", nullptr));
1751 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("binary", nullptr));
1752 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("dict", nullptr));
1753 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("list", nullptr));
1754 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("DNE", nullptr));
1755
1756 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("bool", nullptr));
1757 EXPECT_TRUE(main_dict.GetIntegerWithoutPathExpansion("int", nullptr));
1758 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("double", nullptr));
1759 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("string", nullptr));
1760 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("binary", nullptr));
1761 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("dict", nullptr));
1762 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("list", nullptr));
1763 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("DNE", nullptr));
1764
1765 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("bool", nullptr));
1766 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("int", nullptr));
1767 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("double", nullptr));
1768 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("string", nullptr));
1769 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("binary", nullptr));
1770 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("dict", nullptr));
1771 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("list", nullptr));
1772 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("DNE", nullptr));
1773
1774 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1775 "bool", static_cast<std::string*>(nullptr)));
1776 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1777 "int", static_cast<std::string*>(nullptr)));
1778 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1779 "double", static_cast<std::string*>(nullptr)));
1780 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1781 "string", static_cast<std::string*>(nullptr)));
1782 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1783 "binary", static_cast<std::string*>(nullptr)));
1784 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1785 "dict", static_cast<std::string*>(nullptr)));
1786 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1787 "list", static_cast<std::string*>(nullptr)));
1788 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1789 "DNE", static_cast<std::string*>(nullptr)));
1790
1791 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1792 "bool", static_cast<string16*>(nullptr)));
1793 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1794 "int", static_cast<string16*>(nullptr)));
1795 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1796 "double", static_cast<string16*>(nullptr)));
1797 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1798 "string", static_cast<string16*>(nullptr)));
1799 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1800 "binary", static_cast<string16*>(nullptr)));
1801 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1802 "dict", static_cast<string16*>(nullptr)));
1803 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1804 "list", static_cast<string16*>(nullptr)));
1805 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1806 "DNE", static_cast<string16*>(nullptr)));
1807
1808 // There is no GetBinaryWithoutPathExpansion for some reason, but if there
1809 // were it should be tested here...
1810
1811 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("bool", nullptr));
1812 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("int", nullptr));
1813 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("double", nullptr));
1814 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("string", nullptr));
1815 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("binary", nullptr));
1816 EXPECT_TRUE(main_dict.GetDictionaryWithoutPathExpansion("dict", nullptr));
1817 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("list", nullptr));
1818 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("DNE", nullptr));
1819
1820 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("bool", nullptr));
1821 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("int", nullptr));
1822 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("double", nullptr));
1823 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("string", nullptr));
1824 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("binary", nullptr));
1825 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("dict", nullptr));
1826 EXPECT_TRUE(main_dict.GetListWithoutPathExpansion("list", nullptr));
1827 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("DNE", nullptr));
1828
1829 EXPECT_TRUE(main_list.Get(0, nullptr));
1830 EXPECT_TRUE(main_list.Get(1, nullptr));
1831 EXPECT_TRUE(main_list.Get(2, nullptr));
1832 EXPECT_TRUE(main_list.Get(3, nullptr));
1833 EXPECT_TRUE(main_list.Get(4, nullptr));
1834 EXPECT_TRUE(main_list.Get(5, nullptr));
1835 EXPECT_TRUE(main_list.Get(6, nullptr));
1836 EXPECT_FALSE(main_list.Get(7, nullptr));
1837
1838 EXPECT_TRUE(main_list.GetBoolean(0, nullptr));
1839 EXPECT_FALSE(main_list.GetBoolean(1, nullptr));
1840 EXPECT_FALSE(main_list.GetBoolean(2, nullptr));
1841 EXPECT_FALSE(main_list.GetBoolean(3, nullptr));
1842 EXPECT_FALSE(main_list.GetBoolean(4, nullptr));
1843 EXPECT_FALSE(main_list.GetBoolean(5, nullptr));
1844 EXPECT_FALSE(main_list.GetBoolean(6, nullptr));
1845 EXPECT_FALSE(main_list.GetBoolean(7, nullptr));
1846
1847 EXPECT_FALSE(main_list.GetInteger(0, nullptr));
1848 EXPECT_TRUE(main_list.GetInteger(1, nullptr));
1849 EXPECT_FALSE(main_list.GetInteger(2, nullptr));
1850 EXPECT_FALSE(main_list.GetInteger(3, nullptr));
1851 EXPECT_FALSE(main_list.GetInteger(4, nullptr));
1852 EXPECT_FALSE(main_list.GetInteger(5, nullptr));
1853 EXPECT_FALSE(main_list.GetInteger(6, nullptr));
1854 EXPECT_FALSE(main_list.GetInteger(7, nullptr));
1855
1856 EXPECT_FALSE(main_list.GetDouble(0, nullptr));
1857 EXPECT_TRUE(main_list.GetDouble(1, nullptr));
1858 EXPECT_TRUE(main_list.GetDouble(2, nullptr));
1859 EXPECT_FALSE(main_list.GetDouble(3, nullptr));
1860 EXPECT_FALSE(main_list.GetDouble(4, nullptr));
1861 EXPECT_FALSE(main_list.GetDouble(5, nullptr));
1862 EXPECT_FALSE(main_list.GetDouble(6, nullptr));
1863 EXPECT_FALSE(main_list.GetDouble(7, nullptr));
1864
1865 EXPECT_FALSE(main_list.GetString(0, static_cast<std::string*>(nullptr)));
1866 EXPECT_FALSE(main_list.GetString(1, static_cast<std::string*>(nullptr)));
1867 EXPECT_FALSE(main_list.GetString(2, static_cast<std::string*>(nullptr)));
1868 EXPECT_TRUE(main_list.GetString(3, static_cast<std::string*>(nullptr)));
1869 EXPECT_FALSE(main_list.GetString(4, static_cast<std::string*>(nullptr)));
1870 EXPECT_FALSE(main_list.GetString(5, static_cast<std::string*>(nullptr)));
1871 EXPECT_FALSE(main_list.GetString(6, static_cast<std::string*>(nullptr)));
1872 EXPECT_FALSE(main_list.GetString(7, static_cast<std::string*>(nullptr)));
1873
1874 EXPECT_FALSE(main_list.GetString(0, static_cast<string16*>(nullptr)));
1875 EXPECT_FALSE(main_list.GetString(1, static_cast<string16*>(nullptr)));
1876 EXPECT_FALSE(main_list.GetString(2, static_cast<string16*>(nullptr)));
1877 EXPECT_TRUE(main_list.GetString(3, static_cast<string16*>(nullptr)));
1878 EXPECT_FALSE(main_list.GetString(4, static_cast<string16*>(nullptr)));
1879 EXPECT_FALSE(main_list.GetString(5, static_cast<string16*>(nullptr)));
1880 EXPECT_FALSE(main_list.GetString(6, static_cast<string16*>(nullptr)));
1881 EXPECT_FALSE(main_list.GetString(7, static_cast<string16*>(nullptr)));
1882
1883 EXPECT_FALSE(main_list.GetDictionary(0, nullptr));
1884 EXPECT_FALSE(main_list.GetDictionary(1, nullptr));
1885 EXPECT_FALSE(main_list.GetDictionary(2, nullptr));
1886 EXPECT_FALSE(main_list.GetDictionary(3, nullptr));
1887 EXPECT_FALSE(main_list.GetDictionary(4, nullptr));
1888 EXPECT_TRUE(main_list.GetDictionary(5, nullptr));
1889 EXPECT_FALSE(main_list.GetDictionary(6, nullptr));
1890 EXPECT_FALSE(main_list.GetDictionary(7, nullptr));
1891
1892 EXPECT_FALSE(main_list.GetList(0, nullptr));
1893 EXPECT_FALSE(main_list.GetList(1, nullptr));
1894 EXPECT_FALSE(main_list.GetList(2, nullptr));
1895 EXPECT_FALSE(main_list.GetList(3, nullptr));
1896 EXPECT_FALSE(main_list.GetList(4, nullptr));
1897 EXPECT_FALSE(main_list.GetList(5, nullptr));
1898 EXPECT_TRUE(main_list.GetList(6, nullptr));
1899 EXPECT_FALSE(main_list.GetList(7, nullptr));
1900 }
1901
TEST(ValuesTest,SelfSwap)1902 TEST(ValuesTest, SelfSwap) {
1903 base::Value test(1);
1904 std::swap(test, test);
1905 EXPECT_EQ(1, test.GetInt());
1906 }
1907
TEST(ValuesTest,FromToUniquePtrValue)1908 TEST(ValuesTest, FromToUniquePtrValue) {
1909 std::unique_ptr<DictionaryValue> dict = std::make_unique<DictionaryValue>();
1910 dict->SetString("name", "Froogle");
1911 dict->SetString("url", "http://froogle.com");
1912 Value dict_copy = dict->Clone();
1913
1914 Value dict_converted = Value::FromUniquePtrValue(std::move(dict));
1915 EXPECT_EQ(dict_copy, dict_converted);
1916
1917 std::unique_ptr<Value> val =
1918 Value::ToUniquePtrValue(std::move(dict_converted));
1919 EXPECT_EQ(dict_copy, *val);
1920 }
1921
1922 } // namespace base
1923