OLD | NEW |
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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/json/json_file_value_serializer.h" | 9 #include "base/json/json_file_value_serializer.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 | 378 |
379 base::ScopedTempDir temp_dir_; | 379 base::ScopedTempDir temp_dir_; |
380 }; | 380 }; |
381 | 381 |
382 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 382 TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
383 base::FilePath original_file_path; | 383 base::FilePath original_file_path; |
384 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 384 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); |
385 original_file_path = | 385 original_file_path = |
386 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); | 386 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); |
387 | 387 |
388 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 388 ASSERT_TRUE(PathExists(original_file_path)); |
389 | 389 |
390 JSONFileValueSerializer deserializer(original_file_path); | 390 JSONFileValueSerializer deserializer(original_file_path); |
391 scoped_ptr<Value> root; | 391 scoped_ptr<Value> root; |
392 root.reset(deserializer.Deserialize(NULL, NULL)); | 392 root.reset(deserializer.Deserialize(NULL, NULL)); |
393 | 393 |
394 ASSERT_TRUE(root.get()); | 394 ASSERT_TRUE(root.get()); |
395 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 395 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
396 | 396 |
397 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 397 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
398 | 398 |
(...skipping 11 matching lines...) Expand all Loading... |
410 ASSERT_EQ(42, int_value); | 410 ASSERT_EQ(42, int_value); |
411 | 411 |
412 std::string string_value; | 412 std::string string_value; |
413 ASSERT_TRUE(root_dict->GetString("string", &string_value)); | 413 ASSERT_TRUE(root_dict->GetString("string", &string_value)); |
414 ASSERT_EQ("hello", string_value); | 414 ASSERT_EQ("hello", string_value); |
415 | 415 |
416 // Now try writing. | 416 // Now try writing. |
417 const base::FilePath written_file_path = | 417 const base::FilePath written_file_path = |
418 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); | 418 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); |
419 | 419 |
420 ASSERT_FALSE(file_util::PathExists(written_file_path)); | 420 ASSERT_FALSE(PathExists(written_file_path)); |
421 JSONFileValueSerializer serializer(written_file_path); | 421 JSONFileValueSerializer serializer(written_file_path); |
422 ASSERT_TRUE(serializer.Serialize(*root)); | 422 ASSERT_TRUE(serializer.Serialize(*root)); |
423 ASSERT_TRUE(file_util::PathExists(written_file_path)); | 423 ASSERT_TRUE(PathExists(written_file_path)); |
424 | 424 |
425 // Now compare file contents. | 425 // Now compare file contents. |
426 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 426 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
427 written_file_path)); | 427 written_file_path)); |
428 EXPECT_TRUE(base::Delete(written_file_path, false)); | 428 EXPECT_TRUE(base::Delete(written_file_path, false)); |
429 } | 429 } |
430 | 430 |
431 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | 431 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { |
432 base::FilePath original_file_path; | 432 base::FilePath original_file_path; |
433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); |
434 original_file_path = original_file_path.Append( | 434 original_file_path = original_file_path.Append( |
435 FILE_PATH_LITERAL("serializer_nested_test.json")); | 435 FILE_PATH_LITERAL("serializer_nested_test.json")); |
436 | 436 |
437 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 437 ASSERT_TRUE(PathExists(original_file_path)); |
438 | 438 |
439 JSONFileValueSerializer deserializer(original_file_path); | 439 JSONFileValueSerializer deserializer(original_file_path); |
440 scoped_ptr<Value> root; | 440 scoped_ptr<Value> root; |
441 root.reset(deserializer.Deserialize(NULL, NULL)); | 441 root.reset(deserializer.Deserialize(NULL, NULL)); |
442 ASSERT_TRUE(root.get()); | 442 ASSERT_TRUE(root.get()); |
443 | 443 |
444 // Now try writing. | 444 // Now try writing. |
445 base::FilePath written_file_path = temp_dir_.path().Append( | 445 base::FilePath written_file_path = temp_dir_.path().Append( |
446 FILE_PATH_LITERAL("test_output.json")); | 446 FILE_PATH_LITERAL("test_output.json")); |
447 | 447 |
448 ASSERT_FALSE(file_util::PathExists(written_file_path)); | 448 ASSERT_FALSE(PathExists(written_file_path)); |
449 JSONFileValueSerializer serializer(written_file_path); | 449 JSONFileValueSerializer serializer(written_file_path); |
450 ASSERT_TRUE(serializer.Serialize(*root)); | 450 ASSERT_TRUE(serializer.Serialize(*root)); |
451 ASSERT_TRUE(file_util::PathExists(written_file_path)); | 451 ASSERT_TRUE(PathExists(written_file_path)); |
452 | 452 |
453 // Now compare file contents. | 453 // Now compare file contents. |
454 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 454 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
455 written_file_path)); | 455 written_file_path)); |
456 EXPECT_TRUE(base::Delete(written_file_path, false)); | 456 EXPECT_TRUE(base::Delete(written_file_path, false)); |
457 } | 457 } |
458 | 458 |
459 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 459 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
460 base::FilePath source_file_path; | 460 base::FilePath source_file_path; |
461 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); | 461 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); |
462 source_file_path = source_file_path.Append( | 462 source_file_path = source_file_path.Append( |
463 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); | 463 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); |
464 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 464 ASSERT_TRUE(PathExists(source_file_path)); |
465 JSONFileValueSerializer serializer(source_file_path); | 465 JSONFileValueSerializer serializer(source_file_path); |
466 scoped_ptr<Value> root; | 466 scoped_ptr<Value> root; |
467 root.reset(serializer.Deserialize(NULL, NULL)); | 467 root.reset(serializer.Deserialize(NULL, NULL)); |
468 ASSERT_TRUE(root.get()); | 468 ASSERT_TRUE(root.get()); |
469 } | 469 } |
470 | 470 |
471 } // namespace | 471 } // namespace |
472 | 472 |
473 } // namespace base | 473 } // namespace base |
OLD | NEW |