data_model_wrapper_unittest.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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.
4
5#include "base/guid.h"
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"
18
19using base::ASCIIToUTF16;
20
21namespace autofill {
22
23TEST(AutofillCreditCardWrapperTest, GetInfoCreditCardExpMonth) {
24  CreditCard card;
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)));
31  }
32}
33
34TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenExpired) {
35  CreditCard card;
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));
42}
43
44TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenInvalid) {
45  CreditCard card;
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));
52}
53
54TEST(AutofillCreditCardWrapperTest, GetDisplayTextNotEmptyWhenValid) {
55  CreditCard card;
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));
62}
63
64TEST(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));
71}
72
73TEST(DataModelWrapperTest, GetDisplayTextEmptyWithoutPhone) {
74  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
75      wallet::GetTestMaskedInstrument());
76
77  WalletInstrumentWrapper instrument_wrapper(instrument.get());
78  base::string16 unused, unused2;
79  EXPECT_TRUE(instrument_wrapper.GetDisplayText(&unused, &unused2));
80
81  WalletAddressWrapper address_wrapper(&instrument->address());
82  EXPECT_TRUE(address_wrapper.GetDisplayText(&unused, &unused2));
83
84  const_cast<wallet::Address*>(&instrument->address())->SetPhoneNumber(
85      base::string16());
86
87  EXPECT_EQ(base::string16(),
88            instrument_wrapper.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
89  EXPECT_FALSE(instrument_wrapper.GetDisplayText(&unused, &unused2));
90
91  EXPECT_EQ(base::string16(),
92            address_wrapper.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
93  EXPECT_FALSE(address_wrapper.GetDisplayText(&unused, &unused2));
94}
95
96TEST(DataModelWrapperTest, GetDisplayText) {
97  AutofillProfile profile = test::GetFullProfile();
98  base::string16 vertical, horizontal;
99  EXPECT_TRUE(
100      AutofillProfileWrapper(&profile).GetDisplayText(&horizontal, &vertical));
101  EXPECT_EQ(ASCIIToUTF16(
102                "John H. Doe, 666 Erebus St., Apt 8, Elysium, CA 91111\n"
103                "johndoe@hades.com\n"
104                "1 650-211-1111"),
105            horizontal);
106  EXPECT_EQ(ASCIIToUTF16(
107                "John H. Doe\n"
108                "666 Erebus St.\n"
109                "Apt 8\n"
110                "Elysium, CA 91111\n"
111                "johndoe@hades.com\n"
112                "1 650-211-1111"),
113            vertical);
114
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(
121      ADDRESS_HOME_STATE,
122      base::WideToUTF16(L"\u6771\u4EAC\u90FD"));
123  foreign_profile.SetRawInfo(
124      ADDRESS_HOME_CITY,
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,
131      base::WideToUTF16(
132          L"\u685C\u4E18\u753A26-1\n"
133          L"\u30BB\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC6\u968E"));
134  foreign_profile.SetRawInfo(
135      NAME_FULL,
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"));
140
141  EXPECT_TRUE(AutofillProfileWrapper(&foreign_profile).GetDisplayText(
142      &horizontal, &vertical));
143  EXPECT_EQ(
144      base::WideToUTF16(
145          L"\u3012150-8512\n"
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"
150          L"+81-3-6384-9000"),
151      vertical);
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
155}
156
157TEST(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)));
166  }
167}
168
169TEST(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_unformatted_with_plus =
174      ASCIIToUTF16("+13104567890");
175  const base::string16 international_formatted = ASCIIToUTF16("1 310-456-7890");
176  const base::string16 user_formatted = ASCIIToUTF16("310.456 78 90");
177
178  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
179      wallet::GetTestMaskedInstrument());
180  AutofillProfile profile(test::GetVerifiedProfile());
181
182  // No matter what format a wallet number is in, it gets formatted in a
183  // standard way.
184  WalletInstrumentWrapper instrument_wrapper(instrument.get());
185  const_cast<wallet::Address*>(&instrument->address())->
186      SetPhoneNumber(national_unformatted);
187  EXPECT_EQ(national_formatted,
188            instrument_wrapper.GetInfoForDisplay(
189                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
190  WalletAddressWrapper address_wrapper(&instrument->address());
191  EXPECT_EQ(national_formatted,
192            address_wrapper.GetInfoForDisplay(
193                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
194  AutofillProfileWrapper profile_wrapper(&profile);
195
196  const_cast<wallet::Address*>(&instrument->address())->
197      SetPhoneNumber(national_formatted);
198  EXPECT_EQ(national_formatted,
199            instrument_wrapper.GetInfoForDisplay(
200                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
201  EXPECT_EQ(national_formatted,
202            address_wrapper.GetInfoForDisplay(
203                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
204
205  const_cast<wallet::Address*>(&instrument->address())->
206      SetPhoneNumber(international_unformatted);
207  EXPECT_EQ(national_formatted,
208            instrument_wrapper.GetInfoForDisplay(
209                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
210  EXPECT_EQ(national_formatted,
211            address_wrapper.GetInfoForDisplay(
212                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
213
214  const_cast<wallet::Address*>(&instrument->address())
215      ->SetPhoneNumber(international_unformatted_with_plus);
216  EXPECT_EQ(national_formatted,
217            instrument_wrapper.GetInfoForDisplay(
218                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
219  EXPECT_EQ(
220      national_formatted,
221      address_wrapper.GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
222
223  // Autofill numbers that are unformatted get formatted either nationally or
224  // internationally depending on the presence of a country code. Formatted
225  // numbers stay formatted.
226  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, international_unformatted);
227  EXPECT_EQ(international_formatted,
228            profile_wrapper.GetInfoForDisplay(
229                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
230  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
231                     international_unformatted_with_plus);
232  EXPECT_EQ(
233      international_formatted,
234      profile_wrapper.GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
235  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_unformatted);
236  EXPECT_EQ(national_formatted,
237            profile_wrapper.GetInfoForDisplay(
238                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
239  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_formatted);
240  EXPECT_EQ(national_formatted,
241            profile_wrapper.GetInfoForDisplay(
242                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
243  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, user_formatted);
244  EXPECT_EQ(user_formatted,
245            profile_wrapper.GetInfoForDisplay(
246                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
247}
248
249}  // namespace autofill
250