Commit 71527065 authored by Matthias Körber's avatar Matthias Körber Committed by Chromium LUCI CQ

[Autofill] Simplified NameField testing

Change-Id: I638a03fe56d68c184c954239c7d6d07f2c542828
Bug: 1007974
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626428
Commit-Queue: Matthias Körber <koerber@google.com>
Reviewed-by: default avatarChristoph Schwering <schwering@google.com>
Cr-Commit-Position: refs/heads/master@{#844508}
parent efffce8e
......@@ -653,6 +653,8 @@ source_set("unit_tests") {
"form_parsing/field_candidates_unittest.cc",
"form_parsing/form_field_unittest.cc",
"form_parsing/name_field_unittest.cc",
"form_parsing/parsing_test_utils.cc",
"form_parsing/parsing_test_utils.h",
"form_parsing/phone_field_unittest.cc",
"form_parsing/price_field_unittest.cc",
"form_parsing/search_field_unittest.cc",
......
......@@ -29,13 +29,6 @@ class AddressField : public FormField {
const LanguageCode& page_language,
LogManager* log_manager);
#if defined(UNIT_TEST)
// Assign types to the fields for the testing purposes.
void AddClassificationsForTesting(
FieldCandidatesMap* field_candidates_for_testing) const {
AddClassifications(field_candidates_for_testing);
}
#endif
protected:
void AddClassifications(FieldCandidatesMap* field_candidates) const override;
......
......@@ -9,94 +9,24 @@
#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/form_parsing/autofill_scanner.h"
#include "components/autofill/core/browser/form_parsing/parsing_test_utils.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/form_field_data.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
namespace autofill {
class AddressFieldTest : public testing::Test {
class AddressFieldTest : public FormFieldTest {
public:
AddressFieldTest() = default;
AddressFieldTest(const AddressFieldTest&) = delete;
AddressFieldTest& operator=(const AddressFieldTest&) = delete;
// Add a field with |control_type|, the |name|, the |label| the expected
// parsed type |expected_type|.
void AddFormFieldData(std::string control_type,
std::string name,
std::string label,
ServerFieldType expected_type) {
FormFieldData field_data;
field_data.form_control_type = control_type;
field_data.name = base::UTF8ToUTF16(name);
field_data.label = base::UTF8ToUTF16(label);
field_data.unique_renderer_id = MakeFieldRendererId();
list_.push_back(std::make_unique<AutofillField>(field_data));
expected_classifications_.insert(
std::make_pair(field_data.unique_renderer_id, expected_type));
}
// Convenience wrapper for text control elements.
void AddTextFormFieldData(std::string name,
std::string label,
ServerFieldType expected_classification) {
AddFormFieldData("text", name, label, expected_classification);
}
// Apply parsing and verify the expected types.
// |parsed| indicates if at least one field could be parsed successfully.
// |page_language| the language to be used for parsing, default empty value
// means the language is unknown and patterns of all languages are used.
void ClassifyAndVerify(bool parsed = true,
const LanguageCode& page_language = LanguageCode("")) {
AutofillScanner scanner(list_);
field_ = Parse(&scanner, page_language);
if (!parsed) {
ASSERT_EQ(nullptr, field_.get());
return;
}
ASSERT_NE(nullptr, field_.get());
field_->AddClassificationsForTesting(&field_candidates_map_);
for (const std::pair<FieldRendererId, ServerFieldType> it :
expected_classifications_) {
ASSERT_TRUE(field_candidates_map_.find(it.first) !=
field_candidates_map_.end());
EXPECT_EQ(it.second, field_candidates_map_[it.first].BestHeuristicType());
}
}
protected:
// Downcast for tests.
static std::unique_ptr<AddressField> Parse(
AutofillScanner* scanner,
const LanguageCode& page_language) {
std::unique_ptr<FormField> field =
AddressField::Parse(scanner, page_language, nullptr);
return std::unique_ptr<AddressField>(
static_cast<AddressField*>(field.release()));
}
FieldRendererId MakeFieldRendererId() {
return FieldRendererId(++id_counter_);
std::unique_ptr<FormField> Parse(AutofillScanner* scanner,
const LanguageCode& page_language) override {
return AddressField::Parse(scanner, page_language, nullptr);
}
std::vector<std::unique_ptr<AutofillField>> list_;
std::unique_ptr<AddressField> field_;
FieldCandidatesMap field_candidates_map_;
std::map<FieldRendererId, ServerFieldType> expected_classifications_;
private:
uint64_t id_counter_ = 0;
};
TEST_F(AddressFieldTest, Empty) {
......
......@@ -36,7 +36,7 @@ struct RegExLogging {
// name, phone number, or address field.
class FormField {
public:
virtual ~FormField() {}
virtual ~FormField() = default;
// Classifies each field in |fields| with its heuristically detected type.
// Each field has a derived unique name that is used as the key into the
......@@ -47,6 +47,14 @@ class FormField {
bool is_form_tag,
LogManager* log_manager = nullptr);
#if defined(UNIT_TEST)
// Assign types to the fields for the testing purposes.
void AddClassificationsForTesting(
FieldCandidatesMap* field_candidates_for_testing) const {
AddClassifications(field_candidates_for_testing);
}
#endif
protected:
// Initial values assigned to FieldCandidates by their corresponding parsers.
static const float kBaseEmailParserScore;
......
......@@ -28,14 +28,6 @@ class NameField : public FormField {
const LanguageCode& page_language,
LogManager* log_manager);
#ifdef UNIT_TEST
// Calls the protected method |AddClassification| for testing.
void AddClassificationsForTesting(
FieldCandidatesMap* field_candidates) const {
AddClassifications(field_candidates);
}
#endif
protected:
NameField() = default;
......
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/form_parsing/parsing_test_utils.h"
namespace autofill {
FormFieldTest::FormFieldTest() = default;
FormFieldTest::~FormFieldTest() = default;
} // namespace autofill
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_PARSING_TEST_UTILS_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_PARSING_TEST_UTILS_H_
#include <memory>
#include <vector>
#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_field.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_parsing/address_field.h"
#include "components/autofill/core/browser/form_parsing/autofill_scanner.h"
#include "components/autofill/core/common/form_field_data.h"
#include "components/autofill/core/common/language_code.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
class FormFieldTest : public testing::Test {
public:
FormFieldTest(const FormFieldTest&) = delete;
FormFieldTest();
~FormFieldTest() override;
FormFieldTest& operator=(const FormFieldTest&) = delete;
protected:
// Add a field with |control_type|, the |name|, the |label| the expected
// parsed type |expected_type|.
void AddFormFieldData(std::string control_type,
std::string name,
std::string label,
ServerFieldType expected_type) {
FormFieldData field_data;
field_data.form_control_type = control_type;
field_data.name = base::UTF8ToUTF16(name);
field_data.label = base::UTF8ToUTF16(label);
field_data.unique_renderer_id = MakeFieldRendererId();
list_.push_back(std::make_unique<AutofillField>(field_data));
expected_classifications_.insert(
std::make_pair(field_data.unique_renderer_id, expected_type));
}
// Convenience wrapper for text control elements.
void AddTextFormFieldData(std::string name,
std::string label,
ServerFieldType expected_classification) {
AddFormFieldData("text", name, label, expected_classification);
}
// Apply parsing and verify the expected types.
// |parsed| indicates if at least one field could be parsed successfully.
// |page_language| the language to be used for parsing, default empty value
// means the language is unknown and patterns of all languages are used.
void ClassifyAndVerify(bool parsed = true,
const LanguageCode& page_language = LanguageCode("")) {
AutofillScanner scanner(list_);
field_ = Parse(&scanner, page_language);
if (!parsed) {
ASSERT_EQ(nullptr, field_.get());
return;
}
ASSERT_NE(nullptr, field_.get());
field_->AddClassificationsForTesting(&field_candidates_map_);
for (const std::pair<FieldRendererId, ServerFieldType> it :
expected_classifications_) {
ASSERT_TRUE(field_candidates_map_.find(it.first) !=
field_candidates_map_.end());
EXPECT_EQ(it.second, field_candidates_map_[it.first].BestHeuristicType());
}
}
// Apply the parsing with a specific parser.
virtual std::unique_ptr<FormField> Parse(
AutofillScanner* scanner,
const LanguageCode& page_language) = 0;
FieldRendererId MakeFieldRendererId() {
return FieldRendererId(++id_counter_);
}
std::vector<std::unique_ptr<AutofillField>> list_;
std::unique_ptr<FormField> field_;
FieldCandidatesMap field_candidates_map_;
std::map<FieldRendererId, ServerFieldType> expected_classifications_;
private:
uint64_t id_counter_ = 0;
};
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_FORM_PARSING_PARSING_TEST_UTILS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment