diff options
-rw-r--r-- | chrome/browser/parsers/metadata_parser_filebase.h | 4 | ||||
-rw-r--r-- | chrome/browser/parsers/metadata_parser_filebase_unittest.cc | 98 | ||||
-rwxr-xr-x | chrome/chrome_tests.gypi | 1 |
3 files changed, 101 insertions, 2 deletions
diff --git a/chrome/browser/parsers/metadata_parser_filebase.h b/chrome/browser/parsers/metadata_parser_filebase.h index b49ebfe..c0ecfe4 100644 --- a/chrome/browser/parsers/metadata_parser_filebase.h +++ b/chrome/browser/parsers/metadata_parser_filebase.h @@ -1,4 +1,4 @@ -// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -10,7 +10,7 @@ typedef base::hash_map<std::string, std::string> PropertyMap; // Parser for the file type. Allows for parsing of files, and gets -// properties assiciated with files. +// properties associated with files. class FileMetadataParser : public MetadataParser { public: explicit FileMetadataParser(const FilePath& path); diff --git a/chrome/browser/parsers/metadata_parser_filebase_unittest.cc b/chrome/browser/parsers/metadata_parser_filebase_unittest.cc new file mode 100644 index 0000000..0e954e6 --- /dev/null +++ b/chrome/browser/parsers/metadata_parser_filebase_unittest.cc @@ -0,0 +1,98 @@ +// Copyright (c) 2010 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 <map> +#include <string> + +#include "base/file_path.h" +#include "base/file_util.h" +#include "base/scoped_temp_dir.h" +#include "base/string_util.h" +#include "chrome/browser/parsers/metadata_parser_filebase.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +class FileMetaDataParserTest : public testing::Test { + protected: + virtual void SetUp() { + // Create a temporary directory for testing and fill it with a file. + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + + test_file_ = temp_dir_.path().AppendASCII("FileMetaDataParserTest"); + + // Create the test file. + std::string content = "content"; + int write_size = file_util::WriteFile(test_file_, content.c_str(), + content.length()); + ASSERT_EQ(static_cast<int>(content.length()), write_size); + } + + std::string test_file_str() { +#if defined(OS_POSIX) + return test_file_.BaseName().value(); +#elif defined(OS_WIN) + return WideToASCII(test_file_.BaseName().value()); +#endif // defined(OS_POSIX) + } + + std::string test_file_size() { + int64 size; + EXPECT_TRUE(file_util::GetFileSize(test_file_, &size)); + + return Int64ToString(size); + } + + ScopedTempDir temp_dir_; + FilePath test_file_; +}; + +TEST_F(FileMetaDataParserTest, Parse) { + FileMetadataParser parser(test_file_); + + EXPECT_TRUE(parser.Parse()); + + std::string value; + EXPECT_TRUE(parser.GetProperty(MetadataParser::kPropertyTitle, &value)); + EXPECT_EQ(test_file_str(), value); + + // Verify the file size property. + EXPECT_TRUE(parser.GetProperty(MetadataParser::kPropertyFilesize, &value)); + EXPECT_EQ(test_file_size(), value); + + // FileMetadataParser does not set kPropertyType. + EXPECT_FALSE(parser.GetProperty(MetadataParser::kPropertyType, &value)); +} + +TEST_F(FileMetaDataParserTest, PropertyIterator) { + FileMetadataParser parser(test_file_); + + EXPECT_TRUE(parser.Parse()); + + scoped_ptr<MetadataPropertyIterator> iter(parser.GetPropertyIterator()); + ASSERT_NE(static_cast<MetadataPropertyIterator*>(NULL), iter.get()); + ASSERT_EQ(2, iter->Length()); + + std::map<std::string, std::string> expectations; + expectations[MetadataParser::kPropertyFilesize] = test_file_size(); + expectations[MetadataParser::kPropertyTitle] = test_file_str(); + + std::string key, value; + for (int i = 0; i < iter->Length(); ++i) { + EXPECT_FALSE(iter->IsEnd()); + EXPECT_TRUE(iter->GetNext(&key, &value)); + // No ostream operator<< implementation for map iterator, so can't use + // ASSERT_NE. + ASSERT_TRUE(expectations.find(key) != expectations.end()); + EXPECT_EQ(expectations[key], value); + + expectations.erase(key); + } + + EXPECT_TRUE(iter->IsEnd()); + EXPECT_FALSE(iter->GetNext(&key, &value)); + EXPECT_TRUE(expectations.empty()); +} + +} // namespace diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 7aca192..880dee6 100755 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -870,6 +870,7 @@ 'browser/notifications/desktop_notifications_unittest.h', 'browser/notifications/notification_test_util.h', 'browser/page_menu_model_unittest.cc', + 'browser/parsers/metadata_parser_filebase_unittest.cc', 'browser/password_manager/encryptor_unittest.cc', 'browser/password_manager/login_database_unittest.cc', 'browser/password_manager/password_form_data.cc', |