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

Side by Side Diff: base/values_unittest.cc

Issue 1141793003: Update from https://crrev.com/329939 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/values.cc ('k') | base/win/memory_pressure_monitor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits> 5 #include <limits>
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 12
13 namespace base { 13 namespace base {
14 14
15 TEST(ValuesTest, Basic) { 15 TEST(ValuesTest, Basic) {
16 // Test basic dictionary getting/setting 16 // Test basic dictionary getting/setting
17 DictionaryValue settings; 17 DictionaryValue settings;
18 std::string homepage = "http://google.com"; 18 std::string homepage = "http://google.com";
19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage)); 19 ASSERT_FALSE(settings.GetString("global.homepage", &homepage));
20 ASSERT_EQ(std::string("http://google.com"), homepage); 20 ASSERT_EQ(std::string("http://google.com"), homepage);
21 21
22 ASSERT_FALSE(settings.Get("global", NULL)); 22 ASSERT_FALSE(settings.Get("global", NULL));
23 settings.Set("global", new FundamentalValue(true)); 23 settings.SetBoolean("global", true);
24 ASSERT_TRUE(settings.Get("global", NULL)); 24 ASSERT_TRUE(settings.Get("global", NULL));
25 settings.SetString("global.homepage", "http://scurvy.com"); 25 settings.SetString("global.homepage", "http://scurvy.com");
26 ASSERT_TRUE(settings.Get("global", NULL)); 26 ASSERT_TRUE(settings.Get("global", NULL));
27 homepage = "http://google.com"; 27 homepage = "http://google.com";
28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage)); 28 ASSERT_TRUE(settings.GetString("global.homepage", &homepage));
29 ASSERT_EQ(std::string("http://scurvy.com"), homepage); 29 ASSERT_EQ(std::string("http://scurvy.com"), homepage);
30 30
31 // Test storing a dictionary in a list. 31 // Test storing a dictionary in a list.
32 ListValue* toolbar_bookmarks; 32 ListValue* toolbar_bookmarks;
33 ASSERT_FALSE( 33 ASSERT_FALSE(
34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks)); 34 settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
35 35
36 toolbar_bookmarks = new ListValue; 36 scoped_ptr<ListValue> new_toolbar_bookmarks(new ListValue);
37 settings.Set("global.toolbar.bookmarks", toolbar_bookmarks); 37 settings.Set("global.toolbar.bookmarks", new_toolbar_bookmarks.Pass());
38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks)); 38 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &toolbar_bookmarks));
39 39
40 DictionaryValue* new_bookmark = new DictionaryValue; 40 scoped_ptr<DictionaryValue> new_bookmark(new DictionaryValue);
41 new_bookmark->SetString("name", "Froogle"); 41 new_bookmark->SetString("name", "Froogle");
42 new_bookmark->SetString("url", "http://froogle.com"); 42 new_bookmark->SetString("url", "http://froogle.com");
43 toolbar_bookmarks->Append(new_bookmark); 43 toolbar_bookmarks->Append(new_bookmark.Pass());
44 44
45 ListValue* bookmark_list; 45 ListValue* bookmark_list;
46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list)); 46 ASSERT_TRUE(settings.GetList("global.toolbar.bookmarks", &bookmark_list));
47 DictionaryValue* bookmark; 47 DictionaryValue* bookmark;
48 ASSERT_EQ(1U, bookmark_list->GetSize()); 48 ASSERT_EQ(1U, bookmark_list->GetSize());
49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark)); 49 ASSERT_TRUE(bookmark_list->GetDictionary(0, &bookmark));
50 std::string bookmark_name = "Unnamed"; 50 std::string bookmark_name = "Unnamed";
51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name)); 51 ASSERT_TRUE(bookmark->GetString("name", &bookmark_name));
52 ASSERT_EQ(std::string("Froogle"), bookmark_name); 52 ASSERT_EQ(std::string("Froogle"), bookmark_name);
53 std::string bookmark_url; 53 std::string bookmark_url;
54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url)); 54 ASSERT_TRUE(bookmark->GetString("url", &bookmark_url));
55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url); 55 ASSERT_EQ(std::string("http://froogle.com"), bookmark_url);
56 } 56 }
57 57
58 TEST(ValuesTest, List) { 58 TEST(ValuesTest, List) {
59 scoped_ptr<ListValue> mixed_list(new ListValue()); 59 scoped_ptr<ListValue> mixed_list(new ListValue());
60 mixed_list->Set(0, new FundamentalValue(true)); 60 mixed_list->Set(0, make_scoped_ptr(new FundamentalValue(true)));
61 mixed_list->Set(1, new FundamentalValue(42)); 61 mixed_list->Set(1, make_scoped_ptr(new FundamentalValue(42)));
62 mixed_list->Set(2, new FundamentalValue(88.8)); 62 mixed_list->Set(2, make_scoped_ptr(new FundamentalValue(88.8)));
63 mixed_list->Set(3, new StringValue("foo")); 63 mixed_list->Set(3, make_scoped_ptr(new StringValue("foo")));
64 ASSERT_EQ(4u, mixed_list->GetSize()); 64 ASSERT_EQ(4u, mixed_list->GetSize());
65 65
66 Value *value = NULL; 66 Value *value = NULL;
67 bool bool_value = false; 67 bool bool_value = false;
68 int int_value = 0; 68 int int_value = 0;
69 double double_value = 0.0; 69 double double_value = 0.0;
70 std::string string_value; 70 std::string string_value;
71 71
72 ASSERT_FALSE(mixed_list->Get(4, &value)); 72 ASSERT_FALSE(mixed_list->Get(4, &value));
73 73
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 } 105 }
106 106
107 TEST(ValuesTest, BinaryValue) { 107 TEST(ValuesTest, BinaryValue) {
108 // Default constructor creates a BinaryValue with a null buffer and size 0. 108 // Default constructor creates a BinaryValue with a null buffer and size 0.
109 scoped_ptr<BinaryValue> binary(new BinaryValue()); 109 scoped_ptr<BinaryValue> binary(new BinaryValue());
110 ASSERT_TRUE(binary.get()); 110 ASSERT_TRUE(binary.get());
111 ASSERT_EQ(NULL, binary->GetBuffer()); 111 ASSERT_EQ(NULL, binary->GetBuffer());
112 ASSERT_EQ(0U, binary->GetSize()); 112 ASSERT_EQ(0U, binary->GetSize());
113 113
114 // Test the common case of a non-empty buffer 114 // Test the common case of a non-empty buffer
115 char* buffer = new char[15]; 115 scoped_ptr<char[]> buffer(new char[15]);
116 binary.reset(new BinaryValue(scoped_ptr<char[]>(buffer), 15)); 116 char* original_buffer = buffer.get();
117 binary.reset(new BinaryValue(buffer.Pass(), 15));
117 ASSERT_TRUE(binary.get()); 118 ASSERT_TRUE(binary.get());
118 ASSERT_TRUE(binary->GetBuffer()); 119 ASSERT_TRUE(binary->GetBuffer());
119 ASSERT_EQ(buffer, binary->GetBuffer()); 120 ASSERT_EQ(original_buffer, binary->GetBuffer());
120 ASSERT_EQ(15U, binary->GetSize()); 121 ASSERT_EQ(15U, binary->GetSize());
121 122
122 char stack_buffer[42]; 123 char stack_buffer[42];
123 memset(stack_buffer, '!', 42); 124 memset(stack_buffer, '!', 42);
124 binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42)); 125 binary.reset(BinaryValue::CreateWithCopiedBuffer(stack_buffer, 42));
125 ASSERT_TRUE(binary.get()); 126 ASSERT_TRUE(binary.get());
126 ASSERT_TRUE(binary->GetBuffer()); 127 ASSERT_TRUE(binary->GetBuffer());
127 ASSERT_NE(stack_buffer, binary->GetBuffer()); 128 ASSERT_NE(stack_buffer, binary->GetBuffer());
128 ASSERT_EQ(42U, binary->GetSize()); 129 ASSERT_EQ(42U, binary->GetSize());
129 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize())); 130 ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBuffer(), binary->GetSize()));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 188
188 private: 189 private:
189 bool* deletion_flag_; 190 bool* deletion_flag_;
190 }; 191 };
191 192
192 TEST(ValuesTest, ListDeletion) { 193 TEST(ValuesTest, ListDeletion) {
193 bool deletion_flag = true; 194 bool deletion_flag = true;
194 195
195 { 196 {
196 ListValue list; 197 ListValue list;
197 list.Append(new DeletionTestValue(&deletion_flag)); 198 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
198 EXPECT_FALSE(deletion_flag); 199 EXPECT_FALSE(deletion_flag);
199 } 200 }
200 EXPECT_TRUE(deletion_flag); 201 EXPECT_TRUE(deletion_flag);
201 202
202 { 203 {
203 ListValue list; 204 ListValue list;
204 list.Append(new DeletionTestValue(&deletion_flag)); 205 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
205 EXPECT_FALSE(deletion_flag); 206 EXPECT_FALSE(deletion_flag);
206 list.Clear(); 207 list.Clear();
207 EXPECT_TRUE(deletion_flag); 208 EXPECT_TRUE(deletion_flag);
208 } 209 }
209 210
210 { 211 {
211 ListValue list; 212 ListValue list;
212 list.Append(new DeletionTestValue(&deletion_flag)); 213 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
213 EXPECT_FALSE(deletion_flag); 214 EXPECT_FALSE(deletion_flag);
214 EXPECT_TRUE(list.Set(0, Value::CreateNullValue())); 215 EXPECT_TRUE(list.Set(0, Value::CreateNullValue()));
215 EXPECT_TRUE(deletion_flag); 216 EXPECT_TRUE(deletion_flag);
216 } 217 }
217 } 218 }
218 219
219 TEST(ValuesTest, ListRemoval) { 220 TEST(ValuesTest, ListRemoval) {
220 bool deletion_flag = true; 221 bool deletion_flag = true;
221 scoped_ptr<Value> removed_item; 222 scoped_ptr<Value> removed_item;
222 223
223 { 224 {
224 ListValue list; 225 ListValue list;
225 list.Append(new DeletionTestValue(&deletion_flag)); 226 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
226 EXPECT_FALSE(deletion_flag); 227 EXPECT_FALSE(deletion_flag);
227 EXPECT_EQ(1U, list.GetSize()); 228 EXPECT_EQ(1U, list.GetSize());
228 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(), 229 EXPECT_FALSE(list.Remove(std::numeric_limits<size_t>::max(),
229 &removed_item)); 230 &removed_item));
230 EXPECT_FALSE(list.Remove(1, &removed_item)); 231 EXPECT_FALSE(list.Remove(1, &removed_item));
231 EXPECT_TRUE(list.Remove(0, &removed_item)); 232 EXPECT_TRUE(list.Remove(0, &removed_item));
232 ASSERT_TRUE(removed_item); 233 ASSERT_TRUE(removed_item);
233 EXPECT_EQ(0U, list.GetSize()); 234 EXPECT_EQ(0U, list.GetSize());
234 } 235 }
235 EXPECT_FALSE(deletion_flag); 236 EXPECT_FALSE(deletion_flag);
236 removed_item.reset(); 237 removed_item.reset();
237 EXPECT_TRUE(deletion_flag); 238 EXPECT_TRUE(deletion_flag);
238 239
239 { 240 {
240 ListValue list; 241 ListValue list;
241 list.Append(new DeletionTestValue(&deletion_flag)); 242 list.Append(make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
242 EXPECT_FALSE(deletion_flag); 243 EXPECT_FALSE(deletion_flag);
243 EXPECT_TRUE(list.Remove(0, NULL)); 244 EXPECT_TRUE(list.Remove(0, NULL));
244 EXPECT_TRUE(deletion_flag); 245 EXPECT_TRUE(deletion_flag);
245 EXPECT_EQ(0U, list.GetSize()); 246 EXPECT_EQ(0U, list.GetSize());
246 } 247 }
247 248
248 { 249 {
249 ListValue list; 250 ListValue list;
250 DeletionTestValue* value = new DeletionTestValue(&deletion_flag); 251 scoped_ptr<DeletionTestValue> value(new DeletionTestValue(&deletion_flag));
251 list.Append(value); 252 DeletionTestValue* original_value = value.get();
253 list.Append(value.Pass());
252 EXPECT_FALSE(deletion_flag); 254 EXPECT_FALSE(deletion_flag);
253 size_t index = 0; 255 size_t index = 0;
254 list.Remove(*value, &index); 256 list.Remove(*original_value, &index);
255 EXPECT_EQ(0U, index); 257 EXPECT_EQ(0U, index);
256 EXPECT_TRUE(deletion_flag); 258 EXPECT_TRUE(deletion_flag);
257 EXPECT_EQ(0U, list.GetSize()); 259 EXPECT_EQ(0U, list.GetSize());
258 } 260 }
259 } 261 }
260 262
261 TEST(ValuesTest, DictionaryDeletion) { 263 TEST(ValuesTest, DictionaryDeletion) {
262 std::string key = "test"; 264 std::string key = "test";
263 bool deletion_flag = true; 265 bool deletion_flag = true;
264 266
265 { 267 {
266 DictionaryValue dict; 268 DictionaryValue dict;
267 dict.Set(key, new DeletionTestValue(&deletion_flag)); 269 dict.Set(key, make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
268 EXPECT_FALSE(deletion_flag); 270 EXPECT_FALSE(deletion_flag);
269 } 271 }
270 EXPECT_TRUE(deletion_flag); 272 EXPECT_TRUE(deletion_flag);
271 273
272 { 274 {
273 DictionaryValue dict; 275 DictionaryValue dict;
274 dict.Set(key, new DeletionTestValue(&deletion_flag)); 276 dict.Set(key, make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
275 EXPECT_FALSE(deletion_flag); 277 EXPECT_FALSE(deletion_flag);
276 dict.Clear(); 278 dict.Clear();
277 EXPECT_TRUE(deletion_flag); 279 EXPECT_TRUE(deletion_flag);
278 } 280 }
279 281
280 { 282 {
281 DictionaryValue dict; 283 DictionaryValue dict;
282 dict.Set(key, new DeletionTestValue(&deletion_flag)); 284 dict.Set(key, make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
283 EXPECT_FALSE(deletion_flag); 285 EXPECT_FALSE(deletion_flag);
284 dict.Set(key, Value::CreateNullValue()); 286 dict.Set(key, Value::CreateNullValue());
285 EXPECT_TRUE(deletion_flag); 287 EXPECT_TRUE(deletion_flag);
286 } 288 }
287 } 289 }
288 290
289 TEST(ValuesTest, DictionaryRemoval) { 291 TEST(ValuesTest, DictionaryRemoval) {
290 std::string key = "test"; 292 std::string key = "test";
291 bool deletion_flag = true; 293 bool deletion_flag = true;
292 scoped_ptr<Value> removed_item; 294 scoped_ptr<Value> removed_item;
293 295
294 { 296 {
295 DictionaryValue dict; 297 DictionaryValue dict;
296 dict.Set(key, new DeletionTestValue(&deletion_flag)); 298 dict.Set(key, make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
297 EXPECT_FALSE(deletion_flag); 299 EXPECT_FALSE(deletion_flag);
298 EXPECT_TRUE(dict.HasKey(key)); 300 EXPECT_TRUE(dict.HasKey(key));
299 EXPECT_FALSE(dict.Remove("absent key", &removed_item)); 301 EXPECT_FALSE(dict.Remove("absent key", &removed_item));
300 EXPECT_TRUE(dict.Remove(key, &removed_item)); 302 EXPECT_TRUE(dict.Remove(key, &removed_item));
301 EXPECT_FALSE(dict.HasKey(key)); 303 EXPECT_FALSE(dict.HasKey(key));
302 ASSERT_TRUE(removed_item); 304 ASSERT_TRUE(removed_item);
303 } 305 }
304 EXPECT_FALSE(deletion_flag); 306 EXPECT_FALSE(deletion_flag);
305 removed_item.reset(); 307 removed_item.reset();
306 EXPECT_TRUE(deletion_flag); 308 EXPECT_TRUE(deletion_flag);
307 309
308 { 310 {
309 DictionaryValue dict; 311 DictionaryValue dict;
310 dict.Set(key, new DeletionTestValue(&deletion_flag)); 312 dict.Set(key, make_scoped_ptr(new DeletionTestValue(&deletion_flag)));
311 EXPECT_FALSE(deletion_flag); 313 EXPECT_FALSE(deletion_flag);
312 EXPECT_TRUE(dict.HasKey(key)); 314 EXPECT_TRUE(dict.HasKey(key));
313 EXPECT_TRUE(dict.Remove(key, NULL)); 315 EXPECT_TRUE(dict.Remove(key, NULL));
314 EXPECT_TRUE(deletion_flag); 316 EXPECT_TRUE(deletion_flag);
315 EXPECT_FALSE(dict.HasKey(key)); 317 EXPECT_FALSE(dict.HasKey(key));
316 } 318 }
317 } 319 }
318 320
319 TEST(ValuesTest, DictionaryWithoutPathExpansion) { 321 TEST(ValuesTest, DictionaryWithoutPathExpansion) {
320 DictionaryValue dict; 322 DictionaryValue dict;
321 dict.Set("this.is.expanded", make_scoped_ptr(Value::CreateNullValue())); 323 dict.Set("this.is.expanded", Value::CreateNullValue());
322 dict.SetWithoutPathExpansion("this.isnt.expanded", 324 dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue());
323 make_scoped_ptr(Value::CreateNullValue()));
324 325
325 EXPECT_FALSE(dict.HasKey("this.is.expanded")); 326 EXPECT_FALSE(dict.HasKey("this.is.expanded"));
326 EXPECT_TRUE(dict.HasKey("this")); 327 EXPECT_TRUE(dict.HasKey("this"));
327 Value* value1; 328 Value* value1;
328 EXPECT_TRUE(dict.Get("this", &value1)); 329 EXPECT_TRUE(dict.Get("this", &value1));
329 DictionaryValue* value2; 330 DictionaryValue* value2;
330 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); 331 ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2));
331 EXPECT_EQ(value1, value2); 332 EXPECT_EQ(value1, value2);
332 EXPECT_EQ(1U, value2->size()); 333 EXPECT_EQ(1U, value2->size());
333 334
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 384
384 removed_item.reset(); 385 removed_item.reset();
385 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item)); 386 EXPECT_TRUE(dict.RemovePath("a.long.key.path", &removed_item));
386 ASSERT_TRUE(removed_item); 387 ASSERT_TRUE(removed_item);
387 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN)); 388 EXPECT_TRUE(removed_item->IsType(base::Value::TYPE_BOOLEAN));
388 EXPECT_TRUE(dict.empty()); 389 EXPECT_TRUE(dict.empty());
389 } 390 }
390 391
391 TEST(ValuesTest, DeepCopy) { 392 TEST(ValuesTest, DeepCopy) {
392 DictionaryValue original_dict; 393 DictionaryValue original_dict;
393 Value* original_null = Value::CreateNullValue(); 394 scoped_ptr<Value> scoped_null = Value::CreateNullValue();
394 original_dict.Set("null", make_scoped_ptr(original_null)); 395 Value* original_null = scoped_null.get();
395 FundamentalValue* original_bool = new FundamentalValue(true); 396 original_dict.Set("null", scoped_null.Pass());
396 original_dict.Set("bool", make_scoped_ptr(original_bool)); 397 scoped_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
397 FundamentalValue* original_int = new FundamentalValue(42); 398 FundamentalValue* original_bool = scoped_bool.get();
398 original_dict.Set("int", make_scoped_ptr(original_int)); 399 original_dict.Set("bool", scoped_bool.Pass());
399 FundamentalValue* original_double = new FundamentalValue(3.14); 400 scoped_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
400 original_dict.Set("double", make_scoped_ptr(original_double)); 401 FundamentalValue* original_int = scoped_int.get();
401 StringValue* original_string = new StringValue("hello"); 402 original_dict.Set("int", scoped_int.Pass());
402 original_dict.Set("string", make_scoped_ptr(original_string)); 403 scoped_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
403 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 404 FundamentalValue* original_double = scoped_double.get();
404 original_dict.Set("string16", make_scoped_ptr(original_string16)); 405 original_dict.Set("double", scoped_double.Pass());
406 scoped_ptr<StringValue> scoped_string(new StringValue("hello"));
407 StringValue* original_string = scoped_string.get();
408 original_dict.Set("string", scoped_string.Pass());
409 scoped_ptr<StringValue> scoped_string16(
410 new StringValue(ASCIIToUTF16("hello16")));
411 StringValue* original_string16 = scoped_string16.get();
412 original_dict.Set("string16", scoped_string16.Pass());
405 413
406 scoped_ptr<char[]> original_buffer(new char[42]); 414 scoped_ptr<char[]> original_buffer(new char[42]);
407 memset(original_buffer.get(), '!', 42); 415 memset(original_buffer.get(), '!', 42);
408 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42); 416 scoped_ptr<BinaryValue> scoped_binary(
409 original_dict.Set("binary", original_binary); 417 new BinaryValue(original_buffer.Pass(), 42));
418 BinaryValue* original_binary = scoped_binary.get();
419 original_dict.Set("binary", scoped_binary.Pass());
410 420
411 ListValue* original_list = new ListValue(); 421 scoped_ptr<ListValue> scoped_list(new ListValue());
412 FundamentalValue* original_list_element_0 = new FundamentalValue(0); 422 Value* original_list = scoped_list.get();
413 original_list->Append(original_list_element_0); 423 scoped_ptr<FundamentalValue> scoped_list_element_0(new FundamentalValue(0));
414 FundamentalValue* original_list_element_1 = new FundamentalValue(1); 424 Value* original_list_element_0 = scoped_list_element_0.get();
415 original_list->Append(original_list_element_1); 425 scoped_list->Append(scoped_list_element_0.Pass());
416 original_dict.Set("list", make_scoped_ptr(original_list)); 426 scoped_ptr<FundamentalValue> scoped_list_element_1(new FundamentalValue(1));
427 Value* original_list_element_1 = scoped_list_element_1.get();
428 scoped_list->Append(scoped_list_element_1.Pass());
429 original_dict.Set("list", scoped_list.Pass());
417 430
418 DictionaryValue* original_nested_dictionary = new DictionaryValue(); 431 scoped_ptr<DictionaryValue> scoped_nested_dictionary(new DictionaryValue());
419 original_nested_dictionary->SetString("key", "value"); 432 Value* original_nested_dictionary = scoped_nested_dictionary.get();
420 original_dict.Set("dictionary", make_scoped_ptr(original_nested_dictionary)); 433 scoped_nested_dictionary->SetString("key", "value");
434 original_dict.Set("dictionary", scoped_nested_dictionary.Pass());
421 435
422 scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy()); 436 scoped_ptr<DictionaryValue> copy_dict = original_dict.CreateDeepCopy();
423 ASSERT_TRUE(copy_dict.get()); 437 ASSERT_TRUE(copy_dict.get());
424 ASSERT_NE(copy_dict.get(), &original_dict); 438 ASSERT_NE(copy_dict.get(), &original_dict);
425 439
426 Value* copy_null = NULL; 440 Value* copy_null = NULL;
427 ASSERT_TRUE(copy_dict->Get("null", &copy_null)); 441 ASSERT_TRUE(copy_dict->Get("null", &copy_null));
428 ASSERT_TRUE(copy_null); 442 ASSERT_TRUE(copy_null);
429 ASSERT_NE(copy_null, original_null); 443 ASSERT_NE(copy_null, original_null);
430 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL)); 444 ASSERT_TRUE(copy_null->IsType(Value::TYPE_NULL));
431 445
432 Value* copy_bool = NULL; 446 Value* copy_bool = NULL;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 ASSERT_TRUE(copy_value); 536 ASSERT_TRUE(copy_value);
523 ASSERT_NE(copy_value, original_nested_dictionary); 537 ASSERT_NE(copy_value, original_nested_dictionary);
524 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY)); 538 ASSERT_TRUE(copy_value->IsType(Value::TYPE_DICTIONARY));
525 DictionaryValue* copy_nested_dictionary = NULL; 539 DictionaryValue* copy_nested_dictionary = NULL;
526 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary)); 540 ASSERT_TRUE(copy_value->GetAsDictionary(&copy_nested_dictionary));
527 ASSERT_TRUE(copy_nested_dictionary); 541 ASSERT_TRUE(copy_nested_dictionary);
528 EXPECT_TRUE(copy_nested_dictionary->HasKey("key")); 542 EXPECT_TRUE(copy_nested_dictionary->HasKey("key"));
529 } 543 }
530 544
531 TEST(ValuesTest, Equals) { 545 TEST(ValuesTest, Equals) {
532 Value* null1 = Value::CreateNullValue(); 546 scoped_ptr<Value> null1(Value::CreateNullValue());
533 Value* null2 = Value::CreateNullValue(); 547 scoped_ptr<Value> null2(Value::CreateNullValue());
534 EXPECT_NE(null1, null2); 548 EXPECT_NE(null1.get(), null2.get());
535 EXPECT_TRUE(null1->Equals(null2)); 549 EXPECT_TRUE(null1->Equals(null2.get()));
536 550
537 Value* boolean = new FundamentalValue(false); 551 FundamentalValue boolean(false);
538 EXPECT_FALSE(null1->Equals(boolean)); 552 EXPECT_FALSE(null1->Equals(&boolean));
539 delete null1;
540 delete null2;
541 delete boolean;
542 553
543 DictionaryValue dv; 554 DictionaryValue dv;
544 dv.SetBoolean("a", false); 555 dv.SetBoolean("a", false);
545 dv.SetInteger("b", 2); 556 dv.SetInteger("b", 2);
546 dv.SetDouble("c", 2.5); 557 dv.SetDouble("c", 2.5);
547 dv.SetString("d1", "string"); 558 dv.SetString("d1", "string");
548 dv.SetString("d2", ASCIIToUTF16("http://google.com")); 559 dv.SetString("d2", ASCIIToUTF16("http://google.com"));
549 dv.Set("e", make_scoped_ptr(Value::CreateNullValue())); 560 dv.Set("e", Value::CreateNullValue());
550 561
551 scoped_ptr<DictionaryValue> copy; 562 scoped_ptr<DictionaryValue> copy = dv.CreateDeepCopy();
552 copy.reset(dv.DeepCopy());
553 EXPECT_TRUE(dv.Equals(copy.get())); 563 EXPECT_TRUE(dv.Equals(copy.get()));
554 564
555 ListValue* list = new ListValue; 565 scoped_ptr<ListValue> list(new ListValue);
566 ListValue* original_list = list.get();
556 list->Append(Value::CreateNullValue()); 567 list->Append(Value::CreateNullValue());
557 list->Append(new DictionaryValue); 568 list->Append(make_scoped_ptr(new DictionaryValue));
558 dv.Set("f", make_scoped_ptr(list)); 569 scoped_ptr<Value> list_copy(list->CreateDeepCopy());
559 570
571 dv.Set("f", list.Pass());
560 EXPECT_FALSE(dv.Equals(copy.get())); 572 EXPECT_FALSE(dv.Equals(copy.get()));
561 copy->Set("f", list->DeepCopy()); 573 copy->Set("f", list_copy.Pass());
562 EXPECT_TRUE(dv.Equals(copy.get())); 574 EXPECT_TRUE(dv.Equals(copy.get()));
563 575
564 list->Append(new FundamentalValue(true)); 576 original_list->Append(make_scoped_ptr(new FundamentalValue(true)));
565 EXPECT_FALSE(dv.Equals(copy.get())); 577 EXPECT_FALSE(dv.Equals(copy.get()));
566 578
567 // Check if Equals detects differences in only the keys. 579 // Check if Equals detects differences in only the keys.
568 copy.reset(dv.DeepCopy()); 580 copy = dv.CreateDeepCopy();
569 EXPECT_TRUE(dv.Equals(copy.get())); 581 EXPECT_TRUE(dv.Equals(copy.get()));
570 copy->Remove("a", NULL); 582 copy->Remove("a", NULL);
571 copy->SetBoolean("aa", false); 583 copy->SetBoolean("aa", false);
572 EXPECT_FALSE(dv.Equals(copy.get())); 584 EXPECT_FALSE(dv.Equals(copy.get()));
573 } 585 }
574 586
575 TEST(ValuesTest, StaticEquals) { 587 TEST(ValuesTest, StaticEquals) {
576 scoped_ptr<Value> null1(Value::CreateNullValue()); 588 scoped_ptr<Value> null1(Value::CreateNullValue());
577 scoped_ptr<Value> null2(Value::CreateNullValue()); 589 scoped_ptr<Value> null2(Value::CreateNullValue());
578 EXPECT_TRUE(Value::Equals(null1.get(), null2.get())); 590 EXPECT_TRUE(Value::Equals(null1.get(), null2.get()));
(...skipping 11 matching lines...) Expand all
590 602
591 // NULL and Value::CreateNullValue() are intentionally different: We need 603 // NULL and Value::CreateNullValue() are intentionally different: We need
592 // support for NULL as a return value for "undefined" without caring for 604 // support for NULL as a return value for "undefined" without caring for
593 // ownership of the pointer. 605 // ownership of the pointer.
594 EXPECT_FALSE(Value::Equals(null1.get(), NULL)); 606 EXPECT_FALSE(Value::Equals(null1.get(), NULL));
595 EXPECT_FALSE(Value::Equals(NULL, null1.get())); 607 EXPECT_FALSE(Value::Equals(NULL, null1.get()));
596 } 608 }
597 609
598 TEST(ValuesTest, DeepCopyCovariantReturnTypes) { 610 TEST(ValuesTest, DeepCopyCovariantReturnTypes) {
599 DictionaryValue original_dict; 611 DictionaryValue original_dict;
600 Value* original_null = Value::CreateNullValue(); 612 scoped_ptr<Value> scoped_null(Value::CreateNullValue());
601 original_dict.Set("null", original_null); 613 Value* original_null = scoped_null.get();
602 FundamentalValue* original_bool = new FundamentalValue(true); 614 original_dict.Set("null", scoped_null.Pass());
603 original_dict.Set("bool", original_bool); 615 scoped_ptr<FundamentalValue> scoped_bool(new FundamentalValue(true));
604 FundamentalValue* original_int = new FundamentalValue(42); 616 Value* original_bool = scoped_bool.get();
605 original_dict.Set("int", original_int); 617 original_dict.Set("bool", scoped_bool.Pass());
606 FundamentalValue* original_double = new FundamentalValue(3.14); 618 scoped_ptr<FundamentalValue> scoped_int(new FundamentalValue(42));
607 original_dict.Set("double", original_double); 619 Value* original_int = scoped_int.get();
608 StringValue* original_string = new StringValue("hello"); 620 original_dict.Set("int", scoped_int.Pass());
609 original_dict.Set("string", original_string); 621 scoped_ptr<FundamentalValue> scoped_double(new FundamentalValue(3.14));
610 StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); 622 Value* original_double = scoped_double.get();
611 original_dict.Set("string16", original_string16); 623 original_dict.Set("double", scoped_double.Pass());
624 scoped_ptr<StringValue> scoped_string(new StringValue("hello"));
625 Value* original_string = scoped_string.get();
626 original_dict.Set("string", scoped_string.Pass());
627 scoped_ptr<StringValue> scoped_string16(
628 new StringValue(ASCIIToUTF16("hello16")));
629 Value* original_string16 = scoped_string16.get();
630 original_dict.Set("string16", scoped_string16.Pass());
612 631
613 scoped_ptr<char[]> original_buffer(new char[42]); 632 scoped_ptr<char[]> original_buffer(new char[42]);
614 memset(original_buffer.get(), '!', 42); 633 memset(original_buffer.get(), '!', 42);
615 BinaryValue* original_binary = new BinaryValue(original_buffer.Pass(), 42); 634 scoped_ptr<BinaryValue> scoped_binary(
616 original_dict.Set("binary", original_binary); 635 new BinaryValue(original_buffer.Pass(), 42));
636 Value* original_binary = scoped_binary.get();
637 original_dict.Set("binary", scoped_binary.Pass());
617 638
618 ListValue* original_list = new ListValue(); 639 scoped_ptr<ListValue> scoped_list(new ListValue());
619 FundamentalValue* original_list_element_0 = new FundamentalValue(0); 640 Value* original_list = scoped_list.get();
620 original_list->Append(original_list_element_0); 641 scoped_ptr<FundamentalValue> scoped_list_element_0(new FundamentalValue(0));
621 FundamentalValue* original_list_element_1 = new FundamentalValue(1); 642 scoped_list->Append(scoped_list_element_0.Pass());
622 original_list->Append(original_list_element_1); 643 scoped_ptr<FundamentalValue> scoped_list_element_1(new FundamentalValue(1));
623 original_dict.Set("list", original_list); 644 scoped_list->Append(scoped_list_element_1.Pass());
645 original_dict.Set("list", scoped_list.Pass());
624 646
625 Value* original_dict_value = &original_dict; 647 scoped_ptr<Value> copy_dict = original_dict.CreateDeepCopy();
626 Value* original_bool_value = original_bool; 648 scoped_ptr<Value> copy_null = original_null->CreateDeepCopy();
627 Value* original_int_value = original_int; 649 scoped_ptr<Value> copy_bool = original_bool->CreateDeepCopy();
628 Value* original_double_value = original_double; 650 scoped_ptr<Value> copy_int = original_int->CreateDeepCopy();
629 Value* original_string_value = original_string; 651 scoped_ptr<Value> copy_double = original_double->CreateDeepCopy();
630 Value* original_string16_value = original_string16; 652 scoped_ptr<Value> copy_string = original_string->CreateDeepCopy();
631 Value* original_binary_value = original_binary; 653 scoped_ptr<Value> copy_string16 = original_string16->CreateDeepCopy();
632 Value* original_list_value = original_list; 654 scoped_ptr<Value> copy_binary = original_binary->CreateDeepCopy();
655 scoped_ptr<Value> copy_list = original_list->CreateDeepCopy();
633 656
634 scoped_ptr<Value> copy_dict_value(original_dict_value->DeepCopy()); 657 EXPECT_TRUE(original_dict.Equals(copy_dict.get()));
635 scoped_ptr<Value> copy_bool_value(original_bool_value->DeepCopy()); 658 EXPECT_TRUE(original_null->Equals(copy_null.get()));
636 scoped_ptr<Value> copy_int_value(original_int_value->DeepCopy()); 659 EXPECT_TRUE(original_bool->Equals(copy_bool.get()));
637 scoped_ptr<Value> copy_double_value(original_double_value->DeepCopy()); 660 EXPECT_TRUE(original_int->Equals(copy_int.get()));
638 scoped_ptr<Value> copy_string_value(original_string_value->DeepCopy()); 661 EXPECT_TRUE(original_double->Equals(copy_double.get()));
639 scoped_ptr<Value> copy_string16_value(original_string16_value->DeepCopy()); 662 EXPECT_TRUE(original_string->Equals(copy_string.get()));
640 scoped_ptr<Value> copy_binary_value(original_binary_value->DeepCopy()); 663 EXPECT_TRUE(original_string16->Equals(copy_string16.get()));
641 scoped_ptr<Value> copy_list_value(original_list_value->DeepCopy()); 664 EXPECT_TRUE(original_binary->Equals(copy_binary.get()));
642 665 EXPECT_TRUE(original_list->Equals(copy_list.get()));
643 EXPECT_TRUE(original_dict_value->Equals(copy_dict_value.get()));
644 EXPECT_TRUE(original_bool_value->Equals(copy_bool_value.get()));
645 EXPECT_TRUE(original_int_value->Equals(copy_int_value.get()));
646 EXPECT_TRUE(original_double_value->Equals(copy_double_value.get()));
647 EXPECT_TRUE(original_string_value->Equals(copy_string_value.get()));
648 EXPECT_TRUE(original_string16_value->Equals(copy_string16_value.get()));
649 EXPECT_TRUE(original_binary_value->Equals(copy_binary_value.get()));
650 EXPECT_TRUE(original_list_value->Equals(copy_list_value.get()));
651 } 666 }
652 667
653 TEST(ValuesTest, RemoveEmptyChildren) { 668 TEST(ValuesTest, RemoveEmptyChildren) {
654 scoped_ptr<DictionaryValue> root(new DictionaryValue); 669 scoped_ptr<DictionaryValue> root(new DictionaryValue);
655 // Remove empty lists and dictionaries. 670 // Remove empty lists and dictionaries.
656 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue)); 671 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
657 root->Set("empty_list", make_scoped_ptr(new ListValue)); 672 root->Set("empty_list", make_scoped_ptr(new ListValue));
658 root->SetWithoutPathExpansion("a.b.c.d.e", 673 root->SetWithoutPathExpansion("a.b.c.d.e",
659 make_scoped_ptr(new DictionaryValue)); 674 make_scoped_ptr(new DictionaryValue));
660 root.reset(root->DeepCopyWithoutEmptyChildren()); 675 root.reset(root->DeepCopyWithoutEmptyChildren());
661 EXPECT_TRUE(root->empty()); 676 EXPECT_TRUE(root->empty());
662 677
663 // Make sure we don't prune too much. 678 // Make sure we don't prune too much.
664 root->SetBoolean("bool", true); 679 root->SetBoolean("bool", true);
665 root->Set("empty_dict", new DictionaryValue); 680 root->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
666 root->SetString("empty_string", std::string()); 681 root->SetString("empty_string", std::string());
667 root.reset(root->DeepCopyWithoutEmptyChildren()); 682 root.reset(root->DeepCopyWithoutEmptyChildren());
668 EXPECT_EQ(2U, root->size()); 683 EXPECT_EQ(2U, root->size());
669 684
670 // Should do nothing. 685 // Should do nothing.
671 root.reset(root->DeepCopyWithoutEmptyChildren()); 686 root.reset(root->DeepCopyWithoutEmptyChildren());
672 EXPECT_EQ(2U, root->size()); 687 EXPECT_EQ(2U, root->size());
673 688
674 // Nested test cases. These should all reduce back to the bool and string 689 // Nested test cases. These should all reduce back to the bool and string
675 // set above. 690 // set above.
676 { 691 {
677 root->Set("a.b.c.d.e", new DictionaryValue); 692 root->Set("a.b.c.d.e", make_scoped_ptr(new DictionaryValue));
678 root.reset(root->DeepCopyWithoutEmptyChildren()); 693 root.reset(root->DeepCopyWithoutEmptyChildren());
679 EXPECT_EQ(2U, root->size()); 694 EXPECT_EQ(2U, root->size());
680 } 695 }
681 { 696 {
682 DictionaryValue* inner = new DictionaryValue; 697 scoped_ptr<DictionaryValue> inner(new DictionaryValue);
683 root->Set("dict_with_emtpy_children", inner); 698 inner->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
684 inner->Set("empty_dict", new DictionaryValue); 699 inner->Set("empty_list", make_scoped_ptr(new ListValue));
685 inner->Set("empty_list", new ListValue); 700 root->Set("dict_with_empty_children", inner.Pass());
686 root.reset(root->DeepCopyWithoutEmptyChildren()); 701 root.reset(root->DeepCopyWithoutEmptyChildren());
687 EXPECT_EQ(2U, root->size()); 702 EXPECT_EQ(2U, root->size());
688 } 703 }
689 { 704 {
690 ListValue* inner = new ListValue; 705 scoped_ptr<ListValue> inner(new ListValue);
691 root->Set("list_with_empty_children", inner); 706 inner->Append(make_scoped_ptr(new DictionaryValue));
692 inner->Append(new DictionaryValue); 707 inner->Append(make_scoped_ptr(new ListValue));
693 inner->Append(new ListValue); 708 root->Set("list_with_empty_children", inner.Pass());
694 root.reset(root->DeepCopyWithoutEmptyChildren()); 709 root.reset(root->DeepCopyWithoutEmptyChildren());
695 EXPECT_EQ(2U, root->size()); 710 EXPECT_EQ(2U, root->size());
696 } 711 }
697 712
698 // Nested with siblings. 713 // Nested with siblings.
699 { 714 {
700 ListValue* inner = new ListValue; 715 scoped_ptr<ListValue> inner(new ListValue());
701 root->Set("list_with_empty_children", inner); 716 inner->Append(make_scoped_ptr(new DictionaryValue));
702 inner->Append(new DictionaryValue); 717 inner->Append(make_scoped_ptr(new ListValue));
703 inner->Append(new ListValue); 718 root->Set("list_with_empty_children", inner.Pass());
704 DictionaryValue* inner2 = new DictionaryValue; 719 scoped_ptr<DictionaryValue> inner2(new DictionaryValue);
705 root->Set("dict_with_empty_children", inner2); 720 inner2->Set("empty_dict", make_scoped_ptr(new DictionaryValue));
706 inner2->Set("empty_dict", new DictionaryValue); 721 inner2->Set("empty_list", make_scoped_ptr(new ListValue));
707 inner2->Set("empty_list", new ListValue); 722 root->Set("dict_with_empty_children", inner2.Pass());
708 root.reset(root->DeepCopyWithoutEmptyChildren()); 723 root.reset(root->DeepCopyWithoutEmptyChildren());
709 EXPECT_EQ(2U, root->size()); 724 EXPECT_EQ(2U, root->size());
710 } 725 }
711 726
712 // Make sure nested values don't get pruned. 727 // Make sure nested values don't get pruned.
713 { 728 {
714 ListValue* inner = new ListValue; 729 scoped_ptr<ListValue> inner(new ListValue);
715 root->Set("list_with_empty_children", inner); 730 scoped_ptr<ListValue> inner2(new ListValue);
716 ListValue* inner2 = new ListValue; 731 inner2->Append(make_scoped_ptr(new StringValue("hello")));
717 inner->Append(new DictionaryValue); 732 inner->Append(make_scoped_ptr(new DictionaryValue));
718 inner->Append(inner2); 733 inner->Append(inner2.Pass());
719 inner2->Append(new StringValue("hello")); 734 root->Set("list_with_empty_children", inner.Pass());
720 root.reset(root->DeepCopyWithoutEmptyChildren()); 735 root.reset(root->DeepCopyWithoutEmptyChildren());
721 EXPECT_EQ(3U, root->size()); 736 EXPECT_EQ(3U, root->size());
722 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner)); 737
723 EXPECT_EQ(1U, inner->GetSize()); // Dictionary was pruned. 738 ListValue* inner_value, *inner_value2;
724 EXPECT_TRUE(inner->GetList(0, &inner2)); 739 EXPECT_TRUE(root->GetList("list_with_empty_children", &inner_value));
725 EXPECT_EQ(1U, inner2->GetSize()); 740 EXPECT_EQ(1U, inner_value->GetSize()); // Dictionary was pruned.
741 EXPECT_TRUE(inner_value->GetList(0, &inner_value2));
742 EXPECT_EQ(1U, inner_value2->GetSize());
726 } 743 }
727 } 744 }
728 745
729 TEST(ValuesTest, MergeDictionary) { 746 TEST(ValuesTest, MergeDictionary) {
730 scoped_ptr<DictionaryValue> base(new DictionaryValue); 747 scoped_ptr<DictionaryValue> base(new DictionaryValue);
731 base->SetString("base_key", "base_key_value_base"); 748 base->SetString("base_key", "base_key_value_base");
732 base->SetString("collide_key", "collide_key_value_base"); 749 base->SetString("collide_key", "collide_key_value_base");
733 DictionaryValue* base_sub_dict = new DictionaryValue; 750 scoped_ptr<DictionaryValue> base_sub_dict(new DictionaryValue);
734 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base"); 751 base_sub_dict->SetString("sub_base_key", "sub_base_key_value_base");
735 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base"); 752 base_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_base");
736 base->Set("sub_dict_key", base_sub_dict); 753 base->Set("sub_dict_key", base_sub_dict.Pass());
737 754
738 scoped_ptr<DictionaryValue> merge(new DictionaryValue); 755 scoped_ptr<DictionaryValue> merge(new DictionaryValue);
739 merge->SetString("merge_key", "merge_key_value_merge"); 756 merge->SetString("merge_key", "merge_key_value_merge");
740 merge->SetString("collide_key", "collide_key_value_merge"); 757 merge->SetString("collide_key", "collide_key_value_merge");
741 DictionaryValue* merge_sub_dict = new DictionaryValue; 758 scoped_ptr<DictionaryValue> merge_sub_dict(new DictionaryValue);
742 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge"); 759 merge_sub_dict->SetString("sub_merge_key", "sub_merge_key_value_merge");
743 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge"); 760 merge_sub_dict->SetString("sub_collide_key", "sub_collide_key_value_merge");
744 merge->Set("sub_dict_key", merge_sub_dict); 761 merge->Set("sub_dict_key", merge_sub_dict.Pass());
745 762
746 base->MergeDictionary(merge.get()); 763 base->MergeDictionary(merge.get());
747 764
748 EXPECT_EQ(4U, base->size()); 765 EXPECT_EQ(4U, base->size());
749 std::string base_key_value; 766 std::string base_key_value;
750 EXPECT_TRUE(base->GetString("base_key", &base_key_value)); 767 EXPECT_TRUE(base->GetString("base_key", &base_key_value));
751 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved. 768 EXPECT_EQ("base_key_value_base", base_key_value); // Base value preserved.
752 std::string collide_key_value; 769 std::string collide_key_value;
753 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value)); 770 EXPECT_TRUE(base->GetString("collide_key", &collide_key_value));
754 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced. 771 EXPECT_EQ("collide_key_value_merge", collide_key_value); // Replaced.
(...skipping 10 matching lines...) Expand all
765 std::string sub_collide_key_value; 782 std::string sub_collide_key_value;
766 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key", 783 EXPECT_TRUE(res_sub_dict->GetString("sub_collide_key",
767 &sub_collide_key_value)); 784 &sub_collide_key_value));
768 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced. 785 EXPECT_EQ("sub_collide_key_value_merge", sub_collide_key_value); // Replaced.
769 std::string sub_merge_key_value; 786 std::string sub_merge_key_value;
770 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value)); 787 EXPECT_TRUE(res_sub_dict->GetString("sub_merge_key", &sub_merge_key_value));
771 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in. 788 EXPECT_EQ("sub_merge_key_value_merge", sub_merge_key_value); // Merged in.
772 } 789 }
773 790
774 TEST(ValuesTest, MergeDictionaryDeepCopy) { 791 TEST(ValuesTest, MergeDictionaryDeepCopy) {
775 DictionaryValue* child = new DictionaryValue; 792 scoped_ptr<DictionaryValue> child(new DictionaryValue);
793 DictionaryValue* original_child = child.get();
776 child->SetString("test", "value"); 794 child->SetString("test", "value");
777 EXPECT_EQ(1U, child->size()); 795 EXPECT_EQ(1U, child->size());
778 796
779 std::string value; 797 std::string value;
780 EXPECT_TRUE(child->GetString("test", &value)); 798 EXPECT_TRUE(child->GetString("test", &value));
781 EXPECT_EQ("value", value); 799 EXPECT_EQ("value", value);
782 800
783 scoped_ptr<DictionaryValue> base(new DictionaryValue); 801 scoped_ptr<DictionaryValue> base(new DictionaryValue);
784 base->Set("dict", child); 802 base->Set("dict", child.Pass());
785 EXPECT_EQ(1U, base->size()); 803 EXPECT_EQ(1U, base->size());
786 804
787 DictionaryValue* ptr; 805 DictionaryValue* ptr;
788 EXPECT_TRUE(base->GetDictionary("dict", &ptr)); 806 EXPECT_TRUE(base->GetDictionary("dict", &ptr));
789 EXPECT_EQ(child, ptr); 807 EXPECT_EQ(original_child, ptr);
790 808
791 scoped_ptr<DictionaryValue> merged(new DictionaryValue); 809 scoped_ptr<DictionaryValue> merged(new DictionaryValue);
792 merged->MergeDictionary(base.get()); 810 merged->MergeDictionary(base.get());
793 EXPECT_EQ(1U, merged->size()); 811 EXPECT_EQ(1U, merged->size());
794 EXPECT_TRUE(merged->GetDictionary("dict", &ptr)); 812 EXPECT_TRUE(merged->GetDictionary("dict", &ptr));
795 EXPECT_NE(child, ptr); 813 EXPECT_NE(original_child, ptr);
796 EXPECT_TRUE(ptr->GetString("test", &value)); 814 EXPECT_TRUE(ptr->GetString("test", &value));
797 EXPECT_EQ("value", value); 815 EXPECT_EQ("value", value);
798 816
799 child->SetString("test", "overwrite"); 817 original_child->SetString("test", "overwrite");
800 base.reset(); 818 base.reset();
801 EXPECT_TRUE(ptr->GetString("test", &value)); 819 EXPECT_TRUE(ptr->GetString("test", &value));
802 EXPECT_EQ("value", value); 820 EXPECT_EQ("value", value);
803 } 821 }
804 822
805 TEST(ValuesTest, DictionaryIterator) { 823 TEST(ValuesTest, DictionaryIterator) {
806 DictionaryValue dict; 824 DictionaryValue dict;
807 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 825 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
808 ADD_FAILURE(); 826 ADD_FAILURE();
809 } 827 }
810 828
811 StringValue value1("value1"); 829 StringValue value1("value1");
812 dict.Set("key1", value1.DeepCopy()); 830 dict.Set("key1", value1.CreateDeepCopy());
813 bool seen1 = false; 831 bool seen1 = false;
814 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 832 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
815 EXPECT_FALSE(seen1); 833 EXPECT_FALSE(seen1);
816 EXPECT_EQ("key1", it.key()); 834 EXPECT_EQ("key1", it.key());
817 EXPECT_TRUE(value1.Equals(&it.value())); 835 EXPECT_TRUE(value1.Equals(&it.value()));
818 seen1 = true; 836 seen1 = true;
819 } 837 }
820 EXPECT_TRUE(seen1); 838 EXPECT_TRUE(seen1);
821 839
822 StringValue value2("value2"); 840 StringValue value2("value2");
823 dict.Set("key2", value2.DeepCopy()); 841 dict.Set("key2", value2.CreateDeepCopy());
824 bool seen2 = seen1 = false; 842 bool seen2 = seen1 = false;
825 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) { 843 for (DictionaryValue::Iterator it(dict); !it.IsAtEnd(); it.Advance()) {
826 if (it.key() == "key1") { 844 if (it.key() == "key1") {
827 EXPECT_FALSE(seen1); 845 EXPECT_FALSE(seen1);
828 EXPECT_TRUE(value1.Equals(&it.value())); 846 EXPECT_TRUE(value1.Equals(&it.value()));
829 seen1 = true; 847 seen1 = true;
830 } else if (it.key() == "key2") { 848 } else if (it.key() == "key2") {
831 EXPECT_FALSE(seen2); 849 EXPECT_FALSE(seen2);
832 EXPECT_TRUE(value2.Equals(&it.value())); 850 EXPECT_TRUE(value2.Equals(&it.value()));
833 seen2 = true; 851 seen2 = true;
(...skipping 12 matching lines...) Expand all
846 ListValue main_list; 864 ListValue main_list;
847 865
848 FundamentalValue bool_value(false); 866 FundamentalValue bool_value(false);
849 FundamentalValue int_value(1234); 867 FundamentalValue int_value(1234);
850 FundamentalValue double_value(12.34567); 868 FundamentalValue double_value(12.34567);
851 StringValue string_value("foo"); 869 StringValue string_value("foo");
852 BinaryValue binary_value; 870 BinaryValue binary_value;
853 DictionaryValue dict_value; 871 DictionaryValue dict_value;
854 ListValue list_value; 872 ListValue list_value;
855 873
856 main_dict.Set("bool", bool_value.DeepCopy()); 874 main_dict.Set("bool", bool_value.CreateDeepCopy());
857 main_dict.Set("int", int_value.DeepCopy()); 875 main_dict.Set("int", int_value.CreateDeepCopy());
858 main_dict.Set("double", double_value.DeepCopy()); 876 main_dict.Set("double", double_value.CreateDeepCopy());
859 main_dict.Set("string", string_value.DeepCopy()); 877 main_dict.Set("string", string_value.CreateDeepCopy());
860 main_dict.Set("binary", binary_value.DeepCopy()); 878 main_dict.Set("binary", binary_value.CreateDeepCopy());
861 main_dict.Set("dict", dict_value.DeepCopy()); 879 main_dict.Set("dict", dict_value.CreateDeepCopy());
862 main_dict.Set("list", list_value.DeepCopy()); 880 main_dict.Set("list", list_value.CreateDeepCopy());
863 881
864 main_list.Append(bool_value.DeepCopy()); 882 main_list.Append(bool_value.CreateDeepCopy());
865 main_list.Append(int_value.DeepCopy()); 883 main_list.Append(int_value.CreateDeepCopy());
866 main_list.Append(double_value.DeepCopy()); 884 main_list.Append(double_value.CreateDeepCopy());
867 main_list.Append(string_value.DeepCopy()); 885 main_list.Append(string_value.CreateDeepCopy());
868 main_list.Append(binary_value.DeepCopy()); 886 main_list.Append(binary_value.CreateDeepCopy());
869 main_list.Append(dict_value.DeepCopy()); 887 main_list.Append(dict_value.CreateDeepCopy());
870 main_list.Append(list_value.DeepCopy()); 888 main_list.Append(list_value.CreateDeepCopy());
871 889
872 EXPECT_TRUE(main_dict.Get("bool", NULL)); 890 EXPECT_TRUE(main_dict.Get("bool", NULL));
873 EXPECT_TRUE(main_dict.Get("int", NULL)); 891 EXPECT_TRUE(main_dict.Get("int", NULL));
874 EXPECT_TRUE(main_dict.Get("double", NULL)); 892 EXPECT_TRUE(main_dict.Get("double", NULL));
875 EXPECT_TRUE(main_dict.Get("string", NULL)); 893 EXPECT_TRUE(main_dict.Get("string", NULL));
876 EXPECT_TRUE(main_dict.Get("binary", NULL)); 894 EXPECT_TRUE(main_dict.Get("binary", NULL));
877 EXPECT_TRUE(main_dict.Get("dict", NULL)); 895 EXPECT_TRUE(main_dict.Get("dict", NULL));
878 EXPECT_TRUE(main_dict.Get("list", NULL)); 896 EXPECT_TRUE(main_dict.Get("list", NULL));
879 EXPECT_FALSE(main_dict.Get("DNE", NULL)); 897 EXPECT_FALSE(main_dict.Get("DNE", NULL));
880 898
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after
1118 EXPECT_FALSE(main_list.GetList(1, NULL)); 1136 EXPECT_FALSE(main_list.GetList(1, NULL));
1119 EXPECT_FALSE(main_list.GetList(2, NULL)); 1137 EXPECT_FALSE(main_list.GetList(2, NULL));
1120 EXPECT_FALSE(main_list.GetList(3, NULL)); 1138 EXPECT_FALSE(main_list.GetList(3, NULL));
1121 EXPECT_FALSE(main_list.GetList(4, NULL)); 1139 EXPECT_FALSE(main_list.GetList(4, NULL));
1122 EXPECT_FALSE(main_list.GetList(5, NULL)); 1140 EXPECT_FALSE(main_list.GetList(5, NULL));
1123 EXPECT_TRUE(main_list.GetList(6, NULL)); 1141 EXPECT_TRUE(main_list.GetList(6, NULL));
1124 EXPECT_FALSE(main_list.GetList(7, NULL)); 1142 EXPECT_FALSE(main_list.GetList(7, NULL));
1125 } 1143 }
1126 1144
1127 } // namespace base 1145 } // namespace base
OLDNEW
« no previous file with comments | « base/values.cc ('k') | base/win/memory_pressure_monitor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698