| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/scoped_temp_dir.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/string16.h" | 11 #include "base/string16.h" |
| 11 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
| 15 #include "chrome/common/json_value_serializer.h" | 16 #include "chrome/common/json_value_serializer.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 TEST(JSONValueSerializerTest, Roundtrip) { | 19 TEST(JSONValueSerializerTest, Roundtrip) { |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 ASSERT_FALSE(root.get()); | 233 ASSERT_FALSE(root.get()); |
| 233 | 234 |
| 234 // Not a open comment token. | 235 // Not a open comment token. |
| 235 root.reset(base::JSONReader::Read("/ * * / [1]", false)); | 236 root.reset(base::JSONReader::Read("/ * * / [1]", false)); |
| 236 ASSERT_FALSE(root.get()); | 237 ASSERT_FALSE(root.get()); |
| 237 } | 238 } |
| 238 | 239 |
| 239 class JSONFileValueSerializerTest : public testing::Test { | 240 class JSONFileValueSerializerTest : public testing::Test { |
| 240 protected: | 241 protected: |
| 241 virtual void SetUp() { | 242 virtual void SetUp() { |
| 242 // Name a subdirectory of the temp directory. | 243 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 243 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | |
| 244 test_dir_ = | |
| 245 test_dir_.Append(FILE_PATH_LITERAL("JSONFileValueSerializerTest")); | |
| 246 | |
| 247 // Create a fresh, empty copy of this directory. | |
| 248 file_util::Delete(test_dir_, true); | |
| 249 file_util::CreateDirectory(test_dir_); | |
| 250 } | |
| 251 virtual void TearDown() { | |
| 252 // Clean up test directory | |
| 253 ASSERT_TRUE(file_util::Delete(test_dir_, false)); | |
| 254 ASSERT_FALSE(file_util::PathExists(test_dir_)); | |
| 255 } | 244 } |
| 256 | 245 |
| 257 // the path to temporary directory used to contain the test operations | 246 ScopedTempDir temp_dir_; |
| 258 FilePath test_dir_; | |
| 259 }; | 247 }; |
| 260 | 248 |
| 261 TEST_F(JSONFileValueSerializerTest, Roundtrip) { | 249 TEST_F(JSONFileValueSerializerTest, Roundtrip) { |
| 262 FilePath original_file_path; | 250 FilePath original_file_path; |
| 263 ASSERT_TRUE( | 251 ASSERT_TRUE( |
| 264 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 252 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
| 265 original_file_path = | 253 original_file_path = |
| 266 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); | 254 original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); |
| 267 | 255 |
| 268 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 256 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 288 int int_value = 0; | 276 int int_value = 0; |
| 289 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); | 277 ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); |
| 290 ASSERT_EQ(42, int_value); | 278 ASSERT_EQ(42, int_value); |
| 291 | 279 |
| 292 std::string string_value; | 280 std::string string_value; |
| 293 ASSERT_TRUE(root_dict->GetString("string", &string_value)); | 281 ASSERT_TRUE(root_dict->GetString("string", &string_value)); |
| 294 ASSERT_EQ("hello", string_value); | 282 ASSERT_EQ("hello", string_value); |
| 295 | 283 |
| 296 // Now try writing. | 284 // Now try writing. |
| 297 const FilePath written_file_path = | 285 const FilePath written_file_path = |
| 298 test_dir_.Append(FILE_PATH_LITERAL("test_output.js")); | 286 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); |
| 299 | 287 |
| 300 ASSERT_FALSE(file_util::PathExists(written_file_path)); | 288 ASSERT_FALSE(file_util::PathExists(written_file_path)); |
| 301 JSONFileValueSerializer serializer(written_file_path); | 289 JSONFileValueSerializer serializer(written_file_path); |
| 302 ASSERT_TRUE(serializer.Serialize(*root)); | 290 ASSERT_TRUE(serializer.Serialize(*root)); |
| 303 ASSERT_TRUE(file_util::PathExists(written_file_path)); | 291 ASSERT_TRUE(file_util::PathExists(written_file_path)); |
| 304 | 292 |
| 305 // Now compare file contents. | 293 // Now compare file contents. |
| 306 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 294 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
| 307 written_file_path)); | 295 written_file_path)); |
| 308 EXPECT_TRUE(file_util::Delete(written_file_path, false)); | 296 EXPECT_TRUE(file_util::Delete(written_file_path, false)); |
| 309 } | 297 } |
| 310 | 298 |
| 311 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { | 299 TEST_F(JSONFileValueSerializerTest, RoundtripNested) { |
| 312 FilePath original_file_path; | 300 FilePath original_file_path; |
| 313 ASSERT_TRUE( | 301 ASSERT_TRUE( |
| 314 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); | 302 PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); |
| 315 original_file_path = | 303 original_file_path = |
| 316 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); | 304 original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); |
| 317 | 305 |
| 318 ASSERT_TRUE(file_util::PathExists(original_file_path)); | 306 ASSERT_TRUE(file_util::PathExists(original_file_path)); |
| 319 | 307 |
| 320 JSONFileValueSerializer deserializer(original_file_path); | 308 JSONFileValueSerializer deserializer(original_file_path); |
| 321 scoped_ptr<Value> root; | 309 scoped_ptr<Value> root; |
| 322 root.reset(deserializer.Deserialize(NULL, NULL)); | 310 root.reset(deserializer.Deserialize(NULL, NULL)); |
| 323 ASSERT_TRUE(root.get()); | 311 ASSERT_TRUE(root.get()); |
| 324 | 312 |
| 325 // Now try writing. | 313 // Now try writing. |
| 326 FilePath written_file_path = | 314 FilePath written_file_path = |
| 327 test_dir_.Append(FILE_PATH_LITERAL("test_output.js")); | 315 temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); |
| 328 | 316 |
| 329 ASSERT_FALSE(file_util::PathExists(written_file_path)); | 317 ASSERT_FALSE(file_util::PathExists(written_file_path)); |
| 330 JSONFileValueSerializer serializer(written_file_path); | 318 JSONFileValueSerializer serializer(written_file_path); |
| 331 ASSERT_TRUE(serializer.Serialize(*root)); | 319 ASSERT_TRUE(serializer.Serialize(*root)); |
| 332 ASSERT_TRUE(file_util::PathExists(written_file_path)); | 320 ASSERT_TRUE(file_util::PathExists(written_file_path)); |
| 333 | 321 |
| 334 // Now compare file contents. | 322 // Now compare file contents. |
| 335 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, | 323 EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, |
| 336 written_file_path)); | 324 written_file_path)); |
| 337 EXPECT_TRUE(file_util::Delete(written_file_path, false)); | 325 EXPECT_TRUE(file_util::Delete(written_file_path, false)); |
| 338 } | 326 } |
| 339 | 327 |
| 340 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { | 328 TEST_F(JSONFileValueSerializerTest, NoWhitespace) { |
| 341 FilePath source_file_path; | 329 FilePath source_file_path; |
| 342 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); | 330 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); |
| 343 source_file_path = source_file_path.Append( | 331 source_file_path = source_file_path.Append( |
| 344 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); | 332 FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); |
| 345 ASSERT_TRUE(file_util::PathExists(source_file_path)); | 333 ASSERT_TRUE(file_util::PathExists(source_file_path)); |
| 346 JSONFileValueSerializer serializer(source_file_path); | 334 JSONFileValueSerializer serializer(source_file_path); |
| 347 scoped_ptr<Value> root; | 335 scoped_ptr<Value> root; |
| 348 root.reset(serializer.Deserialize(NULL, NULL)); | 336 root.reset(serializer.Deserialize(NULL, NULL)); |
| 349 ASSERT_TRUE(root.get()); | 337 ASSERT_TRUE(root.get()); |
| 350 } | 338 } |
| OLD | NEW |