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.
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/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"
18 class FileMetaDataParserTest
: public testing::Test
{
20 virtual void SetUp() {
21 // Create a temporary directory for testing and fill it with a file.
22 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
24 test_file_
= temp_dir_
.path().AppendASCII("FileMetaDataParserTest");
26 // Create the test file.
27 std::string content
= "content";
28 int write_size
= file_util::WriteFile(test_file_
, content
.c_str(),
30 ASSERT_EQ(static_cast<int>(content
.length()), write_size
);
33 std::string
test_file_str() {
35 return test_file_
.BaseName().value();
37 return WideToASCII(test_file_
.BaseName().value());
38 #endif // defined(OS_POSIX)
41 std::string
test_file_size() {
43 EXPECT_TRUE(base::GetFileSize(test_file_
, &size
));
45 return base::Int64ToString(size
);
48 base::ScopedTempDir temp_dir_
;
49 base::FilePath test_file_
;
52 TEST_F(FileMetaDataParserTest
, Parse
) {
53 FileMetadataParser
parser(test_file_
);
55 EXPECT_TRUE(parser
.Parse());
58 EXPECT_TRUE(parser
.GetProperty(MetadataParser::kPropertyTitle
, &value
));
59 EXPECT_EQ(test_file_str(), value
);
61 // Verify the file size property.
62 EXPECT_TRUE(parser
.GetProperty(MetadataParser::kPropertyFilesize
, &value
));
63 EXPECT_EQ(test_file_size(), value
);
65 // FileMetadataParser does not set kPropertyType.
66 EXPECT_FALSE(parser
.GetProperty(MetadataParser::kPropertyType
, &value
));
69 TEST_F(FileMetaDataParserTest
, PropertyIterator
) {
70 FileMetadataParser
parser(test_file_
);
72 EXPECT_TRUE(parser
.Parse());
74 scoped_ptr
<MetadataPropertyIterator
> iter(parser
.GetPropertyIterator());
75 ASSERT_NE(static_cast<MetadataPropertyIterator
*>(NULL
), iter
.get());
76 ASSERT_EQ(2, iter
->Length());
78 std::map
<std::string
, std::string
> expectations
;
79 expectations
[MetadataParser::kPropertyFilesize
] = test_file_size();
80 expectations
[MetadataParser::kPropertyTitle
] = test_file_str();
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
88 ASSERT_TRUE(expectations
.find(key
) != expectations
.end());
89 EXPECT_EQ(expectations
[key
], value
);
91 expectations
.erase(key
);
94 EXPECT_TRUE(iter
->IsEnd());
95 EXPECT_FALSE(iter
->GetNext(&key
, &value
));
96 EXPECT_TRUE(expectations
.empty());