data_model_wrapper_unittest.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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/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"
17
18namespace autofill {
19
20TEST(AutofillCreditCardWrapperTest, GetInfoCreditCardExpMonth) {
21  CreditCard card;
22  MonthComboboxModel model;
23  for (int month = 1; month <= 12; ++month) {
24    card.SetRawInfo(CREDIT_CARD_EXP_MONTH, base::IntToString16(month));
25    AutofillCreditCardWrapper wrapper(&card);
26    EXPECT_EQ(model.GetItemAt(month),
27              wrapper.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH)));
28  }
29}
30
31TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenExpired) {
32  CreditCard card;
33  card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1"));
34  card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2010"));
35  card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
36  AutofillCreditCardWrapper wrapper(&card);
37  base::string16 unused, unused2;
38  EXPECT_FALSE(wrapper.GetDisplayText(&unused, &unused2));
39}
40
41TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenInvalid) {
42  CreditCard card;
43  card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
44  card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
45  card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111"));
46  AutofillCreditCardWrapper wrapper(&card);
47  base::string16 unused, unused2;
48  EXPECT_FALSE(wrapper.GetDisplayText(&unused, &unused2));
49}
50
51TEST(AutofillCreditCardWrapperTest, GetDisplayTextNotEmptyWhenValid) {
52  CreditCard card;
53  card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
54  card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
55  card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
56  AutofillCreditCardWrapper wrapper(&card);
57  base::string16 unused, unused2;
58  EXPECT_TRUE(wrapper.GetDisplayText(&unused, &unused2));
59}
60
61TEST(WalletInstrumentWrapperTest, GetInfoCreditCardExpMonth) {
62  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
63      wallet::GetTestMaskedInstrument());
64  MonthComboboxModel model;
65  for (int month = 1; month <= 12; ++month) {
66    instrument->expiration_month_ = month;
67    WalletInstrumentWrapper wrapper(instrument.get());
68    EXPECT_EQ(model.GetItemAt(month),
69              wrapper.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH)));
70  }
71}
72
73TEST(WalletInstrumentWrapperTest, GetDisplayTextEmptyWhenExpired) {
74  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
75      wallet::GetTestMaskedInstrument());
76  instrument->status_ = wallet::WalletItems::MaskedInstrument::EXPIRED;
77  WalletInstrumentWrapper wrapper(instrument.get());
78  base::string16 unused, unused2;
79  EXPECT_FALSE(wrapper.GetDisplayText(&unused, &unused2));
80}
81
82TEST(DataModelWrapperTest, GetDisplayTextEmptyWithoutPhone) {
83  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
84      wallet::GetTestMaskedInstrument());
85
86  WalletInstrumentWrapper instrument_wrapper(instrument.get());
87  base::string16 unused, unused2;
88  EXPECT_TRUE(instrument_wrapper.GetDisplayText(&unused, &unused2));
89
90  WalletAddressWrapper address_wrapper(&instrument->address());
91  EXPECT_TRUE(address_wrapper.GetDisplayText(&unused, &unused2));
92
93  const_cast<wallet::Address*>(&instrument->address())->SetPhoneNumber(
94      base::string16());
95
96  EXPECT_EQ(base::string16(),
97            instrument_wrapper.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
98  EXPECT_FALSE(instrument_wrapper.GetDisplayText(&unused, &unused2));
99
100  EXPECT_EQ(base::string16(),
101            address_wrapper.GetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
102  EXPECT_FALSE(address_wrapper.GetDisplayText(&unused, &unused2));
103}
104
105TEST(DataModelWrapperTest, GetDisplayPhoneNumber) {
106  const base::string16 national_unformatted = ASCIIToUTF16("3104567890");
107  const base::string16 national_formatted = ASCIIToUTF16("(310) 456-7890");
108  const base::string16 international_unformatted = ASCIIToUTF16("13104567890");
109  const base::string16 international_formatted =
110      ASCIIToUTF16("+1 310-456-7890");
111  const base::string16 user_formatted = ASCIIToUTF16("310.456 78 90");
112
113  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument(
114      wallet::GetTestMaskedInstrument());
115  AutofillProfile profile(test::GetVerifiedProfile());
116
117  // No matter what format a wallet number is in, it gets formatted in a
118  // standard way.
119  WalletInstrumentWrapper instrument_wrapper(instrument.get());
120  const_cast<wallet::Address*>(&instrument->address())->
121      SetPhoneNumber(national_unformatted);
122  EXPECT_EQ(national_formatted,
123            instrument_wrapper.GetInfoForDisplay(
124                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
125  WalletAddressWrapper address_wrapper(&instrument->address());
126  EXPECT_EQ(national_formatted,
127            address_wrapper.GetInfoForDisplay(
128                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
129  AutofillProfileWrapper profile_wrapper(&profile);
130
131  const_cast<wallet::Address*>(&instrument->address())->
132      SetPhoneNumber(national_formatted);
133  EXPECT_EQ(national_formatted,
134            instrument_wrapper.GetInfoForDisplay(
135                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
136  EXPECT_EQ(national_formatted,
137            address_wrapper.GetInfoForDisplay(
138                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
139
140  const_cast<wallet::Address*>(&instrument->address())->
141      SetPhoneNumber(international_unformatted);
142  EXPECT_EQ(national_formatted,
143            instrument_wrapper.GetInfoForDisplay(
144                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
145  EXPECT_EQ(national_formatted,
146            address_wrapper.GetInfoForDisplay(
147                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
148
149  // Autofill numbers that are unformatted get formatted either nationally or
150  // internationally depending on the presence of a country code. Formatted
151  // numbers stay formatted.
152  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, international_unformatted);
153  EXPECT_EQ(international_formatted,
154            profile_wrapper.GetInfoForDisplay(
155                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
156  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_unformatted);
157  EXPECT_EQ(national_formatted,
158            profile_wrapper.GetInfoForDisplay(
159                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
160  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_formatted);
161  EXPECT_EQ(national_formatted,
162            profile_wrapper.GetInfoForDisplay(
163                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
164  profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, user_formatted);
165  EXPECT_EQ(user_formatted,
166            profile_wrapper.GetInfoForDisplay(
167                AutofillType(PHONE_HOME_WHOLE_NUMBER)));
168
169}
170
171TEST(FieldMapWrapperTest, BothShippingAndBillingCanCoexist) {
172  DetailInputs inputs;
173
174  DetailInput billing_street;
175  billing_street.type = ADDRESS_BILLING_STREET_ADDRESS;
176  inputs.push_back(billing_street);
177
178  DetailInput shipping_street;
179  shipping_street.type = ADDRESS_HOME_STREET_ADDRESS;
180  inputs.push_back(shipping_street);
181
182  FieldValueMap outputs;
183  outputs[inputs[0].type] = ASCIIToUTF16("123 billing street");
184  outputs[inputs[1].type] = ASCIIToUTF16("123 shipping street");
185
186  FieldMapWrapper wrapper(outputs);
187  wrapper.FillInputs(&inputs);
188
189  EXPECT_NE(inputs[0].initial_value, inputs[1].initial_value);
190}
191
192}  // namespace autofill
193