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