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