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.
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
10 #include "chrome/browser/ui/autofill/data_model_wrapper.h"
11 #include "components/autofill/content/browser/wallet/wallet_items.h"
12 #include "components/autofill/content/browser/wallet/wallet_test_util.h"
13 #include "components/autofill/core/browser/autofill_profile.h"
14 #include "components/autofill/core/browser/autofill_test_utils.h"
15 #include "components/autofill/core/browser/credit_card.h"
16 #include "components/autofill/core/browser/field_types.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using base::ASCIIToUTF16
;
23 TEST(AutofillCreditCardWrapperTest
, GetInfoCreditCardExpMonth
) {
25 MonthComboboxModel model
;
26 for (int month
= 1; month
<= 12; ++month
) {
27 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, base::IntToString16(month
));
28 AutofillCreditCardWrapper
wrapper(&card
);
29 EXPECT_EQ(model
.GetItemAt(month
),
30 wrapper
.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH
)));
34 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextEmptyWhenExpired
) {
36 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("1"));
37 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("2010"));
38 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("4111111111111111"));
39 AutofillCreditCardWrapper
wrapper(&card
);
40 base::string16 unused
, unused2
;
41 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
44 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextEmptyWhenInvalid
) {
46 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
47 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("9999"));
48 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("41111"));
49 AutofillCreditCardWrapper
wrapper(&card
);
50 base::string16 unused
, unused2
;
51 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
54 TEST(AutofillCreditCardWrapperTest
, GetDisplayTextNotEmptyWhenValid
) {
56 card
.SetRawInfo(CREDIT_CARD_EXP_MONTH
, ASCIIToUTF16("12"));
57 card
.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR
, ASCIIToUTF16("9999"));
58 card
.SetRawInfo(CREDIT_CARD_NUMBER
, ASCIIToUTF16("4111111111111111"));
59 AutofillCreditCardWrapper
wrapper(&card
);
60 base::string16 unused
, unused2
;
61 EXPECT_TRUE(wrapper
.GetDisplayText(&unused
, &unused2
));
64 TEST(WalletInstrumentWrapperTest
, GetDisplayTextEmptyWhenExpired
) {
65 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
66 wallet::GetTestMaskedInstrument());
67 instrument
->status_
= wallet::WalletItems::MaskedInstrument::EXPIRED
;
68 WalletInstrumentWrapper
wrapper(instrument
.get());
69 base::string16 unused
, unused2
;
70 EXPECT_FALSE(wrapper
.GetDisplayText(&unused
, &unused2
));
73 TEST(DataModelWrapperTest
, GetDisplayTextEmptyWithoutPhone
) {
74 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
75 wallet::GetTestMaskedInstrument());
77 WalletInstrumentWrapper
instrument_wrapper(instrument
.get());
78 base::string16 unused
, unused2
;
79 EXPECT_TRUE(instrument_wrapper
.GetDisplayText(&unused
, &unused2
));
81 WalletAddressWrapper
address_wrapper(&instrument
->address());
82 EXPECT_TRUE(address_wrapper
.GetDisplayText(&unused
, &unused2
));
84 const_cast<wallet::Address
*>(&instrument
->address())->SetPhoneNumber(
87 EXPECT_EQ(base::string16(),
88 instrument_wrapper
.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
89 EXPECT_FALSE(instrument_wrapper
.GetDisplayText(&unused
, &unused2
));
91 EXPECT_EQ(base::string16(),
92 address_wrapper
.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
93 EXPECT_FALSE(address_wrapper
.GetDisplayText(&unused
, &unused2
));
96 TEST(DataModelWrapperTest
, GetDisplayText
) {
97 AutofillProfile profile
= test::GetFullProfile();
98 base::string16 vertical
, horizontal
;
100 AutofillProfileWrapper(&profile
).GetDisplayText(&horizontal
, &vertical
));
102 ASCIIToUTF16("John H. Doe, 666 Erebus St., Apt 8, Elysium, CA 91111\n"
103 "johndoe@hades.com\n"
107 ASCIIToUTF16("John H. Doe\n"
110 "Elysium, CA 91111\n"
111 "johndoe@hades.com\n"
115 // A Japanese address.
116 AutofillProfile
foreign_profile(
117 base::GenerateGUID(), "http://www.example.com/");
118 foreign_profile
.SetRawInfo(
119 ADDRESS_HOME_COUNTRY
, ASCIIToUTF16("JP"));
120 foreign_profile
.SetRawInfo(
122 base::WideToUTF16(L
"\u6771\u4EAC\u90FD"));
123 foreign_profile
.SetRawInfo(
125 base::WideToUTF16(L
"\u6E0B\u8C37\u533A"));
126 foreign_profile
.SetRawInfo(
127 ADDRESS_HOME_DEPENDENT_LOCALITY
,
128 base::WideToUTF16(L
"\uC911\uAD6C"));
129 foreign_profile
.SetRawInfo(
130 ADDRESS_HOME_STREET_ADDRESS
,
132 L
"\u685C\u4E18\u753A26-1\n"
133 L
"\u30BB\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC6\u968E"));
134 foreign_profile
.SetRawInfo(
136 base::WideToUTF16(L
"\u6751\u4E0A \u7F8E\u7D00"));
137 foreign_profile
.SetRawInfo(ADDRESS_HOME_ZIP
, base::ASCIIToUTF16("150-8512"));
138 foreign_profile
.SetRawInfo(
139 PHONE_HOME_WHOLE_NUMBER
, ASCIIToUTF16("+81-3-6384-9000"));
141 EXPECT_TRUE(AutofillProfileWrapper(&foreign_profile
).GetDisplayText(
142 &horizontal
, &vertical
));
146 L
"\u6771\u4EAC\u90FD\u6E0B\u8C37\u533A\n"
147 L
"\u685C\u4E18\u753A26-1\n"
148 L
"\u30BB\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC6\u968E\n"
149 L
"\u6751\u4E0A \u7F8E\u7D00\n"
152 // TODO(estade): we should also verify that |horizontal| is correct, but right
153 // now it uses the incorrect address line separator. See:
154 // http://crbug.com/270261
157 TEST(WalletInstrumentWrapperTest
, GetInfoCreditCardExpMonth
) {
158 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
159 wallet::GetTestMaskedInstrument());
160 MonthComboboxModel model
;
161 for (int month
= 1; month
<= 12; ++month
) {
162 instrument
->expiration_month_
= month
;
163 WalletInstrumentWrapper
wrapper(instrument
.get());
164 EXPECT_EQ(model
.GetItemAt(month
),
165 wrapper
.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH
)));
169 TEST(DataModelWrapperTest
, GetDisplayPhoneNumber
) {
170 const base::string16 national_unformatted
= ASCIIToUTF16("3104567890");
171 const base::string16 national_formatted
= ASCIIToUTF16("(310) 456-7890");
172 const base::string16 international_unformatted
= ASCIIToUTF16("13104567890");
173 const base::string16 international_formatted
=
174 ASCIIToUTF16("+1 310-456-7890");
175 const base::string16 user_formatted
= ASCIIToUTF16("310.456 78 90");
177 scoped_ptr
<wallet::WalletItems::MaskedInstrument
> instrument(
178 wallet::GetTestMaskedInstrument());
179 AutofillProfile
profile(test::GetVerifiedProfile());
181 // No matter what format a wallet number is in, it gets formatted in a
183 WalletInstrumentWrapper
instrument_wrapper(instrument
.get());
184 const_cast<wallet::Address
*>(&instrument
->address())->
185 SetPhoneNumber(national_unformatted
);
186 EXPECT_EQ(national_formatted
,
187 instrument_wrapper
.GetInfoForDisplay(
188 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
189 WalletAddressWrapper
address_wrapper(&instrument
->address());
190 EXPECT_EQ(national_formatted
,
191 address_wrapper
.GetInfoForDisplay(
192 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
193 AutofillProfileWrapper
profile_wrapper(&profile
);
195 const_cast<wallet::Address
*>(&instrument
->address())->
196 SetPhoneNumber(national_formatted
);
197 EXPECT_EQ(national_formatted
,
198 instrument_wrapper
.GetInfoForDisplay(
199 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
200 EXPECT_EQ(national_formatted
,
201 address_wrapper
.GetInfoForDisplay(
202 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
204 const_cast<wallet::Address
*>(&instrument
->address())->
205 SetPhoneNumber(international_unformatted
);
206 EXPECT_EQ(national_formatted
,
207 instrument_wrapper
.GetInfoForDisplay(
208 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
209 EXPECT_EQ(national_formatted
,
210 address_wrapper
.GetInfoForDisplay(
211 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
213 // Autofill numbers that are unformatted get formatted either nationally or
214 // internationally depending on the presence of a country code. Formatted
215 // numbers stay formatted.
216 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, international_unformatted
);
217 EXPECT_EQ(international_formatted
,
218 profile_wrapper
.GetInfoForDisplay(
219 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
220 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, national_unformatted
);
221 EXPECT_EQ(national_formatted
,
222 profile_wrapper
.GetInfoForDisplay(
223 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
224 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, national_formatted
);
225 EXPECT_EQ(national_formatted
,
226 profile_wrapper
.GetInfoForDisplay(
227 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
228 profile
.SetRawInfo(PHONE_HOME_WHOLE_NUMBER
, user_formatted
);
229 EXPECT_EQ(user_formatted
,
230 profile_wrapper
.GetInfoForDisplay(
231 AutofillType(PHONE_HOME_WHOLE_NUMBER
)));
235 } // namespace autofill