Index: chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc |
diff --git a/chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc b/chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc |
index 7a4bb238102d38ea9f7ab602137e403a54fda51d..99fa99c94b3d1161a62c8ccdbf48727c28b754bb 100644 |
--- a/chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc |
+++ b/chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc |
@@ -5,6 +5,8 @@ |
#include <map> |
#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/callback.h" |
#include "base/command_line.h" |
#include "base/guid.h" |
#include "base/memory/scoped_ptr.h" |
@@ -48,6 +50,8 @@ |
#include "content/public/test/mock_render_process_host.h" |
#include "google_apis/gaia/google_service_auth_error.h" |
#include "grit/webkit_resources.h" |
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h" |
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_validator.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "ui/base/resource/resource_bundle.h" |
@@ -63,6 +67,12 @@ namespace autofill { |
namespace { |
+using ::i18n::addressinput::AddressData; |
+using ::i18n::addressinput::AddressProblemFilter; |
+using ::i18n::addressinput::AddressProblems; |
+using ::i18n::addressinput::AddressValidator; |
+using testing::AtLeast; |
+using testing::Return; |
using testing::_; |
const char kFakeEmail[] = "user@chromium.org"; |
@@ -233,6 +243,17 @@ class TestAutofillDialogView : public AutofillDialogView { |
DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView); |
}; |
+class MockAddressValidator : public AddressValidator { |
+ public: |
+ MOCK_METHOD1(LoadRules, void(const std::string& country_code)); |
+ MOCK_CONST_METHOD3(ValidateAddress, |
+ ::i18n::addressinput::AddressValidator::Status( |
+ const AddressData& address, |
+ const AddressProblemFilter& filter, |
+ AddressProblems* problems)); |
+ using ::i18n::addressinput::AddressValidator::RULES_NOT_READY; |
+}; |
+ |
class TestAutofillDialogController |
: public AutofillDialogControllerImpl, |
public base::SupportsWeakPtr<TestAutofillDialogController> { |
@@ -243,6 +264,7 @@ class TestAutofillDialogController |
const GURL& source_url, |
const AutofillMetrics& metric_logger, |
const base::Callback<void(const FormStructure*)>& callback, |
+ MockAddressValidator* mock_validator, |
MockNewCreditCardBubbleController* mock_new_card_bubble_controller) |
: AutofillDialogControllerImpl(contents, |
form_structure, |
@@ -252,6 +274,7 @@ class TestAutofillDialogController |
mock_wallet_client_( |
Profile::FromBrowserContext(contents->GetBrowserContext())-> |
GetRequestContext(), this, source_url), |
+ mock_validator_(mock_validator), |
mock_new_card_bubble_controller_(mock_new_card_bubble_controller), |
submit_button_delay_count_(0) {} |
@@ -328,6 +351,10 @@ class TestAutofillDialogController |
return &mock_wallet_client_; |
} |
+ virtual AddressValidator* GetValidator() { |
+ return mock_validator_; |
+ } |
+ |
virtual void OpenTabWithUrl(const GURL& url) OVERRIDE { |
open_tab_url_ = url; |
} |
@@ -355,6 +382,7 @@ class TestAutofillDialogController |
const AutofillMetrics& metric_logger_; |
TestPersonalDataManager test_manager_; |
testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_; |
+ MockAddressValidator* mock_validator_; |
GURL open_tab_url_; |
MockNewCreditCardBubbleController* mock_new_card_bubble_controller_; |
@@ -426,6 +454,7 @@ class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { |
GURL(), |
metric_logger_, |
callback, |
+ mock_validator_.get(), |
mock_new_card_bubble_controller_.get()))->AsWeakPtr(); |
controller_->Init(profile()); |
} |
@@ -605,6 +634,13 @@ class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { |
const FormStructure* form_structure() { return form_structure_; } |
+ MockAddressValidator* GetMockValidator() const { |
+ return mock_validator_.get(); |
+ } |
+ void SetMockValidator(scoped_ptr<MockAddressValidator> validator) { |
+ mock_validator_ = validator.Pass(); |
+ } |
+ |
TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() { |
return test_generated_bubble_controller_; |
} |
@@ -632,6 +668,10 @@ class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness { |
// Returned when the dialog closes successfully. |
const FormStructure* form_structure_; |
+ // A mock validator object to prevent network requests and track when |
+ // validation rules are loaded or validation attempts occur. |
+ scoped_ptr<MockAddressValidator> mock_validator_; |
+ |
// Used to monitor if the Autofill credit card bubble is shown. Owned by |
// |web_contents()|. |
TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_; |
@@ -2933,12 +2973,17 @@ TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) { |
} |
} |
-TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) { |
- CommandLine* command_line = CommandLine::ForCurrentProcess(); |
- command_line->AppendSwitch(::switches::kEnableAutofillAddressI18n); |
- |
- Reset(); |
+class AutofillDialogControllerI18nTest : public AutofillDialogControllerTest { |
+ public: |
+ // AutofillDialogControllerTest implementation. |
+ virtual void SetUp() OVERRIDE { |
+ CommandLine* command_line = CommandLine::ForCurrentProcess(); |
+ command_line->AppendSwitch(::switches::kEnableAutofillAddressI18n); |
+ AutofillDialogControllerTest::SetUp(); |
+ } |
+}; |
+TEST_F(AutofillDialogControllerI18nTest, CountryChangeUpdatesSection) { |
TestAutofillDialogView* view = controller()->GetView(); |
view->ClearSectionUpdates(); |
@@ -2978,4 +3023,47 @@ TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) { |
EXPECT_EQ(1U, updates.size()); |
} |
+TEST_F(AutofillDialogControllerI18nTest, CorrectCountryFromInputs) { |
+ FieldValueMap inputs; |
+ inputs[ADDRESS_BILLING_COUNTRY] = base::ASCIIToUTF16("China"); |
+ inputs[ADDRESS_HOME_COUNTRY] = base::ASCIIToUTF16("France"); |
+ |
+ base::Callback<base::string16(DialogSection, const AutofillType&)> get_info = |
+ base::Bind(&AutofillDialogControllerImpl::GetInfoFromInputs, |
Evan Stade
2014/01/31 04:09:52
I feel like you should be able to test this functi
Dan Beam
2014/02/01 00:43:22
Done.
|
+ base::ConstRef(inputs)); |
+ |
+ // Ensure that callbacks bound to the billing section only return |
+ // billing-specific info from |inputs| (e.g. the billing country). |
+ EXPECT_EQ(base::ASCIIToUTF16("China"), |
+ get_info.Run(SECTION_BILLING, AutofillType(ADDRESS_HOME_COUNTRY))); |
+ EXPECT_EQ(base::ASCIIToUTF16("CN"), |
+ get_info.Run(SECTION_BILLING, AutofillType(HTML_TYPE_COUNTRY_CODE, |
+ HTML_MODE_SHIPPING))); |
+ |
+ // And conversely that shipping sections only return the shipping country. |
+ EXPECT_EQ( |
+ base::ASCIIToUTF16("France"), |
+ get_info.Run(SECTION_SHIPPING, AutofillType(ADDRESS_BILLING_COUNTRY))); |
+ EXPECT_EQ(base::ASCIIToUTF16("FR"), |
+ get_info.Run(SECTION_SHIPPING, AutofillType(HTML_TYPE_COUNTRY_CODE, |
+ HTML_MODE_BILLING))); |
+} |
+ |
+TEST_F(AutofillDialogControllerI18nTest, LoadValidationRules) { |
+ SetMockValidator(make_scoped_ptr(new MockAddressValidator)); |
+ EXPECT_CALL(*GetMockValidator(), ValidateAddress(_, _, _)). |
+ WillRepeatedly(Return(MockAddressValidator::RULES_NOT_READY)); |
+ |
+ EXPECT_CALL(*GetMockValidator(), LoadRules("US")).Times(AtLeast(1)); |
+ Reset(); |
+ |
+ EXPECT_CALL(*GetMockValidator(), LoadRules("FR")); |
+ controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING, |
+ ADDRESS_BILLING_COUNTRY, |
+ gfx::NativeView(), |
+ gfx::Rect(), |
+ ASCIIToUTF16("France"), |
+ true); |
+} |
+ |
} // namespace autofill |