blob: 3bcdc16e37e61919d59ea81a8088572f24e94c43 [file] [log] [blame]
[email protected]44106182012-04-06 03:53:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
dcheng093de9b2016-04-04 21:25:515#include "base/values.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
[email protected]836061b2008-08-13 14:57:519#include <limits>
dcheng093de9b2016-04-04 21:25:5110#include <memory>
danakj0c8d4aa2015-11-25 05:29:5811#include <utility>
jdoerriee03e80f2017-02-15 08:42:1412#include <vector>
[email protected]836061b2008-08-13 14:57:5113
dcheng093de9b2016-04-04 21:25:5114#include "base/memory/ptr_util.h"
[email protected]c851cfd2013-06-10 20:11:1415#include "base/strings/string16.h"
[email protected]a4ea1f12013-06-07 18:37:0716#include "base/strings/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:3817#include "testing/gtest/include/gtest/gtest.h"
18
[email protected]58b916e2011-06-28 22:56:3319namespace base {
initial.commitd7cae122008-07-26 21:49:3820
jdoerrie05eb3162017-02-01 10:36:5621// Group of tests for the value constructors.
22TEST(ValuesTest, ConstructBool) {
jdoerrie239723572017-03-02 12:09:1923 Value true_value(true);
jdoerrie05eb3162017-02-01 10:36:5624 EXPECT_EQ(Value::Type::BOOLEAN, true_value.type());
25 EXPECT_TRUE(true_value.GetBool());
26
jdoerrie239723572017-03-02 12:09:1927 Value false_value(false);
jdoerrie05eb3162017-02-01 10:36:5628 EXPECT_EQ(Value::Type::BOOLEAN, false_value.type());
29 EXPECT_FALSE(false_value.GetBool());
30}
31
32TEST(ValuesTest, ConstructInt) {
jdoerrie239723572017-03-02 12:09:1933 Value value(-37);
jdoerrie05eb3162017-02-01 10:36:5634 EXPECT_EQ(Value::Type::INTEGER, value.type());
35 EXPECT_EQ(-37, value.GetInt());
36}
37
38TEST(ValuesTest, ConstructDouble) {
jdoerrie239723572017-03-02 12:09:1939 Value value(-4.655);
jdoerrie05eb3162017-02-01 10:36:5640 EXPECT_EQ(Value::Type::DOUBLE, value.type());
41 EXPECT_EQ(-4.655, value.GetDouble());
42}
43
jdoerrief38f37b2017-02-01 14:38:3244TEST(ValuesTest, ConstructStringFromConstCharPtr) {
45 const char* str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0446 Value value(str);
jdoerrief38f37b2017-02-01 14:38:3247 EXPECT_EQ(Value::Type::STRING, value.type());
48 EXPECT_EQ("foobar", value.GetString());
49}
50
51TEST(ValuesTest, ConstructStringFromStdStringConstRef) {
52 std::string str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0453 Value value(str);
jdoerrief38f37b2017-02-01 14:38:3254 EXPECT_EQ(Value::Type::STRING, value.type());
55 EXPECT_EQ("foobar", value.GetString());
56}
57
58TEST(ValuesTest, ConstructStringFromStdStringRefRef) {
59 std::string str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0460 Value value(std::move(str));
jdoerrief38f37b2017-02-01 14:38:3261 EXPECT_EQ(Value::Type::STRING, value.type());
62 EXPECT_EQ("foobar", value.GetString());
63}
64
65TEST(ValuesTest, ConstructStringFromConstChar16Ptr) {
66 string16 str = ASCIIToUTF16("foobar");
jdoerrie122c4da2017-03-06 11:12:0467 Value value(str.c_str());
jdoerrief38f37b2017-02-01 14:38:3268 EXPECT_EQ(Value::Type::STRING, value.type());
69 EXPECT_EQ("foobar", value.GetString());
70}
71
72TEST(ValuesTest, ConstructStringFromString16) {
73 string16 str = ASCIIToUTF16("foobar");
jdoerrie122c4da2017-03-06 11:12:0474 Value value(str);
jdoerrief38f37b2017-02-01 14:38:3275 EXPECT_EQ(Value::Type::STRING, value.type());
76 EXPECT_EQ("foobar", value.GetString());
77}
78
79TEST(ValuesTest, ConstructStringFromStringPiece) {
80 StringPiece str = "foobar";
jdoerrie122c4da2017-03-06 11:12:0481 Value value(str);
jdoerrief38f37b2017-02-01 14:38:3282 EXPECT_EQ(Value::Type::STRING, value.type());
83 EXPECT_EQ("foobar", value.GetString());
84}
85
jdoerriee03e80f2017-02-15 08:42:1486TEST(ValuesTest, ConstructBinary) {
87 BinaryValue value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
88 EXPECT_EQ(Value::Type::BINARY, value.type());
89 EXPECT_EQ(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}), value.GetBlob());
90}
91
jdoerrie8e945542017-02-17 13:54:4992TEST(ValuesTest, ConstructDict) {
93 DictionaryValue value;
94 EXPECT_EQ(Value::Type::DICTIONARY, value.type());
95}
96
97TEST(ValuesTest, ConstructList) {
98 ListValue value;
99 EXPECT_EQ(Value::Type::LIST, value.type());
100}
101
jdoerrie05eb3162017-02-01 10:36:56102// Group of tests for the copy constructors and copy-assigmnent. For equality
103// checks comparisons of the interesting fields are done instead of relying on
104// Equals being correct.
105TEST(ValuesTest, CopyBool) {
jdoerrie239723572017-03-02 12:09:19106 Value true_value(true);
107 Value copied_true_value(true_value);
jdoerrie05eb3162017-02-01 10:36:56108 EXPECT_EQ(true_value.type(), copied_true_value.type());
109 EXPECT_EQ(true_value.GetBool(), copied_true_value.GetBool());
110
jdoerrie239723572017-03-02 12:09:19111 Value false_value(false);
112 Value copied_false_value(false_value);
jdoerrie05eb3162017-02-01 10:36:56113 EXPECT_EQ(false_value.type(), copied_false_value.type());
114 EXPECT_EQ(false_value.GetBool(), copied_false_value.GetBool());
115
116 Value blank;
117
118 blank = true_value;
119 EXPECT_EQ(true_value.type(), blank.type());
120 EXPECT_EQ(true_value.GetBool(), blank.GetBool());
121
122 blank = false_value;
123 EXPECT_EQ(false_value.type(), blank.type());
124 EXPECT_EQ(false_value.GetBool(), blank.GetBool());
125}
126
127TEST(ValuesTest, CopyInt) {
jdoerrie239723572017-03-02 12:09:19128 Value value(74);
129 Value copied_value(value);
jdoerrie05eb3162017-02-01 10:36:56130 EXPECT_EQ(value.type(), copied_value.type());
131 EXPECT_EQ(value.GetInt(), copied_value.GetInt());
132
133 Value blank;
134
135 blank = value;
136 EXPECT_EQ(value.type(), blank.type());
137 EXPECT_EQ(value.GetInt(), blank.GetInt());
138}
139
140TEST(ValuesTest, CopyDouble) {
jdoerrie239723572017-03-02 12:09:19141 Value value(74.896);
142 Value copied_value(value);
jdoerrie05eb3162017-02-01 10:36:56143 EXPECT_EQ(value.type(), copied_value.type());
144 EXPECT_EQ(value.GetDouble(), copied_value.GetDouble());
145
146 Value blank;
147
148 blank = value;
149 EXPECT_EQ(value.type(), blank.type());
150 EXPECT_EQ(value.GetDouble(), blank.GetDouble());
151}
152
jdoerrief38f37b2017-02-01 14:38:32153TEST(ValuesTest, CopyString) {
jdoerrie122c4da2017-03-06 11:12:04154 Value value("foobar");
155 Value copied_value(value);
jdoerrief38f37b2017-02-01 14:38:32156 EXPECT_EQ(value.type(), copied_value.type());
157 EXPECT_EQ(value.GetString(), copied_value.GetString());
158
159 Value blank;
160
161 blank = value;
162 EXPECT_EQ(value.type(), blank.type());
163 EXPECT_EQ(value.GetString(), blank.GetString());
164}
165
jdoerriee03e80f2017-02-15 08:42:14166TEST(ValuesTest, CopyBinary) {
167 BinaryValue value(std::vector<char>({0xF, 0x0, 0x0, 0xB, 0xA, 0x2}));
168 BinaryValue copied_value(value);
169 EXPECT_EQ(value.type(), copied_value.type());
170 EXPECT_EQ(value.GetBlob(), copied_value.GetBlob());
171
172 Value blank;
173
174 blank = value;
175 EXPECT_EQ(value.type(), blank.type());
176 EXPECT_EQ(value.GetBlob(), blank.GetBlob());
177}
178
jdoerrie8e945542017-02-17 13:54:49179TEST(ValuesTest, CopyDictionary) {
180 // TODO(crbug.com/646113): Clean this up once DictionaryValue switched to
181 // value semantics.
182 int copy;
183 DictionaryValue value;
184 value.SetInteger("Int", 123);
185
186 DictionaryValue copied_value(value);
187 copied_value.GetInteger("Int", &copy);
188
189 EXPECT_EQ(value.type(), copied_value.type());
190 EXPECT_EQ(123, copy);
191
192 auto blank = MakeUnique<Value>();
193
194 *blank = value;
195 EXPECT_EQ(Value::Type::DICTIONARY, blank->type());
196
197 static_cast<DictionaryValue*>(blank.get())->GetInteger("Int", &copy);
198 EXPECT_EQ(123, copy);
199}
200
201TEST(ValuesTest, CopyList) {
202 // TODO(crbug.com/646113): Clean this up once ListValue switched to
203 // value semantics.
204 int copy;
205 ListValue value;
206 value.AppendInteger(123);
207
208 ListValue copied_value(value);
209 copied_value.GetInteger(0, &copy);
210
211 EXPECT_EQ(value.type(), copied_value.type());
212 EXPECT_EQ(123, copy);
213
214 auto blank = MakeUnique<Value>();
215
216 *blank = value;
217 EXPECT_EQ(Value::Type::LIST, blank->type());
218
219 static_cast<ListValue*>(blank.get())->GetInteger(0, &copy);
220 EXPECT_EQ(123, copy);
221}
222
jdoerrie05eb3162017-02-01 10:36:56223// Group of tests for the move constructors and move-assigmnent.
224TEST(ValuesTest, MoveBool) {
jdoerrie239723572017-03-02 12:09:19225 Value true_value(true);
226 Value moved_true_value(std::move(true_value));
jdoerrie05eb3162017-02-01 10:36:56227 EXPECT_EQ(Value::Type::BOOLEAN, moved_true_value.type());
228 EXPECT_TRUE(moved_true_value.GetBool());
229
jdoerrie239723572017-03-02 12:09:19230 Value false_value(false);
231 Value moved_false_value(std::move(false_value));
jdoerrie05eb3162017-02-01 10:36:56232 EXPECT_EQ(Value::Type::BOOLEAN, moved_false_value.type());
233 EXPECT_FALSE(moved_false_value.GetBool());
234
235 Value blank;
236
jdoerrie239723572017-03-02 12:09:19237 blank = Value(true);
jdoerrie05eb3162017-02-01 10:36:56238 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
239 EXPECT_TRUE(blank.GetBool());
240
jdoerrie239723572017-03-02 12:09:19241 blank = Value(false);
jdoerrie05eb3162017-02-01 10:36:56242 EXPECT_EQ(Value::Type::BOOLEAN, blank.type());
243 EXPECT_FALSE(blank.GetBool());
244}
245
246TEST(ValuesTest, MoveInt) {
jdoerrie239723572017-03-02 12:09:19247 Value value(74);
248 Value moved_value(std::move(value));
jdoerrie05eb3162017-02-01 10:36:56249 EXPECT_EQ(Value::Type::INTEGER, moved_value.type());
250 EXPECT_EQ(74, moved_value.GetInt());
251
252 Value blank;
253
jdoerrie239723572017-03-02 12:09:19254 blank = Value(47);
jdoerrie05eb3162017-02-01 10:36:56255 EXPECT_EQ(Value::Type::INTEGER, blank.type());
256 EXPECT_EQ(47, blank.GetInt());
257}
258
259TEST(ValuesTest, MoveDouble) {
jdoerrie239723572017-03-02 12:09:19260 Value value(74.896);
261 Value moved_value(std::move(value));
jdoerrie05eb3162017-02-01 10:36:56262 EXPECT_EQ(Value::Type::DOUBLE, moved_value.type());
263 EXPECT_EQ(74.896, moved_value.GetDouble());
264
265 Value blank;
266
jdoerrie239723572017-03-02 12:09:19267 blank = Value(654.38);
jdoerrie05eb3162017-02-01 10:36:56268 EXPECT_EQ(Value::Type::DOUBLE, blank.type());
269 EXPECT_EQ(654.38, blank.GetDouble());
270}
271
jdoerrief38f37b2017-02-01 14:38:32272TEST(ValuesTest, MoveString) {
jdoerrie122c4da2017-03-06 11:12:04273 Value value("foobar");
274 Value moved_value(std::move(value));
jdoerrief38f37b2017-02-01 14:38:32275 EXPECT_EQ(Value::Type::STRING, moved_value.type());
276 EXPECT_EQ("foobar", moved_value.GetString());
277
278 Value blank;
279
jdoerrie122c4da2017-03-06 11:12:04280 blank = Value("foobar");
jdoerrief38f37b2017-02-01 14:38:32281 EXPECT_EQ(Value::Type::STRING, blank.type());
282 EXPECT_EQ("foobar", blank.GetString());
283}
284
jdoerriee03e80f2017-02-15 08:42:14285TEST(ValuesTest, MoveBinary) {
286 const std::vector<char> buffer = {0xF, 0x0, 0x0, 0xB, 0xA, 0x2};
287 BinaryValue value(buffer);
288 BinaryValue moved_value(std::move(value));
289 EXPECT_EQ(Value::Type::BINARY, moved_value.type());
290 EXPECT_EQ(buffer, moved_value.GetBlob());
291
292 Value blank;
293
294 blank = BinaryValue(buffer);
295 EXPECT_EQ(Value::Type::BINARY, blank.type());
296 EXPECT_EQ(buffer, blank.GetBlob());
297}
298
jdoerrie8e945542017-02-17 13:54:49299TEST(ValuesTest, MoveDictionary) {
300 // TODO(crbug.com/646113): Clean this up once DictionaryValue switched to
301 // value semantics.
302 int move;
303 DictionaryValue value;
304 value.SetInteger("Int", 123);
305
306 DictionaryValue moved_value(std::move(value));
307 moved_value.GetInteger("Int", &move);
308
309 EXPECT_EQ(Value::Type::DICTIONARY, moved_value.type());
310 EXPECT_EQ(123, move);
311
312 Value blank;
313
314 blank = DictionaryValue();
315 EXPECT_EQ(Value::Type::DICTIONARY, blank.type());
316}
317
318TEST(ValuesTest, MoveList) {
319 // TODO(crbug.com/646113): Clean this up once ListValue switched to
320 // value semantics.
321 int move;
322 ListValue value;
323 value.AppendInteger(123);
324
325 ListValue moved_value(std::move(value));
326 moved_value.GetInteger(0, &move);
327
328 EXPECT_EQ(Value::Type::LIST, moved_value.type());
329 EXPECT_EQ(123, move);
330
331 Value blank;
332
333 blank = ListValue();
334 EXPECT_EQ(Value::Type::LIST, blank.type());
335}
336
[email protected]58b916e2011-06-28 22:56:33337TEST(ValuesTest, Basic) {
initial.commitd7cae122008-07-26 21:49:38338 // Test basic dictionary getting/setting
339 DictionaryValue settings;
[email protected]9e4cda7332010-07-31 04:56:14340 std::string homepage = "http://google.com";
341 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
342 ASSERT_EQ(std::string("http://google.com"), homepage);
343
344 ASSERT_FALSE(settings.Get("global", NULL));
estade7bc801fb2015-05-07 01:53:08345 settings.SetBoolean("global", true);
[email protected]9e4cda7332010-07-31 04:56:14346 ASSERT_TRUE(settings.Get("global", NULL));
347 settings.SetString("global.homepage", "http://scurvy.com");
348 ASSERT_TRUE(settings.Get("global", NULL));
349 homepage = "http://google.com";
350 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
351 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
352
353 // Test storing a dictionary in a list.
354 ListValue* toolbar_bookmarks;
355 ASSERT_FALSE(
356 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
357
dcheng093de9b2016-04-04 21:25:51358 std::unique_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
danakj0c8d4aa2015-11-25 05:29:58359 settings.Set("global.toolbar.bookmarks", std::move(new_toolbar_bookmarks));
[email protected]9e4cda7332010-07-31 04:56:14360 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
361
dcheng093de9b2016-04-04 21:25:51362 std::unique_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
[email protected]9e4cda7332010-07-31 04:56:14363 new_bookmark->SetString("name", "Froogle");
364 new_bookmark->SetString("url", "http://froogle.com");
danakj0c8d4aa2015-11-25 05:29:58365 toolbar_bookmarks->Append(std::move(new_bookmark));
[email protected]9e4cda7332010-07-31 04:56:14366
367 ListValue* bookmark_list;
368 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
369 DictionaryValue* bookmark;
370 ASSERT_EQ(1U, bookmark_list->GetSize());
371 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
372 std::string bookmark_name = "Unnamed";
373 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
374 ASSERT_EQ(std::string("Froogle"), bookmark_name);
375 std::string bookmark_url;
376 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
377 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
378}
379
[email protected]58b916e2011-06-28 22:56:33380TEST(ValuesTest, List) {
dcheng093de9b2016-04-04 21:25:51381 std::unique_ptr<ListValue> mixed_list(new ListValue());
jdoerrie239723572017-03-02 12:09:19382 mixed_list->Set(0, MakeUnique<Value>(true));
383 mixed_list->Set(1, MakeUnique<Value>(42));
384 mixed_list->Set(2, MakeUnique<Value>(88.8));
jdoerrie122c4da2017-03-06 11:12:04385 mixed_list->Set(3, MakeUnique<Value>("foo"));
[email protected]f82fb4952009-01-20 21:05:32386 ASSERT_EQ(4u, mixed_list->GetSize());
[email protected]52a261f2009-03-03 15:01:12387
[email protected]f82fb4952009-01-20 21:05:32388 Value *value = NULL;
389 bool bool_value = false;
390 int int_value = 0;
391 double double_value = 0.0;
392 std::string string_value;
393
394 ASSERT_FALSE(mixed_list->Get(4, &value));
395
396 ASSERT_FALSE(mixed_list->GetInteger(0, &int_value));
397 ASSERT_EQ(0, int_value);
[email protected]d3accda2011-05-02 01:59:21398 ASSERT_FALSE(mixed_list->GetBoolean(1, &bool_value));
399 ASSERT_FALSE(bool_value);
[email protected]f82fb4952009-01-20 21:05:32400 ASSERT_FALSE(mixed_list->GetString(2, &string_value));
401 ASSERT_EQ("", string_value);
[email protected]d3accda2011-05-02 01:59:21402 ASSERT_FALSE(mixed_list->GetInteger(2, &int_value));
403 ASSERT_EQ(0, int_value);
[email protected]f82fb4952009-01-20 21:05:32404 ASSERT_FALSE(mixed_list->GetBoolean(3, &bool_value));
[email protected]74e3af72010-10-03 21:44:39405 ASSERT_FALSE(bool_value);
[email protected]f82fb4952009-01-20 21:05:32406
407 ASSERT_TRUE(mixed_list->GetBoolean(0, &bool_value));
[email protected]74e3af72010-10-03 21:44:39408 ASSERT_TRUE(bool_value);
[email protected]f82fb4952009-01-20 21:05:32409 ASSERT_TRUE(mixed_list->GetInteger(1, &int_value));
410 ASSERT_EQ(42, int_value);
[email protected]d3accda2011-05-02 01:59:21411 // implicit conversion from Integer to Double should be possible.
412 ASSERT_TRUE(mixed_list->GetDouble(1, &double_value));
413 ASSERT_EQ(42, double_value);
[email protected]fb534c92011-02-01 01:02:07414 ASSERT_TRUE(mixed_list->GetDouble(2, &double_value));
[email protected]f82fb4952009-01-20 21:05:32415 ASSERT_EQ(88.8, double_value);
416 ASSERT_TRUE(mixed_list->GetString(3, &string_value));
417 ASSERT_EQ("foo", string_value);
[email protected]5fb35372011-09-19 15:23:10418
419 // Try searching in the mixed list.
jdoerrie239723572017-03-02 12:09:19420 base::Value sought_value(42);
421 base::Value not_found_value(false);
[email protected]5fb35372011-09-19 15:23:10422
[email protected]7e3ec42c2012-12-16 05:13:21423 ASSERT_NE(mixed_list->end(), mixed_list->Find(sought_value));
424 ASSERT_TRUE((*mixed_list->Find(sought_value))->GetAsInteger(&int_value));
[email protected]5fb35372011-09-19 15:23:10425 ASSERT_EQ(42, int_value);
[email protected]7e3ec42c2012-12-16 05:13:21426 ASSERT_EQ(mixed_list->end(), mixed_list->Find(not_found_value));
[email protected]f82fb4952009-01-20 21:05:32427}
428
[email protected]58b916e2011-06-28 22:56:33429TEST(ValuesTest, BinaryValue) {
jdoerriee03e80f2017-02-15 08:42:14430 // Default constructor creates a BinaryValue with a buffer of size 0.
431 auto binary = MakeUnique<Value>(Value::Type::BINARY);
[email protected]0a9a0fc2009-03-24 23:37:14432 ASSERT_TRUE(binary.get());
[email protected]f297d182008-08-21 16:24:51433 ASSERT_EQ(0U, binary->GetSize());
initial.commitd7cae122008-07-26 21:49:38434
435 // Test the common case of a non-empty buffer
jdoerriee03e80f2017-02-15 08:42:14436 std::vector<char> buffer(15);
437 char* original_buffer = buffer.data();
438 binary.reset(new BinaryValue(std::move(buffer)));
[email protected]0a9a0fc2009-03-24 23:37:14439 ASSERT_TRUE(binary.get());
initial.commitd7cae122008-07-26 21:49:38440 ASSERT_TRUE(binary->GetBuffer());
danakj8dba5a52015-05-11 21:24:09441 ASSERT_EQ(original_buffer, binary->GetBuffer());
[email protected]f297d182008-08-21 16:24:51442 ASSERT_EQ(15U, binary->GetSize());
initial.commitd7cae122008-07-26 21:49:38443
444 char stack_buffer[42];
445 memset(stack_buffer, '!', 42);
dcheng338b88292016-06-16 10:48:42446 binary = BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42);
[email protected]0a9a0fc2009-03-24 23:37:14447 ASSERT_TRUE(binary.get());
initial.commitd7cae122008-07-26 21:49:38448 ASSERT_TRUE(binary->GetBuffer());
449 ASSERT_NE(stack_buffer, binary->GetBuffer());
[email protected]f297d182008-08-21 16:24:51450 ASSERT_EQ(42U, binary->GetSize());
initial.commitd7cae122008-07-26 21:49:38451 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
pneubeck93871252015-01-20 11:26:36452
453 // Test overloaded GetAsBinary.
454 Value* narrow_value = binary.get();
455 const BinaryValue* narrow_binary = NULL;
456 ASSERT_TRUE(narrow_value->GetAsBinary(&narrow_binary));
457 EXPECT_EQ(binary.get(), narrow_binary);
initial.commitd7cae122008-07-26 21:49:38458}
459
[email protected]58b916e2011-06-28 22:56:33460TEST(ValuesTest, StringValue) {
[email protected]b54e6252014-01-30 10:32:41461 // Test overloaded StringValue constructor.
jdoerrie122c4da2017-03-06 11:12:04462 std::unique_ptr<Value> narrow_value(new Value("narrow"));
[email protected]0a9a0fc2009-03-24 23:37:14463 ASSERT_TRUE(narrow_value.get());
jdoerriedc72ee942016-12-07 15:43:28464 ASSERT_TRUE(narrow_value->IsType(Value::Type::STRING));
jdoerrie122c4da2017-03-06 11:12:04465 std::unique_ptr<Value> utf16_value(new Value(ASCIIToUTF16("utf16")));
[email protected]e2e593d2010-08-03 15:42:58466 ASSERT_TRUE(utf16_value.get());
jdoerriedc72ee942016-12-07 15:43:28467 ASSERT_TRUE(utf16_value->IsType(Value::Type::STRING));
[email protected]e2e593d2010-08-03 15:42:58468
[email protected]e5525182014-02-11 22:57:34469 // Test overloaded GetAsString.
[email protected]e2e593d2010-08-03 15:42:58470 std::string narrow = "http://google.com";
471 string16 utf16 = ASCIIToUTF16("http://google.com");
jdoerrie122c4da2017-03-06 11:12:04472 const Value* string_value = NULL;
[email protected]e2e593d2010-08-03 15:42:58473 ASSERT_TRUE(narrow_value->GetAsString(&narrow));
474 ASSERT_TRUE(narrow_value->GetAsString(&utf16));
[email protected]e5525182014-02-11 22:57:34475 ASSERT_TRUE(narrow_value->GetAsString(&string_value));
[email protected]e2e593d2010-08-03 15:42:58476 ASSERT_EQ(std::string("narrow"), narrow);
477 ASSERT_EQ(ASCIIToUTF16("narrow"), utf16);
[email protected]e5525182014-02-11 22:57:34478 ASSERT_EQ(string_value->GetString(), narrow);
[email protected]e2e593d2010-08-03 15:42:58479
480 ASSERT_TRUE(utf16_value->GetAsString(&narrow));
481 ASSERT_TRUE(utf16_value->GetAsString(&utf16));
[email protected]e5525182014-02-11 22:57:34482 ASSERT_TRUE(utf16_value->GetAsString(&string_value));
[email protected]e2e593d2010-08-03 15:42:58483 ASSERT_EQ(std::string("utf16"), narrow);
484 ASSERT_EQ(ASCIIToUTF16("utf16"), utf16);
[email protected]e5525182014-02-11 22:57:34485 ASSERT_EQ(string_value->GetString(), narrow);
486
487 // Don't choke on NULL values.
488 ASSERT_TRUE(narrow_value->GetAsString(static_cast<string16*>(NULL)));
489 ASSERT_TRUE(narrow_value->GetAsString(static_cast<std::string*>(NULL)));
jdoerrie122c4da2017-03-06 11:12:04490 ASSERT_TRUE(narrow_value->GetAsString(static_cast<const Value**>(NULL)));
[email protected]e2e593d2010-08-03 15:42:58491}
492
[email protected]58b916e2011-06-28 22:56:33493TEST(ValuesTest, ListDeletion) {
jdoerrie8e945542017-02-17 13:54:49494 ListValue list;
495 list.Append(MakeUnique<Value>());
496 EXPECT_FALSE(list.empty());
497 list.Clear();
498 EXPECT_TRUE(list.empty());
initial.commitd7cae122008-07-26 21:49:38499}
500
[email protected]58b916e2011-06-28 22:56:33501TEST(ValuesTest, ListRemoval) {
dcheng093de9b2016-04-04 21:25:51502 std::unique_ptr<Value> removed_item;
initial.commitd7cae122008-07-26 21:49:38503
504 {
505 ListValue list;
jdoerrie8e945542017-02-17 13:54:49506 list.Append(MakeUnique<Value>());
[email protected]f297d182008-08-21 16:24:51507 EXPECT_EQ(1U, list.GetSize());
[email protected]836061b2008-08-13 14:57:51508 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
509 &removed_item));
initial.commitd7cae122008-07-26 21:49:38510 EXPECT_FALSE(list.Remove(1, &removed_item));
511 EXPECT_TRUE(list.Remove(0, &removed_item));
512 ASSERT_TRUE(removed_item);
[email protected]f297d182008-08-21 16:24:51513 EXPECT_EQ(0U, list.GetSize());
initial.commitd7cae122008-07-26 21:49:38514 }
[email protected]d814a8852013-08-06 13:33:04515 removed_item.reset();
initial.commitd7cae122008-07-26 21:49:38516
517 {
518 ListValue list;
jdoerrie8e945542017-02-17 13:54:49519 list.Append(MakeUnique<Value>());
initial.commitd7cae122008-07-26 21:49:38520 EXPECT_TRUE(list.Remove(0, NULL));
[email protected]f297d182008-08-21 16:24:51521 EXPECT_EQ(0U, list.GetSize());
initial.commitd7cae122008-07-26 21:49:38522 }
[email protected]6832cbe2009-11-30 19:59:11523
524 {
525 ListValue list;
jdoerrie8e945542017-02-17 13:54:49526 auto value = MakeUnique<Value>();
527 Value* original_value = value.get();
danakj0c8d4aa2015-11-25 05:29:58528 list.Append(std::move(value));
[email protected]4fc3c5642011-08-13 17:34:31529 size_t index = 0;
danakj8dba5a52015-05-11 21:24:09530 list.Remove(*original_value, &index);
[email protected]4fc3c5642011-08-13 17:34:31531 EXPECT_EQ(0U, index);
[email protected]6832cbe2009-11-30 19:59:11532 EXPECT_EQ(0U, list.GetSize());
533 }
initial.commitd7cae122008-07-26 21:49:38534}
535
[email protected]58b916e2011-06-28 22:56:33536TEST(ValuesTest, DictionaryDeletion) {
[email protected]9e4cda7332010-07-31 04:56:14537 std::string key = "test";
jdoerrie8e945542017-02-17 13:54:49538 DictionaryValue dict;
539 dict.Set(key, MakeUnique<Value>());
540 EXPECT_FALSE(dict.empty());
541 dict.Clear();
542 EXPECT_TRUE(dict.empty());
[email protected]9e4cda7332010-07-31 04:56:14543}
544
[email protected]58b916e2011-06-28 22:56:33545TEST(ValuesTest, DictionaryRemoval) {
[email protected]9e4cda7332010-07-31 04:56:14546 std::string key = "test";
dcheng093de9b2016-04-04 21:25:51547 std::unique_ptr<Value> removed_item;
[email protected]9e4cda7332010-07-31 04:56:14548
549 {
550 DictionaryValue dict;
jdoerrie8e945542017-02-17 13:54:49551 dict.Set(key, MakeUnique<Value>());
[email protected]9e4cda7332010-07-31 04:56:14552 EXPECT_TRUE(dict.HasKey(key));
553 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
554 EXPECT_TRUE(dict.Remove(key, &removed_item));
555 EXPECT_FALSE(dict.HasKey(key));
556 ASSERT_TRUE(removed_item);
557 }
[email protected]9e4cda7332010-07-31 04:56:14558
559 {
560 DictionaryValue dict;
jdoerrie8e945542017-02-17 13:54:49561 dict.Set(key, MakeUnique<Value>());
[email protected]9e4cda7332010-07-31 04:56:14562 EXPECT_TRUE(dict.HasKey(key));
563 EXPECT_TRUE(dict.Remove(key, NULL));
[email protected]9e4cda7332010-07-31 04:56:14564 EXPECT_FALSE(dict.HasKey(key));
565 }
566}
567
[email protected]58b916e2011-06-28 22:56:33568TEST(ValuesTest, DictionaryWithoutPathExpansion) {
[email protected]4dad9ad82009-11-25 20:47:52569 DictionaryValue dict;
estadea68b0442015-05-12 18:11:50570 dict.Set("this.is.expanded", Value::CreateNullValue());
571 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
estadeca798482015-01-06 20:06:50572
573 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
574 EXPECT_TRUE(dict.HasKey("this"));
575 Value* value1;
576 EXPECT_TRUE(dict.Get("this", &value1));
577 DictionaryValue* value2;
578 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
579 EXPECT_EQ(value1, value2);
580 EXPECT_EQ(1U, value2->size());
581
582 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
583 Value* value3;
584 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
585 Value* value4;
586 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
jdoerriedc72ee942016-12-07 15:43:28587 EXPECT_EQ(Value::Type::NONE, value4->GetType());
estadeca798482015-01-06 20:06:50588}
589
590// Tests the deprecated version of SetWithoutPathExpansion.
591// TODO(estade): remove.
592TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) {
593 DictionaryValue dict;
[email protected]9e4cda7332010-07-31 04:56:14594 dict.Set("this.is.expanded", Value::CreateNullValue());
595 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
596
597 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
598 EXPECT_TRUE(dict.HasKey("this"));
599 Value* value1;
600 EXPECT_TRUE(dict.Get("this", &value1));
601 DictionaryValue* value2;
602 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
603 EXPECT_EQ(value1, value2);
604 EXPECT_EQ(1U, value2->size());
605
606 EXPECT_TRUE(dict.HasKey("this.isnt.expanded"));
607 Value* value3;
608 EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3));
609 Value* value4;
610 ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4));
jdoerriedc72ee942016-12-07 15:43:28611 EXPECT_EQ(Value::Type::NONE, value4->GetType());
[email protected]9e4cda7332010-07-31 04:56:14612}
613
[email protected]aa3283392013-11-27 01:38:24614TEST(ValuesTest, DictionaryRemovePath) {
615 DictionaryValue dict;
estadeca798482015-01-06 20:06:50616 dict.SetInteger("a.long.way.down", 1);
617 dict.SetBoolean("a.long.key.path", true);
[email protected]aa3283392013-11-27 01:38:24618
dcheng093de9b2016-04-04 21:25:51619 std::unique_ptr<Value> removed_item;
[email protected]aa3283392013-11-27 01:38:24620 EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item));
621 ASSERT_TRUE(removed_item);
jdoerriedc72ee942016-12-07 15:43:28622 EXPECT_TRUE(removed_item->IsType(base::Value::Type::INTEGER));
[email protected]aa3283392013-11-27 01:38:24623 EXPECT_FALSE(dict.HasKey("a.long.way.down"));
624 EXPECT_FALSE(dict.HasKey("a.long.way"));
625 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
626
627 removed_item.reset();
628 EXPECT_FALSE(dict.RemovePath("a.long.way.down", &removed_item));
629 EXPECT_FALSE(removed_item);
630 EXPECT_TRUE(dict.Get("a.long.key.path", NULL));
631
632 removed_item.reset();
633 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
634 ASSERT_TRUE(removed_item);
jdoerriedc72ee942016-12-07 15:43:28635 EXPECT_TRUE(removed_item->IsType(base::Value::Type::BOOLEAN));
[email protected]aa3283392013-11-27 01:38:24636 EXPECT_TRUE(dict.empty());
637}
638
[email protected]58b916e2011-06-28 22:56:33639TEST(ValuesTest, DeepCopy) {
initial.commitd7cae122008-07-26 21:49:38640 DictionaryValue original_dict;
dcheng093de9b2016-04-04 21:25:51641 std::unique_ptr<Value> scoped_null = Value::CreateNullValue();
danakj8dba5a52015-05-11 21:24:09642 Value* original_null = scoped_null.get();
danakj0c8d4aa2015-11-25 05:29:58643 original_dict.Set("null", std::move(scoped_null));
jdoerrie239723572017-03-02 12:09:19644 std::unique_ptr<Value> scoped_bool(new Value(true));
645 Value* original_bool = scoped_bool.get();
danakj0c8d4aa2015-11-25 05:29:58646 original_dict.Set("bool", std::move(scoped_bool));
jdoerrie239723572017-03-02 12:09:19647 std::unique_ptr<Value> scoped_int(new Value(42));
648 Value* original_int = scoped_int.get();
danakj0c8d4aa2015-11-25 05:29:58649 original_dict.Set("int", std::move(scoped_int));
jdoerrie239723572017-03-02 12:09:19650 std::unique_ptr<Value> scoped_double(new Value(3.14));
651 Value* original_double = scoped_double.get();
danakj0c8d4aa2015-11-25 05:29:58652 original_dict.Set("double", std::move(scoped_double));
jdoerrie122c4da2017-03-06 11:12:04653 std::unique_ptr<Value> scoped_string(new Value("hello"));
654 Value* original_string = scoped_string.get();
danakj0c8d4aa2015-11-25 05:29:58655 original_dict.Set("string", std::move(scoped_string));
jdoerrie122c4da2017-03-06 11:12:04656 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16")));
657 Value* original_string16 = scoped_string16.get();
danakj0c8d4aa2015-11-25 05:29:58658 original_dict.Set("string16", std::move(scoped_string16));
[email protected]9e4cda7332010-07-31 04:56:14659
jdoerriee03e80f2017-02-15 08:42:14660 std::vector<char> original_buffer(42, '!');
dcheng093de9b2016-04-04 21:25:51661 std::unique_ptr<BinaryValue> scoped_binary(
jdoerriee03e80f2017-02-15 08:42:14662 new BinaryValue(std::move(original_buffer)));
danakj8dba5a52015-05-11 21:24:09663 BinaryValue* original_binary = scoped_binary.get();
danakj0c8d4aa2015-11-25 05:29:58664 original_dict.Set("binary", std::move(scoped_binary));
[email protected]9e4cda7332010-07-31 04:56:14665
dcheng093de9b2016-04-04 21:25:51666 std::unique_ptr<ListValue> scoped_list(new ListValue());
danakj8dba5a52015-05-11 21:24:09667 Value* original_list = scoped_list.get();
jdoerrie239723572017-03-02 12:09:19668 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
danakj8dba5a52015-05-11 21:24:09669 Value* original_list_element_0 = scoped_list_element_0.get();
danakj0c8d4aa2015-11-25 05:29:58670 scoped_list->Append(std::move(scoped_list_element_0));
jdoerrie239723572017-03-02 12:09:19671 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
danakj8dba5a52015-05-11 21:24:09672 Value* original_list_element_1 = scoped_list_element_1.get();
danakj0c8d4aa2015-11-25 05:29:58673 scoped_list->Append(std::move(scoped_list_element_1));
674 original_dict.Set("list", std::move(scoped_list));
[email protected]9e4cda7332010-07-31 04:56:14675
dcheng093de9b2016-04-04 21:25:51676 std::unique_ptr<DictionaryValue> scoped_nested_dictionary(
677 new DictionaryValue());
danakj8dba5a52015-05-11 21:24:09678 Value* original_nested_dictionary = scoped_nested_dictionary.get();
679 scoped_nested_dictionary->SetString("key", "value");
danakj0c8d4aa2015-11-25 05:29:58680 original_dict.Set("dictionary", std::move(scoped_nested_dictionary));
[email protected]5cf906f82011-11-26 01:11:44681
dcheng093de9b2016-04-04 21:25:51682 std::unique_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy();
[email protected]9e4cda7332010-07-31 04:56:14683 ASSERT_TRUE(copy_dict.get());
684 ASSERT_NE(copy_dict.get(), &original_dict);
685
686 Value* copy_null = NULL;
687 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
688 ASSERT_TRUE(copy_null);
689 ASSERT_NE(copy_null, original_null);
jdoerriedc72ee942016-12-07 15:43:28690 ASSERT_TRUE(copy_null->IsType(Value::Type::NONE));
[email protected]9e4cda7332010-07-31 04:56:14691
692 Value* copy_bool = NULL;
693 ASSERT_TRUE(copy_dict->Get("bool", &copy_bool));
694 ASSERT_TRUE(copy_bool);
695 ASSERT_NE(copy_bool, original_bool);
jdoerriedc72ee942016-12-07 15:43:28696 ASSERT_TRUE(copy_bool->IsType(Value::Type::BOOLEAN));
[email protected]9e4cda7332010-07-31 04:56:14697 bool copy_bool_value = false;
698 ASSERT_TRUE(copy_bool->GetAsBoolean(&copy_bool_value));
699 ASSERT_TRUE(copy_bool_value);
700
701 Value* copy_int = NULL;
702 ASSERT_TRUE(copy_dict->Get("int", &copy_int));
703 ASSERT_TRUE(copy_int);
704 ASSERT_NE(copy_int, original_int);
jdoerriedc72ee942016-12-07 15:43:28705 ASSERT_TRUE(copy_int->IsType(Value::Type::INTEGER));
[email protected]9e4cda7332010-07-31 04:56:14706 int copy_int_value = 0;
707 ASSERT_TRUE(copy_int->GetAsInteger(&copy_int_value));
708 ASSERT_EQ(42, copy_int_value);
709
[email protected]fb534c92011-02-01 01:02:07710 Value* copy_double = NULL;
711 ASSERT_TRUE(copy_dict->Get("double", &copy_double));
712 ASSERT_TRUE(copy_double);
713 ASSERT_NE(copy_double, original_double);
jdoerriedc72ee942016-12-07 15:43:28714 ASSERT_TRUE(copy_double->IsType(Value::Type::DOUBLE));
[email protected]fb534c92011-02-01 01:02:07715 double copy_double_value = 0;
716 ASSERT_TRUE(copy_double->GetAsDouble(&copy_double_value));
717 ASSERT_EQ(3.14, copy_double_value);
[email protected]9e4cda7332010-07-31 04:56:14718
719 Value* copy_string = NULL;
720 ASSERT_TRUE(copy_dict->Get("string", &copy_string));
721 ASSERT_TRUE(copy_string);
722 ASSERT_NE(copy_string, original_string);
jdoerriedc72ee942016-12-07 15:43:28723 ASSERT_TRUE(copy_string->IsType(Value::Type::STRING));
[email protected]9e4cda7332010-07-31 04:56:14724 std::string copy_string_value;
[email protected]e2e593d2010-08-03 15:42:58725 string16 copy_string16_value;
[email protected]9e4cda7332010-07-31 04:56:14726 ASSERT_TRUE(copy_string->GetAsString(&copy_string_value));
[email protected]e2e593d2010-08-03 15:42:58727 ASSERT_TRUE(copy_string->GetAsString(&copy_string16_value));
[email protected]9e4cda7332010-07-31 04:56:14728 ASSERT_EQ(std::string("hello"), copy_string_value);
[email protected]e2e593d2010-08-03 15:42:58729 ASSERT_EQ(ASCIIToUTF16("hello"), copy_string16_value);
[email protected]9e4cda7332010-07-31 04:56:14730
[email protected]e2e593d2010-08-03 15:42:58731 Value* copy_string16 = NULL;
732 ASSERT_TRUE(copy_dict->Get("string16", &copy_string16));
733 ASSERT_TRUE(copy_string16);
734 ASSERT_NE(copy_string16, original_string16);
jdoerriedc72ee942016-12-07 15:43:28735 ASSERT_TRUE(copy_string16->IsType(Value::Type::STRING));
[email protected]e2e593d2010-08-03 15:42:58736 ASSERT_TRUE(copy_string16->GetAsString(&copy_string_value));
737 ASSERT_TRUE(copy_string16->GetAsString(&copy_string16_value));
[email protected]9e4cda7332010-07-31 04:56:14738 ASSERT_EQ(std::string("hello16"), copy_string_value);
[email protected]e2e593d2010-08-03 15:42:58739 ASSERT_EQ(ASCIIToUTF16("hello16"), copy_string16_value);
[email protected]9e4cda7332010-07-31 04:56:14740
741 Value* copy_binary = NULL;
742 ASSERT_TRUE(copy_dict->Get("binary", &copy_binary));
743 ASSERT_TRUE(copy_binary);
744 ASSERT_NE(copy_binary, original_binary);
jdoerriedc72ee942016-12-07 15:43:28745 ASSERT_TRUE(copy_binary->IsType(Value::Type::BINARY));
jdoerriee03e80f2017-02-15 08:42:14746 ASSERT_NE(original_binary->GetBuffer(), copy_binary->GetBuffer());
747 ASSERT_EQ(original_binary->GetSize(), copy_binary->GetSize());
748 ASSERT_EQ(0, memcmp(original_binary->GetBuffer(), copy_binary->GetBuffer(),
749 original_binary->GetSize()));
[email protected]9e4cda7332010-07-31 04:56:14750
751 Value* copy_value = NULL;
752 ASSERT_TRUE(copy_dict->Get("list", &copy_value));
753 ASSERT_TRUE(copy_value);
754 ASSERT_NE(copy_value, original_list);
jdoerriedc72ee942016-12-07 15:43:28755 ASSERT_TRUE(copy_value->IsType(Value::Type::LIST));
[email protected]5cf906f82011-11-26 01:11:44756 ListValue* copy_list = NULL;
757 ASSERT_TRUE(copy_value->GetAsList(&copy_list));
758 ASSERT_TRUE(copy_list);
[email protected]9e4cda7332010-07-31 04:56:14759 ASSERT_EQ(2U, copy_list->GetSize());
760
761 Value* copy_list_element_0;
762 ASSERT_TRUE(copy_list->Get(0, &copy_list_element_0));
763 ASSERT_TRUE(copy_list_element_0);
764 ASSERT_NE(copy_list_element_0, original_list_element_0);
765 int copy_list_element_0_value;
766 ASSERT_TRUE(copy_list_element_0->GetAsInteger(&copy_list_element_0_value));
767 ASSERT_EQ(0, copy_list_element_0_value);
768
769 Value* copy_list_element_1;
770 ASSERT_TRUE(copy_list->Get(1, &copy_list_element_1));
771 ASSERT_TRUE(copy_list_element_1);
772 ASSERT_NE(copy_list_element_1, original_list_element_1);
773 int copy_list_element_1_value;
774 ASSERT_TRUE(copy_list_element_1->GetAsInteger(&copy_list_element_1_value));
775 ASSERT_EQ(1, copy_list_element_1_value);
[email protected]5cf906f82011-11-26 01:11:44776
777 copy_value = NULL;
778 ASSERT_TRUE(copy_dict->Get("dictionary", &copy_value));
779 ASSERT_TRUE(copy_value);
780 ASSERT_NE(copy_value, original_nested_dictionary);
jdoerriedc72ee942016-12-07 15:43:28781 ASSERT_TRUE(copy_value->IsType(Value::Type::DICTIONARY));
[email protected]5cf906f82011-11-26 01:11:44782 DictionaryValue* copy_nested_dictionary = NULL;
783 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
784 ASSERT_TRUE(copy_nested_dictionary);
785 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
[email protected]9e4cda7332010-07-31 04:56:14786}
787
[email protected]58b916e2011-06-28 22:56:33788TEST(ValuesTest, Equals) {
dcheng093de9b2016-04-04 21:25:51789 std::unique_ptr<Value> null1(Value::CreateNullValue());
790 std::unique_ptr<Value> null2(Value::CreateNullValue());
danakj8dba5a52015-05-11 21:24:09791 EXPECT_NE(null1.get(), null2.get());
792 EXPECT_TRUE(null1->Equals(null2.get()));
initial.commitd7cae122008-07-26 21:49:38793
jdoerrie239723572017-03-02 12:09:19794 Value boolean(false);
estadea68b0442015-05-12 18:11:50795 EXPECT_FALSE(null1->Equals(&boolean));
initial.commitd7cae122008-07-26 21:49:38796
797 DictionaryValue dv;
[email protected]9e4cda7332010-07-31 04:56:14798 dv.SetBoolean("a", false);
799 dv.SetInteger("b", 2);
[email protected]fb534c92011-02-01 01:02:07800 dv.SetDouble("c", 2.5);
[email protected]9e4cda7332010-07-31 04:56:14801 dv.SetString("d1", "string");
[email protected]ff4c1d82010-08-04 16:58:12802 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
estadea68b0442015-05-12 18:11:50803 dv.Set("e", Value::CreateNullValue());
[email protected]9e4cda7332010-07-31 04:56:14804
dcheng093de9b2016-04-04 21:25:51805 std::unique_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
[email protected]dc1f2442010-08-18 16:23:40806 EXPECT_TRUE(dv.Equals(copy.get()));
[email protected]9e4cda7332010-07-31 04:56:14807
dcheng093de9b2016-04-04 21:25:51808 std::unique_ptr<ListValue> list(new ListValue);
danakj8dba5a52015-05-11 21:24:09809 ListValue* original_list = list.get();
estadea68b0442015-05-12 18:11:50810 list->Append(Value::CreateNullValue());
dcheng093de9b2016-04-04 21:25:51811 list->Append(WrapUnique(new DictionaryValue));
812 std::unique_ptr<Value> list_copy(list->CreateDeepCopy());
[email protected]9e4cda7332010-07-31 04:56:14813
danakj0c8d4aa2015-11-25 05:29:58814 dv.Set("f", std::move(list));
[email protected]dc1f2442010-08-18 16:23:40815 EXPECT_FALSE(dv.Equals(copy.get()));
danakj0c8d4aa2015-11-25 05:29:58816 copy->Set("f", std::move(list_copy));
[email protected]dc1f2442010-08-18 16:23:40817 EXPECT_TRUE(dv.Equals(copy.get()));
[email protected]9e4cda7332010-07-31 04:56:14818
jdoerrie239723572017-03-02 12:09:19819 original_list->Append(MakeUnique<Value>(true));
[email protected]dc1f2442010-08-18 16:23:40820 EXPECT_FALSE(dv.Equals(copy.get()));
821
822 // Check if Equals detects differences in only the keys.
estade7bc801fb2015-05-07 01:53:08823 copy = dv.CreateDeepCopy();
[email protected]dc1f2442010-08-18 16:23:40824 EXPECT_TRUE(dv.Equals(copy.get()));
825 copy->Remove("a", NULL);
826 copy->SetBoolean("aa", false);
827 EXPECT_FALSE(dv.Equals(copy.get()));
[email protected]9e4cda7332010-07-31 04:56:14828}
829
[email protected]58b916e2011-06-28 22:56:33830TEST(ValuesTest, StaticEquals) {
dcheng093de9b2016-04-04 21:25:51831 std::unique_ptr<Value> null1(Value::CreateNullValue());
832 std::unique_ptr<Value> null2(Value::CreateNullValue());
[email protected]73c47932010-12-06 18:13:43833 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
834 EXPECT_TRUE(Value::Equals(NULL, NULL));
835
jdoerrie239723572017-03-02 12:09:19836 std::unique_ptr<Value> i42(new Value(42));
837 std::unique_ptr<Value> j42(new Value(42));
838 std::unique_ptr<Value> i17(new Value(17));
[email protected]73c47932010-12-06 18:13:43839 EXPECT_TRUE(Value::Equals(i42.get(), i42.get()));
840 EXPECT_TRUE(Value::Equals(j42.get(), i42.get()));
841 EXPECT_TRUE(Value::Equals(i42.get(), j42.get()));
842 EXPECT_FALSE(Value::Equals(i42.get(), i17.get()));
843 EXPECT_FALSE(Value::Equals(i42.get(), NULL));
844 EXPECT_FALSE(Value::Equals(NULL, i42.get()));
845
846 // NULL and Value::CreateNullValue() are intentionally different: We need
847 // support for NULL as a return value for "undefined" without caring for
848 // ownership of the pointer.
849 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
850 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
851}
852
[email protected]58b916e2011-06-28 22:56:33853TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
[email protected]16f47e082011-01-18 02:16:59854 DictionaryValue original_dict;
dcheng093de9b2016-04-04 21:25:51855 std::unique_ptr<Value> scoped_null(Value::CreateNullValue());
danakj8dba5a52015-05-11 21:24:09856 Value* original_null = scoped_null.get();
danakj0c8d4aa2015-11-25 05:29:58857 original_dict.Set("null", std::move(scoped_null));
jdoerrie239723572017-03-02 12:09:19858 std::unique_ptr<Value> scoped_bool(new Value(true));
danakj8dba5a52015-05-11 21:24:09859 Value* original_bool = scoped_bool.get();
danakj0c8d4aa2015-11-25 05:29:58860 original_dict.Set("bool", std::move(scoped_bool));
jdoerrie239723572017-03-02 12:09:19861 std::unique_ptr<Value> scoped_int(new Value(42));
danakj8dba5a52015-05-11 21:24:09862 Value* original_int = scoped_int.get();
danakj0c8d4aa2015-11-25 05:29:58863 original_dict.Set("int", std::move(scoped_int));
jdoerrie239723572017-03-02 12:09:19864 std::unique_ptr<Value> scoped_double(new Value(3.14));
danakj8dba5a52015-05-11 21:24:09865 Value* original_double = scoped_double.get();
danakj0c8d4aa2015-11-25 05:29:58866 original_dict.Set("double", std::move(scoped_double));
jdoerrie122c4da2017-03-06 11:12:04867 std::unique_ptr<Value> scoped_string(new Value("hello"));
danakj8dba5a52015-05-11 21:24:09868 Value* original_string = scoped_string.get();
danakj0c8d4aa2015-11-25 05:29:58869 original_dict.Set("string", std::move(scoped_string));
jdoerrie122c4da2017-03-06 11:12:04870 std::unique_ptr<Value> scoped_string16(new Value(ASCIIToUTF16("hello16")));
danakj8dba5a52015-05-11 21:24:09871 Value* original_string16 = scoped_string16.get();
danakj0c8d4aa2015-11-25 05:29:58872 original_dict.Set("string16", std::move(scoped_string16));
[email protected]16f47e082011-01-18 02:16:59873
jdoerriee03e80f2017-02-15 08:42:14874 std::vector<char> original_buffer(42, '!');
dcheng093de9b2016-04-04 21:25:51875 std::unique_ptr<BinaryValue> scoped_binary(
jdoerriee03e80f2017-02-15 08:42:14876 new BinaryValue(std::move(original_buffer)));
danakj8dba5a52015-05-11 21:24:09877 Value* original_binary = scoped_binary.get();
danakj0c8d4aa2015-11-25 05:29:58878 original_dict.Set("binary", std::move(scoped_binary));
[email protected]16f47e082011-01-18 02:16:59879
dcheng093de9b2016-04-04 21:25:51880 std::unique_ptr<ListValue> scoped_list(new ListValue());
danakj8dba5a52015-05-11 21:24:09881 Value* original_list = scoped_list.get();
jdoerrie239723572017-03-02 12:09:19882 std::unique_ptr<Value> scoped_list_element_0(new Value(0));
danakj0c8d4aa2015-11-25 05:29:58883 scoped_list->Append(std::move(scoped_list_element_0));
jdoerrie239723572017-03-02 12:09:19884 std::unique_ptr<Value> scoped_list_element_1(new Value(1));
danakj0c8d4aa2015-11-25 05:29:58885 scoped_list->Append(std::move(scoped_list_element_1));
886 original_dict.Set("list", std::move(scoped_list));
[email protected]16f47e082011-01-18 02:16:59887
dcheng093de9b2016-04-04 21:25:51888 std::unique_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
889 std::unique_ptr<Value> copy_null = original_null->CreateDeepCopy();
890 std::unique_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
891 std::unique_ptr<Value> copy_int = original_int->CreateDeepCopy();
892 std::unique_ptr<Value> copy_double = original_double->CreateDeepCopy();
893 std::unique_ptr<Value> copy_string = original_string->CreateDeepCopy();
894 std::unique_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
895 std::unique_ptr<Value> copy_binary = original_binary->CreateDeepCopy();
896 std::unique_ptr<Value> copy_list = original_list->CreateDeepCopy();
[email protected]16f47e082011-01-18 02:16:59897
danakj8dba5a52015-05-11 21:24:09898 EXPECT_TRUE(original_dict.Equals(copy_dict.get()));
899 EXPECT_TRUE(original_null->Equals(copy_null.get()));
900 EXPECT_TRUE(original_bool->Equals(copy_bool.get()));
901 EXPECT_TRUE(original_int->Equals(copy_int.get()));
902 EXPECT_TRUE(original_double->Equals(copy_double.get()));
903 EXPECT_TRUE(original_string->Equals(copy_string.get()));
904 EXPECT_TRUE(original_string16->Equals(copy_string16.get()));
905 EXPECT_TRUE(original_binary->Equals(copy_binary.get()));
906 EXPECT_TRUE(original_list->Equals(copy_list.get()));
[email protected]16f47e082011-01-18 02:16:59907}
908
[email protected]58b916e2011-06-28 22:56:33909TEST(ValuesTest, RemoveEmptyChildren) {
dcheng093de9b2016-04-04 21:25:51910 std::unique_ptr<DictionaryValue> root(new DictionaryValue);
[email protected]ec330b52009-12-02 00:20:32911 // Remove empty lists and dictionaries.
dcheng093de9b2016-04-04 21:25:51912 root->Set("empty_dict", WrapUnique(new DictionaryValue));
913 root->Set("empty_list", WrapUnique(new ListValue));
estadeca798482015-01-06 20:06:50914 root->SetWithoutPathExpansion("a.b.c.d.e",
dcheng093de9b2016-04-04 21:25:51915 WrapUnique(new DictionaryValue));
estade6e04d502015-05-23 02:42:09916 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14917 EXPECT_TRUE(root->empty());
918
919 // Make sure we don't prune too much.
920 root->SetBoolean("bool", true);
dcheng093de9b2016-04-04 21:25:51921 root->Set("empty_dict", WrapUnique(new DictionaryValue));
[email protected]007b3f82013-04-09 08:46:45922 root->SetString("empty_string", std::string());
estade6e04d502015-05-23 02:42:09923 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14924 EXPECT_EQ(2U, root->size());
925
926 // Should do nothing.
estade6e04d502015-05-23 02:42:09927 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14928 EXPECT_EQ(2U, root->size());
929
930 // Nested test cases. These should all reduce back to the bool and string
931 // set above.
932 {
dcheng093de9b2016-04-04 21:25:51933 root->Set("a.b.c.d.e", WrapUnique(new DictionaryValue));
estade6e04d502015-05-23 02:42:09934 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14935 EXPECT_EQ(2U, root->size());
936 }
937 {
dcheng093de9b2016-04-04 21:25:51938 std::unique_ptr<DictionaryValue> inner(new DictionaryValue);
939 inner->Set("empty_dict", WrapUnique(new DictionaryValue));
940 inner->Set("empty_list", WrapUnique(new ListValue));
danakj0c8d4aa2015-11-25 05:29:58941 root->Set("dict_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:09942 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14943 EXPECT_EQ(2U, root->size());
944 }
945 {
dcheng093de9b2016-04-04 21:25:51946 std::unique_ptr<ListValue> inner(new ListValue);
947 inner->Append(WrapUnique(new DictionaryValue));
948 inner->Append(WrapUnique(new ListValue));
danakj0c8d4aa2015-11-25 05:29:58949 root->Set("list_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:09950 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14951 EXPECT_EQ(2U, root->size());
952 }
953
954 // Nested with siblings.
955 {
dcheng093de9b2016-04-04 21:25:51956 std::unique_ptr<ListValue> inner(new ListValue());
957 inner->Append(WrapUnique(new DictionaryValue));
958 inner->Append(WrapUnique(new ListValue));
danakj0c8d4aa2015-11-25 05:29:58959 root->Set("list_with_empty_children", std::move(inner));
dcheng093de9b2016-04-04 21:25:51960 std::unique_ptr<DictionaryValue> inner2(new DictionaryValue);
961 inner2->Set("empty_dict", WrapUnique(new DictionaryValue));
962 inner2->Set("empty_list", WrapUnique(new ListValue));
danakj0c8d4aa2015-11-25 05:29:58963 root->Set("dict_with_empty_children", std::move(inner2));
estade6e04d502015-05-23 02:42:09964 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14965 EXPECT_EQ(2U, root->size());
966 }
967
968 // Make sure nested values don't get pruned.
969 {
dcheng093de9b2016-04-04 21:25:51970 std::unique_ptr<ListValue> inner(new ListValue);
971 std::unique_ptr<ListValue> inner2(new ListValue);
jdoerrie122c4da2017-03-06 11:12:04972 inner2->Append(MakeUnique<Value>("hello"));
dcheng093de9b2016-04-04 21:25:51973 inner->Append(WrapUnique(new DictionaryValue));
danakj0c8d4aa2015-11-25 05:29:58974 inner->Append(std::move(inner2));
975 root->Set("list_with_empty_children", std::move(inner));
estade6e04d502015-05-23 02:42:09976 root = root->DeepCopyWithoutEmptyChildren();
[email protected]9e4cda7332010-07-31 04:56:14977 EXPECT_EQ(3U, root->size());
estade7bc801fb2015-05-07 01:53:08978
979 ListValue* inner_value, *inner_value2;
980 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
981 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
982 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
983 EXPECT_EQ(1U, inner_value2->GetSize());
[email protected]9e4cda7332010-07-31 04:56:14984 }
985}
986
[email protected]58b916e2011-06-28 22:56:33987TEST(ValuesTest, MergeDictionary) {
dcheng093de9b2016-04-04 21:25:51988 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
[email protected]9e4cda7332010-07-31 04:56:14989 base->SetString("base_key", "base_key_value_base");
990 base->SetString("collide_key", "collide_key_value_base");
dcheng093de9b2016-04-04 21:25:51991 std::unique_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
[email protected]9e4cda7332010-07-31 04:56:14992 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
993 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
danakj0c8d4aa2015-11-25 05:29:58994 base->Set("sub_dict_key", std::move(base_sub_dict));
[email protected]9e4cda7332010-07-31 04:56:14995
dcheng093de9b2016-04-04 21:25:51996 std::unique_ptr<DictionaryValue> merge(new DictionaryValue);
[email protected]9e4cda7332010-07-31 04:56:14997 merge->SetString("merge_key", "merge_key_value_merge");
998 merge->SetString("collide_key", "collide_key_value_merge");
dcheng093de9b2016-04-04 21:25:51999 std::unique_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
[email protected]9e4cda7332010-07-31 04:56:141000 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
1001 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
danakj0c8d4aa2015-11-25 05:29:581002 merge->Set("sub_dict_key", std::move(merge_sub_dict));
[email protected]9e4cda7332010-07-31 04:56:141003
1004 base->MergeDictionary(merge.get());
1005
1006 EXPECT_EQ(4U, base->size());
1007 std::string base_key_value;
1008 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
1009 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
1010 std::string collide_key_value;
1011 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
1012 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
1013 std::string merge_key_value;
1014 EXPECT_TRUE(base->GetString("merge_key", &merge_key_value));
1015 EXPECT_EQ("merge_key_value_merge", merge_key_value); // Merged in.
1016
1017 DictionaryValue* res_sub_dict;
1018 EXPECT_TRUE(base->GetDictionary("sub_dict_key", &res_sub_dict));
1019 EXPECT_EQ(3U, res_sub_dict->size());
1020 std::string sub_base_key_value;
1021 EXPECT_TRUE(res_sub_dict->GetString("sub_base_key", &sub_base_key_value));
1022 EXPECT_EQ("sub_base_key_value_base", sub_base_key_value); // Preserved.
1023 std::string sub_collide_key_value;
1024 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
1025 &sub_collide_key_value));
1026 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
1027 std::string sub_merge_key_value;
1028 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
1029 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
1030}
[email protected]58b916e2011-06-28 22:56:331031
[email protected]13502562012-05-09 21:54:271032TEST(ValuesTest, MergeDictionaryDeepCopy) {
dcheng093de9b2016-04-04 21:25:511033 std::unique_ptr<DictionaryValue> child(new DictionaryValue);
danakj8dba5a52015-05-11 21:24:091034 DictionaryValue* original_child = child.get();
[email protected]13502562012-05-09 21:54:271035 child->SetString("test", "value");
1036 EXPECT_EQ(1U, child->size());
1037
1038 std::string value;
1039 EXPECT_TRUE(child->GetString("test", &value));
1040 EXPECT_EQ("value", value);
1041
dcheng093de9b2016-04-04 21:25:511042 std::unique_ptr<DictionaryValue> base(new DictionaryValue);
danakj0c8d4aa2015-11-25 05:29:581043 base->Set("dict", std::move(child));
[email protected]13502562012-05-09 21:54:271044 EXPECT_EQ(1U, base->size());
1045
1046 DictionaryValue* ptr;
1047 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
danakj8dba5a52015-05-11 21:24:091048 EXPECT_EQ(original_child, ptr);
[email protected]13502562012-05-09 21:54:271049
dcheng093de9b2016-04-04 21:25:511050 std::unique_ptr<DictionaryValue> merged(new DictionaryValue);
[email protected]13502562012-05-09 21:54:271051 merged->MergeDictionary(base.get());
1052 EXPECT_EQ(1U, merged->size());
1053 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
danakj8dba5a52015-05-11 21:24:091054 EXPECT_NE(original_child, ptr);
[email protected]13502562012-05-09 21:54:271055 EXPECT_TRUE(ptr->GetString("test", &value));
1056 EXPECT_EQ("value", value);
1057
danakj8dba5a52015-05-11 21:24:091058 original_child->SetString("test", "overwrite");
[email protected]13502562012-05-09 21:54:271059 base.reset();
1060 EXPECT_TRUE(ptr->GetString("test", &value));
1061 EXPECT_EQ("value", value);
1062}
1063
[email protected]32c0e002011-11-08 21:26:411064TEST(ValuesTest, DictionaryIterator) {
1065 DictionaryValue dict;
[email protected]ee6ba60f2013-04-12 09:25:231066 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
[email protected]32c0e002011-11-08 21:26:411067 ADD_FAILURE();
1068 }
1069
jdoerrie122c4da2017-03-06 11:12:041070 Value value1("value1");
estade7bc801fb2015-05-07 01:53:081071 dict.Set("key1", value1.CreateDeepCopy());
[email protected]32c0e002011-11-08 21:26:411072 bool seen1 = false;
[email protected]ee6ba60f2013-04-12 09:25:231073 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
[email protected]32c0e002011-11-08 21:26:411074 EXPECT_FALSE(seen1);
1075 EXPECT_EQ("key1", it.key());
1076 EXPECT_TRUE(value1.Equals(&it.value()));
1077 seen1 = true;
1078 }
1079 EXPECT_TRUE(seen1);
1080
jdoerrie122c4da2017-03-06 11:12:041081 Value value2("value2");
estade7bc801fb2015-05-07 01:53:081082 dict.Set("key2", value2.CreateDeepCopy());
[email protected]32c0e002011-11-08 21:26:411083 bool seen2 = seen1 = false;
[email protected]ee6ba60f2013-04-12 09:25:231084 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
[email protected]32c0e002011-11-08 21:26:411085 if (it.key() == "key1") {
1086 EXPECT_FALSE(seen1);
1087 EXPECT_TRUE(value1.Equals(&it.value()));
1088 seen1 = true;
1089 } else if (it.key() == "key2") {
1090 EXPECT_FALSE(seen2);
1091 EXPECT_TRUE(value2.Equals(&it.value()));
1092 seen2 = true;
1093 } else {
1094 ADD_FAILURE();
1095 }
1096 }
1097 EXPECT_TRUE(seen1);
1098 EXPECT_TRUE(seen2);
1099}
1100
[email protected]78c03a42014-03-09 07:13:231101// DictionaryValue/ListValue's Get*() methods should accept NULL as an out-value
1102// and still return true/false based on success.
1103TEST(ValuesTest, GetWithNullOutValue) {
1104 DictionaryValue main_dict;
1105 ListValue main_list;
1106
jdoerrie239723572017-03-02 12:09:191107 Value bool_value(false);
1108 Value int_value(1234);
1109 Value double_value(12.34567);
jdoerrie122c4da2017-03-06 11:12:041110 Value string_value("foo");
jdoerriee03e80f2017-02-15 08:42:141111 BinaryValue binary_value(Value::Type::BINARY);
[email protected]78c03a42014-03-09 07:13:231112 DictionaryValue dict_value;
1113 ListValue list_value;
1114
estade7bc801fb2015-05-07 01:53:081115 main_dict.Set("bool", bool_value.CreateDeepCopy());
1116 main_dict.Set("int", int_value.CreateDeepCopy());
1117 main_dict.Set("double", double_value.CreateDeepCopy());
1118 main_dict.Set("string", string_value.CreateDeepCopy());
1119 main_dict.Set("binary", binary_value.CreateDeepCopy());
1120 main_dict.Set("dict", dict_value.CreateDeepCopy());
1121 main_dict.Set("list", list_value.CreateDeepCopy());
[email protected]78c03a42014-03-09 07:13:231122
estade7bc801fb2015-05-07 01:53:081123 main_list.Append(bool_value.CreateDeepCopy());
1124 main_list.Append(int_value.CreateDeepCopy());
1125 main_list.Append(double_value.CreateDeepCopy());
1126 main_list.Append(string_value.CreateDeepCopy());
1127 main_list.Append(binary_value.CreateDeepCopy());
1128 main_list.Append(dict_value.CreateDeepCopy());
1129 main_list.Append(list_value.CreateDeepCopy());
[email protected]78c03a42014-03-09 07:13:231130
1131 EXPECT_TRUE(main_dict.Get("bool", NULL));
1132 EXPECT_TRUE(main_dict.Get("int", NULL));
1133 EXPECT_TRUE(main_dict.Get("double", NULL));
1134 EXPECT_TRUE(main_dict.Get("string", NULL));
1135 EXPECT_TRUE(main_dict.Get("binary", NULL));
1136 EXPECT_TRUE(main_dict.Get("dict", NULL));
1137 EXPECT_TRUE(main_dict.Get("list", NULL));
1138 EXPECT_FALSE(main_dict.Get("DNE", NULL));
1139
1140 EXPECT_TRUE(main_dict.GetBoolean("bool", NULL));
1141 EXPECT_FALSE(main_dict.GetBoolean("int", NULL));
1142 EXPECT_FALSE(main_dict.GetBoolean("double", NULL));
1143 EXPECT_FALSE(main_dict.GetBoolean("string", NULL));
1144 EXPECT_FALSE(main_dict.GetBoolean("binary", NULL));
1145 EXPECT_FALSE(main_dict.GetBoolean("dict", NULL));
1146 EXPECT_FALSE(main_dict.GetBoolean("list", NULL));
1147 EXPECT_FALSE(main_dict.GetBoolean("DNE", NULL));
1148
1149 EXPECT_FALSE(main_dict.GetInteger("bool", NULL));
1150 EXPECT_TRUE(main_dict.GetInteger("int", NULL));
1151 EXPECT_FALSE(main_dict.GetInteger("double", NULL));
1152 EXPECT_FALSE(main_dict.GetInteger("string", NULL));
1153 EXPECT_FALSE(main_dict.GetInteger("binary", NULL));
1154 EXPECT_FALSE(main_dict.GetInteger("dict", NULL));
1155 EXPECT_FALSE(main_dict.GetInteger("list", NULL));
1156 EXPECT_FALSE(main_dict.GetInteger("DNE", NULL));
1157
1158 // Both int and double values can be obtained from GetDouble.
1159 EXPECT_FALSE(main_dict.GetDouble("bool", NULL));
1160 EXPECT_TRUE(main_dict.GetDouble("int", NULL));
1161 EXPECT_TRUE(main_dict.GetDouble("double", NULL));
1162 EXPECT_FALSE(main_dict.GetDouble("string", NULL));
1163 EXPECT_FALSE(main_dict.GetDouble("binary", NULL));
1164 EXPECT_FALSE(main_dict.GetDouble("dict", NULL));
1165 EXPECT_FALSE(main_dict.GetDouble("list", NULL));
1166 EXPECT_FALSE(main_dict.GetDouble("DNE", NULL));
1167
1168 EXPECT_FALSE(main_dict.GetString("bool", static_cast<std::string*>(NULL)));
1169 EXPECT_FALSE(main_dict.GetString("int", static_cast<std::string*>(NULL)));
1170 EXPECT_FALSE(main_dict.GetString("double", static_cast<std::string*>(NULL)));
1171 EXPECT_TRUE(main_dict.GetString("string", static_cast<std::string*>(NULL)));
1172 EXPECT_FALSE(main_dict.GetString("binary", static_cast<std::string*>(NULL)));
1173 EXPECT_FALSE(main_dict.GetString("dict", static_cast<std::string*>(NULL)));
1174 EXPECT_FALSE(main_dict.GetString("list", static_cast<std::string*>(NULL)));
1175 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<std::string*>(NULL)));
1176
1177 EXPECT_FALSE(main_dict.GetString("bool", static_cast<string16*>(NULL)));
1178 EXPECT_FALSE(main_dict.GetString("int", static_cast<string16*>(NULL)));
1179 EXPECT_FALSE(main_dict.GetString("double", static_cast<string16*>(NULL)));
1180 EXPECT_TRUE(main_dict.GetString("string", static_cast<string16*>(NULL)));
1181 EXPECT_FALSE(main_dict.GetString("binary", static_cast<string16*>(NULL)));
1182 EXPECT_FALSE(main_dict.GetString("dict", static_cast<string16*>(NULL)));
1183 EXPECT_FALSE(main_dict.GetString("list", static_cast<string16*>(NULL)));
1184 EXPECT_FALSE(main_dict.GetString("DNE", static_cast<string16*>(NULL)));
1185
1186 EXPECT_FALSE(main_dict.GetBinary("bool", NULL));
1187 EXPECT_FALSE(main_dict.GetBinary("int", NULL));
1188 EXPECT_FALSE(main_dict.GetBinary("double", NULL));
1189 EXPECT_FALSE(main_dict.GetBinary("string", NULL));
1190 EXPECT_TRUE(main_dict.GetBinary("binary", NULL));
1191 EXPECT_FALSE(main_dict.GetBinary("dict", NULL));
1192 EXPECT_FALSE(main_dict.GetBinary("list", NULL));
1193 EXPECT_FALSE(main_dict.GetBinary("DNE", NULL));
1194
1195 EXPECT_FALSE(main_dict.GetDictionary("bool", NULL));
1196 EXPECT_FALSE(main_dict.GetDictionary("int", NULL));
1197 EXPECT_FALSE(main_dict.GetDictionary("double", NULL));
1198 EXPECT_FALSE(main_dict.GetDictionary("string", NULL));
1199 EXPECT_FALSE(main_dict.GetDictionary("binary", NULL));
1200 EXPECT_TRUE(main_dict.GetDictionary("dict", NULL));
1201 EXPECT_FALSE(main_dict.GetDictionary("list", NULL));
1202 EXPECT_FALSE(main_dict.GetDictionary("DNE", NULL));
1203
1204 EXPECT_FALSE(main_dict.GetList("bool", NULL));
1205 EXPECT_FALSE(main_dict.GetList("int", NULL));
1206 EXPECT_FALSE(main_dict.GetList("double", NULL));
1207 EXPECT_FALSE(main_dict.GetList("string", NULL));
1208 EXPECT_FALSE(main_dict.GetList("binary", NULL));
1209 EXPECT_FALSE(main_dict.GetList("dict", NULL));
1210 EXPECT_TRUE(main_dict.GetList("list", NULL));
1211 EXPECT_FALSE(main_dict.GetList("DNE", NULL));
1212
1213 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("bool", NULL));
1214 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("int", NULL));
1215 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("double", NULL));
1216 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("string", NULL));
1217 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("binary", NULL));
1218 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("dict", NULL));
1219 EXPECT_TRUE(main_dict.GetWithoutPathExpansion("list", NULL));
1220 EXPECT_FALSE(main_dict.GetWithoutPathExpansion("DNE", NULL));
1221
1222 EXPECT_TRUE(main_dict.GetBooleanWithoutPathExpansion("bool", NULL));
1223 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("int", NULL));
1224 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("double", NULL));
1225 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("string", NULL));
1226 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("binary", NULL));
1227 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("dict", NULL));
1228 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("list", NULL));
1229 EXPECT_FALSE(main_dict.GetBooleanWithoutPathExpansion("DNE", NULL));
1230
1231 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("bool", NULL));
1232 EXPECT_TRUE(main_dict.GetIntegerWithoutPathExpansion("int", NULL));
1233 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("double", NULL));
1234 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("string", NULL));
1235 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("binary", NULL));
1236 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("dict", NULL));
1237 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("list", NULL));
1238 EXPECT_FALSE(main_dict.GetIntegerWithoutPathExpansion("DNE", NULL));
1239
1240 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("bool", NULL));
1241 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("int", NULL));
1242 EXPECT_TRUE(main_dict.GetDoubleWithoutPathExpansion("double", NULL));
1243 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("string", NULL));
1244 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("binary", NULL));
1245 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("dict", NULL));
1246 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("list", NULL));
1247 EXPECT_FALSE(main_dict.GetDoubleWithoutPathExpansion("DNE", NULL));
1248
1249 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1250 "bool", static_cast<std::string*>(NULL)));
1251 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1252 "int", static_cast<std::string*>(NULL)));
1253 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1254 "double", static_cast<std::string*>(NULL)));
1255 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1256 "string", static_cast<std::string*>(NULL)));
1257 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1258 "binary", static_cast<std::string*>(NULL)));
1259 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1260 "dict", static_cast<std::string*>(NULL)));
1261 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1262 "list", static_cast<std::string*>(NULL)));
1263 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1264 "DNE", static_cast<std::string*>(NULL)));
1265
1266 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1267 "bool", static_cast<string16*>(NULL)));
1268 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1269 "int", static_cast<string16*>(NULL)));
1270 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1271 "double", static_cast<string16*>(NULL)));
1272 EXPECT_TRUE(main_dict.GetStringWithoutPathExpansion(
1273 "string", static_cast<string16*>(NULL)));
1274 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1275 "binary", static_cast<string16*>(NULL)));
1276 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1277 "dict", static_cast<string16*>(NULL)));
1278 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1279 "list", static_cast<string16*>(NULL)));
1280 EXPECT_FALSE(main_dict.GetStringWithoutPathExpansion(
1281 "DNE", static_cast<string16*>(NULL)));
1282
1283 // There is no GetBinaryWithoutPathExpansion for some reason, but if there
1284 // were it should be tested here...
1285
1286 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("bool", NULL));
1287 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("int", NULL));
1288 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("double", NULL));
1289 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("string", NULL));
1290 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("binary", NULL));
1291 EXPECT_TRUE(main_dict.GetDictionaryWithoutPathExpansion("dict", NULL));
1292 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("list", NULL));
1293 EXPECT_FALSE(main_dict.GetDictionaryWithoutPathExpansion("DNE", NULL));
1294
1295 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("bool", NULL));
1296 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("int", NULL));
1297 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("double", NULL));
1298 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("string", NULL));
1299 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("binary", NULL));
1300 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("dict", NULL));
1301 EXPECT_TRUE(main_dict.GetListWithoutPathExpansion("list", NULL));
1302 EXPECT_FALSE(main_dict.GetListWithoutPathExpansion("DNE", NULL));
1303
1304 EXPECT_TRUE(main_list.Get(0, NULL));
1305 EXPECT_TRUE(main_list.Get(1, NULL));
1306 EXPECT_TRUE(main_list.Get(2, NULL));
1307 EXPECT_TRUE(main_list.Get(3, NULL));
1308 EXPECT_TRUE(main_list.Get(4, NULL));
1309 EXPECT_TRUE(main_list.Get(5, NULL));
1310 EXPECT_TRUE(main_list.Get(6, NULL));
1311 EXPECT_FALSE(main_list.Get(7, NULL));
1312
1313 EXPECT_TRUE(main_list.GetBoolean(0, NULL));
1314 EXPECT_FALSE(main_list.GetBoolean(1, NULL));
1315 EXPECT_FALSE(main_list.GetBoolean(2, NULL));
1316 EXPECT_FALSE(main_list.GetBoolean(3, NULL));
1317 EXPECT_FALSE(main_list.GetBoolean(4, NULL));
1318 EXPECT_FALSE(main_list.GetBoolean(5, NULL));
1319 EXPECT_FALSE(main_list.GetBoolean(6, NULL));
1320 EXPECT_FALSE(main_list.GetBoolean(7, NULL));
1321
1322 EXPECT_FALSE(main_list.GetInteger(0, NULL));
1323 EXPECT_TRUE(main_list.GetInteger(1, NULL));
1324 EXPECT_FALSE(main_list.GetInteger(2, NULL));
1325 EXPECT_FALSE(main_list.GetInteger(3, NULL));
1326 EXPECT_FALSE(main_list.GetInteger(4, NULL));
1327 EXPECT_FALSE(main_list.GetInteger(5, NULL));
1328 EXPECT_FALSE(main_list.GetInteger(6, NULL));
1329 EXPECT_FALSE(main_list.GetInteger(7, NULL));
1330
1331 EXPECT_FALSE(main_list.GetDouble(0, NULL));
1332 EXPECT_TRUE(main_list.GetDouble(1, NULL));
1333 EXPECT_TRUE(main_list.GetDouble(2, NULL));
1334 EXPECT_FALSE(main_list.GetDouble(3, NULL));
1335 EXPECT_FALSE(main_list.GetDouble(4, NULL));
1336 EXPECT_FALSE(main_list.GetDouble(5, NULL));
1337 EXPECT_FALSE(main_list.GetDouble(6, NULL));
1338 EXPECT_FALSE(main_list.GetDouble(7, NULL));
1339
1340 EXPECT_FALSE(main_list.GetString(0, static_cast<std::string*>(NULL)));
1341 EXPECT_FALSE(main_list.GetString(1, static_cast<std::string*>(NULL)));
1342 EXPECT_FALSE(main_list.GetString(2, static_cast<std::string*>(NULL)));
1343 EXPECT_TRUE(main_list.GetString(3, static_cast<std::string*>(NULL)));
1344 EXPECT_FALSE(main_list.GetString(4, static_cast<std::string*>(NULL)));
1345 EXPECT_FALSE(main_list.GetString(5, static_cast<std::string*>(NULL)));
1346 EXPECT_FALSE(main_list.GetString(6, static_cast<std::string*>(NULL)));
1347 EXPECT_FALSE(main_list.GetString(7, static_cast<std::string*>(NULL)));
1348
1349 EXPECT_FALSE(main_list.GetString(0, static_cast<string16*>(NULL)));
1350 EXPECT_FALSE(main_list.GetString(1, static_cast<string16*>(NULL)));
1351 EXPECT_FALSE(main_list.GetString(2, static_cast<string16*>(NULL)));
1352 EXPECT_TRUE(main_list.GetString(3, static_cast<string16*>(NULL)));
1353 EXPECT_FALSE(main_list.GetString(4, static_cast<string16*>(NULL)));
1354 EXPECT_FALSE(main_list.GetString(5, static_cast<string16*>(NULL)));
1355 EXPECT_FALSE(main_list.GetString(6, static_cast<string16*>(NULL)));
1356 EXPECT_FALSE(main_list.GetString(7, static_cast<string16*>(NULL)));
1357
1358 EXPECT_FALSE(main_list.GetBinary(0, NULL));
1359 EXPECT_FALSE(main_list.GetBinary(1, NULL));
1360 EXPECT_FALSE(main_list.GetBinary(2, NULL));
1361 EXPECT_FALSE(main_list.GetBinary(3, NULL));
1362 EXPECT_TRUE(main_list.GetBinary(4, NULL));
1363 EXPECT_FALSE(main_list.GetBinary(5, NULL));
1364 EXPECT_FALSE(main_list.GetBinary(6, NULL));
1365 EXPECT_FALSE(main_list.GetBinary(7, NULL));
1366
1367 EXPECT_FALSE(main_list.GetDictionary(0, NULL));
1368 EXPECT_FALSE(main_list.GetDictionary(1, NULL));
1369 EXPECT_FALSE(main_list.GetDictionary(2, NULL));
1370 EXPECT_FALSE(main_list.GetDictionary(3, NULL));
1371 EXPECT_FALSE(main_list.GetDictionary(4, NULL));
1372 EXPECT_TRUE(main_list.GetDictionary(5, NULL));
1373 EXPECT_FALSE(main_list.GetDictionary(6, NULL));
1374 EXPECT_FALSE(main_list.GetDictionary(7, NULL));
1375
1376 EXPECT_FALSE(main_list.GetList(0, NULL));
1377 EXPECT_FALSE(main_list.GetList(1, NULL));
1378 EXPECT_FALSE(main_list.GetList(2, NULL));
1379 EXPECT_FALSE(main_list.GetList(3, NULL));
1380 EXPECT_FALSE(main_list.GetList(4, NULL));
1381 EXPECT_FALSE(main_list.GetList(5, NULL));
1382 EXPECT_TRUE(main_list.GetList(6, NULL));
1383 EXPECT_FALSE(main_list.GetList(7, NULL));
1384}
1385
[email protected]58b916e2011-06-28 22:56:331386} // namespace base