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 405 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(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(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(TextContentsEqual(original_file_path, written_file_path)); |
427 written_file_path)); | |
428 EXPECT_TRUE(base::Delete(written_file_path, false)); | 427 EXPECT_TRUE(base::Delete(written_file_path, false)); |
429 } | 428 } |
430 | 429 |
431 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | 430 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { |
432 base::FilePath original_file_path; | 431 base::FilePath original_file_path; |
433 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); | 432 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); |
434 original_file_path = original_file_path.Append( | 433 original_file_path = original_file_path.Append( |
435 FILE_PATH_LITERAL("serializer_nested_test.json")); | 434 FILE_PATH_LITERAL("serializer_nested_test.json")); |
436 | 435 |
437 ASSERT_TRUE(PathExists(original_file_path)); | 436 ASSERT_TRUE(PathExists(original_file_path)); |
438 | 437 |
439 JSONFileValueSerializer deserializer(original_file_path); | 438 JSONFileValueSerializer deserializer(original_file_path); |
440 scoped_ptr<Value> root; | 439 scoped_ptr<Value> root; |
441 root.reset(deserializer.Deserialize(NULL, NULL)); | 440 root.reset(deserializer.Deserialize(NULL, NULL)); |
442 ASSERT_TRUE(root.get()); | 441 ASSERT_TRUE(root.get()); |
443 | 442 |
444 // Now try writing. | 443 // Now try writing. |
445 base::FilePath written_file_path = temp_dir_.path().Append( | 444 base::FilePath written_file_path = temp_dir_.path().Append( |
446 FILE_PATH_LITERAL("test_output.json")); | 445 FILE_PATH_LITERAL("test_output.json")); |
447 | 446 |
448 ASSERT_FALSE(PathExists(written_file_path)); | 447 ASSERT_FALSE(PathExists(written_file_path)); |
449 JSONFileValueSerializer serializer(written_file_path); | 448 JSONFileValueSerializer serializer(written_file_path); |
450 ASSERT_TRUE(serializer.Serialize(*root)); | 449 ASSERT_TRUE(serializer.Serialize(*root)); |
451 ASSERT_TRUE(PathExists(written_file_path)); | 450 ASSERT_TRUE(PathExists(written_file_path)); |
452 | 451 |
453 // Now compare file contents. | 452 // Now compare file contents. |
454 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 453 EXPECT_TRUE(TextContentsEqual(original_file_path, written_file_path)); |
455 written_file_path)); | |
456 EXPECT_TRUE(base::Delete(written_file_path, false)); | 454 EXPECT_TRUE(base::Delete(written_file_path, false)); |
457 } | 455 } |
458 | 456 |
459 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 457 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
460 base::FilePath source_file_path; | 458 base::FilePath source_file_path; |
461 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); | 459 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); |
462 source_file_path = source_file_path.Append( | 460 source_file_path = source_file_path.Append( |
463 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); | 461 FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); |
464 ASSERT_TRUE(PathExists(source_file_path)); | 462 ASSERT_TRUE(PathExists(source_file_path)); |
465 JSONFileValueSerializer serializer(source_file_path); | 463 JSONFileValueSerializer serializer(source_file_path); |
466 scoped_ptr<Value> root; | 464 scoped_ptr<Value> root; |
467 root.reset(serializer.Deserialize(NULL, NULL)); | 465 root.reset(serializer.Deserialize(NULL, NULL)); |
468 ASSERT_TRUE(root.get()); | 466 ASSERT_TRUE(root.get()); |
469 } | 467 } |
470 | 468 |
471 } // namespace | 469 } // namespace |
472 | 470 |
473 } // namespace base | 471 } // namespace base |
OLD | NEW |