Skip to content

Commit

Permalink
base: Move the rest of JSONValueSerializer unit tests from c/common t…
Browse files Browse the repository at this point in the history
…o 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
  • Loading branch information
tfarina@chromium.org committed Mar 28, 2013
1 parent 33c8676 commit c4803e4
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 145 deletions.
3 changes: 2 additions & 1 deletion base/base.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
Expand Down
19 changes: 14 additions & 5 deletions base/base_paths.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 14 additions & 11 deletions base/base_paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
99 changes: 99 additions & 0 deletions base/json/json_value_serializer_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
File renamed without changes.
File renamed without changes.
10 changes: 7 additions & 3 deletions build/android/pylib/gtest/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
1 change: 0 additions & 1 deletion chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
13 changes: 7 additions & 6 deletions chrome/common/json_value_serializer_perftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
118 changes: 0 additions & 118 deletions chrome/common/json_value_serializer_unittest.cc

This file was deleted.

0 comments on commit c4803e4

Please sign in to comment.