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 3 // found in the LICENSE file. 4 5 #include <map> 6 #include <string> 7 8 #include "base/file_path.h" 9 #include "base/file_util.h" 10 #include "base/memory/scoped_temp_dir.h" 11 #include "base/string_number_conversions.h" 12 #include "base/string_util.h" // TODO(brettw) remove when WideToASCII moves. 13 #include "chrome/browser/parsers/metadata_parser_filebase.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace { 17 18 class FileMetaDataParserTest : public testing::Test { 19 protected: 20 virtual void SetUp() { 21 // Create a temporary directory for testing and fill it with a file. 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 23 24 test_file_ = temp_dir_.path().AppendASCII("FileMetaDataParserTest"); 25 26 // Create the test file. 27 std::string content = "content"; 28 int write_size = file_util::WriteFile(test_file_, content.c_str(), 29 content.length()); 30 ASSERT_EQ(static_cast<int>(content.length()), write_size); 31 } 32 33 std::string test_file_str() { 34 #if defined(OS_POSIX) 35 return test_file_.BaseName().value(); 36 #elif defined(OS_WIN) 37 return WideToASCII(test_file_.BaseName().value()); 38 #endif // defined(OS_POSIX) 39 } 40 41 std::string test_file_size() { 42 int64 size; 43 EXPECT_TRUE(file_util::GetFileSize(test_file_, &size)); 44 45 return base::Int64ToString(size); 46 } 47 48 ScopedTempDir temp_dir_; 49 FilePath test_file_; 50 }; 51 52 TEST_F(FileMetaDataParserTest, Parse) { 53 FileMetadataParser parser(test_file_); 54 55 EXPECT_TRUE(parser.Parse()); 56 57 std::string value; 58 EXPECT_TRUE(parser.GetProperty(MetadataParser::kPropertyTitle, &value)); 59 EXPECT_EQ(test_file_str(), value); 60 61 // Verify the file size property. 62 EXPECT_TRUE(parser.GetProperty(MetadataParser::kPropertyFilesize, &value)); 63 EXPECT_EQ(test_file_size(), value); 64 65 // FileMetadataParser does not set kPropertyType. 66 EXPECT_FALSE(parser.GetProperty(MetadataParser::kPropertyType, &value)); 67 } 68 69 TEST_F(FileMetaDataParserTest, PropertyIterator) { 70 FileMetadataParser parser(test_file_); 71 72 EXPECT_TRUE(parser.Parse()); 73 74 scoped_ptr<MetadataPropertyIterator> iter(parser.GetPropertyIterator()); 75 ASSERT_NE(static_cast<MetadataPropertyIterator*>(NULL), iter.get()); 76 ASSERT_EQ(2, iter->Length()); 77 78 std::map<std::string, std::string> expectations; 79 expectations[MetadataParser::kPropertyFilesize] = test_file_size(); 80 expectations[MetadataParser::kPropertyTitle] = test_file_str(); 81 82 std::string key, value; 83 for (int i = 0; i < iter->Length(); ++i) { 84 EXPECT_FALSE(iter->IsEnd()); 85 EXPECT_TRUE(iter->GetNext(&key, &value)); 86 // No ostream operator<< implementation for map iterator, so can't use 87 // ASSERT_NE. 88 ASSERT_TRUE(expectations.find(key) != expectations.end()); 89 EXPECT_EQ(expectations[key], value); 90 91 expectations.erase(key); 92 } 93 94 EXPECT_TRUE(iter->IsEnd()); 95 EXPECT_FALSE(iter->GetNext(&key, &value)); 96 EXPECT_TRUE(expectations.empty()); 97 } 98 99 } // namespace 100