OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/basictypes.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/scoped_temp_dir.h" | |
8 #include "base/json/json_file_value_serializer.h" | |
9 #include "base/json/json_reader.h" | |
10 #include "base/json/json_string_value_serializer.h" | |
11 #include "base/json/json_writer.h" | |
12 #include "base/path_service.h" | |
13 #include "base/string16.h" | |
14 #include "base/string_util.h" | |
15 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | |
17 #include "chrome/common/chrome_paths.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 class JSONFileValueSerializerTest : public testing::Test { | |
21 protected: | |
22 virtual void SetUp() OVERRIDE { | |
23 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
24 } | |
25 | |
26 base::ScopedTempDir temp_dir_; | |
27 }; | |
28 | |
29 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | |
30 base::FilePath original_file_path; | |
31 ASSERT_TRUE( | |
32 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | |
33 original_file_path = | |
34 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); | |
35 | |
36 ASSERT_TRUE(file_util::PathExists(original_file_path)); | |
37 | |
38 JSONFileValueSerializer deserializer(original_file_path); | |
39 scoped_ptr<Value> root; | |
40 root.reset(deserializer.Deserialize(NULL, NULL)); | |
41 | |
42 ASSERT_TRUE(root.get()); | |
43 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | |
44 | |
45 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | |
46 | |
47 Value* null_value = NULL; | |
48 ASSERT_TRUE(root_dict->Get("null", &null_value)); | |
49 ASSERT_TRUE(null_value); | |
50 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | |
51 | |
52 bool bool_value = false; | |
53 ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); | |
54 ASSERT_TRUE(bool_value); | |
55 | |
56 int int_value = 0; | |
57 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); | |
58 ASSERT_EQ(42, int_value); | |
59 | |
60 std::string string_value; | |
61 ASSERT_TRUE(root_dict->GetString("string", &string_value)); | |
62 ASSERT_EQ("hello", string_value); | |
63 | |
64 // Now try writing. | |
65 const base::FilePath written_file_path = | |
66 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); | |
67 | |
68 ASSERT_FALSE(file_util::PathExists(written_file_path)); | |
69 JSONFileValueSerializer serializer(written_file_path); | |
70 ASSERT_TRUE(serializer.Serialize(*root)); | |
71 ASSERT_TRUE(file_util::PathExists(written_file_path)); | |
72 | |
73 // Now compare file contents. | |
74 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | |
75 written_file_path)); | |
76 EXPECT_TRUE(file_util::Delete(written_file_path, false)); | |
77 } | |
78 | |
79 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | |
80 base::FilePath original_file_path; | |
81 ASSERT_TRUE( | |
82 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | |
83 original_file_path = | |
84 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); | |
85 | |
86 ASSERT_TRUE(file_util::PathExists(original_file_path)); | |
87 | |
88 JSONFileValueSerializer deserializer(original_file_path); | |
89 scoped_ptr<Value> root; | |
90 root.reset(deserializer.Deserialize(NULL, NULL)); | |
91 ASSERT_TRUE(root.get()); | |
92 | |
93 // Now try writing. | |
94 base::FilePath written_file_path = | |
95 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); | |
96 | |
97 ASSERT_FALSE(file_util::PathExists(written_file_path)); | |
98 JSONFileValueSerializer serializer(written_file_path); | |
99 ASSERT_TRUE(serializer.Serialize(*root)); | |
100 ASSERT_TRUE(file_util::PathExists(written_file_path)); | |
101 | |
102 // Now compare file contents. | |
103 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | |
104 written_file_path)); | |
105 EXPECT_TRUE(file_util::Delete(written_file_path, false)); | |
106 } | |
107 | |
108 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | |
109 base::FilePath source_file_path; | |
110 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | |
111 source_file_path = source_file_path.Append( | |
112 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | |
113 ASSERT_TRUE(file_util::PathExists(source_file_path)); | |
114 JSONFileValueSerializer serializer(source_file_path); | |
115 scoped_ptr<Value> root; | |
116 root.reset(serializer.Deserialize(NULL, NULL)); | |
117 ASSERT_TRUE(root.get()); | |
118 } | |
OLD | NEW |