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 <limits>
10 #include <memory>
11 #include <utility>
12
13 #include "base/memory/ptr_util.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19
TEST(ValuesTest,Basic)20 TEST(ValuesTest, Basic) {
21 // Test basic dictionary getting/setting
22 DictionaryValue settings;
23 std::string homepage = "http://google.com";
24 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
25 ASSERT_EQ(std::string("http://google.com"), homepage);
26
27 ASSERT_FALSE(settings.Get("global", NULL));
28 settings.SetBoolean("global", true);
29 ASSERT_TRUE(settings.Get("global", NULL));
30 settings.SetString("global.homepage", "http://scurvy.com");
31 ASSERT_TRUE(settings.Get("global", NULL));
32 homepage = "http://google.com";
33 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
34 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
35
36 // Test storing a dictionary in a list.
37 ListValue* toolbar_bookmarks;
38 ASSERT_FALSE(
39 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
40
41 std::unique_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
42 settings.Set("global.toolbar.bookmarks", std::move(new_toolbar_bookmarks));
43 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
44
45 std::unique_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
46 new_bookmark->SetString("name", "Froogle");
47 new_bookmark->SetString("url", "http://froogle.com");
48 toolbar_bookmarks->Append(std::move(new_bookmark));
49
50 ListValue* bookmark_list;
51 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
52 DictionaryValue* bookmark;
53 ASSERT_EQ(1U, bookmark_list->GetSize());
54 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
55 std::string bookmark_name = "Unnamed";
56 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
57 ASSERT_EQ(std::string("Froogle"), bookmark_name);
58 std::string bookmark_url;
59 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
60 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
61 }
62
TEST(ValuesTest,List)63 TEST(ValuesTest, List) {
64 std::unique_ptr<ListValue> mixed_list(new ListValue());
65 mixed_list->Set(0, WrapUnique(new FundamentalValue(true)));
66 mixed_list->Set(1, WrapUnique(new FundamentalValue(42)));
67 mixed_list->Set(2, WrapUnique(new FundamentalValue(88.8)));
68 mixed_list->Set(3, WrapUnique(new StringValue("foo")));
69 ASSERT_EQ(4u, mixed_list->GetSize());
70
71 Value *value = NULL;
72 bool bool_value = false;
73 int int_value = 0;
74 double double_value = 0.0;
75 std::string string_value;
76
77 ASSERT_FALSE(mixed_list->Get(4, &value));
78
79 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
80 ASSERT_EQ(0, int_value);
81 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
82 ASSERT_FALSE(bool_value);
83 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
84 ASSERT_EQ("", string_value);
85 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
86 ASSERT_EQ(0, int_value);
87 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
88 ASSERT_FALSE(bool_value);
89
90 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
91 ASSERT_TRUE(bool_value);
92 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
93 ASSERT_EQ(42, int_value);
94 // implicit conversion from Integer to Double should be possible.
95 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
96 ASSERT_EQ(42, double_value);
97 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
98 ASSERT_EQ(88.8, double_value);
99 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
100 ASSERT_EQ("foo", string_value);
101
102 // Try searching in the mixed list.
103 base::FundamentalValue sought_value(42);
104 base::FundamentalValue not_found_value(false);
105
106 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
107 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value));
108 ASSERT_EQ(42, int_value);
109 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
110 }
111
TEST(ValuesTest,BinaryValue)112 TEST(ValuesTest, BinaryValue) {
113 // Default constructor creates a BinaryValue with a null buffer and size 0.
114 std::unique_ptr<BinaryValue> binary(new BinaryValue());
115 ASSERT_TRUE(binary.get());
116 ASSERT_EQ(NULL, binary->GetBuffer());
117 ASSERT_EQ(0U, binary->GetSize());
118
119 // Test the common case of a non-empty buffer
120 std::unique_ptr<char[]> buffer(new char[15]);
121 char* original_buffer = buffer.get();
122 binary.reset(new BinaryValue(std::move(buffer), 15));
123 ASSERT_TRUE(binary.get());
124 ASSERT_TRUE(binary->GetBuffer());
125 ASSERT_EQ(original_buffer, binary->GetBuffer());
126 ASSERT_EQ(15U, binary->GetSize());
127
128 char stack_buffer[42];
129 memset(stack_buffer, '!', 42);
130 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42);
131 ASSERT_TRUE(binary.get());
132 ASSERT_TRUE(binary->GetBuffer());
133 ASSERT_NE(stack_buffer, binary->GetBuffer());
134 ASSERT_EQ(42U, binary->GetSize());
135 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
136
137 // Test overloaded GetAsBinary.
138 Value* narrow_value = binary.get();
139 const BinaryValue* narrow_binary = NULL;
140 ASSERT_TRUE(narrow_value->GetAsBinary(&narrow_binary));
141 EXPECT_EQ(binary.get(), narrow_binary);
142 }
143
TEST(ValuesTest,StringValue)144 TEST(ValuesTest, StringValue) {
145 // Test overloaded StringValue constructor.
146 std::unique_ptr<Value> narrow_value(new StringValue("narrow"));
147 ASSERT_TRUE(narrow_value.get());
148 ASSERT_TRUE(narrow_value->IsType(Value::TYPE_STRING));
149 std::unique_ptr<Value> utf16_value(new StringValue(ASCIIToUTF16("utf16")));
150 ASSERT_TRUE(utf16_value.get());
151 ASSERT_TRUE(utf16_value->IsType(Value::TYPE_STRING));
152
153 // Test overloaded GetAsString.
154 std::string narrow = "http://google.com";
155 string16 utf16 = ASCIIToUTF16("http://google.com");
156 const StringValue* string_value = NULL;
157 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
158 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
159 ASSERT_TRUE(narrow_value->GetAsString(&string_value));
160 ASSERT_EQ(std::string("narrow"), narrow);
161 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
162 ASSERT_EQ(string_value->GetString(), narrow);
163
164 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
165 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
166 ASSERT_TRUE(utf16_value->GetAsString(&string_value));
167 ASSERT_EQ(std::string("utf16"), narrow);
168 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
169 ASSERT_EQ(string_value->GetString(), narrow);
170
171 // Don't choke on NULL values.
172 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(NULL)));
173 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(NULL)));
174 ASSERT_TRUE(narrow_value->GetAsString(
175 static_cast<const StringValue**>(NULL)));
176 }
177
178 // This is a Value object that allows us to tell if it's been
179 // properly deleted by modifying the value of external flag on destruction.
180 class DeletionTestValue : public Value {
181 public:
DeletionTestValue(bool * deletion_flag)182 explicit DeletionTestValue(bool* deletion_flag) : Value(TYPE_NULL) {
183 Init(deletion_flag); // Separate function so that we can use ASSERT_*
184 }
185
Init(bool * deletion_flag)186 void Init(bool* deletion_flag) {
187 ASSERT_TRUE(deletion_flag);
188 deletion_flag_ = deletion_flag;
189 *deletion_flag_ = false;
190 }
191
~DeletionTestValue()192 ~DeletionTestValue() override { *deletion_flag_ = true; }
193
194 private:
195 bool* deletion_flag_;
196 };
197
TEST(ValuesTest,ListDeletion)198 TEST(ValuesTest, ListDeletion) {
199 bool deletion_flag = true;
200
201 {
202 ListValue list;
203 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag)));
204 EXPECT_FALSE(deletion_flag);
205 }
206 EXPECT_TRUE(deletion_flag);
207
208 {
209 ListValue list;
210 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag)));
211 EXPECT_FALSE(deletion_flag);
212 list.Clear();
213 EXPECT_TRUE(deletion_flag);
214 }
215
216 {
217 ListValue list;
218 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag)));
219 EXPECT_FALSE(deletion_flag);
220 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
221 EXPECT_TRUE(deletion_flag);
222 }
223 }
224
TEST(ValuesTest,ListRemoval)225 TEST(ValuesTest, ListRemoval) {
226 bool deletion_flag = true;
227 std::unique_ptr<Value> removed_item;
228
229 {
230 ListValue list;
231 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag)));
232 EXPECT_FALSE(deletion_flag);
233 EXPECT_EQ(1U, list.GetSize());
234 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
235 &removed_item));
236 EXPECT_FALSE(list.Remove(1, &removed_item));
237 EXPECT_TRUE(list.Remove(0, &removed_item));
238 ASSERT_TRUE(removed_item);
239 EXPECT_EQ(0U, list.GetSize());
240 }
241 EXPECT_FALSE(deletion_flag);
242 removed_item.reset();
243 EXPECT_TRUE(deletion_flag);
244
245 {
246 ListValue list;
247 list.Append(WrapUnique(new DeletionTestValue(&deletion_flag)));
248 EXPECT_FALSE(deletion_flag);
249 EXPECT_TRUE(list.Remove(0, NULL));
250 EXPECT_TRUE(deletion_flag);
251 EXPECT_EQ(0U, list.GetSize());
252 }
253
254 {
255 ListValue list;
256 std::unique_ptr<DeletionTestValue> value(
257 new DeletionTestValue(&deletion_flag));
258 DeletionTestValue* original_value = value.get();
259 list.Append(std::move(value));
260 EXPECT_FALSE(deletion_flag);
261 size_t index = 0;
262 list.Remove(*original_value, &index);
263 EXPECT_EQ(0U, index);
264 EXPECT_TRUE(deletion_flag);
265 EXPECT_EQ(0U, list.GetSize());
266 }
267 }
268
TEST(ValuesTest,DictionaryDeletion)269 TEST(ValuesTest, DictionaryDeletion) {
270 std::string key = "test";
271 bool deletion_flag = true;
272
273 {
274 DictionaryValue dict;
275 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag)));
276 EXPECT_FALSE(deletion_flag);
277 }
278 EXPECT_TRUE(deletion_flag);
279
280 {
281 DictionaryValue dict;
282 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag)));
283 EXPECT_FALSE(deletion_flag);
284 dict.Clear();
285 EXPECT_TRUE(deletion_flag);
286 }
287
288 {
289 DictionaryValue dict;
290 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag)));
291 EXPECT_FALSE(deletion_flag);
292 dict.Set(key, Value::CreateNullValue());
293 EXPECT_TRUE(deletion_flag);
294 }
295 }
296
TEST(ValuesTest,DictionaryRemoval)297 TEST(ValuesTest, DictionaryRemoval) {
298 std::string key = "test";
299 bool deletion_flag = true;
300 std::unique_ptr<Value> removed_item;
301
302 {
303 DictionaryValue dict;
304 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag)));
305 EXPECT_FALSE(deletion_flag);
306 EXPECT_TRUE(dict.HasKey(key));
307 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
308 EXPECT_TRUE(dict.Remove(key, &removed_item));
309 EXPECT_FALSE(dict.HasKey(key));
310 ASSERT_TRUE(removed_item);
311 }
312 EXPECT_FALSE(deletion_flag);
313 removed_item.reset();
314 EXPECT_TRUE(deletion_flag);
315
316 {
317 DictionaryValue dict;
318 dict.Set(key, WrapUnique(new DeletionTestValue(&deletion_flag)));
319 EXPECT_FALSE(deletion_flag);
320 EXPECT_TRUE(dict.HasKey(key));
321 EXPECT_TRUE(dict.Remove(key, NULL));
322 EXPECT_TRUE(deletion_flag);
323 EXPECT_FALSE(dict.HasKey(key));
324 }
325 }
326
TEST(ValuesTest,DictionaryWithoutPathExpansion)327 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
328 DictionaryValue dict;
329 dict.Set("this.is.expanded", Value::CreateNullValue());
330 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
331
332 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
333 EXPECT_TRUE(dict.HasKey("this"));
334 Value* value1;
335 EXPECT_TRUE(dict.Get("this", &value1));
336 DictionaryValue* value2;
337 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
338 EXPECT_EQ(value1, value2);
339 EXPECT_EQ(1U, value2->size());
340
341 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
342 Value* value3;
343 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
344 Value* value4;
345 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
346 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
347 }
348
349 // Tests the deprecated version of SetWithoutPathExpansion.
350 // TODO(estade): remove.
TEST(ValuesTest,DictionaryWithoutPathExpansionDeprecated)351 TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
352 DictionaryValue dict;
353 dict.Set("this.is.expanded", Value::CreateNullValue());
354 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
355
356 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
357 EXPECT_TRUE(dict.HasKey("this"));
358 Value* value1;
359 EXPECT_TRUE(dict.Get("this", &value1));
360 DictionaryValue* value2;
361 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
362 EXPECT_EQ(value1, value2);
363 EXPECT_EQ(1U, value2->size());
364
365 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
366 Value* value3;
367 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
368 Value* value4;
369 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
370 EXPECT_EQ(Value::TYPE_NULL, value4->GetType());
371 }
372
TEST(ValuesTest,DictionaryRemovePath)373 TEST(ValuesTest, DictionaryRemovePath) {
374 DictionaryValue dict;
375 dict.SetInteger("a.long.way.down", 1);
376 dict.SetBoolean("a.long.key.path", true);
377
378 std::unique_ptr<Value> removed_item;
379 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item));
380 ASSERT_TRUE(removed_item);
381 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_INTEGER));
382 EXPECT_FALSE(dict.HasKey("a.long.way.down"));
383 EXPECT_FALSE(dict.HasKey("a.long.way"));
384 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
385
386 removed_item.reset();
387 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item));
388 EXPECT_FALSE(removed_item);
389 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
390
391 removed_item.reset();
392 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
393 ASSERT_TRUE(removed_item);
394 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN));
395 EXPECT_TRUE(dict.empty());
396 }
397
TEST(ValuesTest,DeepCopy)398 TEST(ValuesTest, DeepCopy) {
399 DictionaryValue original_dict;
400 std::unique_ptr<Value> scoped_null = Value::CreateNullValue();
401 Value* original_null = scoped_null.get();
402 original_dict.Set("null", std::move(scoped_null));
403 std::unique_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
404 FundamentalValue* original_bool = scoped_bool.get();
405 original_dict.Set("bool", std::move(scoped_bool));
406 std::unique_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
407 FundamentalValue* original_int = scoped_int.get();
408 original_dict.Set("int", std::move(scoped_int));
409 std::unique_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
410 FundamentalValue* original_double = scoped_double.get();
411 original_dict.Set("double", std::move(scoped_double));
412 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
413 StringValue* original_string = scoped_string.get();
414 original_dict.Set("string", std::move(scoped_string));
415 std::unique_ptr<StringValue> scoped_string16(
416 new StringValue(ASCIIToUTF16("hello16")));
417 StringValue* original_string16 = scoped_string16.get();
418 original_dict.Set("string16", std::move(scoped_string16));
419
420 std::unique_ptr<char[]> original_buffer(new char[42]);
421 memset(original_buffer.get(), '!', 42);
422 std::unique_ptr<BinaryValue> scoped_binary(
423 new BinaryValue(std::move(original_buffer), 42));
424 BinaryValue* original_binary = scoped_binary.get();
425 original_dict.Set("binary", std::move(scoped_binary));
426
427 std::unique_ptr<ListValue> scoped_list(new ListValue());
428 Value* original_list = scoped_list.get();
429 std::unique_ptr<FundamentalValue> scoped_list_element_0(
430 new FundamentalValue(0));
431 Value* original_list_element_0 = scoped_list_element_0.get();
432 scoped_list->Append(std::move(scoped_list_element_0));
433 std::unique_ptr<FundamentalValue> scoped_list_element_1(
434 new FundamentalValue(1));
435 Value* original_list_element_1 = scoped_list_element_1.get();
436 scoped_list->Append(std::move(scoped_list_element_1));
437 original_dict.Set("list", std::move(scoped_list));
438
439 std::unique_ptr<DictionaryValue> scoped_nested_dictionary(
440 new DictionaryValue());
441 Value* original_nested_dictionary = scoped_nested_dictionary.get();
442 scoped_nested_dictionary->SetString("key", "value");
443 original_dict.Set("dictionary", std::move(scoped_nested_dictionary));
444
445 std::unique_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy();
446 ASSERT_TRUE(copy_dict.get());
447 ASSERT_NE(copy_dict.get(), &original_dict);
448
449 Value* copy_null = NULL;
450 ASSERT_TRUE(copy_dict->Get("null", ©_null));
451 ASSERT_TRUE(copy_null);
452 ASSERT_NE(copy_null, original_null);
453 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
454
455 Value* copy_bool = NULL;
456 ASSERT_TRUE(copy_dict->Get("bool", ©_bool));
457 ASSERT_TRUE(copy_bool);
458 ASSERT_NE(copy_bool, original_bool);
459 ASSERT_TRUE(copy_bool->IsType(Value::TYPE_BOOLEAN));
460 bool copy_bool_value = false;
461 ASSERT_TRUE(copy_bool->GetAsBoolean(©_bool_value));
462 ASSERT_TRUE(copy_bool_value);
463
464 Value* copy_int = NULL;
465 ASSERT_TRUE(copy_dict->Get("int", ©_int));
466 ASSERT_TRUE(copy_int);
467 ASSERT_NE(copy_int, original_int);
468 ASSERT_TRUE(copy_int->IsType(Value::TYPE_INTEGER));
469 int copy_int_value = 0;
470 ASSERT_TRUE(copy_int->GetAsInteger(©_int_value));
471 ASSERT_EQ(42, copy_int_value);
472
473 Value* copy_double = NULL;
474 ASSERT_TRUE(copy_dict->Get("double", ©_double));
475 ASSERT_TRUE(copy_double);
476 ASSERT_NE(copy_double, original_double);
477 ASSERT_TRUE(copy_double->IsType(Value::TYPE_DOUBLE));
478 double copy_double_value = 0;
479 ASSERT_TRUE(copy_double->GetAsDouble(©_double_value));
480 ASSERT_EQ(3.14, copy_double_value);
481
482 Value* copy_string = NULL;
483 ASSERT_TRUE(copy_dict->Get("string", ©_string));
484 ASSERT_TRUE(copy_string);
485 ASSERT_NE(copy_string, original_string);
486 ASSERT_TRUE(copy_string->IsType(Value::TYPE_STRING));
487 std::string copy_string_value;
488 string16 copy_string16_value;
489 ASSERT_TRUE(copy_string->GetAsString(©_string_value));
490 ASSERT_TRUE(copy_string->GetAsString(©_string16_value));
491 ASSERT_EQ(std::string("hello"), copy_string_value);
492 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
493
494 Value* copy_string16 = NULL;
495 ASSERT_TRUE(copy_dict->Get("string16", ©_string16));
496 ASSERT_TRUE(copy_string16);
497 ASSERT_NE(copy_string16, original_string16);
498 ASSERT_TRUE(copy_string16->IsType(Value::TYPE_STRING));
499 ASSERT_TRUE(copy_string16->GetAsString(©_string_value));
500 ASSERT_TRUE(copy_string16->GetAsString(©_string16_value));
501 ASSERT_EQ(std::string("hello16"), copy_string_value);
502 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
503
504 Value* copy_binary = NULL;
505 ASSERT_TRUE(copy_dict->Get("binary", ©_binary));
506 ASSERT_TRUE(copy_binary);
507 ASSERT_NE(copy_binary, original_binary);
508 ASSERT_TRUE(copy_binary->IsType(Value::TYPE_BINARY));
509 ASSERT_NE(original_binary->GetBuffer(),
510 static_cast<BinaryValue*>(copy_binary)->GetBuffer());
511 ASSERT_EQ(original_binary->GetSize(),
512 static_cast<BinaryValue*>(copy_binary)->GetSize());
513 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(),
514 static_cast<BinaryValue*>(copy_binary)->GetBuffer(),
515 original_binary->GetSize()));
516
517 Value* copy_value = NULL;
518 ASSERT_TRUE(copy_dict->Get("list", ©_value));
519 ASSERT_TRUE(copy_value);
520 ASSERT_NE(copy_value, original_list);
521 ASSERT_TRUE(copy_value->IsType(Value::TYPE_LIST));
522 ListValue* copy_list = NULL;
523 ASSERT_TRUE(copy_value->GetAsList(©_list));
524 ASSERT_TRUE(copy_list);
525 ASSERT_EQ(2U, copy_list->GetSize());
526
527 Value* copy_list_element_0;
528 ASSERT_TRUE(copy_list->Get(0, ©_list_element_0));
529 ASSERT_TRUE(copy_list_element_0);
530 ASSERT_NE(copy_list_element_0, original_list_element_0);
531 int copy_list_element_0_value;
532 ASSERT_TRUE(copy_list_element_0->GetAsInteger(©_list_element_0_value));
533 ASSERT_EQ(0, copy_list_element_0_value);
534
535 Value* copy_list_element_1;
536 ASSERT_TRUE(copy_list->Get(1, ©_list_element_1));
537 ASSERT_TRUE(copy_list_element_1);
538 ASSERT_NE(copy_list_element_1, original_list_element_1);
539 int copy_list_element_1_value;
540 ASSERT_TRUE(copy_list_element_1->GetAsInteger(©_list_element_1_value));
541 ASSERT_EQ(1, copy_list_element_1_value);
542
543 copy_value = NULL;
544 ASSERT_TRUE(copy_dict->Get("dictionary", ©_value));
545 ASSERT_TRUE(copy_value);
546 ASSERT_NE(copy_value, original_nested_dictionary);
547 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY));
548 DictionaryValue* copy_nested_dictionary = NULL;
549 ASSERT_TRUE(copy_value->GetAsDictionary(©_nested_dictionary));
550 ASSERT_TRUE(copy_nested_dictionary);
551 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
552 }
553
TEST(ValuesTest,Equals)554 TEST(ValuesTest, Equals) {
555 std::unique_ptr<Value> null1(Value::CreateNullValue());
556 std::unique_ptr<Value> null2(Value::CreateNullValue());
557 EXPECT_NE(null1.get(), null2.get());
558 EXPECT_TRUE(null1->Equals(null2.get()));
559
560 FundamentalValue boolean(false);
561 EXPECT_FALSE(null1->Equals(&boolean));
562
563 DictionaryValue dv;
564 dv.SetBoolean("a", false);
565 dv.SetInteger("b", 2);
566 dv.SetDouble("c", 2.5);
567 dv.SetString("d1", "string");
568 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
569 dv.Set("e", Value::CreateNullValue());
570
571 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
572 EXPECT_TRUE(dv.Equals(copy.get()));
573
574 std::unique_ptr<ListValue> list(new ListValue);
575 ListValue* original_list = list.get();
576 list->Append(Value::CreateNullValue());
577 list->Append(WrapUnique(new DictionaryValue));
578 std::unique_ptr<Value> list_copy(list->CreateDeepCopy());
579
580 dv.Set("f", std::move(list));
581 EXPECT_FALSE(dv.Equals(copy.get()));
582 copy->Set("f", std::move(list_copy));
583 EXPECT_TRUE(dv.Equals(copy.get()));
584
585 original_list->Append(WrapUnique(new FundamentalValue(true)));
586 EXPECT_FALSE(dv.Equals(copy.get()));
587
588 // Check if Equals detects differences in only the keys.
589 copy = dv.CreateDeepCopy();
590 EXPECT_TRUE(dv.Equals(copy.get()));
591 copy->Remove("a", NULL);
592 copy->SetBoolean("aa", false);
593 EXPECT_FALSE(dv.Equals(copy.get()));
594 }
595
TEST(ValuesTest,StaticEquals)596 TEST(ValuesTest, StaticEquals) {
597 std::unique_ptr<Value> null1(Value::CreateNullValue());
598 std::unique_ptr<Value> null2(Value::CreateNullValue());
599 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
600 EXPECT_TRUE(Value::Equals(NULL, NULL));
601
602 std::unique_ptr<Value> i42(new FundamentalValue(42));
603 std::unique_ptr<Value> j42(new FundamentalValue(42));
604 std::unique_ptr<Value> i17(new FundamentalValue(17));
605 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
606 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
607 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
608 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
609 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
610 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
611
612 // NULL and Value::CreateNullValue() are intentionally different: We need
613 // support for NULL as a return value for "undefined" without caring for
614 // ownership of the pointer.
615 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
616 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
617 }
618
TEST(ValuesTest,DeepCopyCovariantReturnTypes)619 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
620 DictionaryValue original_dict;
621 std::unique_ptr<Value> scoped_null(Value::CreateNullValue());
622 Value* original_null = scoped_null.get();
623 original_dict.Set("null", std::move(scoped_null));
624 std::unique_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
625 Value* original_bool = scoped_bool.get();
626 original_dict.Set("bool", std::move(scoped_bool));
627 std::unique_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
628 Value* original_int = scoped_int.get();
629 original_dict.Set("int", std::move(scoped_int));
630 std::unique_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
631 Value* original_double = scoped_double.get();
632 original_dict.Set("double", std::move(scoped_double));
633 std::unique_ptr<StringValue> scoped_string(new StringValue("hello"));
634 Value* original_string = scoped_string.get();
635 original_dict.Set("string", std::move(scoped_string));
636 std::unique_ptr<StringValue> scoped_string16(
637 new StringValue(ASCIIToUTF16("hello16")));
638 Value* original_string16 = scoped_string16.get();
639 original_dict.Set("string16", std::move(scoped_string16));
640
641 std::unique_ptr<char[]> original_buffer(new char[42]);
642 memset(original_buffer.get(), '!', 42);
643 std::unique_ptr<BinaryValue> scoped_binary(
644 new BinaryValue(std::move(original_buffer), 42));
645 Value* original_binary = scoped_binary.get();
646 original_dict.Set("binary", std::move(scoped_binary));
647
648 std::unique_ptr<ListValue> scoped_list(new ListValue());
649 Value* original_list = scoped_list.get();
650 std::unique_ptr<FundamentalValue> scoped_list_element_0(
651 new FundamentalValue(0));
652 scoped_list->Append(std::move(scoped_list_element_0));
653 std::unique_ptr<FundamentalValue> scoped_list_element_1(
654 new FundamentalValue(1));
655 scoped_list->Append(std::move(scoped_list_element_1));
656 original_dict.Set("list", std::move(scoped_list));
657
658 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
659 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy();
660 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
661 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy();
662 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy();
663 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy();
664 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
665 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy();
666 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy();
667
668 EXPECT_TRUE(original_dict.Equals(copy_dict.get()));
669 EXPECT_TRUE(original_null->Equals(copy_null.get()));
670 EXPECT_TRUE(original_bool->Equals(copy_bool.get()));
671 EXPECT_TRUE(original_int->Equals(copy_int.get()));
672 EXPECT_TRUE(original_double->Equals(copy_double.get()));
673 EXPECT_TRUE(original_string->Equals(copy_string.get()));
674 EXPECT_TRUE(original_string16->Equals(copy_string16.get()));
675 EXPECT_TRUE(original_binary->Equals(copy_binary.get()));
676 EXPECT_TRUE(original_list->Equals(copy_list.get()));
677 }
678
TEST(ValuesTest,RemoveEmptyChildren)679 TEST(ValuesTest, RemoveEmptyChildren) {
680 std::unique_ptr<DictionaryValue> root(new DictionaryValue);
681 // Remove empty lists and dictionaries.
682 root->Set("empty_dict", WrapUnique(new DictionaryValue));
683 root->Set("empty_list", WrapUnique(new ListValue));
684 root->SetWithoutPathExpansion("a.b.c.d.e",
685 WrapUnique(new DictionaryValue));
686 root = root->DeepCopyWithoutEmptyChildren();
687 EXPECT_TRUE(root->empty());
688
689 // Make sure we don't prune too much.
690 root->SetBoolean("bool", true);
691 root->Set("empty_dict", WrapUnique(new DictionaryValue));
692 root->SetString("empty_string", std::string());
693 root = root->DeepCopyWithoutEmptyChildren();
694 EXPECT_EQ(2U, root->size());
695
696 // Should do nothing.
697 root = root->DeepCopyWithoutEmptyChildren();
698 EXPECT_EQ(2U, root->size());
699
700 // Nested test cases. These should all reduce back to the bool and string
701 // set above.
702 {
703 root->Set("a.b.c.d.e", WrapUnique(new DictionaryValue));
704 root = root->DeepCopyWithoutEmptyChildren();
705 EXPECT_EQ(2U, root->size());
706 }
707 {
708 std::unique_ptr<DictionaryValue> inner(new DictionaryValue);
709 inner->Set("empty_dict", WrapUnique(new DictionaryValue));
710 inner->Set("empty_list", WrapUnique(new ListValue));
711 root->Set("dict_with_empty_children", std::move(inner));
712 root = root->DeepCopyWithoutEmptyChildren();
713 EXPECT_EQ(2U, root->size());
714 }
715 {
716 std::unique_ptr<ListValue> inner(new ListValue);
717 inner->Append(WrapUnique(new DictionaryValue));
718 inner->Append(WrapUnique(new ListValue));
719 root->Set("list_with_empty_children", std::move(inner));
720 root = root->DeepCopyWithoutEmptyChildren();
721 EXPECT_EQ(2U, root->size());
722 }
723
724 // Nested with siblings.
725 {
726 std::unique_ptr<ListValue> inner(new ListValue());
727 inner->Append(WrapUnique(new DictionaryValue));
728 inner->Append(WrapUnique(new ListValue));
729 root->Set("list_with_empty_children", std::move(inner));
730 std::unique_ptr<DictionaryValue> inner2(new DictionaryValue);
731 inner2->Set("empty_dict", WrapUnique(new DictionaryValue));
732 inner2->Set("empty_list", WrapUnique(new ListValue));
733 root->Set("dict_with_empty_children", std::move(inner2));
734 root = root->DeepCopyWithoutEmptyChildren();
735 EXPECT_EQ(2U, root->size());
736 }
737
738 // Make sure nested values don't get pruned.
739 {
740 std::unique_ptr<ListValue> inner(new ListValue);
741 std::unique_ptr<ListValue> inner2(new ListValue);
742 inner2->Append(WrapUnique(new StringValue("hello")));
743 inner->Append(WrapUnique(new DictionaryValue));
744 inner->Append(std::move(inner2));
745 root->Set("list_with_empty_children", std::move(inner));
746 root = root->DeepCopyWithoutEmptyChildren();
747 EXPECT_EQ(3U, root->size());
748
749 ListValue* inner_value, *inner_value2;
750 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
751 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
752 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
753 EXPECT_EQ(1U, inner_value2->GetSize());
754 }
755 }
756
TEST(ValuesTest,MergeDictionary)757 TEST(ValuesTest, MergeDictionary) {
758 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
759 base->SetString("base_key", "base_key_value_base");
760 base->SetString("collide_key", "collide_key_value_base");
761 std::unique_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
762 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
763 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
764 base->Set("sub_dict_key", std::move(base_sub_dict));
765
766 std::unique_ptr<DictionaryValue> merge(new DictionaryValue);
767 merge->SetString("merge_key", "merge_key_value_merge");
768 merge->SetString("collide_key", "collide_key_value_merge");
769 std::unique_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
770 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
771 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
772 merge->Set("sub_dict_key", std::move(merge_sub_dict));
773
774 base->MergeDictionary(merge.get());
775
776 EXPECT_EQ(4U, base->size());
777 std::string base_key_value;
778 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
779 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
780 std::string collide_key_value;
781 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
782 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
783 std::string merge_key_value;
784 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
785 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
786
787 DictionaryValue* res_sub_dict;
788 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
789 EXPECT_EQ(3U, res_sub_dict->size());
790 std::string sub_base_key_value;
791 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
792 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
793 std::string sub_collide_key_value;
794 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
795 &sub_collide_key_value));
796 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
797 std::string sub_merge_key_value;
798 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
799 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
800 }
801
TEST(ValuesTest,MergeDictionaryDeepCopy)802 TEST(ValuesTest, MergeDictionaryDeepCopy) {
803 std::unique_ptr<DictionaryValue> child(new DictionaryValue);
804 DictionaryValue* original_child = child.get();
805 child->SetString("test", "value");
806 EXPECT_EQ(1U, child->size());
807
808 std::string value;
809 EXPECT_TRUE(child->GetString("test", &value));
810 EXPECT_EQ("value", value);
811
812 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
813 base->Set("dict", std::move(child));
814 EXPECT_EQ(1U, base->size());
815
816 DictionaryValue* ptr;
817 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
818 EXPECT_EQ(original_child, ptr);
819
820 std::unique_ptr<DictionaryValue> merged(new DictionaryValue);
821 merged->MergeDictionary(base.get());
822 EXPECT_EQ(1U, merged->size());
823 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
824 EXPECT_NE(original_child, ptr);
825 EXPECT_TRUE(ptr->GetString("test", &value));
826 EXPECT_EQ("value", value);
827
828 original_child->SetString("test", "overwrite");
829 base.reset();
830 EXPECT_TRUE(ptr->GetString("test", &value));
831 EXPECT_EQ("value", value);
832 }
833
TEST(ValuesTest,DictionaryIterator)834 TEST(ValuesTest, DictionaryIterator) {
835 DictionaryValue dict;
836 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
837 ADD_FAILURE();
838 }
839
840 StringValue value1("value1");
841 dict.Set("key1", value1.CreateDeepCopy());
842 bool seen1 = false;
843 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
844 EXPECT_FALSE(seen1);
845 EXPECT_EQ("key1", it.key());
846 EXPECT_TRUE(value1.Equals(&it.value()));
847 seen1 = true;
848 }
849 EXPECT_TRUE(seen1);
850
851 StringValue value2("value2");
852 dict.Set("key2", value2.CreateDeepCopy());
853 bool seen2 = seen1 = false;
854 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
855 if (it.key() == "key1") {
856 EXPECT_FALSE(seen1);
857 EXPECT_TRUE(value1.Equals(&it.value()));
858 seen1 = true;
859 } else if (it.key() == "key2") {
860 EXPECT_FALSE(seen2);
861 EXPECT_TRUE(value2.Equals(&it.value()));
862 seen2 = true;
863 } else {
864 ADD_FAILURE();
865 }
866 }
867 EXPECT_TRUE(seen1);
868 EXPECT_TRUE(seen2);
869 }
870
871 // DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
872 // and still return true/false based on success.
TEST(ValuesTest,GetWithNullOutValue)873 TEST(ValuesTest, GetWithNullOutValue) {
874 DictionaryValue main_dict;
875 ListValue main_list;
876
877 FundamentalValue bool_value(false);
878 FundamentalValue int_value(1234);
879 FundamentalValue double_value(12.34567);
880 StringValue string_value("foo");
881 BinaryValue binary_value;
882 DictionaryValue dict_value;
883 ListValue list_value;
884
885 main_dict.Set("bool", bool_value.CreateDeepCopy());
886 main_dict.Set("int", int_value.CreateDeepCopy());
887 main_dict.Set("double", double_value.CreateDeepCopy());
888 main_dict.Set("string", string_value.CreateDeepCopy());
889 main_dict.Set("binary", binary_value.CreateDeepCopy());
890 main_dict.Set("dict", dict_value.CreateDeepCopy());
891 main_dict.Set("list", list_value.CreateDeepCopy());
892
893 main_list.Append(bool_value.CreateDeepCopy());
894 main_list.Append(int_value.CreateDeepCopy());
895 main_list.Append(double_value.CreateDeepCopy());
896 main_list.Append(string_value.CreateDeepCopy());
897 main_list.Append(binary_value.CreateDeepCopy());
898 main_list.Append(dict_value.CreateDeepCopy());
899 main_list.Append(list_value.CreateDeepCopy());
900
901 EXPECT_TRUE(main_dict.Get("bool", NULL));
902 EXPECT_TRUE(main_dict.Get("int", NULL));
903 EXPECT_TRUE(main_dict.Get("double", NULL));
904 EXPECT_TRUE(main_dict.Get("string", NULL));
905 EXPECT_TRUE(main_dict.Get("binary", NULL));
906 EXPECT_TRUE(main_dict.Get("dict", NULL));
907 EXPECT_TRUE(main_dict.Get("list", NULL));
908 EXPECT_FALSE(main_dict.Get("DNE", NULL));
909
910 EXPECT_TRUE(main_dict.GetBoolean("bool", NULL));
911 EXPECT_FALSE(main_dict.GetBoolean("int", NULL));
912 EXPECT_FALSE(main_dict.GetBoolean("double", NULL));
913 EXPECT_FALSE(main_dict.GetBoolean("string", NULL));
914 EXPECT_FALSE(main_dict.GetBoolean("binary", NULL));
915 EXPECT_FALSE(main_dict.GetBoolean("dict", NULL));
916 EXPECT_FALSE(main_dict.GetBoolean("list", NULL));
917 EXPECT_FALSE(main_dict.GetBoolean("DNE", NULL));
918
919 EXPECT_FALSE(main_dict.GetInteger("bool", NULL));
920 EXPECT_TRUE(main_dict.GetInteger("int", NULL));
921 EXPECT_FALSE(main_dict.GetInteger("double", NULL));
922 EXPECT_FALSE(main_dict.GetInteger("string", NULL));
923 EXPECT_FALSE(main_dict.GetInteger("binary", NULL));
924 EXPECT_FALSE(main_dict.GetInteger("dict", NULL));
925 EXPECT_FALSE(main_dict.GetInteger("list", NULL));
926 EXPECT_FALSE(main_dict.GetInteger("DNE", NULL));
927
928 // Both int and double values can be obtained from GetDouble.
929 EXPECT_FALSE(main_dict.GetDouble("bool", NULL));
930 EXPECT_TRUE(main_dict.GetDouble("int", NULL));
931 EXPECT_TRUE(main_dict.GetDouble("double", NULL));
932 EXPECT_FALSE(main_dict.GetDouble("string", NULL));
933 EXPECT_FALSE(main_dict.GetDouble("binary", NULL));
934 EXPECT_FALSE(main_dict.GetDouble("dict", NULL));
935 EXPECT_FALSE(main_dict.GetDouble("list", NULL));
936 EXPECT_FALSE(main_dict.GetDouble("DNE", NULL));
937
938 EXPECT_FALSE(main_dict.GetString("bool", static_cast<std::string*>(NULL)));
939 EXPECT_FALSE(main_dict.GetString("int", static_cast<std::string*>(NULL)));
940 EXPECT_FALSE(main_dict.GetString("double", static_cast<std::string*>(NULL)));
941 EXPECT_TRUE(main_dict.GetString("string", static_cast<std::string*>(NULL)));
942 EXPECT_FALSE(main_dict.GetString("binary", static_cast<std::string*>(NULL)));
943 EXPECT_FALSE(main_dict.GetString("dict", static_cast<std::string*>(NULL)));
944 EXPECT_FALSE(main_dict.GetString("list", static_cast<std::string*>(NULL)));
945 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<std::string*>(NULL)));
946
947 EXPECT_FALSE(main_dict.GetString("bool", static_cast<string16*>(NULL)));
948 EXPECT_FALSE(main_dict.GetString("int", static_cast<string16*>(NULL)));
949 EXPECT_FALSE(main_dict.GetString("double", static_cast<string16*>(NULL)));
950 EXPECT_TRUE(main_dict.GetString("string", static_cast<string16*>(NULL)));
951 EXPECT_FALSE(main_dict.GetString("binary", static_cast<string16*>(NULL)));
952 EXPECT_FALSE(main_dict.GetString("dict", static_cast<string16*>(NULL)));
953 EXPECT_FALSE(main_dict.GetString("list", static_cast<string16*>(NULL)));
954 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<string16*>(NULL)));
955
956 EXPECT_FALSE(main_dict.GetBinary("bool", NULL));
957 EXPECT_FALSE(main_dict.GetBinary("int", NULL));
958 EXPECT_FALSE(main_dict.GetBinary("double", NULL));
959 EXPECT_FALSE(main_dict.GetBinary("string", NULL));
960 EXPECT_TRUE(main_dict.GetBinary("binary", NULL));
961 EXPECT_FALSE(main_dict.GetBinary("dict", NULL));
962 EXPECT_FALSE(main_dict.GetBinary("list", NULL));
963 EXPECT_FALSE(main_dict.GetBinary("DNE", NULL));
964
965 EXPECT_FALSE(main_dict.GetDictionary("bool", NULL));
966 EXPECT_FALSE(main_dict.GetDictionary("int", NULL));
967 EXPECT_FALSE(main_dict.GetDictionary("double", NULL));
968 EXPECT_FALSE(main_dict.GetDictionary("string", NULL));
969 EXPECT_FALSE(main_dict.GetDictionary("binary", NULL));
970 EXPECT_TRUE(main_dict.GetDictionary("dict", NULL));
971 EXPECT_FALSE(main_dict.GetDictionary("list", NULL));
972 EXPECT_FALSE(main_dict.GetDictionary("DNE", NULL));
973
974 EXPECT_FALSE(main_dict.GetList("bool", NULL));
975 EXPECT_FALSE(main_dict.GetList("int", NULL));
976 EXPECT_FALSE(main_dict.GetList("double", NULL));
977 EXPECT_FALSE(main_dict.GetList("string", NULL));
978 EXPECT_FALSE(main_dict.GetList("binary", NULL));
979 EXPECT_FALSE(main_dict.GetList("dict", NULL));
980 EXPECT_TRUE(main_dict.GetList("list", NULL));
981 EXPECT_FALSE(main_dict.GetList("DNE", NULL));
982
983 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("bool", NULL));
984 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("int", NULL));
985 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("double", NULL));
986 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("string", NULL));
987 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("binary", NULL));
988 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("dict", NULL));
989 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("list", NULL));
990 EXPECT_FALSE(main_dict.GetWithoutPathExpansion("DNE", NULL));
991
992 EXPECT_TRUE(main_dict.GetBooleanWithoutPathExpansion("bool", NULL));
993 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("int", NULL));
994 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("double", NULL));
995 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("string", NULL));
996 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("binary", NULL));
997 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("dict", NULL));
998 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("list", NULL));
999 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("DNE", NULL));
1000
1001 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("bool", NULL));
1002 EXPECT_TRUE(main_dict.GetIntegerWithoutPathExpansion("int", NULL));
1003 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("double", NULL));
1004 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("string", NULL));
1005 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("binary", NULL));
1006 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("dict", NULL));
1007 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("list", NULL));
1008 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("DNE", NULL));
1009
1010 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("bool", NULL));
1011 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("int", NULL));
1012 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("double", NULL));
1013 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("string", NULL));
1014 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("binary", NULL));
1015 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("dict", NULL));
1016 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("list", NULL));
1017 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("DNE", NULL));
1018
1019 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1020 "bool", static_cast<std::string*>(NULL)));
1021 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1022 "int", static_cast<std::string*>(NULL)));
1023 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1024 "double", static_cast<std::string*>(NULL)));
1025 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1026 "string", static_cast<std::string*>(NULL)));
1027 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1028 "binary", static_cast<std::string*>(NULL)));
1029 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1030 "dict", static_cast<std::string*>(NULL)));
1031 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1032 "list", static_cast<std::string*>(NULL)));
1033 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1034 "DNE", static_cast<std::string*>(NULL)));
1035
1036 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1037 "bool", static_cast<string16*>(NULL)));
1038 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1039 "int", static_cast<string16*>(NULL)));
1040 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1041 "double", static_cast<string16*>(NULL)));
1042 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1043 "string", static_cast<string16*>(NULL)));
1044 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1045 "binary", static_cast<string16*>(NULL)));
1046 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1047 "dict", static_cast<string16*>(NULL)));
1048 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1049 "list", static_cast<string16*>(NULL)));
1050 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1051 "DNE", static_cast<string16*>(NULL)));
1052
1053 // There is no GetBinaryWithoutPathExpansion for some reason, but if there
1054 // were it should be tested here...
1055
1056 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("bool", NULL));
1057 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("int", NULL));
1058 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("double", NULL));
1059 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("string", NULL));
1060 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("binary", NULL));
1061 EXPECT_TRUE(main_dict.GetDictionaryWithoutPathExpansion("dict", NULL));
1062 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("list", NULL));
1063 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("DNE", NULL));
1064
1065 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("bool", NULL));
1066 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("int", NULL));
1067 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("double", NULL));
1068 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("string", NULL));
1069 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("binary", NULL));
1070 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("dict", NULL));
1071 EXPECT_TRUE(main_dict.GetListWithoutPathExpansion("list", NULL));
1072 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("DNE", NULL));
1073
1074 EXPECT_TRUE(main_list.Get(0, NULL));
1075 EXPECT_TRUE(main_list.Get(1, NULL));
1076 EXPECT_TRUE(main_list.Get(2, NULL));
1077 EXPECT_TRUE(main_list.Get(3, NULL));
1078 EXPECT_TRUE(main_list.Get(4, NULL));
1079 EXPECT_TRUE(main_list.Get(5, NULL));
1080 EXPECT_TRUE(main_list.Get(6, NULL));
1081 EXPECT_FALSE(main_list.Get(7, NULL));
1082
1083 EXPECT_TRUE(main_list.GetBoolean(0, NULL));
1084 EXPECT_FALSE(main_list.GetBoolean(1, NULL));
1085 EXPECT_FALSE(main_list.GetBoolean(2, NULL));
1086 EXPECT_FALSE(main_list.GetBoolean(3, NULL));
1087 EXPECT_FALSE(main_list.GetBoolean(4, NULL));
1088 EXPECT_FALSE(main_list.GetBoolean(5, NULL));
1089 EXPECT_FALSE(main_list.GetBoolean(6, NULL));
1090 EXPECT_FALSE(main_list.GetBoolean(7, NULL));
1091
1092 EXPECT_FALSE(main_list.GetInteger(0, NULL));
1093 EXPECT_TRUE(main_list.GetInteger(1, NULL));
1094 EXPECT_FALSE(main_list.GetInteger(2, NULL));
1095 EXPECT_FALSE(main_list.GetInteger(3, NULL));
1096 EXPECT_FALSE(main_list.GetInteger(4, NULL));
1097 EXPECT_FALSE(main_list.GetInteger(5, NULL));
1098 EXPECT_FALSE(main_list.GetInteger(6, NULL));
1099 EXPECT_FALSE(main_list.GetInteger(7, NULL));
1100
1101 EXPECT_FALSE(main_list.GetDouble(0, NULL));
1102 EXPECT_TRUE(main_list.GetDouble(1, NULL));
1103 EXPECT_TRUE(main_list.GetDouble(2, NULL));
1104 EXPECT_FALSE(main_list.GetDouble(3, NULL));
1105 EXPECT_FALSE(main_list.GetDouble(4, NULL));
1106 EXPECT_FALSE(main_list.GetDouble(5, NULL));
1107 EXPECT_FALSE(main_list.GetDouble(6, NULL));
1108 EXPECT_FALSE(main_list.GetDouble(7, NULL));
1109
1110 EXPECT_FALSE(main_list.GetString(0, static_cast<std::string*>(NULL)));
1111 EXPECT_FALSE(main_list.GetString(1, static_cast<std::string*>(NULL)));
1112 EXPECT_FALSE(main_list.GetString(2, static_cast<std::string*>(NULL)));
1113 EXPECT_TRUE(main_list.GetString(3, static_cast<std::string*>(NULL)));
1114 EXPECT_FALSE(main_list.GetString(4, static_cast<std::string*>(NULL)));
1115 EXPECT_FALSE(main_list.GetString(5, static_cast<std::string*>(NULL)));
1116 EXPECT_FALSE(main_list.GetString(6, static_cast<std::string*>(NULL)));
1117 EXPECT_FALSE(main_list.GetString(7, static_cast<std::string*>(NULL)));
1118
1119 EXPECT_FALSE(main_list.GetString(0, static_cast<string16*>(NULL)));
1120 EXPECT_FALSE(main_list.GetString(1, static_cast<string16*>(NULL)));
1121 EXPECT_FALSE(main_list.GetString(2, static_cast<string16*>(NULL)));
1122 EXPECT_TRUE(main_list.GetString(3, static_cast<string16*>(NULL)));
1123 EXPECT_FALSE(main_list.GetString(4, static_cast<string16*>(NULL)));
1124 EXPECT_FALSE(main_list.GetString(5, static_cast<string16*>(NULL)));
1125 EXPECT_FALSE(main_list.GetString(6, static_cast<string16*>(NULL)));
1126 EXPECT_FALSE(main_list.GetString(7, static_cast<string16*>(NULL)));
1127
1128 EXPECT_FALSE(main_list.GetBinary(0, NULL));
1129 EXPECT_FALSE(main_list.GetBinary(1, NULL));
1130 EXPECT_FALSE(main_list.GetBinary(2, NULL));
1131 EXPECT_FALSE(main_list.GetBinary(3, NULL));
1132 EXPECT_TRUE(main_list.GetBinary(4, NULL));
1133 EXPECT_FALSE(main_list.GetBinary(5, NULL));
1134 EXPECT_FALSE(main_list.GetBinary(6, NULL));
1135 EXPECT_FALSE(main_list.GetBinary(7, NULL));
1136
1137 EXPECT_FALSE(main_list.GetDictionary(0, NULL));
1138 EXPECT_FALSE(main_list.GetDictionary(1, NULL));
1139 EXPECT_FALSE(main_list.GetDictionary(2, NULL));
1140 EXPECT_FALSE(main_list.GetDictionary(3, NULL));
1141 EXPECT_FALSE(main_list.GetDictionary(4, NULL));
1142 EXPECT_TRUE(main_list.GetDictionary(5, NULL));
1143 EXPECT_FALSE(main_list.GetDictionary(6, NULL));
1144 EXPECT_FALSE(main_list.GetDictionary(7, NULL));
1145
1146 EXPECT_FALSE(main_list.GetList(0, NULL));
1147 EXPECT_FALSE(main_list.GetList(1, NULL));
1148 EXPECT_FALSE(main_list.GetList(2, NULL));
1149 EXPECT_FALSE(main_list.GetList(3, NULL));
1150 EXPECT_FALSE(main_list.GetList(4, NULL));
1151 EXPECT_FALSE(main_list.GetList(5, NULL));
1152 EXPECT_TRUE(main_list.GetList(6, NULL));
1153 EXPECT_FALSE(main_list.GetList(7, NULL));
1154 }
1155
1156 } // namespace base
1157