[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 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 | |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 5 | #include "components/autofill/browser/data_driven_test.h" |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 6 | |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/path_service.h" |
| 9 | #include "base/string_util.h" |
| 10 | #include "chrome/common/chrome_paths.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 13 | namespace autofill { |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | // Reads |file| into |content|, and converts Windows line-endings to Unix ones. |
| 17 | // Returns true on success. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 18 | bool ReadFile(const base::FilePath& file, std::string* content) { |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 19 | if (!file_util::ReadFileToString(file, content)) |
| 20 | return false; |
| 21 | |
| 22 | ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n"); |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | // Write |content| to |file|. Returns true on success. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 27 | bool WriteFile(const base::FilePath& file, const std::string& content) { |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 28 | int write_size = file_util::WriteFile(file, content.c_str(), |
| 29 | content.length()); |
| 30 | return write_size == static_cast<int>(content.length()); |
| 31 | } |
| 32 | |
| 33 | } // namespace |
| 34 | |
| 35 | void DataDrivenTest::RunDataDrivenTest( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 36 | const base::FilePath& input_directory, |
| 37 | const base::FilePath& output_directory, |
| 38 | const base::FilePath::StringType& file_name_pattern) { |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 39 | file_util::FileEnumerator input_files(input_directory, |
| 40 | false, |
| 41 | file_util::FileEnumerator::FILES, |
| 42 | file_name_pattern); |
| 43 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 44 | for (base::FilePath input_file = input_files.Next(); |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 45 | !input_file.empty(); |
| 46 | input_file = input_files.Next()) { |
[email protected] | 1ea35dc3 | 2011-03-27 11:42:16 | [diff] [blame] | 47 | SCOPED_TRACE(input_file.BaseName().value()); |
| 48 | |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 49 | std::string input; |
| 50 | ASSERT_TRUE(ReadFile(input_file, &input)); |
| 51 | |
| 52 | std::string output; |
| 53 | GenerateResults(input, &output); |
| 54 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 55 | base::FilePath output_file = output_directory.Append( |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 56 | input_file.BaseName().StripTrailingSeparators().ReplaceExtension( |
| 57 | FILE_PATH_LITERAL(".out"))); |
| 58 | |
| 59 | std::string output_file_contents; |
| 60 | if (ReadFile(output_file, &output_file_contents)) |
| 61 | EXPECT_EQ(output_file_contents, output); |
| 62 | else |
| 63 | ASSERT_TRUE(WriteFile(output_file, output)); |
| 64 | } |
| 65 | } |
| 66 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 67 | base::FilePath DataDrivenTest::GetInputDirectory( |
| 68 | const base::FilePath::StringType& test_name) { |
| 69 | base::FilePath test_data_dir_; |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 70 | PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 71 | test_data_dir_ = test_data_dir_.AppendASCII("autofill") |
| 72 | .Append(test_name) |
| 73 | .AppendASCII("input"); |
| 74 | return test_data_dir_; |
| 75 | } |
| 76 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 77 | base::FilePath DataDrivenTest::GetOutputDirectory( |
| 78 | const base::FilePath::StringType& test_name) { |
| 79 | base::FilePath test_data_dir_; |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 80 | PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); |
| 81 | test_data_dir_ = test_data_dir_.AppendASCII("autofill") |
| 82 | .Append(test_name) |
| 83 | .AppendASCII("output"); |
| 84 | return test_data_dir_; |
| 85 | } |
| 86 | |
| 87 | DataDrivenTest::DataDrivenTest() { |
| 88 | } |
| 89 | |
| 90 | DataDrivenTest::~DataDrivenTest() { |
| 91 | } |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 92 | |
| 93 | } // namespace autofill |