Commit 774453da authored by sebsg's avatar sebsg Committed by Commit Bot

[Autofill] Refactor Profile Validation API

Changes the callback return value of the API to be a pointer to
the profile instead of being the validity state of the profile.

The concept of a validity state for the whole profile was removed since
validity depends on the context (Autofill != Payment Request).

Using a pointer to the validated profile is more useful for the called,
which can now start the validation of all profiles at the same time and
handle the result from the callback.

Another modification was to put all of the validation utils in the same
file.

Also make the validator keep a weakptr to an AutofillProfile in case
it gets deleted while loading the rules.

Bug: 
Change-Id: I9d41a0cea577351d8a1618bc9f771af1524e38c6
Reviewed-on: https://chromium-review.googlesource.com/709779
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516344}
parent cf578d07
......@@ -22,8 +22,6 @@ static_library("browser") {
"address_rewriter.cc",
"address_rewriter.h",
"address_rewriter_rules.cc",
"address_validation_util.cc",
"address_validation_util.h",
"autocomplete_history_manager.cc",
"autocomplete_history_manager.h",
"autofill-inl.h",
......@@ -63,6 +61,8 @@ static_library("browser") {
"autofill_profile.h",
"autofill_profile_comparator.cc",
"autofill_profile_comparator.h",
"autofill_profile_validation_util.cc",
"autofill_profile_validation_util.h",
"autofill_profile_validator.cc",
"autofill_profile_validator.h",
"autofill_provider.cc",
......@@ -122,8 +122,6 @@ static_library("browser") {
"personal_data_manager.cc",
"personal_data_manager.h",
"personal_data_manager_observer.h",
"phone_email_validation_util.cc",
"phone_email_validation_util.h",
"phone_field.cc",
"phone_field.h",
"phone_number.cc",
......@@ -361,7 +359,6 @@ source_set("unit_tests") {
"address_normalizer_impl_unittest.cc",
"address_rewriter_unittest.cc",
"address_unittest.cc",
"address_validation_util_unittest.cc",
"autocomplete_history_manager_unittest.cc",
"autofill_country_unittest.cc",
"autofill_data_model_unittest.cc",
......@@ -376,6 +373,7 @@ source_set("unit_tests") {
"autofill_metrics_unittest.cc",
"autofill_profile_comparator_unittest.cc",
"autofill_profile_unittest.cc",
"autofill_profile_validation_util_unittest.cc",
"autofill_profile_validator_unittest.cc",
"autofill_type_unittest.cc",
"autofill_wallet_data_type_controller_unittest.cc",
......@@ -397,7 +395,6 @@ source_set("unit_tests") {
"payments/payments_client_unittest.cc",
"payments/payments_service_url_unittest.cc",
"personal_data_manager_unittest.cc",
"phone_email_validation_util_unittest.cc",
"phone_field_unittest.cc",
"phone_number_i18n_unittest.cc",
"phone_number_unittest.cc",
......
// Copyright 2017 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_ADDRESS_VALIDATION_UTIL_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_VALIDATION_UTIL_H_
#include "components/autofill/core/browser/autofill_profile.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
namespace autofill {
namespace address_validation_util {
// Validates the address fields of the |profile|.
// Returns the ValidityState of the |profile| according to its address fields.
AutofillProfile::ValidityState ValidateAddress(
AutofillProfile* profile,
AddressValidator* address_validator);
} // namespace address_validation_util
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_VALIDATION_UTIL_H_
......@@ -13,6 +13,7 @@
#include <vector>
#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string16.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/address.h"
......@@ -27,7 +28,8 @@ namespace autofill {
// implements the FormGroup interface so that owners of this object can request
// form information from the profile, and the profile will delegate the request
// to the requested form group type.
class AutofillProfile : public AutofillDataModel {
class AutofillProfile : public AutofillDataModel,
public base::SupportsWeakPtr<AutofillProfile> {
public:
enum RecordType {
// A profile stored and editable locally.
......
......@@ -2,7 +2,7 @@
// 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/address_validation_util.h"
#include "components/autofill/core/browser/autofill_profile_validation_util.h"
#include <utility>
......@@ -13,8 +13,10 @@
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/address_i18n.h"
#include "components/autofill/core/browser/country_data.h"
#include "components/autofill/core/browser/validation.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
#include "third_party/libphonenumber/dist/cpp/src/phonenumbers/phonenumberutil.h"
namespace autofill {
......@@ -40,6 +42,8 @@ using ::i18n::addressinput::MISSING_REQUIRED_FIELD;
using ::i18n::addressinput::UNEXPECTED_FIELD;
using ::i18n::addressinput::UNKNOWN_VALUE;
using ::i18n::phonenumbers::PhoneNumberUtil;
const AddressField kFields[] = {COUNTRY, ADMIN_AREA, LOCALITY,
DEPENDENT_LOCALITY, POSTAL_CODE};
const AddressProblem kProblems[] = {UNEXPECTED_FIELD, MISSING_REQUIRED_FIELD,
......@@ -62,8 +66,8 @@ bool SetValidityStateForAddressField(AutofillProfile* profile,
}
// Set the validity state of all address fields in the |profile| to |state|.
void SetAllValidityStates(AutofillProfile* profile,
AutofillProfile::ValidityState state) {
void SetAllAddressValidityStates(AutofillProfile* profile,
AutofillProfile::ValidityState state) {
DCHECK(profile);
for (auto field : kFields)
SetValidityStateForAddressField(profile, field, state);
......@@ -120,14 +124,21 @@ void SetEmptyValidityIfEmpty(AutofillProfile* profile) {
} // namespace
namespace address_validation_util {
namespace profile_validation_util {
void ValidateProfile(AutofillProfile* profile,
AddressValidator* address_validator) {
DCHECK(address_validator);
DCHECK(profile);
ValidateAddress(profile, address_validator);
ValidatePhoneNumber(profile);
ValidateEmailAddress(profile);
}
AutofillProfile::ValidityState ValidateAddress(
AutofillProfile* profile,
AddressValidator* address_validator) {
void ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator) {
DCHECK(address_validator);
if (!profile)
return AutofillProfile::UNVALIDATED;
DCHECK(profile);
if (!base::ContainsValue(
CountryDataMap::GetInstance()->country_codes(),
......@@ -135,16 +146,15 @@ AutofillProfile::ValidityState ValidateAddress(
// If the country code is not in the database, the country code and the
// profile are invalid, and other fields cannot be validated, because it is
// unclear which, if any, rule should apply.
SetAllValidityStates(profile, AutofillProfile::UNVALIDATED);
SetAllAddressValidityStates(profile, AutofillProfile::UNVALIDATED);
SetValidityStateForAddressField(profile, COUNTRY, AutofillProfile::INVALID);
SetEmptyValidityIfEmpty(profile);
return AutofillProfile::INVALID;
return;
}
AddressData address;
InitializeAddressFromProfile(*profile, &address);
AutofillProfile::ValidityState profile_validity;
FieldProblemMap problems;
// status denotes if the rule was successfully loaded before validation.
AddressValidator::Status status =
......@@ -153,28 +163,61 @@ AutofillProfile::ValidityState ValidateAddress(
if (status == AddressValidator::SUCCESS) {
// The rules were found and applied. Initialize all fields to VALID here and
// update the fields with problems below.
profile_validity = AutofillProfile::VALID;
SetAllValidityStates(profile, AutofillProfile::VALID);
SetAllAddressValidityStates(profile, AutofillProfile::VALID);
} else {
// If the rules are not yet available, ValidateAddress can still check for
// MISSING_REQUIRED_FIELD. In this case, the address fields will be either
// UNVALIDATED or INVALID.
profile_validity = AutofillProfile::UNVALIDATED;
SetAllValidityStates(profile, AutofillProfile::UNVALIDATED);
SetAllAddressValidityStates(profile, AutofillProfile::UNVALIDATED);
SetValidityStateForAddressField(profile, COUNTRY, AutofillProfile::VALID);
}
for (auto problem : problems) {
if (SetValidityStateForAddressField(profile, problem.first,
AutofillProfile::INVALID)) {
profile_validity = AutofillProfile::INVALID;
}
SetValidityStateForAddressField(profile, problem.first,
AutofillProfile::INVALID);
}
SetEmptyValidityIfEmpty(profile);
}
void ValidateEmailAddress(AutofillProfile* profile) {
const base::string16& email = profile->GetRawInfo(EMAIL_ADDRESS);
if (email.empty()) {
profile->SetValidityState(EMAIL_ADDRESS, AutofillProfile::EMPTY);
return;
}
profile->SetValidityState(EMAIL_ADDRESS, autofill::IsValidEmailAddress(email)
? AutofillProfile::VALID
: AutofillProfile::INVALID);
}
void ValidatePhoneNumber(AutofillProfile* profile) {
const std::string& phone_number =
base::UTF16ToUTF8(profile->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
if (phone_number.empty()) {
profile->SetValidityState(PHONE_HOME_WHOLE_NUMBER, AutofillProfile::EMPTY);
return;
}
const std::string& country_code =
base::UTF16ToUTF8(profile->GetRawInfo(ADDRESS_HOME_COUNTRY));
if (!base::ContainsValue(CountryDataMap::GetInstance()->country_codes(),
country_code)) {
// If the country code is not in the database, the phone number cannot be
// validated.
profile->SetValidityState(PHONE_HOME_WHOLE_NUMBER,
AutofillProfile::UNVALIDATED);
return;
}
return profile_validity;
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
profile->SetValidityState(
PHONE_HOME_WHOLE_NUMBER,
phone_util->IsPossibleNumberForString(phone_number, country_code)
? AutofillProfile::VALID
: AutofillProfile::INVALID);
}
} // namespace address_validation_util
} // namespace profile_validation_util
} // namespace autofill
// Copyright 2017 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_AUTOFILL_PROFILE_VALIDATION_UTIL_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_VALIDATION_UTIL_H_
#include "components/autofill/core/browser/autofill_profile.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
namespace autofill {
namespace profile_validation_util {
// Sets the validity state of the autofill |profile|.
void ValidateProfile(AutofillProfile* profile,
AddressValidator* address_validator);
// Sets the validity state of the address fields of the |profile|.
void ValidateAddress(AutofillProfile* profile,
AddressValidator* address_validator);
// Sets the validity state of the phone number field of the |profile|.
void ValidatePhoneNumber(AutofillProfile* profile);
// Sets the validity state of the email address field of the |profile|.
void ValidateEmailAddress(AutofillProfile* profile);
} // namespace profile_validation_util
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_VALIDATION_UTIL_H_
......@@ -14,8 +14,8 @@
#include "base/strings/utf_string_conversions.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/address_validation_util.h"
#include "components/autofill/core/browser/phone_email_validation_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_profile_validation_util.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
......@@ -36,7 +36,7 @@ const int kRulesLoadingTimeoutSeconds = 5;
} // namespace
AutofillProfileValidator::ValidationRequest::ValidationRequest(
AutofillProfile* profile,
base::WeakPtr<AutofillProfile> profile,
autofill::AddressValidator* validator,
AutofillProfileValidatorCallback on_validated)
: profile_(profile),
......@@ -61,42 +61,31 @@ void AutofillProfileValidator::ValidationRequest::OnRulesLoaded() {
if (has_responded_)
return;
has_responded_ = true;
AutofillProfile::ValidityState profile_validity =
AutofillProfile::UNVALIDATED;
AutofillProfile::ValidityState address_validity =
address_validation_util::ValidateAddress(profile_, validator_);
AutofillProfile::ValidityState phone_email_validity =
phone_email_validation_util::ValidatePhoneAndEmail(profile_);
if (address_validity == AutofillProfile::INVALID ||
phone_email_validity == AutofillProfile::INVALID) {
profile_validity = AutofillProfile::INVALID;
} else if (address_validity == AutofillProfile::VALID &&
phone_email_validity == AutofillProfile::VALID) {
profile_validity = AutofillProfile::VALID;
}
std::move(on_validated_).Run(profile_validity);
if (!profile_)
return;
profile_validation_util::ValidateProfile(profile_.get(), validator_);
std::move(on_validated_).Run(profile_.get());
}
AutofillProfileValidator::AutofillProfileValidator(
std::unique_ptr<Source> source,
std::unique_ptr<Storage> storage)
std::unique_ptr<::i18n::addressinput::Source> source,
std::unique_ptr<::i18n::addressinput::Storage> storage)
: address_validator_(std::move(source), std::move(storage), this) {}
AutofillProfileValidator::~AutofillProfileValidator() {}
void AutofillProfileValidator::ValidateProfile(
void AutofillProfileValidator::StartProfileValidation(
AutofillProfile* profile,
AutofillProfileValidatorCallback cb) {
if (!profile) {
// An null profile is an unvalidated profile.
std::move(cb).Run(AutofillProfile::UNVALIDATED);
DCHECK(profile);
if (!profile)
return;
}
std::unique_ptr<ValidationRequest> request(
std::make_unique<ValidationRequest>(profile, &address_validator_,
std::move(cb)));
std::make_unique<ValidationRequest>(profile->AsWeakPtr(),
&address_validator_, std::move(cb)));
// If the |region_code| is not a valid code according to our source, calling
// LoadRules would result in calling OnAddressValidationRulesLoaded with
......
......@@ -14,23 +14,16 @@
#include "base/cancelable_callback.h"
#include "base/macros.h"
#include "components/autofill/core/browser/address_i18n.h"
#include "components/autofill/core/browser/address_validation_util.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
namespace autofill {
using ::i18n::addressinput::BuildCallback;
using ::i18n::addressinput::PreloadSupplier;
using ::i18n::addressinput::Source;
using ::i18n::addressinput::Storage;
class AutofillProfile;
using AutofillProfileValidatorCallback =
base::OnceCallback<void(AutofillProfile::ValidityState)>;
base::OnceCallback<void(AutofillProfile*)>;
// AutofillProfileValidator Loads Rules from the server and validates an
// autofill profile. For a given autofill profile, it will set the ValidityState
......@@ -49,16 +42,16 @@ class AutofillProfileValidator : public autofill::LoadRulesListener {
// profile, synchronously. If it is not loaded yet, it sets up a
// task to validate the profile when the rule is loaded (asynchronous). If the
// loading has not yet started, it will also start loading the rules.
void ValidateProfile(AutofillProfile* profile,
AutofillProfileValidatorCallback cb);
void StartProfileValidation(AutofillProfile* profile,
AutofillProfileValidatorCallback cb);
private:
// ValidationRequest loads Rules from the server and validates various fields
// in an autofill profile.
class ValidationRequest {
public:
ValidationRequest(AutofillProfile* profile,
autofill::AddressValidator* validator,
ValidationRequest(base::WeakPtr<AutofillProfile> profile,
AddressValidator* validator,
AutofillProfileValidatorCallback on_validated);
~ValidationRequest();
......@@ -67,10 +60,10 @@ class AutofillProfileValidator : public autofill::LoadRulesListener {
void OnRulesLoaded();
private:
// Not owned. Not Null. Outlives this object.
AutofillProfile* profile_;
base::WeakPtr<AutofillProfile> profile_;
// Not owned. Outlives this object.
autofill::AddressValidator* validator_;
AddressValidator* validator_;
AutofillProfileValidatorCallback on_validated_;
......@@ -98,7 +91,7 @@ class AutofillProfileValidator : public autofill::LoadRulesListener {
pending_requests_;
// The address validator used to load rules.
autofill::AddressValidator address_validator_;
AddressValidator address_validator_;
DISALLOW_COPY_AND_ASSIGN(AutofillProfileValidator);
};
......
// Copyright 2017 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/phone_email_validation_util.h"
#include <string>
#include "base/logging.h"
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/country_data.h"
#include "components/autofill/core/browser/validation.h"
#include "third_party/libphonenumber/dist/cpp/src/phonenumbers/phonenumberutil.h"
namespace autofill {
namespace {
using ::i18n::phonenumbers::PhoneNumberUtil;
} // namespace
namespace phone_email_validation_util {
AutofillProfile::ValidityState ValidateEmailAddress(
const base::string16& email) {
if (email.empty())
return AutofillProfile::EMPTY;
return (autofill::IsValidEmailAddress(email) ? AutofillProfile::VALID
: AutofillProfile::INVALID);
}
AutofillProfile::ValidityState ValidatePhoneNumber(
const std::string& phone_number,
const std::string& country_code) {
if (phone_number.empty())
return AutofillProfile::EMPTY;
if (!base::ContainsValue(CountryDataMap::GetInstance()->country_codes(),
country_code)) {
// If the country code is not in the database, the phone number cannot be
// validated.
return AutofillProfile::UNVALIDATED;
}
PhoneNumberUtil* phone_util = PhoneNumberUtil::GetInstance();
return phone_util->IsPossibleNumberForString(phone_number, country_code)
? AutofillProfile::VALID
: AutofillProfile::INVALID;
}
AutofillProfile::ValidityState ValidatePhoneAndEmail(AutofillProfile* profile) {
DCHECK(profile);
AutofillProfile::ValidityState phone_validity = ValidatePhoneNumber(
base::UTF16ToUTF8(profile->GetRawInfo(PHONE_HOME_WHOLE_NUMBER)),
base::UTF16ToUTF8(profile->GetRawInfo(ADDRESS_HOME_COUNTRY)));
profile->SetValidityState(PHONE_HOME_WHOLE_NUMBER, phone_validity);
AutofillProfile::ValidityState email_validity =
ValidateEmailAddress(profile->GetRawInfo(EMAIL_ADDRESS));
profile->SetValidityState(EMAIL_ADDRESS, email_validity);
if (phone_validity == AutofillProfile::VALID &&
email_validity == AutofillProfile::VALID) {
return AutofillProfile::VALID;
}
return AutofillProfile::INVALID;
}
} // namespace phone_email_validation_util
} // namespace autofill
// Copyright 2017 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_PHONE_EMAIL_VALIDATION_UTIL_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_EMAIL_VALIDATION_UTIL_H_
#include "components/autofill/core/browser/autofill_profile.h"
namespace autofill {
namespace phone_email_validation_util {
// Validates the phone number and email field of the |profile|.
// Returns the ValidityState of the |profile| according to its phone fields.
AutofillProfile::ValidityState ValidatePhoneAndEmail(AutofillProfile* profile);
} // namespace phone_email_validation_util
} // namespace autofill
#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_PHONE_EMAIL_VALIDATION_UTIL_H_
// Copyright 2017 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/phone_email_validation_util.h"
#include <string>
#include "base/strings/utf_string_conversions.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
class AutofillPhoneValidationTest : public testing::Test {
public:
AutofillPhoneValidationTest() {}
AutofillProfile::ValidityState ValidatePhoneAndEmailTest(
AutofillProfile* profile) {
return phone_email_validation_util::ValidatePhoneAndEmail(profile);
}
~AutofillPhoneValidationTest() override {}
private:
DISALLOW_COPY_AND_ASSIGN(AutofillPhoneValidationTest);
};
TEST_F(AutofillPhoneValidationTest, ValidateFullValidProfile) {
// This is a full valid profile:
// Country Code: "CA", Phone Number: "15141112233",
// Email: "alice@wonderland.ca"
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateEmptyPhoneNumber) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, base::string16());
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::EMPTY,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateValidPhone_CountryCodeNotExist) {
// This is a profile with invalid country code, therefore the phone number
// cannot be validated.
const std::string country_code = "PP";
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::UTF8ToUTF16(country_code));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::UNVALIDATED,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateEmptyPhone_CountryCodeNotExist) {
// This is a profile with invalid country code, but a missing phone number.
// Therefore, it's an invalid phone number.
const std::string country_code = "PP";
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::UTF8ToUTF16(country_code));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, base::string16());
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::EMPTY,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateInvalidPhoneNumber) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, base::ASCIIToUTF16("33"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("151411122334"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("1(514)111-22-334"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("251411122334"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, base::ASCIIToUTF16("Hello!"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
}
TEST_F(AutofillPhoneValidationTest, ValidateValidPhoneNumber) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, base::ASCIIToUTF16("5141112233"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("514-111-2233"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("1(514)111-22-33"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("+1 514 111 22 33"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("+1 (514)-111-22-33"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("(514)-111-22-33"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("+1 650 GOO OGLE"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
base::ASCIIToUTF16("778 111 22 33"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
}
TEST_F(AutofillPhoneValidationTest, ValidateEmptyEmailAddress) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID,
profile.GetValidityState(PHONE_HOME_WHOLE_NUMBER));
EXPECT_EQ(AutofillProfile::EMPTY, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateInvalidEmailAddress) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("Hello!"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID, profile.GetValidityState(EMAIL_ADDRESS));
profile.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("alice.wonderland"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID, profile.GetValidityState(EMAIL_ADDRESS));
profile.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("alice@"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID, profile.GetValidityState(EMAIL_ADDRESS));
profile.SetRawInfo(EMAIL_ADDRESS,
base::ASCIIToUTF16("alice@=wonderland.com"));
EXPECT_EQ(AutofillProfile::INVALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::INVALID, profile.GetValidityState(EMAIL_ADDRESS));
}
TEST_F(AutofillPhoneValidationTest, ValidateValidEmailAddress) {
AutofillProfile profile(autofill::test::GetFullValidProfileForCanada());
profile.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("alice@wonderland"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
profile.SetRawInfo(EMAIL_ADDRESS,
base::ASCIIToUTF16("alice@wonderland.fiction"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
profile.SetRawInfo(EMAIL_ADDRESS,
base::ASCIIToUTF16("alice+cat@wonderland.fiction.book"));
EXPECT_EQ(AutofillProfile::VALID, ValidatePhoneAndEmailTest(&profile));
EXPECT_EQ(AutofillProfile::VALID, profile.GetValidityState(EMAIL_ADDRESS));
}
} // 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