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

[Autofill] Simplified PriceField tests

This CL also introduces a class enum for the parsing outcome and moves
some code from the test utils header into the cc file.

Change-Id: I34c6538a68fe2c33a02646eddb6692b88b155a6e
Bug: 1007974
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2626647
Commit-Queue: Matthias Körber <koerber@google.com>
Reviewed-by: default avatarChristoph Schwering <schwering@google.com>
Cr-Commit-Position: refs/heads/master@{#844537}
parent 5ac5832a
......@@ -30,12 +30,12 @@ class AddressFieldTest : public FormFieldTest {
};
TEST_F(AddressFieldTest, Empty) {
ClassifyAndVerify(/*parsed=*/false);
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
TEST_F(AddressFieldTest, NonParse) {
AddTextFormFieldData("", "", UNKNOWN_TYPE);
ClassifyAndVerify(/*parsed=*/false);
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
TEST_F(AddressFieldTest, ParseOneLineAddress) {
......@@ -117,7 +117,7 @@ TEST_F(AddressFieldTest, NotParseHouseNumberWithoutStreetName) {
features::kAutofillEnableSupportForMoreStructureInAddresses);
AddTextFormFieldData("house-number", "House number", UNKNOWN_TYPE);
ClassifyAndVerify(/*parsed=*/false);
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
// Tests that the dependent locality is correctly classified with
......@@ -209,20 +209,20 @@ TEST_F(AddressFieldTest, ParseTurkishCityStateWithLabelPrecedence) {
AddTextFormFieldData("city", "Il", ADDRESS_HOME_STATE);
AddTextFormFieldData("county", "Ilce", ADDRESS_HOME_CITY);
ClassifyAndVerify(/*parsed=*/true, LanguageCode("tr"));
ClassifyAndVerify(ParseResult::PARSED, LanguageCode("tr"));
}
// Tests that address name is not misclassified as address.
TEST_F(AddressFieldTest, NotParseAddressName) {
AddTextFormFieldData("address", "Adres Başlığı", UNKNOWN_TYPE);
ClassifyAndVerify(/*parsed=*/false, LanguageCode("tr"));
ClassifyAndVerify(ParseResult::NOT_PARSED, LanguageCode("tr"));
}
// Tests that the address components sequence in a label is classified
// as |ADDRESS_HOME_LINE1|.
TEST_F(AddressFieldTest, ParseAddressComponentsSequenceAsAddressLine1) {
AddTextFormFieldData("detail", "Улица, дом, квартира", ADDRESS_HOME_LINE1);
ClassifyAndVerify(/*parsed=*/true, LanguageCode("ru"));
ClassifyAndVerify(ParseResult::PARSED, LanguageCode("ru"));
}
// Tests that the address components sequence in a label is classified
......@@ -231,7 +231,7 @@ TEST_F(AddressFieldTest, ParseAddressComponentsSequenceAsStreetAddress) {
AddFormFieldData("textarea", "detail",
"Mahalle, sokak, cadde ve diğer bilgilerinizi girin",
ADDRESS_HOME_STREET_ADDRESS);
ClassifyAndVerify(/*parsed=*/true, LanguageCode("tr"));
ClassifyAndVerify(ParseResult::PARSED, LanguageCode("tr"));
}
} // namespace autofill
......@@ -38,7 +38,7 @@ TEST_F(NameFieldTest, FirstMiddleLast) {
AddTextFormFieldData("Name Middle", "Middle", NAME_MIDDLE);
AddTextFormFieldData("Last Name", "Last", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstMiddleLast2) {
......@@ -46,7 +46,7 @@ TEST_F(NameFieldTest, FirstMiddleLast2) {
AddTextFormFieldData("middleName", "", NAME_MIDDLE);
AddTextFormFieldData("lastName", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
// Test that a field for a honoric title is parsed correctly.
......@@ -62,21 +62,21 @@ TEST_F(NameFieldTest, HonorificPrefixFirstLast) {
AddTextFormFieldData("first_name", "", NAME_FIRST);
AddTextFormFieldData("last_name", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstLast) {
AddTextFormFieldData("first_name", "", NAME_FIRST);
AddTextFormFieldData("last_name", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstLast2) {
AddTextFormFieldData("first_name", "Name", NAME_FIRST);
AddTextFormFieldData("last_name", "Name", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstLastMiddleWithSpaces) {
......@@ -84,14 +84,14 @@ TEST_F(NameFieldTest, FirstLastMiddleWithSpaces) {
AddTextFormFieldData("middle_name", "Middle Name", NAME_MIDDLE);
AddTextFormFieldData("last_name", "Last Name", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstLastEmpty) {
AddTextFormFieldData("first_name", "Name", NAME_FIRST);
AddTextFormFieldData("last_name", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, FirstMiddleLastEmpty) {
......@@ -99,7 +99,7 @@ TEST_F(NameFieldTest, FirstMiddleLastEmpty) {
AddTextFormFieldData("middle_name", "", NAME_MIDDLE_INITIAL);
AddTextFormFieldData("last_name", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, MiddleInitial) {
......@@ -107,14 +107,14 @@ TEST_F(NameFieldTest, MiddleInitial) {
AddTextFormFieldData("middle_name", "MI", NAME_MIDDLE_INITIAL);
AddTextFormFieldData("last_name", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(NameFieldTest, MiddleInitialNoLastName) {
AddTextFormFieldData("first_name", "First Name", UNKNOWN_TYPE);
AddTextFormFieldData("middle_name", "MI", UNKNOWN_TYPE);
ClassifyAndVerify(/*parsed=*/false);
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
// Tests that a website with a first and second surname field is parsed
......@@ -133,7 +133,7 @@ TEST_F(NameFieldTest, HonorificPrefixAndFirstNameAndHispanicLastNames) {
AddTextFormFieldData("segunda apellido", "segunda_apellido",
NAME_LAST_SECOND);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
// Tests that a website with a first and second surname field is parsed
......@@ -151,7 +151,7 @@ TEST_F(NameFieldTest, FirstNameAndOptionalMiddleNameAndHispanicLastNames) {
AddTextFormFieldData("segunda apellido", "segunda_apellido",
NAME_LAST_SECOND);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
// This case is from the dell.com checkout page. The middle initial "mi" string
......@@ -161,7 +161,7 @@ TEST_F(NameFieldTest, MiddleInitialAtEnd) {
AddTextFormFieldData("XXXnameXXXmi", "", NAME_MIDDLE_INITIAL);
AddTextFormFieldData("XXXnameXXXlast", "", NAME_LAST);
ClassifyAndVerify(/*parsed=*/true);
ClassifyAndVerify(ParseResult::PARSED);
}
// Test the coverage of all found strings for first and second last names.
......@@ -206,26 +206,14 @@ TEST_F(NameFieldTest, NotAddressName) {
AddTextFormFieldData("name", "Identificação do Endereço", UNKNOWN_TYPE);
AddTextFormFieldData("title", "Adres Adı", UNKNOWN_TYPE);
ClassifyAndVerify(/*parsed=*/false);
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
// Tests that contact name is classified as full name.
TEST_F(NameFieldTest, ContactNameFull) {
FormFieldData field;
field.form_control_type = "text";
field.label = base::UTF8ToUTF16("Контактное лицо");
field.name = base::UTF8ToUTF16("contact person");
field.unique_renderer_id = MakeFieldRendererId();
list_.push_back(std::make_unique<AutofillField>(field));
FieldRendererId name = list_.back()->unique_renderer_id;
AutofillScanner scanner(list_);
field_ = Parse(&scanner);
ASSERT_NE(nullptr, field_.get());
field_->AddClassificationsForTesting(&field_candidates_map_);
ASSERT_TRUE(field_candidates_map_.find(name) != field_candidates_map_.end());
EXPECT_EQ(NAME_FULL, field_candidates_map_[name].BestHeuristicType());
AddTextFormFieldData("contact", "Контактное лицо", NAME_FULL);
ClassifyAndVerify(ParseResult::PARSED);
}
} // namespace autofill
......@@ -8,4 +8,54 @@ namespace autofill {
FormFieldTest::FormFieldTest() = default;
FormFieldTest::~FormFieldTest() = default;
void FormFieldTest::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 FormFieldTest::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 FormFieldTest::ClassifyAndVerify(ParseResult parse_result,
const LanguageCode& page_language) {
AutofillScanner scanner(list_);
field_ = Parse(&scanner, page_language);
if (parse_result == ParseResult::NOT_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());
}
}
FieldRendererId FormFieldTest::MakeFieldRendererId() {
return FieldRendererId(++id_counter_);
}
} // namespace autofill
......@@ -21,6 +21,14 @@
namespace autofill {
enum class ParseResult {
// The form was succesfully parsed and at least one type was assigned.
PARSED = 0,
// Not a single type was assigned.
NOT_PARSED,
kMaxValue = NOT_PARSED
};
class FormFieldTest : public testing::Test {
public:
FormFieldTest(const FormFieldTest&) = delete;
......@@ -34,56 +42,26 @@ class FormFieldTest : public testing::Test {
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));
}
ServerFieldType 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);
}
ServerFieldType 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());
}
}
void ClassifyAndVerify(ParseResult parse_result = ParseResult::PARSED,
const LanguageCode& page_language = LanguageCode(""));
// 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_);
}
FieldRendererId MakeFieldRendererId();
std::vector<std::unique_ptr<AutofillField>> list_;
std::unique_ptr<FormField> field_;
......
......@@ -26,7 +26,7 @@ class PriceField : public FormField {
static std::unique_ptr<FormField> Parse(AutofillScanner* scanner,
const LanguageCode& page_language,
LogManager* log_manager);
PriceField(const AutofillField* field);
explicit PriceField(const AutofillField* field);
protected:
void AddClassifications(FieldCandidatesMap* field_candidates) const override;
......
......@@ -4,84 +4,36 @@
#include "components/autofill/core/browser/form_parsing/price_field.h"
#include <memory>
#include <vector>
#include "base/macros.h"
#include "base/memory/ptr_util.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/form_parsing/autofill_scanner.h"
#include "components/autofill/core/common/form_field_data.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "components/autofill/core/browser/form_parsing/parsing_test_utils.h"
using base::ASCIIToUTF16;
namespace autofill {
class PriceFieldTest : public testing::Test {
class PriceFieldTest : public FormFieldTest {
public:
PriceFieldTest() = default;
PriceFieldTest(const PriceFieldTest&) = delete;
PriceFieldTest& operator=(const PriceFieldTest&) = delete;
protected:
// Downcast for tests.
static std::unique_ptr<PriceField> Parse(AutofillScanner* scanner) {
// An empty page_language means the language is unknown and patterns of all
// languages are used.
std::unique_ptr<FormField> field =
PriceField::Parse(scanner, LanguageCode(""), nullptr);
return std::unique_ptr<PriceField>(
static_cast<PriceField*>(field.release()));
std::unique_ptr<FormField> Parse(
AutofillScanner* scanner,
const LanguageCode& page_language = LanguageCode("en")) override {
return PriceField::Parse(scanner, page_language, nullptr);
}
std::vector<std::unique_ptr<AutofillField>> list_;
std::unique_ptr<PriceField> field_;
FieldCandidatesMap field_candidates_map_;
FieldRendererId MakeFieldRendererId() {
return FieldRendererId(++id_counter_);
}
private:
uint64_t id_counter_ = 0;
};
TEST_F(PriceFieldTest, ParsePrice) {
FormFieldData price_field;
price_field.form_control_type = "text";
AddTextFormFieldData("name your price", "userPrice", PRICE);
price_field.label = ASCIIToUTF16("name your price");
price_field.name = ASCIIToUTF16("userPrice");
price_field.unique_renderer_id = MakeFieldRendererId();
list_.push_back(std::make_unique<AutofillField>(price_field));
FieldRendererId price1 = list_.back()->unique_renderer_id;
AutofillScanner scanner(list_);
field_ = Parse(&scanner);
ASSERT_NE(nullptr, field_.get());
field_->AddClassifications(&field_candidates_map_);
ASSERT_TRUE(field_candidates_map_.find(price1) !=
field_candidates_map_.end());
EXPECT_EQ(PRICE, field_candidates_map_[price1].BestHeuristicType());
ClassifyAndVerify(ParseResult::PARSED);
}
TEST_F(PriceFieldTest, ParseNonPrice) {
FormFieldData address_field;
address_field.form_control_type = "text";
address_field.label = ASCIIToUTF16("Name");
address_field.name = ASCIIToUTF16("firstName");
address_field.unique_renderer_id = MakeFieldRendererId();
list_.push_back(std::make_unique<AutofillField>(address_field));
AddTextFormFieldData("firstName", "Name", UNKNOWN_TYPE);
AutofillScanner scanner(list_);
field_ = Parse(&scanner);
ASSERT_EQ(nullptr, field_.get());
ClassifyAndVerify(ParseResult::NOT_PARSED);
}
} // namespace autofill
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