OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/values.h" | 6 #include "base/values.h" |
| 7 #include "chromecast/base/scoped_temp_file.h" |
7 #include "chromecast/base/serializers.h" | 8 #include "chromecast/base/serializers.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace chromecast { | 11 namespace chromecast { |
11 namespace { | 12 namespace { |
12 const char kEmptyJsonString[] = "{}"; | 13 const char kEmptyJsonString[] = "{}"; |
13 const char kEmptyJsonFileString[] = "{\n\n}\n"; | 14 const char kEmptyJsonFileString[] = "{\n\n}\n"; |
14 const char kProperJsonString[] = | 15 const char kProperJsonString[] = |
15 "{\n" | 16 "{\n" |
16 " \"compound\": {\n" | 17 " \"compound\": {\n" |
17 " \"a\": 1,\n" | 18 " \"a\": 1,\n" |
18 " \"b\": 2\n" | 19 " \"b\": 2\n" |
19 " },\n" | 20 " },\n" |
20 " \"some_String\": \"1337\",\n" | 21 " \"some_String\": \"1337\",\n" |
21 " \"some_int\": 42,\n" | 22 " \"some_int\": 42,\n" |
22 " \"the_list\": [ \"val1\", \"val2\" ]\n" | 23 " \"the_list\": [ \"val1\", \"val2\" ]\n" |
23 "}\n"; | 24 "}\n"; |
24 const char kPoorlyFormedJsonString[] = "{\"key\":"; | 25 const char kPoorlyFormedJsonString[] = "{\"key\":"; |
25 const char kTestKey[] = "test_key"; | 26 const char kTestKey[] = "test_key"; |
26 const char kTestValue[] = "test_value"; | 27 const char kTestValue[] = "test_value"; |
27 const char kTempfileName[] = "temp"; | |
28 | 28 |
29 } // namespace | 29 } // namespace |
30 | 30 |
31 TEST(DeserializeFromJson, EmptyString) { | 31 TEST(DeserializeFromJson, EmptyString) { |
32 std::string str; | 32 std::string str; |
33 scoped_ptr<base::Value> value = DeserializeFromJson(str); | 33 scoped_ptr<base::Value> value = DeserializeFromJson(str); |
34 EXPECT_EQ(nullptr, value.get()); | 34 EXPECT_EQ(nullptr, value.get()); |
35 } | 35 } |
36 | 36 |
37 TEST(DeserializeFromJson, EmptyJsonObject) { | 37 TEST(DeserializeFromJson, EmptyJsonObject) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 base::DictionaryValue orig_value; | 69 base::DictionaryValue orig_value; |
70 orig_value.SetString(kTestKey, kTestValue); | 70 orig_value.SetString(kTestKey, kTestValue); |
71 scoped_ptr<std::string> str = SerializeToJson(orig_value); | 71 scoped_ptr<std::string> str = SerializeToJson(orig_value); |
72 ASSERT_NE(nullptr, str.get()); | 72 ASSERT_NE(nullptr, str.get()); |
73 | 73 |
74 scoped_ptr<base::Value> new_value = DeserializeFromJson(*str); | 74 scoped_ptr<base::Value> new_value = DeserializeFromJson(*str); |
75 ASSERT_NE(nullptr, new_value.get()); | 75 ASSERT_NE(nullptr, new_value.get()); |
76 EXPECT_TRUE(new_value->Equals(&orig_value)); | 76 EXPECT_TRUE(new_value->Equals(&orig_value)); |
77 } | 77 } |
78 | 78 |
79 class ScopedTempFile { | |
80 public: | |
81 ScopedTempFile() { | |
82 // Create a temporary file | |
83 base::CreateNewTempDirectory("", &dir_); | |
84 file_ = dir_.Append(kTempfileName); | |
85 } | |
86 | |
87 ~ScopedTempFile() { | |
88 // Remove the temp directory. | |
89 base::DeleteFile(dir_, true); | |
90 } | |
91 | |
92 const base::FilePath& file() const { return file_; } | |
93 const base::FilePath& dir() const { return dir_; } | |
94 | |
95 std::size_t Write(const char* str) { | |
96 return static_cast<std::size_t>(base::WriteFile(file_, str, strlen(str))); | |
97 } | |
98 | |
99 std::string Read() { | |
100 std::string result; | |
101 ReadFileToString(file_, &result); | |
102 return result; | |
103 } | |
104 | |
105 private: | |
106 base::FilePath file_; | |
107 base::FilePath dir_; | |
108 | |
109 DISALLOW_COPY_AND_ASSIGN(ScopedTempFile); | |
110 }; | |
111 | |
112 TEST(DeserializeJsonFromFile, NoFile) { | 79 TEST(DeserializeJsonFromFile, NoFile) { |
113 ScopedTempFile temp; | 80 scoped_ptr<base::Value> value = |
114 | 81 DeserializeJsonFromFile(base::FilePath("/file/does/not/exist.json")); |
115 ASSERT_TRUE(base::IsDirectoryEmpty(temp.dir())); | |
116 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file()); | |
117 EXPECT_EQ(nullptr, value.get()); | 82 EXPECT_EQ(nullptr, value.get()); |
118 } | 83 } |
119 | 84 |
120 TEST(DeserializeJsonFromFile, EmptyString) { | 85 TEST(DeserializeJsonFromFile, EmptyString) { |
121 ScopedTempFile temp; | 86 ScopedTempFile temp; |
122 EXPECT_EQ(strlen(""), temp.Write("")); | 87 EXPECT_EQ(static_cast<int>(strlen("")), temp.Write("")); |
123 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file()); | 88 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
124 EXPECT_EQ(nullptr, value.get()); | 89 EXPECT_EQ(nullptr, value.get()); |
125 } | 90 } |
126 | 91 |
127 TEST(DeserializeJsonFromFile, EmptyJsonObject) { | 92 TEST(DeserializeJsonFromFile, EmptyJsonObject) { |
128 ScopedTempFile temp; | 93 ScopedTempFile temp; |
129 EXPECT_EQ(strlen(kEmptyJsonString), temp.Write(kEmptyJsonString)); | 94 EXPECT_EQ(static_cast<int>(strlen(kEmptyJsonString)), |
130 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file()); | 95 temp.Write(kEmptyJsonString)); |
| 96 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
131 EXPECT_NE(nullptr, value.get()); | 97 EXPECT_NE(nullptr, value.get()); |
132 } | 98 } |
133 | 99 |
134 TEST(DeserializeJsonFromFile, ProperJsonObject) { | 100 TEST(DeserializeJsonFromFile, ProperJsonObject) { |
135 ScopedTempFile temp; | 101 ScopedTempFile temp; |
136 EXPECT_EQ(strlen(kProperJsonString), temp.Write(kProperJsonString)); | 102 EXPECT_EQ(static_cast<int>(strlen(kProperJsonString)), |
137 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file()); | 103 temp.Write(kProperJsonString)); |
| 104 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
138 EXPECT_NE(nullptr, value.get()); | 105 EXPECT_NE(nullptr, value.get()); |
139 } | 106 } |
140 | 107 |
141 TEST(DeserializeJsonFromFile, PoorlyFormedJsonObject) { | 108 TEST(DeserializeJsonFromFile, PoorlyFormedJsonObject) { |
142 ScopedTempFile temp; | 109 ScopedTempFile temp; |
143 EXPECT_EQ(strlen(kPoorlyFormedJsonString), | 110 EXPECT_EQ(static_cast<int>(strlen(kPoorlyFormedJsonString)), |
144 temp.Write(kPoorlyFormedJsonString)); | 111 temp.Write(kPoorlyFormedJsonString)); |
145 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.file()); | 112 scoped_ptr<base::Value> value = DeserializeJsonFromFile(temp.path()); |
146 EXPECT_EQ(nullptr, value.get()); | 113 EXPECT_EQ(nullptr, value.get()); |
147 } | 114 } |
148 | 115 |
149 TEST(SerializeJsonToFile, BadValue) { | 116 TEST(SerializeJsonToFile, BadValue) { |
150 ScopedTempFile temp; | 117 ScopedTempFile temp; |
151 | 118 |
152 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12); | 119 base::BinaryValue value(scoped_ptr<char[]>(new char[12]), 12); |
153 ASSERT_FALSE(SerializeJsonToFile(temp.file(), value)); | 120 ASSERT_FALSE(SerializeJsonToFile(temp.path(), value)); |
154 std::string str(temp.Read()); | 121 std::string str(temp.Read()); |
155 EXPECT_TRUE(str.empty()); | 122 EXPECT_TRUE(str.empty()); |
156 } | 123 } |
157 | 124 |
158 TEST(SerializeJsonToFile, EmptyValue) { | 125 TEST(SerializeJsonToFile, EmptyValue) { |
159 ScopedTempFile temp; | 126 ScopedTempFile temp; |
160 | 127 |
161 base::DictionaryValue value; | 128 base::DictionaryValue value; |
162 ASSERT_TRUE(SerializeJsonToFile(temp.file(), value)); | 129 ASSERT_TRUE(SerializeJsonToFile(temp.path(), value)); |
163 std::string str(temp.Read()); | 130 std::string str(temp.Read()); |
164 ASSERT_FALSE(str.empty()); | 131 ASSERT_FALSE(str.empty()); |
165 EXPECT_EQ(kEmptyJsonFileString, str); | 132 EXPECT_EQ(kEmptyJsonFileString, str); |
166 } | 133 } |
167 | 134 |
168 TEST(SerializeJsonToFile, PopulatedValue) { | 135 TEST(SerializeJsonToFile, PopulatedValue) { |
169 ScopedTempFile temp; | 136 ScopedTempFile temp; |
170 | 137 |
171 base::DictionaryValue orig_value; | 138 base::DictionaryValue orig_value; |
172 orig_value.SetString(kTestKey, kTestValue); | 139 orig_value.SetString(kTestKey, kTestValue); |
173 ASSERT_TRUE(SerializeJsonToFile(temp.file(), orig_value)); | 140 ASSERT_TRUE(SerializeJsonToFile(temp.path(), orig_value)); |
174 std::string str(temp.Read()); | 141 std::string str(temp.Read()); |
175 ASSERT_FALSE(str.empty()); | 142 ASSERT_FALSE(str.empty()); |
176 | 143 |
177 scoped_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.file()); | 144 scoped_ptr<base::Value> new_value = DeserializeJsonFromFile(temp.path()); |
178 ASSERT_NE(nullptr, new_value.get()); | 145 ASSERT_NE(nullptr, new_value.get()); |
179 EXPECT_TRUE(new_value->Equals(&orig_value)); | 146 EXPECT_TRUE(new_value->Equals(&orig_value)); |
180 } | 147 } |
181 | 148 |
182 } // namespace chromecast | 149 } // namespace chromecast |
OLD | NEW |