1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/views/autofill/address_editor_view.h"
#include <memory>
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/ui/autofill/address_editor_controller.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/views/chrome_views_test_base.h"
#include "components/autofill/core/browser/data_manager/test_personal_data_manager.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/test_renderer_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
namespace autofill {
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::ReturnRef;
// TODO(crbug.com/40277889): write interactive UI tests instead of unit tests.
class AddressEditorViewTest : public ChromeViewsTestBase {
public:
AddressEditorViewTest() = default;
~AddressEditorViewTest() override = default;
void SetUp() override {
ChromeViewsTestBase::SetUp();
pref_service_.registry()->RegisterBooleanPref(
prefs::kAutofillProfileEnabled, true);
pref_service_.registry()->RegisterBooleanPref(
prefs::kAutofillCreditCardEnabled, true);
pdm_.SetPrefService(&pref_service_);
profile_to_edit_ = test::GetFullProfile();
auto controller = std::make_unique<AddressEditorController>(
profile_to_edit_, &pdm_, true);
controller_ = controller.get();
view_ = std::make_unique<AddressEditorView>(std::move(controller));
}
void TearDown() override {
controller_ = nullptr;
view_.reset();
ChromeViewsTestBase::TearDown();
}
protected:
// Required for test_web_content.
content::RenderViewHostTestEnabler test_render_host_factories_;
autofill::AutofillProfile profile_to_edit_{
i18n_model_definition::kLegacyHierarchyCountryCode};
TestingProfile profile_;
TestingPrefServiceSimple pref_service_;
TestPersonalDataManager pdm_;
raw_ptr<AddressEditorController> controller_;
std::unique_ptr<AddressEditorView> view_;
};
TEST_F(AddressEditorViewTest, FormValidation) {
view_->ValidateAllFields();
EXPECT_TRUE(*controller_->is_valid())
<< "The form initailized from a full profile should be valid.";
view_->SetTextInputFieldValueForTesting(
autofill::FieldType::ADDRESS_HOME_STREET_ADDRESS, u"");
EXPECT_FALSE(*controller_->is_valid())
<< "Street address is required for US, the form should be invalid.";
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELD_FORM_ERROR),
view_->GetValidationErrorForTesting());
view_->SetTextInputFieldValueForTesting(
autofill::FieldType::ADDRESS_HOME_CITY, u"");
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELDS_FORM_ERROR),
view_->GetValidationErrorForTesting())
<< "The error message should denote multiple invalid fileds now.";
view_->SetTextInputFieldValueForTesting(
autofill::FieldType::ADDRESS_HOME_STREET_ADDRESS, u"Some text");
view_->SetTextInputFieldValueForTesting(
autofill::FieldType::ADDRESS_HOME_CITY, u"Some text");
EXPECT_TRUE(*controller_->is_valid())
<< "All the required fields are filled in, the form should be valid.";
EXPECT_EQ(u"", view_->GetValidationErrorForTesting())
<< "The error message should be empty for a valid form.";
}
TEST_F(AddressEditorViewTest, NoValidatableFormValidation) {
auto controller =
std::make_unique<AddressEditorController>(profile_to_edit_, &pdm_, false);
controller_ = controller.get();
view_ = std::make_unique<AddressEditorView>(std::move(controller));
view_->SetTextInputFieldValueForTesting(
autofill::FieldType::ADDRESS_HOME_STREET_ADDRESS, u"");
view_->ValidateAllFields();
EXPECT_FALSE(controller_->is_valid().has_value())
<< "Street address is required for US, but the form is not validatable.";
EXPECT_EQ(u"", view_->GetValidationErrorForTesting());
}
TEST_F(AddressEditorViewTest, CountryChangeValidity) {
view_->SetTextInputFieldValueForTesting(
FieldType::ADDRESS_HOME_STREET_ADDRESS, u"");
view_->ValidateAllFields();
EXPECT_FALSE(*controller_->is_valid())
<< "Street address is required for US, the form should be invalid.";
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELD_FORM_ERROR),
view_->GetValidationErrorForTesting());
view_->SelectCountryForTesting(u"Afghanistan");
EXPECT_FALSE(*controller_->is_valid())
<< "Street address is required for AF, the invalid state should not be "
"reset.";
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELD_FORM_ERROR),
view_->GetValidationErrorForTesting());
}
TEST_F(AddressEditorViewTest, CountryChangeValidity2) {
view_->SetTextInputFieldValueForTesting(FieldType::ADDRESS_HOME_ZIP, u"");
view_->ValidateAllFields();
EXPECT_FALSE(*controller_->is_valid())
<< "ZIP code is required for US, the form should be invalid";
view_->SelectCountryForTesting(u"Belarus");
EXPECT_TRUE(*controller_->is_valid())
<< "ZIP code is not required for BY, the form should be valid";
}
TEST_F(AddressEditorViewTest, WholeFormValidationState) {
view_->SetTextInputFieldValueForTesting(FieldType::ADDRESS_HOME_STATE, u"");
view_->SetTextInputFieldValueForTesting(
FieldType::ADDRESS_HOME_STREET_ADDRESS, u"");
view_->SetTextInputFieldValueForTesting(FieldType::ADDRESS_HOME_ZIP, u"");
EXPECT_FALSE(controller_->is_valid().has_value())
<< "Some required field for a US address was cleared, but we didn't call "
"`ValidateAllFields()` and the validation state is unknown.";
EXPECT_EQ(view_->GetValidationErrorForTesting(), u"");
view_->SetTextInputFieldValueForTesting(FieldType::ADDRESS_HOME_STATE,
u"California");
EXPECT_FALSE(controller_->is_valid().has_value())
<< "The validation state should not be updated by fixing one of the "
"invalid fields as the whole form validation was not performed yet.";
EXPECT_EQ(view_->GetValidationErrorForTesting(), u"");
view_->ValidateAllFields();
EXPECT_FALSE(*controller_->is_valid());
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELDS_FORM_ERROR),
view_->GetValidationErrorForTesting());
view_->SetTextInputFieldValueForTesting(FieldType::ADDRESS_HOME_ZIP, u"1234");
EXPECT_FALSE(*controller_->is_valid()) << "";
EXPECT_EQ(l10n_util::GetStringUTF16(
IDS_AUTOFILL_EDIT_ADDRESS_REQUIRED_FIELD_FORM_ERROR),
view_->GetValidationErrorForTesting());
view_->SetTextInputFieldValueForTesting(
FieldType::ADDRESS_HOME_STREET_ADDRESS, u"12 Park avenue");
EXPECT_TRUE(*controller_->is_valid());
EXPECT_EQ(view_->GetValidationErrorForTesting(), u"");
}
TEST_F(AddressEditorViewTest, InitialFocusViewPointsToCountryCombobox) {
EXPECT_NE(view_->initial_focus_view(), nullptr);
EXPECT_EQ(view_->initial_focus_view()->GetClassName(), "Combobox");
}
TEST_F(AddressEditorViewTest, FocusIsNotLostAfterEditorContentChange) {
// The view is temporarily added into a Widget to have a FocusManager.
std::unique_ptr<views::Widget> widget =
CreateTestWidget(views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
widget->SetContentsView(view_.get());
widget->Show();
EXPECT_NE(view_->initial_focus_view(), nullptr);
view_->SelectCountryForTesting(u"Belarus");
EXPECT_NE(view_->initial_focus_view(), nullptr);
EXPECT_TRUE(view_->initial_focus_view()->HasFocus());
// The view is managed by the test class, remove it from the widget.
widget->GetRootView()->RemoveChildView(view_.get());
}
} // namespace autofill
|