Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / autofill / data_model_wrapper_unittest.cc
blobf79bf362f3f6f92be5d2c74587c3a218c8c55303
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/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/core/browser/autofill_profile.h"
12 #include "components/autofill/core/browser/autofill_test_utils.h"
13 #include "components/autofill/core/browser/credit_card.h"
14 #include "components/autofill/core/browser/field_types.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using base::ASCIIToUTF16;
19 namespace autofill {
21 TEST(AutofillCreditCardWrapperTest, GetInfoCreditCardExpMonth) {
22 CreditCard card;
23 MonthComboboxModel model;
24 for (int month = 1; month <= 12; ++month) {
25 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, base::IntToString16(month));
26 AutofillCreditCardWrapper wrapper(&card);
27 EXPECT_EQ(model.GetItemAt(month),
28 wrapper.GetInfo(AutofillType(CREDIT_CARD_EXP_MONTH)));
32 TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenExpired) {
33 CreditCard card;
34 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("1"));
35 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("2010"));
36 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
37 AutofillCreditCardWrapper wrapper(&card);
38 base::string16 unused, unused2;
39 EXPECT_FALSE(wrapper.GetDisplayText(&unused, &unused2));
42 TEST(AutofillCreditCardWrapperTest, GetDisplayTextEmptyWhenInvalid) {
43 CreditCard card;
44 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
45 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
46 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("41111"));
47 AutofillCreditCardWrapper wrapper(&card);
48 base::string16 unused, unused2;
49 EXPECT_FALSE(wrapper.GetDisplayText(&unused, &unused2));
52 TEST(AutofillCreditCardWrapperTest, GetDisplayTextNotEmptyWhenValid) {
53 CreditCard card;
54 card.SetRawInfo(CREDIT_CARD_EXP_MONTH, ASCIIToUTF16("12"));
55 card.SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, ASCIIToUTF16("9999"));
56 card.SetRawInfo(CREDIT_CARD_NUMBER, ASCIIToUTF16("4111111111111111"));
57 AutofillCreditCardWrapper wrapper(&card);
58 base::string16 unused, unused2;
59 EXPECT_TRUE(wrapper.GetDisplayText(&unused, &unused2));
62 TEST(DataModelWrapperTest, GetDisplayText) {
63 AutofillProfile profile = test::GetFullProfile();
64 base::string16 vertical, horizontal;
65 EXPECT_TRUE(
66 AutofillProfileWrapper(&profile).GetDisplayText(&horizontal, &vertical));
67 EXPECT_EQ(ASCIIToUTF16(
68 "John H. Doe, 666 Erebus St., Apt 8, Elysium, CA 91111\n"
69 "johndoe@hades.com\n"
70 "1 650-211-1111"),
71 horizontal);
72 EXPECT_EQ(ASCIIToUTF16(
73 "John H. Doe\n"
74 "666 Erebus St.\n"
75 "Apt 8\n"
76 "Elysium, CA 91111\n"
77 "johndoe@hades.com\n"
78 "1 650-211-1111"),
79 vertical);
81 // A Japanese address.
82 AutofillProfile foreign_profile(
83 base::GenerateGUID(), "http://www.example.com/");
84 foreign_profile.SetRawInfo(
85 ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
86 foreign_profile.SetRawInfo(
87 ADDRESS_HOME_STATE,
88 base::WideToUTF16(L"\u6771\u4EAC\u90FD"));
89 foreign_profile.SetRawInfo(
90 ADDRESS_HOME_CITY,
91 base::WideToUTF16(L"\u6E0B\u8C37\u533A"));
92 foreign_profile.SetRawInfo(
93 ADDRESS_HOME_DEPENDENT_LOCALITY,
94 base::WideToUTF16(L"\uC911\uAD6C"));
95 foreign_profile.SetRawInfo(
96 ADDRESS_HOME_STREET_ADDRESS,
97 base::WideToUTF16(
98 L"\u685C\u4E18\u753A26-1\n"
99 L"\u30BB\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC6\u968E"));
100 foreign_profile.SetRawInfo(
101 NAME_FULL,
102 base::WideToUTF16(L"\u6751\u4E0A \u7F8E\u7D00"));
103 foreign_profile.SetRawInfo(ADDRESS_HOME_ZIP, base::ASCIIToUTF16("150-8512"));
104 foreign_profile.SetRawInfo(
105 PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("+81-3-6384-9000"));
107 EXPECT_TRUE(AutofillProfileWrapper(&foreign_profile).GetDisplayText(
108 &horizontal, &vertical));
109 EXPECT_EQ(
110 base::WideToUTF16(
111 L"\u3012150-8512\n"
112 L"\u6771\u4EAC\u90FD\u6E0B\u8C37\u533A\n"
113 L"\u685C\u4E18\u753A26-1\n"
114 L"\u30BB\u30EB\u30EA\u30A2\u30F3\u30BF\u30EF\u30FC6\u968E\n"
115 L"\u6751\u4E0A \u7F8E\u7D00\n"
116 L"+81-3-6384-9000"),
117 vertical);
118 // TODO(estade): we should also verify that |horizontal| is correct, but right
119 // now it uses the incorrect address line separator. See:
120 // http://crbug.com/270261
123 TEST(DataModelWrapperTest, GetDisplayPhoneNumber) {
124 const base::string16 national_unformatted = ASCIIToUTF16("3104567890");
125 const base::string16 national_formatted = ASCIIToUTF16("(310) 456-7890");
126 const base::string16 international_unformatted = ASCIIToUTF16("13104567890");
127 const base::string16 international_unformatted_with_plus =
128 ASCIIToUTF16("+13104567890");
129 const base::string16 international_formatted = ASCIIToUTF16("1 310-456-7890");
130 const base::string16 user_formatted = ASCIIToUTF16("310.456 78 90");
132 // Autofill numbers that are unformatted get formatted either nationally or
133 // internationally depending on the presence of a country code. Formatted
134 // numbers stay formatted.
135 AutofillProfile profile(test::GetVerifiedProfile());
136 AutofillProfileWrapper profile_wrapper(&profile);
137 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, international_unformatted);
138 EXPECT_EQ(international_formatted,
139 profile_wrapper.GetInfoForDisplay(
140 AutofillType(PHONE_HOME_WHOLE_NUMBER)));
141 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
142 international_unformatted_with_plus);
143 EXPECT_EQ(
144 international_formatted,
145 profile_wrapper.GetInfoForDisplay(AutofillType(PHONE_HOME_WHOLE_NUMBER)));
146 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_unformatted);
147 EXPECT_EQ(national_formatted,
148 profile_wrapper.GetInfoForDisplay(
149 AutofillType(PHONE_HOME_WHOLE_NUMBER)));
150 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, national_formatted);
151 EXPECT_EQ(national_formatted,
152 profile_wrapper.GetInfoForDisplay(
153 AutofillType(PHONE_HOME_WHOLE_NUMBER)));
154 profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, user_formatted);
155 EXPECT_EQ(user_formatted,
156 profile_wrapper.GetInfoForDisplay(
157 AutofillType(PHONE_HOME_WHOLE_NUMBER)));
160 } // namespace autofill