diff options
author | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-28 00:40:04 +0000 |
---|---|---|
committer | tfarina@chromium.org <tfarina@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-28 00:40:04 +0000 |
commit | c4803e43232864180e5db4fb3b8d185dc0c5ba84 (patch) | |
tree | b22d2ccf1a6f384698261627d91c55838d815657 | |
parent | 33c867665307e75078bea129ea7cf2a94fda1395 (diff) | |
download | chromium_src-c4803e43232864180e5db4fb3b8d185dc0c5ba84.zip chromium_src-c4803e43232864180e5db4fb3b8d185dc0c5ba84.tar.gz chromium_src-c4803e43232864180e5db4fb3b8d185dc0c5ba84.tar.bz2 |
base: Move the rest of JSONValueSerializer unit tests from c/common to base/json.
This is the follow up patch to r189315 - https://codereview.chromium.org/12910004
where we moved the half of tests that didn't depend on json test files.
json_value_serializer_perftest.cc was not moved yet because it depends on
chrome/common/logging_chrome.h
TEST=base_unittests --gtest_filter=*JSON*
TEST=perf_tests --gtest_filter=JSONValueSerializerTests*
R=darin@chromium.org,bulach@chromium.org
Review URL: https://chromiumcodereview.appspot.com/12481028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191077 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/base.gyp | 3 | ||||
-rw-r--r-- | base/base_paths.cc | 19 | ||||
-rw-r--r-- | base/base_paths.h | 25 | ||||
-rw-r--r-- | base/json/json_value_serializer_unittest.cc | 99 | ||||
-rw-r--r-- | base/test/data/serializer_nested_test.json (renamed from chrome/test/data/serializer_nested_test.js) | 0 | ||||
-rw-r--r-- | base/test/data/serializer_test.json (renamed from chrome/test/data/serializer_test.js) | 0 | ||||
-rw-r--r-- | base/test/data/serializer_test_nowhitespace.json (renamed from chrome/test/data/serializer_test_nowhitespace.js) | 0 | ||||
-rw-r--r-- | build/android/pylib/gtest/test_runner.py | 10 | ||||
-rw-r--r-- | chrome/chrome_tests_unit.gypi | 1 | ||||
-rw-r--r-- | chrome/common/json_value_serializer_perftest.cc | 13 | ||||
-rw-r--r-- | chrome/common/json_value_serializer_unittest.cc | 118 |
11 files changed, 143 insertions, 145 deletions
diff --git a/base/base.gyp b/base/base.gyp index ef1d60f..a120182 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -699,9 +699,10 @@ 'action_name': 'copy_test_data', 'variables': { 'test_data_files': [ - 'data/json/bom_feff.json', 'data/file_util_unittest', + 'data/json/bom_feff.json', 'prefs/test/data/pref_service', + 'test/data', ], 'test_data_prefix': 'base', }, diff --git a/base/base_paths.cc b/base/base_paths.cc index 05a55af..b90efba 100644 --- a/base/base_paths.cc +++ b/base/base_paths.cc @@ -15,18 +15,27 @@ bool PathProvider(int key, FilePath* result) { FilePath cur; switch (key) { - case base::DIR_EXE: - PathService::Get(base::FILE_EXE, &cur); + case DIR_EXE: + PathService::Get(FILE_EXE, &cur); cur = cur.DirName(); break; - case base::DIR_MODULE: - PathService::Get(base::FILE_MODULE, &cur); + case DIR_MODULE: + PathService::Get(FILE_MODULE, &cur); cur = cur.DirName(); break; - case base::DIR_TEMP: + case DIR_TEMP: if (!file_util::GetTempDir(&cur)) return false; break; + case DIR_TEST_DATA: + if (!PathService::Get(DIR_SOURCE_ROOT, &cur)) + return false; + cur = cur.Append(FILE_PATH_LITERAL("base")); + cur = cur.Append(FILE_PATH_LITERAL("test")); + cur = cur.Append(FILE_PATH_LITERAL("data")); + if (!file_util::PathExists(cur)) // We don't want to create this. + return false; + break; default: return false; } diff --git a/base/base_paths.h b/base/base_paths.h index 3251a84..f9601a2 100644 --- a/base/base_paths.h +++ b/base/base_paths.h @@ -27,19 +27,22 @@ namespace base { enum BasePathKey { PATH_START = 0, - DIR_CURRENT, // current directory - DIR_EXE, // directory containing FILE_EXE - DIR_MODULE, // directory containing FILE_MODULE - DIR_TEMP, // temporary directory - FILE_EXE, // Path and filename of the current executable. - FILE_MODULE, // Path and filename of the module containing the code for the - // PathService (which could differ from FILE_EXE if the - // PathService were compiled into a shared object, for example). - DIR_SOURCE_ROOT, // Returns the root of the source tree. This key is useful - // for tests that need to locate various resources. It - // should not be used outside of test code. + DIR_CURRENT, // Current directory. + DIR_EXE, // Directory containing FILE_EXE. + DIR_MODULE, // Directory containing FILE_MODULE. + DIR_TEMP, // Temporary directory. + FILE_EXE, // Path and filename of the current executable. + FILE_MODULE, // Path and filename of the module containing the code for + // the PathService (which could differ from FILE_EXE if the + // PathService were compiled into a shared object, for + // example). + DIR_SOURCE_ROOT, // Returns the root of the source tree. This key is useful + // for tests that need to locate various resources. It + // should not be used outside of test code. DIR_USER_DESKTOP, // The current user's Desktop. + DIR_TEST_DATA, // Used only for testing. + PATH_END }; diff --git a/base/json/json_value_serializer_unittest.cc b/base/json/json_value_serializer_unittest.cc index c5bef81..e510500 100644 --- a/base/json/json_value_serializer_unittest.cc +++ b/base/json/json_value_serializer_unittest.cc @@ -11,6 +11,7 @@ #include "base/json/json_string_value_serializer.h" #include "base/json/json_writer.h" #include "base/memory/scoped_ptr.h" +#include "base/path_service.h" #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/values.h" @@ -369,6 +370,104 @@ TEST(JSONValueSerializerTest, JSONReaderComments) { ASSERT_FALSE(root.get()); } +class JSONFileValueSerializerTest : public testing::Test { + protected: + virtual void SetUp() OVERRIDE { + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + } + + base::ScopedTempDir temp_dir_; +}; + +TEST_F(JSONFileValueSerializerTest, Roundtrip) { + base::FilePath original_file_path; + ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); + original_file_path = + original_file_path.Append(FILE_PATH_LITERAL("serializer_test.json")); + + ASSERT_TRUE(file_util::PathExists(original_file_path)); + + JSONFileValueSerializer deserializer(original_file_path); + scoped_ptr<Value> root; + root.reset(deserializer.Deserialize(NULL, NULL)); + + ASSERT_TRUE(root.get()); + ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); + + DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); + + Value* null_value = NULL; + ASSERT_TRUE(root_dict->Get("null", &null_value)); + ASSERT_TRUE(null_value); + ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); + + bool bool_value = false; + ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); + ASSERT_TRUE(bool_value); + + int int_value = 0; + ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); + ASSERT_EQ(42, int_value); + + std::string string_value; + ASSERT_TRUE(root_dict->GetString("string", &string_value)); + ASSERT_EQ("hello", string_value); + + // Now try writing. + const base::FilePath written_file_path = + temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); + + ASSERT_FALSE(file_util::PathExists(written_file_path)); + JSONFileValueSerializer serializer(written_file_path); + ASSERT_TRUE(serializer.Serialize(*root)); + ASSERT_TRUE(file_util::PathExists(written_file_path)); + + // Now compare file contents. + EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, + written_file_path)); + EXPECT_TRUE(file_util::Delete(written_file_path, false)); +} + +TEST_F(JSONFileValueSerializerTest, RoundtripNested) { + base::FilePath original_file_path; + ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_file_path)); + original_file_path = original_file_path.Append( + FILE_PATH_LITERAL("serializer_nested_test.json")); + + ASSERT_TRUE(file_util::PathExists(original_file_path)); + + JSONFileValueSerializer deserializer(original_file_path); + scoped_ptr<Value> root; + root.reset(deserializer.Deserialize(NULL, NULL)); + ASSERT_TRUE(root.get()); + + // Now try writing. + base::FilePath written_file_path = temp_dir_.path().Append( + FILE_PATH_LITERAL("test_output.json")); + + ASSERT_FALSE(file_util::PathExists(written_file_path)); + JSONFileValueSerializer serializer(written_file_path); + ASSERT_TRUE(serializer.Serialize(*root)); + ASSERT_TRUE(file_util::PathExists(written_file_path)); + + // Now compare file contents. + EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, + written_file_path)); + EXPECT_TRUE(file_util::Delete(written_file_path, false)); +} + +TEST_F(JSONFileValueSerializerTest, NoWhitespace) { + base::FilePath source_file_path; + ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &source_file_path)); + source_file_path = source_file_path.Append( + FILE_PATH_LITERAL("serializer_test_nowhitespace.json")); + ASSERT_TRUE(file_util::PathExists(source_file_path)); + JSONFileValueSerializer serializer(source_file_path); + scoped_ptr<Value> root; + root.reset(serializer.Deserialize(NULL, NULL)); + ASSERT_TRUE(root.get()); +} + } // namespace } // namespace base diff --git a/chrome/test/data/serializer_nested_test.js b/base/test/data/serializer_nested_test.json index cfea8e8..cfea8e8 100644 --- a/chrome/test/data/serializer_nested_test.js +++ b/base/test/data/serializer_nested_test.json diff --git a/chrome/test/data/serializer_test.js b/base/test/data/serializer_test.json index 446925e..446925e 100644 --- a/chrome/test/data/serializer_test.js +++ b/base/test/data/serializer_test.json diff --git a/chrome/test/data/serializer_test_nowhitespace.js b/base/test/data/serializer_test_nowhitespace.json index a1afdc5..a1afdc5 100644 --- a/chrome/test/data/serializer_test_nowhitespace.js +++ b/base/test/data/serializer_test_nowhitespace.json diff --git a/build/android/pylib/gtest/test_runner.py b/build/android/pylib/gtest/test_runner.py index 77fa0a5..1be28a2 100644 --- a/build/android/pylib/gtest/test_runner.py +++ b/build/android/pylib/gtest/test_runner.py @@ -35,6 +35,9 @@ def _GetDataFilesForTestSuite(test_suite_basename): 'base/data/file_util_unittest', 'base/data/json/bom_feff.json', 'base/prefs/test/data/pref_service', + 'base/test/data/serializer_nested_test.json', + 'base/test/data/serializer_test.json', + 'base/test/data/serializer_test_nowhitespace.json', 'chrome/test/data/download-test1.lib', 'chrome/test/data/extensions/bad_magic.crx', 'chrome/test/data/extensions/good.crx', @@ -58,9 +61,6 @@ def _GetDataFilesForTestSuite(test_suite_basename): 'chrome/test/data/History/', 'chrome/test/data/json_schema_validator/', 'chrome/test/data/pref_service/', - 'chrome/test/data/serializer_nested_test.js', - 'chrome/test/data/serializer_test.js', - 'chrome/test/data/serializer_test_nowhitespace.js', 'chrome/test/data/top_sites/', 'chrome/test/data/web_app_info/', 'chrome/test/data/web_database', @@ -121,6 +121,10 @@ def _GetDataFilesForTestSuite(test_suite_basename): return [ 'cc/test/data', ] + elif test_suite_basename == 'perf_tests': + return [ + 'base/test/data', + ] elif test_suite_basename == 'content_browsertests': return [ 'content/test/data', diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index 99a7724..9507b05 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -1561,7 +1561,6 @@ 'common/json_schema/json_schema_validator_unittest.cc', 'common/json_schema/json_schema_validator_unittest_base.cc', 'common/json_schema/json_schema_validator_unittest_base.h', - 'common/json_value_serializer_unittest.cc', 'common/mac/cfbundle_blocker_unittest.mm', 'common/mac/mock_launchd.cc', 'common/mac/mock_launchd.h', diff --git a/chrome/common/json_value_serializer_perftest.cc b/chrome/common/json_value_serializer_perftest.cc index 35fa440..ce9482d 100644 --- a/chrome/common/json_value_serializer_perftest.cc +++ b/chrome/common/json_value_serializer_perftest.cc @@ -4,30 +4,31 @@ #include <vector> +#include "base/base_paths.h" #include "base/file_util.h" #include "base/json/json_string_value_serializer.h" #include "base/path_service.h" #include "base/perftimer.h" #include "base/string_util.h" #include "base/values.h" -#include "chrome/common/chrome_paths.h" #include "chrome/common/logging_chrome.h" #include "testing/gtest/include/gtest/gtest.h" namespace { + class JSONValueSerializerTests : public testing::Test { protected: - virtual void SetUp() { + virtual void SetUp() OVERRIDE { static const char* const kTestFilenames[] = { - "serializer_nested_test.js", - "serializer_test.js", - "serializer_test_nowhitespace.js", + "serializer_nested_test.json", + "serializer_test.json", + "serializer_test_nowhitespace.json", }; // Load test cases for (size_t i = 0; i < arraysize(kTestFilenames); ++i) { base::FilePath filename; - EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &filename)); + EXPECT_TRUE(PathService::Get(base::DIR_TEST_DATA, &filename)); filename = filename.AppendASCII(kTestFilenames[i]); std::string test_case; diff --git a/chrome/common/json_value_serializer_unittest.cc b/chrome/common/json_value_serializer_unittest.cc deleted file mode 100644 index 48f971b..0000000 --- a/chrome/common/json_value_serializer_unittest.cc +++ /dev/null @@ -1,118 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/basictypes.h" -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/json/json_file_value_serializer.h" -#include "base/json/json_reader.h" -#include "base/json/json_string_value_serializer.h" -#include "base/json/json_writer.h" -#include "base/path_service.h" -#include "base/string16.h" -#include "base/string_util.h" -#include "base/utf_string_conversions.h" -#include "base/values.h" -#include "chrome/common/chrome_paths.h" -#include "testing/gtest/include/gtest/gtest.h" - -class JSONFileValueSerializerTest : public testing::Test { - protected: - virtual void SetUp() OVERRIDE { - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - } - - base::ScopedTempDir temp_dir_; -}; - -TEST_F(JSONFileValueSerializerTest, Roundtrip) { - base::FilePath original_file_path; - ASSERT_TRUE( - PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); - original_file_path = - original_file_path.Append(FILE_PATH_LITERAL("serializer_test.js")); - - ASSERT_TRUE(file_util::PathExists(original_file_path)); - - JSONFileValueSerializer deserializer(original_file_path); - scoped_ptr<Value> root; - root.reset(deserializer.Deserialize(NULL, NULL)); - - ASSERT_TRUE(root.get()); - ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); - - DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get()); - - Value* null_value = NULL; - ASSERT_TRUE(root_dict->Get("null", &null_value)); - ASSERT_TRUE(null_value); - ASSERT_TRUE(null_value->IsType(Value::TYPE_NULL)); - - bool bool_value = false; - ASSERT_TRUE(root_dict->GetBoolean("bool", &bool_value)); - ASSERT_TRUE(bool_value); - - int int_value = 0; - ASSERT_TRUE(root_dict->GetInteger("int", &int_value)); - ASSERT_EQ(42, int_value); - - std::string string_value; - ASSERT_TRUE(root_dict->GetString("string", &string_value)); - ASSERT_EQ("hello", string_value); - - // Now try writing. - const base::FilePath written_file_path = - temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); - - ASSERT_FALSE(file_util::PathExists(written_file_path)); - JSONFileValueSerializer serializer(written_file_path); - ASSERT_TRUE(serializer.Serialize(*root)); - ASSERT_TRUE(file_util::PathExists(written_file_path)); - - // Now compare file contents. - EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, - written_file_path)); - EXPECT_TRUE(file_util::Delete(written_file_path, false)); -} - -TEST_F(JSONFileValueSerializerTest, RoundtripNested) { - base::FilePath original_file_path; - ASSERT_TRUE( - PathService::Get(chrome::DIR_TEST_DATA, &original_file_path)); - original_file_path = - original_file_path.Append(FILE_PATH_LITERAL("serializer_nested_test.js")); - - ASSERT_TRUE(file_util::PathExists(original_file_path)); - - JSONFileValueSerializer deserializer(original_file_path); - scoped_ptr<Value> root; - root.reset(deserializer.Deserialize(NULL, NULL)); - ASSERT_TRUE(root.get()); - - // Now try writing. - base::FilePath written_file_path = - temp_dir_.path().Append(FILE_PATH_LITERAL("test_output.js")); - - ASSERT_FALSE(file_util::PathExists(written_file_path)); - JSONFileValueSerializer serializer(written_file_path); - ASSERT_TRUE(serializer.Serialize(*root)); - ASSERT_TRUE(file_util::PathExists(written_file_path)); - - // Now compare file contents. - EXPECT_TRUE(file_util::TextContentsEqual(original_file_path, - written_file_path)); - EXPECT_TRUE(file_util::Delete(written_file_path, false)); -} - -TEST_F(JSONFileValueSerializerTest, NoWhitespace) { - base::FilePath source_file_path; - ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_file_path)); - source_file_path = source_file_path.Append( - FILE_PATH_LITERAL("serializer_test_nowhitespace.js")); - ASSERT_TRUE(file_util::PathExists(source_file_path)); - JSONFileValueSerializer serializer(source_file_path); - scoped_ptr<Value> root; - root.reset(serializer.Deserialize(NULL, NULL)); - ASSERT_TRUE(root.get()); -} |