| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/files/file_enumerator.h" |
| 11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
| 14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "components/autofill/core/browser/autofill_test_utils.h" | 18 #include "components/autofill/core/browser/autofill_test_utils.h" |
| 18 #include "components/autofill/core/browser/autofill_type.h" | 19 #include "components/autofill/core/browser/autofill_type.h" |
| 19 #include "components/autofill/core/browser/country_names.h" | 20 #include "components/autofill/core/browser/country_names.h" |
| 20 #include "components/autofill/core/browser/data_driven_test.h" | 21 #include "components/autofill/core/browser/data_driven_test.h" |
| 21 #include "components/autofill/core/browser/form_structure.h" | 22 #include "components/autofill/core/browser/form_structure.h" |
| 22 #include "components/autofill/core/browser/personal_data_manager.h" | 23 #include "components/autofill/core/browser/personal_data_manager.h" |
| 23 #include "components/autofill/core/common/form_data.h" | 24 #include "components/autofill/core/common/form_data.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 28 #if defined(OS_MACOSX) |
| 29 #include "base/mac/foundation_util.h" |
| 30 #endif |
| 31 |
| 27 namespace autofill { | 32 namespace autofill { |
| 28 | 33 |
| 29 namespace { | 34 namespace { |
| 30 | 35 |
| 31 const base::FilePath::CharType kTestName[] = FILE_PATH_LITERAL("merge"); | 36 const base::FilePath::CharType kTestName[] = FILE_PATH_LITERAL("merge"); |
| 32 const base::FilePath::CharType kFileNamePattern[] = FILE_PATH_LITERAL("*.in"); | 37 const base::FilePath::CharType kFileNamePattern[] = FILE_PATH_LITERAL("*.in"); |
| 33 | 38 |
| 34 const char kFieldSeparator[] = ": "; | 39 const char kFieldSeparator[] = ": "; |
| 35 const char kProfileSeparator[] = "---"; | 40 const char kProfileSeparator[] = "---"; |
| 36 const size_t kFieldOffset = arraysize(kFieldSeparator) - 1; | 41 const size_t kFieldOffset = arraysize(kFieldSeparator) - 1; |
| 37 | 42 |
| 38 const ServerFieldType kProfileFieldTypes[] = { | 43 const ServerFieldType kProfileFieldTypes[] = {NAME_FIRST, |
| 39 NAME_FIRST, | 44 NAME_MIDDLE, |
| 40 NAME_MIDDLE, | 45 NAME_LAST, |
| 41 NAME_LAST, | 46 NAME_FULL, |
| 42 EMAIL_ADDRESS, | 47 EMAIL_ADDRESS, |
| 43 COMPANY_NAME, | 48 COMPANY_NAME, |
| 44 ADDRESS_HOME_LINE1, | 49 ADDRESS_HOME_STREET_ADDRESS, |
| 45 ADDRESS_HOME_LINE2, | 50 ADDRESS_HOME_CITY, |
| 46 ADDRESS_HOME_CITY, | 51 ADDRESS_HOME_STATE, |
| 47 ADDRESS_HOME_STATE, | 52 ADDRESS_HOME_ZIP, |
| 48 ADDRESS_HOME_ZIP, | 53 ADDRESS_HOME_COUNTRY, |
| 49 ADDRESS_HOME_COUNTRY, | 54 PHONE_HOME_WHOLE_NUMBER}; |
| 50 PHONE_HOME_WHOLE_NUMBER | |
| 51 }; | |
| 52 | 55 |
| 53 const base::FilePath& GetTestDataDir() { | 56 const base::FilePath& GetTestDataDir() { |
| 54 CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ()); | 57 CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ()); |
| 55 if (dir.empty()) { | 58 if (dir.empty()) { |
| 56 PathService::Get(base::DIR_SOURCE_ROOT, &dir); | 59 PathService::Get(base::DIR_SOURCE_ROOT, &dir); |
| 57 dir = dir.AppendASCII("components"); | 60 dir = dir.AppendASCII("components"); |
| 58 dir = dir.AppendASCII("test"); | 61 dir = dir.AppendASCII("test"); |
| 59 dir = dir.AppendASCII("data"); | 62 dir = dir.AppendASCII("data"); |
| 60 } | 63 } |
| 61 return dir; | 64 return dir; |
| 62 } | 65 } |
| 63 | 66 |
| 67 const std::vector<base::FilePath> GetTestFiles() { |
| 68 base::FilePath dir = GetTestDataDir(); |
| 69 dir = dir.AppendASCII("autofill").AppendASCII("merge").AppendASCII("input"); |
| 70 base::FileEnumerator input_files(dir, false, base::FileEnumerator::FILES, |
| 71 kFileNamePattern); |
| 72 std::vector<base::FilePath> files; |
| 73 for (base::FilePath input_file = input_files.Next(); !input_file.empty(); |
| 74 input_file = input_files.Next()) { |
| 75 files.push_back(input_file); |
| 76 } |
| 77 std::sort(files.begin(), files.end()); |
| 78 |
| 79 #if defined(OS_MACOSX) |
| 80 base::mac::ClearAmIBundledCache(); |
| 81 #endif // defined(OS_MACOSX) |
| 82 |
| 83 return files; |
| 84 } |
| 85 |
| 64 // Serializes the |profiles| into a string. | 86 // Serializes the |profiles| into a string. |
| 65 std::string SerializeProfiles(const std::vector<AutofillProfile*>& profiles) { | 87 std::string SerializeProfiles(const std::vector<AutofillProfile*>& profiles) { |
| 66 std::string result; | 88 std::string result; |
| 67 for (size_t i = 0; i < profiles.size(); ++i) { | 89 for (size_t i = 0; i < profiles.size(); ++i) { |
| 68 result += kProfileSeparator; | 90 result += kProfileSeparator; |
| 69 result += "\n"; | 91 result += "\n"; |
| 70 for (size_t j = 0; j < arraysize(kProfileFieldTypes); ++j) { | 92 for (size_t j = 0; j < arraysize(kProfileFieldTypes); ++j) { |
| 71 ServerFieldType type = kProfileFieldTypes[j]; | 93 ServerFieldType type = kProfileFieldTypes[j]; |
| 72 base::string16 value = profiles[i]->GetRawInfo(type); | 94 base::string16 value = profiles[i]->GetRawInfo(type); |
| 73 result += AutofillType(type).ToString(); | 95 result += AutofillType(type).ToString(); |
| 74 result += kFieldSeparator; | 96 result += kFieldSeparator; |
| 97 base::ReplaceFirstSubstringAfterOffset( |
| 98 &value, 0, base::ASCIIToUTF16("\n"), base::ASCIIToUTF16("\\n")); |
| 75 result += base::UTF16ToUTF8(value); | 99 result += base::UTF16ToUTF8(value); |
| 76 result += "\n"; | 100 result += "\n"; |
| 77 } | 101 } |
| 78 } | 102 } |
| 79 | 103 |
| 80 return result; | 104 return result; |
| 81 } | 105 } |
| 82 | 106 |
| 83 class PersonalDataManagerMock : public PersonalDataManager { | 107 class PersonalDataManagerMock : public PersonalDataManager { |
| 84 public: | 108 public: |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 return profiles_.get(); | 148 return profiles_.get(); |
| 125 } | 149 } |
| 126 | 150 |
| 127 } // namespace | 151 } // namespace |
| 128 | 152 |
| 129 // A data-driven test for verifying merging of Autofill profiles. Each input is | 153 // A data-driven test for verifying merging of Autofill profiles. Each input is |
| 130 // a structured dump of a set of implicitly detected autofill profiles. The | 154 // a structured dump of a set of implicitly detected autofill profiles. The |
| 131 // corresponding output file is a dump of the saved profiles that result from | 155 // corresponding output file is a dump of the saved profiles that result from |
| 132 // importing the input profiles. The output file format is identical to the | 156 // importing the input profiles. The output file format is identical to the |
| 133 // input format. | 157 // input format. |
| 134 class AutofillMergeTest : public testing::Test, | 158 class AutofillMergeTest : public DataDrivenTest, |
| 135 public DataDrivenTest { | 159 public testing::TestWithParam<base::FilePath> { |
| 136 protected: | 160 protected: |
| 137 AutofillMergeTest(); | 161 AutofillMergeTest(); |
| 138 ~AutofillMergeTest() override; | 162 ~AutofillMergeTest() override; |
| 139 | 163 |
| 140 // testing::Test: | 164 // testing::Test: |
| 141 void SetUp() override; | 165 void SetUp() override; |
| 142 | 166 |
| 143 // DataDrivenTest: | 167 // DataDrivenTest: |
| 144 void GenerateResults(const std::string& input, std::string* output) override; | 168 void GenerateResults(const std::string& input, std::string* output) override; |
| 145 | 169 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 FormData form; | 211 FormData form; |
| 188 form.name = base::ASCIIToUTF16("MyTestForm"); | 212 form.name = base::ASCIIToUTF16("MyTestForm"); |
| 189 form.origin = GURL("https://www.example.com/origin.html"); | 213 form.origin = GURL("https://www.example.com/origin.html"); |
| 190 form.action = GURL("https://www.example.com/action.html"); | 214 form.action = GURL("https://www.example.com/action.html"); |
| 191 | 215 |
| 192 // Parse the input line by line. | 216 // Parse the input line by line. |
| 193 std::vector<std::string> lines = base::SplitString( | 217 std::vector<std::string> lines = base::SplitString( |
| 194 profiles, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | 218 profiles, "\n", base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 195 for (size_t i = 0; i < lines.size(); ++i) { | 219 for (size_t i = 0; i < lines.size(); ++i) { |
| 196 std::string line = lines[i]; | 220 std::string line = lines[i]; |
| 197 | |
| 198 if (line != kProfileSeparator) { | 221 if (line != kProfileSeparator) { |
| 199 // Add a field to the current profile. | 222 // Add a field to the current profile. |
| 200 size_t separator_pos = line.find(kFieldSeparator); | 223 size_t separator_pos = line.find(kFieldSeparator); |
| 201 ASSERT_NE(std::string::npos, separator_pos); | 224 ASSERT_NE(std::string::npos, separator_pos); |
| 202 base::string16 field_type = | 225 base::string16 field_type = |
| 203 base::UTF8ToUTF16(line.substr(0, separator_pos)); | 226 base::UTF8ToUTF16(line.substr(0, separator_pos)); |
| 204 base::string16 value = | 227 base::string16 value = |
| 205 base::UTF8ToUTF16(line.substr(separator_pos + kFieldOffset)); | 228 base::UTF8ToUTF16(line.substr(separator_pos + kFieldOffset)); |
| 206 | 229 |
| 207 FormFieldData field; | 230 FormFieldData field; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 } | 263 } |
| 241 } | 264 } |
| 242 | 265 |
| 243 *merged_profiles = SerializeProfiles(personal_data_.web_profiles()); | 266 *merged_profiles = SerializeProfiles(personal_data_.web_profiles()); |
| 244 } | 267 } |
| 245 | 268 |
| 246 ServerFieldType AutofillMergeTest::StringToFieldType(const std::string& str) { | 269 ServerFieldType AutofillMergeTest::StringToFieldType(const std::string& str) { |
| 247 return string_to_field_type_map_[str]; | 270 return string_to_field_type_map_[str]; |
| 248 } | 271 } |
| 249 | 272 |
| 250 TEST_F(AutofillMergeTest, DataDrivenMergeProfiles) { | 273 TEST_P(AutofillMergeTest, DataDrivenMergeProfiles) { |
| 251 RunDataDrivenTest(GetInputDirectory(kTestName), GetOutputDirectory(kTestName), | 274 RunOneDataDrivenTest(GetParam(), GetOutputDirectory(kTestName)); |
| 252 kFileNamePattern); | |
| 253 } | 275 } |
| 254 | 276 |
| 277 INSTANTIATE_TEST_CASE_P(, AutofillMergeTest, testing::ValuesIn(GetTestFiles())); |
| 278 |
| 255 } // namespace autofill | 279 } // namespace autofill |
| OLD | NEW |