ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / data_driven_test.cc
blob01d37a2fe64b9c5b049b52b37df8c91ecde1c09b
1 // Copyright 2013 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.
5 #include "components/autofill/core/browser/data_driven_test.h"
7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace autofill {
13 namespace {
15 // Reads |file| into |content|, and converts Windows line-endings to Unix ones.
16 // Returns true on success.
17 bool ReadFile(const base::FilePath& file, std::string* content) {
18 if (!base::ReadFileToString(file, content))
19 return false;
21 ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n");
22 return true;
25 // Write |content| to |file|. Returns true on success.
26 bool WriteFile(const base::FilePath& file, const std::string& content) {
27 int write_size = base::WriteFile(file, content.c_str(),
28 static_cast<int>(content.length()));
29 return write_size == static_cast<int>(content.length());
32 } // namespace
34 void DataDrivenTest::RunDataDrivenTest(
35 const base::FilePath& input_directory,
36 const base::FilePath& output_directory,
37 const base::FilePath::StringType& file_name_pattern) {
38 ASSERT_TRUE(base::DirectoryExists(input_directory));
39 ASSERT_TRUE(base::DirectoryExists(output_directory));
40 base::FileEnumerator input_files(input_directory,
41 false,
42 base::FileEnumerator::FILES,
43 file_name_pattern);
45 for (base::FilePath input_file = input_files.Next();
46 !input_file.empty();
47 input_file = input_files.Next()) {
48 RunOneDataDrivenTest(input_file, output_directory);
52 void DataDrivenTest::RunOneDataDrivenTest(
53 const base::FilePath& test_file_name,
54 const base::FilePath& output_directory) {
55 ASSERT_TRUE(base::DirectoryExists(output_directory));
56 SCOPED_TRACE(test_file_name.BaseName().value());
58 std::string input;
59 ReadFile(test_file_name, &input);
61 std::string output;
62 GenerateResults(input, &output);
64 base::FilePath output_file = output_directory.Append(
65 test_file_name.BaseName().StripTrailingSeparators().ReplaceExtension(
66 FILE_PATH_LITERAL(".out")));
68 std::string output_file_contents;
69 if (ReadFile(output_file, &output_file_contents))
70 EXPECT_EQ(output_file_contents, output);
71 else
72 ASSERT_TRUE(WriteFile(output_file, output));
75 base::FilePath DataDrivenTest::GetInputDirectory(
76 const base::FilePath::StringType& test_name) {
77 base::FilePath dir;
78 dir = test_data_directory_.AppendASCII("autofill")
79 .Append(test_name)
80 .AppendASCII("input");
81 return dir;
84 base::FilePath DataDrivenTest::GetOutputDirectory(
85 const base::FilePath::StringType& test_name) {
86 base::FilePath dir;
87 dir = test_data_directory_.AppendASCII("autofill")
88 .Append(test_name)
89 .AppendASCII("output");
90 return dir;
93 DataDrivenTest::DataDrivenTest(const base::FilePath& test_data_directory)
94 : test_data_directory_(test_data_directory) {
97 DataDrivenTest::~DataDrivenTest() {
100 } // namespace autofill