OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/autofill/core/browser/autofill_manager.h" |
| 14 #include "components/autofill/core/browser/data_driven_test.h" |
| 15 #include "components/autofill/ios/browser/autofill_driver_ios.h" |
| 16 #import "ios/chrome/browser/autofill/autofill_agent.h" |
| 17 #import "ios/chrome/browser/autofill/autofill_controller.h" |
| 18 #import "ios/chrome/browser/autofill/form_suggestion_controller.h" |
| 19 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
| 20 #include "ios/chrome/browser/chrome_paths.h" |
| 21 #include "ios/chrome/browser/infobars/infobar_manager_impl.h" |
| 22 #import "ios/chrome/browser/web/chrome_web_test.h" |
| 23 #import "ios/web/public/web_state/web_state.h" |
| 24 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." |
| 27 #endif |
| 28 |
| 29 namespace autofill { |
| 30 |
| 31 namespace { |
| 32 |
| 33 const base::FilePath::CharType kTestName[] = FILE_PATH_LITERAL("heuristics"); |
| 34 |
| 35 const base::FilePath& GetTestDataDir() { |
| 36 CR_DEFINE_STATIC_LOCAL(base::FilePath, dir, ()); |
| 37 if (dir.empty()) |
| 38 PathService::Get(ios::DIR_TEST_DATA, &dir); |
| 39 return dir; |
| 40 } |
| 41 |
| 42 const std::vector<base::FilePath> GetTestFiles() { |
| 43 base::FilePath dir; |
| 44 CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &dir)); |
| 45 dir = dir.AppendASCII("ios/chrome/test/data/autofill") |
| 46 .Append(kTestName) |
| 47 .AppendASCII("input"); |
| 48 base::FileEnumerator input_files(dir, false, base::FileEnumerator::FILES); |
| 49 std::vector<base::FilePath> files; |
| 50 for (base::FilePath input_file = input_files.Next(); !input_file.empty(); |
| 51 input_file = input_files.Next()) { |
| 52 files.push_back(input_file); |
| 53 } |
| 54 std::sort(files.begin(), files.end()); |
| 55 |
| 56 base::mac::ClearAmIBundledCache(); |
| 57 return files; |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
| 62 // Test fixture for verifying Autofill heuristics. Each input is an HTML |
| 63 // file that contains one or more forms. The corresponding output file lists the |
| 64 // heuristically detected type for each field. |
| 65 // This is based on FormStructureBrowserTest from the Chromium Project. |
| 66 // TODO(crbug.com/245246): Unify the tests. |
| 67 class FormStructureBrowserTest |
| 68 : public ChromeWebTest, |
| 69 public DataDrivenTest, |
| 70 public ::testing::WithParamInterface<base::FilePath> { |
| 71 protected: |
| 72 FormStructureBrowserTest(); |
| 73 ~FormStructureBrowserTest() override {} |
| 74 |
| 75 void SetUp() override; |
| 76 void TearDown() override; |
| 77 |
| 78 // DataDrivenTest: |
| 79 void GenerateResults(const std::string& input, std::string* output) override; |
| 80 |
| 81 // Serializes the given |forms| into a string. |
| 82 std::string FormStructuresToString( |
| 83 const std::vector<std::unique_ptr<FormStructure>>& forms); |
| 84 |
| 85 FormSuggestionController* suggestionController_; |
| 86 AutofillController* autofillController_; |
| 87 |
| 88 private: |
| 89 DISALLOW_COPY_AND_ASSIGN(FormStructureBrowserTest); |
| 90 }; |
| 91 |
| 92 FormStructureBrowserTest::FormStructureBrowserTest() |
| 93 : DataDrivenTest(GetTestDataDir()) {} |
| 94 |
| 95 void FormStructureBrowserTest::SetUp() { |
| 96 ChromeWebTest::SetUp(); |
| 97 |
| 98 InfoBarManagerImpl::CreateForWebState(web_state()); |
| 99 AutofillAgent* autofillAgent = |
| 100 [[AutofillAgent alloc] initWithBrowserState:chrome_browser_state_.get() |
| 101 webState:web_state()]; |
| 102 suggestionController_ = |
| 103 [[FormSuggestionController alloc] initWithWebState:web_state() |
| 104 providers:@[ autofillAgent ]]; |
| 105 autofillController_ = [[AutofillController alloc] |
| 106 initWithBrowserState:chrome_browser_state_.get() |
| 107 webState:web_state() |
| 108 autofillAgent:autofillAgent |
| 109 passwordGenerationManager:nullptr |
| 110 downloadEnabled:NO]; |
| 111 } |
| 112 |
| 113 void FormStructureBrowserTest::TearDown() { |
| 114 [autofillController_ detachFromWebState]; |
| 115 |
| 116 ChromeWebTest::TearDown(); |
| 117 } |
| 118 |
| 119 void FormStructureBrowserTest::GenerateResults(const std::string& input, |
| 120 std::string* output) { |
| 121 LoadHtml(input); |
| 122 AutofillManager* autofill_manager = |
| 123 AutofillDriverIOS::FromWebState(web_state())->autofill_manager(); |
| 124 ASSERT_NE(nullptr, autofill_manager); |
| 125 const std::vector<std::unique_ptr<FormStructure>>& forms = |
| 126 autofill_manager->form_structures_; |
| 127 *output = FormStructureBrowserTest::FormStructuresToString(forms); |
| 128 } |
| 129 |
| 130 std::string FormStructureBrowserTest::FormStructuresToString( |
| 131 const std::vector<std::unique_ptr<FormStructure>>& forms) { |
| 132 std::string forms_string; |
| 133 for (const std::unique_ptr<FormStructure>& form : forms) { |
| 134 for (const AutofillField* field : *form) { |
| 135 forms_string += field->Type().ToString(); |
| 136 forms_string += " | " + base::UTF16ToUTF8(field->name); |
| 137 forms_string += " | " + base::UTF16ToUTF8(field->label); |
| 138 forms_string += " | " + base::UTF16ToUTF8(field->value); |
| 139 forms_string += " | " + field->section(); |
| 140 forms_string += "\n"; |
| 141 } |
| 142 } |
| 143 return forms_string; |
| 144 } |
| 145 |
| 146 // Disabled because the tests don't pass yet: http://crbug.com/427614 |
| 147 // Disabled because the tests crash flakily: http://crbug.com/464383 |
| 148 TEST_P(FormStructureBrowserTest, DISABLED_DataDrivenHeuristics) { |
| 149 RunOneDataDrivenTest(GetParam(), GetOutputDirectory(kTestName)); |
| 150 } |
| 151 |
| 152 INSTANTIATE_TEST_CASE_P(AllForms, |
| 153 FormStructureBrowserTest, |
| 154 testing::ValuesIn(GetTestFiles())); |
| 155 |
| 156 } // namespace autofill |
OLD | NEW |