1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/strings/string_number_conversions.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
9 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
10 #include "components/autofill/content/browser/wallet/wallet_items.h"
11 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
12 #include "components/autofill/core/browser/autofill_profile.h"
13 #include "components/autofill/core/browser/autofill_test_utils.h"
14 #include "components/autofill/core/browser/credit_card.h"
15 #include "components/autofill/core/browser/field_types.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 using base::ASCIIToUTF16
;
22 TEST(AutofillCreditCardWrapperTest
, GetInfoCreditCardExpMonth
) {
24 MonthComboboxModel model
;
25 for (int month
= 1; month
<= 12; ++month
) {
26 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, base::IntToString16(month
));
27 AutofillCreditCardWrapper
wrapper(&card
);
28 EXPECT_EQ(model
.GetItemAt(month
),
29 wrapper
.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH
)));
33 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextEmptyWhenExpired
) {
35 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
36 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2010"));
37 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("4111111111111111"));
38 AutofillCreditCardWrapper
wrapper(&card
);
39 base::string16 unused
, unused2
;
40 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
43 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextEmptyWhenInvalid
) {
45 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
46 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("9999"));
47 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("41111"));
48 AutofillCreditCardWrapper
wrapper(&card
);
49 base::string16 unused
, unused2
;
50 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
53 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextNotEmptyWhenValid
) {
55 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
56 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("9999"));
57 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("4111111111111111"));
58 AutofillCreditCardWrapper
wrapper(&card
);
59 base::string16 unused
, unused2
;
60 EXPECT_TRUE(wrapper
.GetDisplayText(&unused
, &unused2
));
63 TEST(WalletInstrumentWrapperTest
, GetInfoCreditCardExpMonth
) {
64 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
65 wallet::GetTestMaskedInstrument());
66 MonthComboboxModel model
;
67 for (int month
= 1; month
<= 12; ++month
) {
68 instrument
->expiration_month_
= month
;
69 WalletInstrumentWrapper
wrapper(instrument
.get());
70 EXPECT_EQ(model
.GetItemAt(month
),
71 wrapper
.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH
)));
75 TEST(WalletInstrumentWrapperTest
, GetDisplayTextEmptyWhenExpired
) {
76 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
77 wallet::GetTestMaskedInstrument());
78 instrument
->status_
= wallet::WalletItems::MaskedInstrument::EXPIRED
;
79 WalletInstrumentWrapper
wrapper(instrument
.get());
80 base::string16 unused
, unused2
;
81 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
84 TEST(DataModelWrapperTest
, GetDisplayTextEmptyWithoutPhone
) {
85 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
86 wallet::GetTestMaskedInstrument());
88 WalletInstrumentWrapper
instrument_wrapper(instrument
.get());
89 base::string16 unused
, unused2
;
90 EXPECT_TRUE(instrument_wrapper
.GetDisplayText(&unused
, &unused2
));
92 WalletAddressWrapper
address_wrapper(&instrument
->address());
93 EXPECT_TRUE(address_wrapper
.GetDisplayText(&unused
, &unused2
));
95 const_cast<wallet::Address
*>(&instrument
->address())->SetPhoneNumber(
98 EXPECT_EQ(base::string16(),
99 instrument_wrapper
.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
100 EXPECT_FALSE(instrument_wrapper
.GetDisplayText(&unused
, &unused2
));
102 EXPECT_EQ(base::string16(),
103 address_wrapper
.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
104 EXPECT_FALSE(address_wrapper
.GetDisplayText(&unused
, &unused2
));
107 TEST(DataModelWrapperTest
, GetDisplayPhoneNumber
) {
108 const base::string16 national_unformatted
= ASCIIToUTF16("3104567890");
109 const base::string16 national_formatted
= ASCIIToUTF16("(310) 456-7890");
110 const base::string16 international_unformatted
= ASCIIToUTF16("13104567890");
111 const base::string16 international_formatted
=
112 ASCIIToUTF16("+1 310-456-7890");
113 const base::string16 user_formatted
= ASCIIToUTF16("310.456 78 90");
115 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
116 wallet::GetTestMaskedInstrument());
117 AutofillProfile
profile(test::GetVerifiedProfile());
119 // No matter what format a wallet number is in, it gets formatted in a
121 WalletInstrumentWrapper
instrument_wrapper(instrument
.get());
122 const_cast<wallet::Address
*>(&instrument
->address())->
123 SetPhoneNumber(national_unformatted
);
124 EXPECT_EQ(national_formatted
,
125 instrument_wrapper
.GetInfoForDisplay(
126 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
127 WalletAddressWrapper
address_wrapper(&instrument
->address());
128 EXPECT_EQ(national_formatted
,
129 address_wrapper
.GetInfoForDisplay(
130 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
131 AutofillProfileWrapper
profile_wrapper(&profile
);
133 const_cast<wallet::Address
*>(&instrument
->address())->
134 SetPhoneNumber(national_formatted
);
135 EXPECT_EQ(national_formatted
,
136 instrument_wrapper
.GetInfoForDisplay(
137 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
138 EXPECT_EQ(national_formatted
,
139 address_wrapper
.GetInfoForDisplay(
140 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
142 const_cast<wallet::Address
*>(&instrument
->address())->
143 SetPhoneNumber(international_unformatted
);
144 EXPECT_EQ(national_formatted
,
145 instrument_wrapper
.GetInfoForDisplay(
146 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
147 EXPECT_EQ(national_formatted
,
148 address_wrapper
.GetInfoForDisplay(
149 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
151 // Autofill numbers that are unformatted get formatted either nationally or
152 // internationally depending on the presence of a country code. Formatted
153 // numbers stay formatted.
154 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, international_unformatted
);
155 EXPECT_EQ(international_formatted
,
156 profile_wrapper
.GetInfoForDisplay(
157 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
158 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, national_unformatted
);
159 EXPECT_EQ(national_formatted
,
160 profile_wrapper
.GetInfoForDisplay(
161 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
162 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, national_formatted
);
163 EXPECT_EQ(national_formatted
,
164 profile_wrapper
.GetInfoForDisplay(
165 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
166 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, user_formatted
);
167 EXPECT_EQ(user_formatted
,
168 profile_wrapper
.GetInfoForDisplay(
169 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
173 TEST(FieldMapWrapperTest
, BothShippingAndBillingCanCoexist
) {
176 DetailInput billing_street
;
177 billing_street
.type
= ADDRESS_BILLING_STREET_ADDRESS
;
178 inputs
.push_back(billing_street
);
180 DetailInput shipping_street
;
181 shipping_street
.type
= ADDRESS_HOME_STREET_ADDRESS
;
182 inputs
.push_back(shipping_street
);
184 FieldValueMap outputs
;
185 outputs
[inputs
[0].type
] = ASCIIToUTF16("123 billing street");
186 outputs
[inputs
[1].type
] = ASCIIToUTF16("123 shipping street");
188 FieldMapWrapper
wrapper(outputs
);
189 wrapper
.FillInputs(&inputs
);
191 EXPECT_NE(inputs
[0].initial_value
, inputs
[1].initial_value
);
194 } // namespace autofill