OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
13 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 TEST(JSONValueSerializerTest, Roundtrip) { | 16 TEST(JSONValueSerializerTest, Roundtrip) { |
17 const std::string original_serialization = | 17 const std::string original_serialization = |
18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; | 18 "{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}"; |
19 JSONStringValueSerializer serializer(original_serialization); | 19 JSONStringValueSerializer serializer(original_serialization); |
20 scoped_ptr<Value> root(serializer.Deserialize(NULL)); | 20 scoped_ptr<Value> root(serializer.Deserialize(NULL, NULL)); |
21 ASSERT_TRUE(root.get()); | 21 ASSERT_TRUE(root.get()); |
22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 22 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
23 | 23 |
24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 24 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
25 | 25 |
26 Value* null_value = NULL; | 26 Value* null_value = NULL; |
27 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); | 27 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); |
28 ASSERT_TRUE(null_value); | 28 ASSERT_TRUE(null_value); |
29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | 29 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); |
30 | 30 |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 | 123 |
124 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; | 124 std::string expected = "{\"web\":\"\\u7F51\\u9875\"}"; |
125 | 125 |
126 std::string actual; | 126 std::string actual; |
127 JSONStringValueSerializer serializer(&actual); | 127 JSONStringValueSerializer serializer(&actual); |
128 ASSERT_TRUE(serializer.Serialize(root)); | 128 ASSERT_TRUE(serializer.Serialize(root)); |
129 ASSERT_EQ(expected, actual); | 129 ASSERT_EQ(expected, actual); |
130 | 130 |
131 // escaped ascii text -> json | 131 // escaped ascii text -> json |
132 JSONStringValueSerializer deserializer(expected); | 132 JSONStringValueSerializer deserializer(expected); |
133 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); | 133 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
134 ASSERT_TRUE(deserial_root.get()); | 134 ASSERT_TRUE(deserial_root.get()); |
135 DictionaryValue* dict_root = | 135 DictionaryValue* dict_root = |
136 static_cast<DictionaryValue*>(deserial_root.get()); | 136 static_cast<DictionaryValue*>(deserial_root.get()); |
137 std::wstring web_value; | 137 std::wstring web_value; |
138 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); | 138 ASSERT_TRUE(dict_root->GetString(L"web", &web_value)); |
139 ASSERT_EQ(test, web_value); | 139 ASSERT_EQ(test, web_value); |
140 } | 140 } |
141 | 141 |
142 TEST(JSONValueSerializerTest, HexStrings) { | 142 TEST(JSONValueSerializerTest, HexStrings) { |
143 // hex string json -> escaped ascii text | 143 // hex string json -> escaped ascii text |
144 DictionaryValue root; | 144 DictionaryValue root; |
145 std::wstring test(L"\x01\x02"); | 145 std::wstring test(L"\x01\x02"); |
146 root.SetString(L"test", test); | 146 root.SetString(L"test", test); |
147 | 147 |
148 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; | 148 std::string expected = "{\"test\":\"\\u0001\\u0002\"}"; |
149 | 149 |
150 std::string actual; | 150 std::string actual; |
151 JSONStringValueSerializer serializer(&actual); | 151 JSONStringValueSerializer serializer(&actual); |
152 ASSERT_TRUE(serializer.Serialize(root)); | 152 ASSERT_TRUE(serializer.Serialize(root)); |
153 ASSERT_EQ(expected, actual); | 153 ASSERT_EQ(expected, actual); |
154 | 154 |
155 // escaped ascii text -> json | 155 // escaped ascii text -> json |
156 JSONStringValueSerializer deserializer(expected); | 156 JSONStringValueSerializer deserializer(expected); |
157 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL)); | 157 scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL, NULL)); |
158 ASSERT_TRUE(deserial_root.get()); | 158 ASSERT_TRUE(deserial_root.get()); |
159 DictionaryValue* dict_root = | 159 DictionaryValue* dict_root = |
160 static_cast<DictionaryValue*>(deserial_root.get()); | 160 static_cast<DictionaryValue*>(deserial_root.get()); |
161 std::wstring test_value; | 161 std::wstring test_value; |
162 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); | 162 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); |
163 ASSERT_EQ(test, test_value); | 163 ASSERT_EQ(test, test_value); |
164 | 164 |
165 // Test converting escaped regular chars | 165 // Test converting escaped regular chars |
166 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; | 166 std::string escaped_chars = "{\"test\":\"\\u0067\\u006f\"}"; |
167 JSONStringValueSerializer deserializer2(escaped_chars); | 167 JSONStringValueSerializer deserializer2(escaped_chars); |
168 deserial_root.reset(deserializer2.Deserialize(NULL)); | 168 deserial_root.reset(deserializer2.Deserialize(NULL, NULL)); |
169 ASSERT_TRUE(deserial_root.get()); | 169 ASSERT_TRUE(deserial_root.get()); |
170 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); | 170 dict_root = static_cast<DictionaryValue*>(deserial_root.get()); |
171 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); | 171 ASSERT_TRUE(dict_root->GetString(L"test", &test_value)); |
172 ASSERT_EQ(std::wstring(L"go"), test_value); | 172 ASSERT_EQ(std::wstring(L"go"), test_value); |
173 } | 173 } |
174 | 174 |
175 TEST(JSONValueSerializerTest, AllowTrailingComma) { | 175 TEST(JSONValueSerializerTest, AllowTrailingComma) { |
176 scoped_ptr<Value> root; | 176 scoped_ptr<Value> root; |
177 scoped_ptr<Value> root_expected; | 177 scoped_ptr<Value> root_expected; |
178 std::string test_with_commas("{\"key\": [true,],}"); | 178 std::string test_with_commas("{\"key\": [true,],}"); |
179 std::string test_no_commas("{\"key\": [true]}"); | 179 std::string test_no_commas("{\"key\": [true]}"); |
180 | 180 |
181 JSONStringValueSerializer serializer(test_with_commas); | 181 JSONStringValueSerializer serializer(test_with_commas); |
182 serializer.set_allow_trailing_comma(true); | 182 serializer.set_allow_trailing_comma(true); |
183 JSONStringValueSerializer serializer_expected(test_no_commas); | 183 JSONStringValueSerializer serializer_expected(test_no_commas); |
184 root.reset(serializer.Deserialize(NULL)); | 184 root.reset(serializer.Deserialize(NULL, NULL)); |
185 ASSERT_TRUE(root.get()); | 185 ASSERT_TRUE(root.get()); |
186 root_expected.reset(serializer_expected.Deserialize(NULL)); | 186 root_expected.reset(serializer_expected.Deserialize(NULL, NULL)); |
187 ASSERT_TRUE(root_expected.get()); | 187 ASSERT_TRUE(root_expected.get()); |
188 ASSERT_TRUE(root->Equals(root_expected.get())); | 188 ASSERT_TRUE(root->Equals(root_expected.get())); |
189 } | 189 } |
190 | 190 |
191 namespace { | 191 namespace { |
192 | 192 |
193 void ValidateJsonList(const std::string& json) { | 193 void ValidateJsonList(const std::string& json) { |
194 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); | 194 scoped_ptr<Value> root(base::JSONReader::Read(json, false)); |
195 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); | 195 ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST)); |
196 ListValue* list = static_cast<ListValue*>(root.get()); | 196 ListValue* list = static_cast<ListValue*>(root.get()); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 FilePath original_file_path; | 260 FilePath original_file_path; |
261 ASSERT_TRUE( | 261 ASSERT_TRUE( |
262 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 262 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
263 original_file_path = | 263 original_file_path = |
264 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); | 264 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); |
265 | 265 |
266 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 266 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
267 | 267 |
268 JSONFileValueSerializer deserializer(original_file_path); | 268 JSONFileValueSerializer deserializer(original_file_path); |
269 scoped_ptr<Value> root; | 269 scoped_ptr<Value> root; |
270 root.reset(deserializer.Deserialize(NULL)); | 270 root.reset(deserializer.Deserialize(NULL, NULL)); |
271 | 271 |
272 ASSERT_TRUE(root.get()); | 272 ASSERT_TRUE(root.get()); |
273 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); | 273 ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); |
274 | 274 |
275 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); | 275 DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); |
276 | 276 |
277 Value* null_value = NULL; | 277 Value* null_value = NULL; |
278 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); | 278 ASSERT_TRUE(root_dict->Get(L"null", &null_value)); |
279 ASSERT_TRUE(null_value); | 279 ASSERT_TRUE(null_value); |
280 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); | 280 ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); |
(...skipping 29 matching lines...) Expand all Loading... |
310 FilePath original_file_path; | 310 FilePath original_file_path; |
311 ASSERT_TRUE( | 311 ASSERT_TRUE( |
312 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 312 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
313 original_file_path = | 313 original_file_path = |
314 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); | 314 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); |
315 | 315 |
316 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 316 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
317 | 317 |
318 JSONFileValueSerializer deserializer(original_file_path); | 318 JSONFileValueSerializer deserializer(original_file_path); |
319 scoped_ptr<Value> root; | 319 scoped_ptr<Value> root; |
320 root.reset(deserializer.Deserialize(NULL)); | 320 root.reset(deserializer.Deserialize(NULL, NULL)); |
321 ASSERT_TRUE(root.get()); | 321 ASSERT_TRUE(root.get()); |
322 | 322 |
323 // Now try writing. | 323 // Now try writing. |
324 FilePath written_file_path = | 324 FilePath written_file_path = |
325 test_dir_.Append(FILE_PATH_LITERAL("test_output.js")); | 325 test_dir_.Append(FILE_PATH_LITERAL("test_output.js")); |
326 | 326 |
327 ASSERT_FALSE(file_util::PathExists(written_file_path)); | 327 ASSERT_FALSE(file_util::PathExists(written_file_path)); |
328 JSONFileValueSerializer serializer(written_file_path); | 328 JSONFileValueSerializer serializer(written_file_path); |
329 ASSERT_TRUE(serializer.Serialize(*root)); | 329 ASSERT_TRUE(serializer.Serialize(*root)); |
330 ASSERT_TRUE(file_util::PathExists(written_file_path)); | 330 ASSERT_TRUE(file_util::PathExists(written_file_path)); |
331 | 331 |
332 // Now compare file contents. | 332 // Now compare file contents. |
333 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 333 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
334 written_file_path)); | 334 written_file_path)); |
335 EXPECT_TRUE(file_util::Delete(written_file_path, false)); | 335 EXPECT_TRUE(file_util::Delete(written_file_path, false)); |
336 } | 336 } |
337 | 337 |
338 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 338 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
339 FilePath source_file_path; | 339 FilePath source_file_path; |
340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 340 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
341 source_file_path = source_file_path.Append( | 341 source_file_path = source_file_path.Append( |
342 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 342 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
343 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 343 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
344 JSONFileValueSerializer serializer(source_file_path); | 344 JSONFileValueSerializer serializer(source_file_path); |
345 scoped_ptr<Value> root; | 345 scoped_ptr<Value> root; |
346 root.reset(serializer.Deserialize(NULL)); | 346 root.reset(serializer.Deserialize(NULL, NULL)); |
347 ASSERT_TRUE(root.get()); | 347 ASSERT_TRUE(root.get()); |
348 } | 348 } |
OLD | NEW |