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
5package org.chromium.chrome.browser.autofill;
6
7import android.test.suitebuilder.annotation.SmallTest;
8
9import org.chromium.base.test.util.Feature;
10import org.chromium.chrome.browser.autofill.PersonalDataManager.AutofillProfile;
11import org.chromium.chrome.browser.autofill.PersonalDataManager.CreditCard;
12import org.chromium.chrome.shell.ChromeShellTestBase;
13
14import java.util.LinkedList;
15import java.util.List;
16import java.util.concurrent.ExecutionException;
17
18/**
19 * Tests for Chrome on Android's usage of the PersonalDataManager API.
20 */
21public class PersonalDataManagerTest extends ChromeShellTestBase {
22
23    private AutofillTestHelper mHelper;
24
25    @Override
26    public void setUp() throws Exception {
27        super.setUp();
28        clearAppData();
29        launchChromeShellWithBlankPage();
30        assertTrue(waitForActiveShellToBeDoneLoading());
31
32        mHelper = new AutofillTestHelper();
33    }
34
35    @SmallTest
36    @Feature({"Autofill"})
37    public void testAddAndEditProfiles() throws InterruptedException, ExecutionException {
38        AutofillProfile profile = new AutofillProfile(
39                "" /* guid */, "https://www.example.com" /* origin */,
40                "John Smith", "Acme Inc.",
41                "1 Main\nApt A", "CA", "San Francisco", "",
42                "94102", "",
43                "US", "4158889999", "john@acme.inc", "");
44        String profileOneGUID = mHelper.setProfile(profile);
45        assertEquals(1, mHelper.getNumberOfProfiles());
46
47        AutofillProfile profile2 = new AutofillProfile(
48                "" /* guid */, "http://www.example.com" /* origin */,
49                "John Hackock", "Acme Inc.",
50                "1 Main\nApt A", "CA", "San Francisco", "",
51                "94102", "",
52                "US", "4158889999", "john@acme.inc", "");
53        String profileTwoGUID = mHelper.setProfile(profile2);
54        assertEquals(2, mHelper.getNumberOfProfiles());
55
56        profile.setGUID(profileOneGUID);
57        profile.setCountryCode("CA");
58        mHelper.setProfile(profile);
59        assertEquals("Should still have only two profiles", 2, mHelper.getNumberOfProfiles());
60
61        AutofillProfile storedProfile = mHelper.getProfile(profileOneGUID);
62        assertEquals(profileOneGUID, storedProfile.getGUID());
63        assertEquals("https://www.example.com", storedProfile.getOrigin());
64        assertEquals("CA", storedProfile.getCountryCode());
65        assertEquals("San Francisco", storedProfile.getLocality());
66        assertNotNull(mHelper.getProfile(profileTwoGUID));
67    }
68
69    @SmallTest
70    @Feature({"Autofill"})
71    public void testUpdateLanguageCodeInProfile() throws InterruptedException, ExecutionException {
72        AutofillProfile profile = new AutofillProfile(
73                "" /* guid */, "https://www.example.com" /* origin */,
74                "John Smith", "Acme Inc.",
75                "1 Main\nApt A", "CA", "San Francisco", "",
76                "94102", "",
77                "US", "4158889999", "john@acme.inc", "fr");
78        assertEquals("fr", profile.getLanguageCode());
79        String profileOneGUID = mHelper.setProfile(profile);
80        assertEquals(1, mHelper.getNumberOfProfiles());
81
82        AutofillProfile storedProfile = mHelper.getProfile(profileOneGUID);
83        assertEquals(profileOneGUID, storedProfile.getGUID());
84        assertEquals("fr", storedProfile.getLanguageCode());
85        assertEquals("US", storedProfile.getCountryCode());
86
87        profile.setGUID(profileOneGUID);
88        profile.setLanguageCode("en");
89        mHelper.setProfile(profile);
90
91        AutofillProfile storedProfile2 = mHelper.getProfile(profileOneGUID);
92        assertEquals(profileOneGUID, storedProfile2.getGUID());
93        assertEquals("en", storedProfile2.getLanguageCode());
94        assertEquals("US", storedProfile2.getCountryCode());
95        assertEquals("San Francisco", storedProfile2.getLocality());
96        assertEquals("https://www.example.com", storedProfile2.getOrigin());
97    }
98
99    @SmallTest
100    @Feature({"Autofill"})
101    public void testAddAndDeleteProfile() throws InterruptedException, ExecutionException {
102        AutofillProfile profile = new AutofillProfile(
103                "" /* guid */, "Chrome settings" /* origin */,
104                "John Smith", "Acme Inc.",
105                "1 Main\nApt A", "CA", "San Francisco", "",
106                "94102", "",
107                "US", "4158889999", "john@acme.inc", "");
108        String profileOneGUID = mHelper.setProfile(profile);
109        assertEquals(1, mHelper.getNumberOfProfiles());
110
111        mHelper.deleteProfile(profileOneGUID);
112        assertEquals(0, mHelper.getNumberOfProfiles());
113    }
114
115    @SmallTest
116    @Feature({"Autofill"})
117    public void testAddAndEditCreditCards() throws InterruptedException, ExecutionException {
118        CreditCard card = new CreditCard(
119                "" /* guid */, "https://www.example.com" /* origin */,
120                "Visa", "1234123412341234", "", "5", "2020");
121        String cardOneGUID = mHelper.setCreditCard(card);
122        assertEquals(1, mHelper.getNumberOfCreditCards());
123
124        CreditCard card2 = new CreditCard(
125                "" /* guid */, "http://www.example.com" /* origin */,
126                "American Express", "1234123412341234", "", "8", "2020");
127        String cardTwoGUID = mHelper.setCreditCard(card2);
128        assertEquals(2, mHelper.getNumberOfCreditCards());
129
130        card.setGUID(cardOneGUID);
131        card.setMonth("10");
132        card.setNumber("5678567856785678");
133        mHelper.setCreditCard(card);
134        assertEquals("Should still have only two cards", 2, mHelper.getNumberOfCreditCards());
135
136        CreditCard storedCard = mHelper.getCreditCard(cardOneGUID);
137        assertEquals(cardOneGUID, storedCard.getGUID());
138        assertEquals("https://www.example.com", storedCard.getOrigin());
139        assertEquals("Visa", storedCard.getName());
140        assertEquals("10", storedCard.getMonth());
141        assertEquals("5678567856785678", storedCard.getNumber());
142        assertEquals("************5678", storedCard.getObfuscatedNumber());
143        assertNotNull(mHelper.getCreditCard(cardTwoGUID));
144    }
145
146    @SmallTest
147    @Feature({"Autofill"})
148    public void testAddAndDeleteCreditCard() throws InterruptedException, ExecutionException {
149        CreditCard card = new CreditCard(
150                "" /* guid */, "Chrome settings" /* origin */,
151                "Visa", "1234123412341234", "", "5", "2020");
152        String cardOneGUID = mHelper.setCreditCard(card);
153        assertEquals(1, mHelper.getNumberOfCreditCards());
154
155        mHelper.deleteCreditCard(cardOneGUID);
156        assertEquals(0, mHelper.getNumberOfCreditCards());
157    }
158
159    @SmallTest
160    @Feature({"Autofill"})
161    public void testRespectCountryCodes() throws InterruptedException, ExecutionException {
162        // The constructor should accept country names and ISO 3166-1-alpha-2 country codes.
163        // getCountryCode() should return a country code.
164        AutofillProfile profile1 = new AutofillProfile(
165                "" /* guid */, "https://www.example.com" /* origin */,
166                "John Smith", "Acme Inc.",
167                "1 Main\nApt A", "Quebec", "Montreal", "",
168                "H3B 2Y5", "",
169                "Canada", "514-670-1234", "john@acme.inc", "");
170        String profileGuid1 = mHelper.setProfile(profile1);
171
172        AutofillProfile profile2 = new AutofillProfile(
173                "" /* guid */, "https://www.example.com" /* origin */,
174                "Greg Smith", "Ucme Inc.",
175                "123 Bush\nApt 125", "Quebec", "Montreal", "",
176                "H3B 2Y5", "",
177                "CA", "514-670-4321", "greg@ucme.inc", "");
178        String profileGuid2 = mHelper.setProfile(profile2);
179
180        assertEquals(2, mHelper.getNumberOfProfiles());
181
182        AutofillProfile storedProfile1 = mHelper.getProfile(profileGuid1);
183        assertEquals("CA", storedProfile1.getCountryCode());
184
185        AutofillProfile storedProfile2 = mHelper.getProfile(profileGuid2);
186        assertEquals("CA", storedProfile2.getCountryCode());
187    }
188
189    @SmallTest
190    @Feature({"Autofill"})
191    public void testMultilineStreetAddress() throws InterruptedException, ExecutionException {
192        final String streetAddress1 = "Chez Mireille COPEAU Appartment. 2\n"
193                + "Entree A Batiment Jonquille\n"
194                + "25 RUE DE L'EGLISE";
195        final String streetAddress2 = streetAddress1 + "\n"
196                + "Fourth floor\n"
197                + "The red bell";
198        AutofillProfile profile = new AutofillProfile(
199                "" /* guid */, "https://www.example.com" /* origin */,
200                "Monsieur Jean DELHOURME", "Acme Inc.",
201                streetAddress1,
202                "Tahiti", "Mahina", "Orofara",
203                "98709", "CEDEX 98703",
204                "French Polynesia", "50.71.53", "john@acme.inc", "");
205        String profileGuid1 = mHelper.setProfile(profile);
206        assertEquals(1, mHelper.getNumberOfProfiles());
207        AutofillProfile storedProfile1 = mHelper.getProfile(profileGuid1);
208        assertEquals("PF", storedProfile1.getCountryCode());
209        assertEquals("Monsieur Jean DELHOURME", storedProfile1.getFullName());
210        assertEquals(streetAddress1, storedProfile1.getStreetAddress());
211        assertEquals("Tahiti", storedProfile1.getRegion());
212        assertEquals("Mahina", storedProfile1.getLocality());
213        assertEquals("Orofara", storedProfile1.getDependentLocality());
214        assertEquals("98709", storedProfile1.getPostalCode());
215        assertEquals("CEDEX 98703", storedProfile1.getSortingCode());
216        assertEquals("50.71.53", storedProfile1.getPhoneNumber());
217        assertEquals("john@acme.inc", storedProfile1.getEmailAddress());
218
219        profile.setStreetAddress(streetAddress2);
220        String profileGuid2 = mHelper.setProfile(profile);
221        assertEquals(2, mHelper.getNumberOfProfiles());
222        AutofillProfile storedProfile2 = mHelper.getProfile(profileGuid2);
223        assertEquals(streetAddress2, storedProfile2.getStreetAddress());
224    }
225
226    @SmallTest
227    @Feature({"Autofill"})
228    public void testLabels()  throws InterruptedException, ExecutionException {
229        AutofillProfile profile1 = new AutofillProfile(
230                 "" /* guid */, "https://www.example.com" /* origin */,
231                 "John Major", "Acme Inc.",
232                 "123 Main", "California", "Los Angeles", "",
233                 "90210", "",
234                 "US", "555 123-4567", "jm@example.com", "");
235        // An almost identical profile.
236        AutofillProfile profile2 = new AutofillProfile(
237                 "" /* guid */, "https://www.example.com" /* origin */,
238                 "John Major", "Acme Inc.",
239                 "123 Main", "California", "Los Angeles", "",
240                 "90210", "",
241                 "US", "555 123-4567", "jm-work@example.com", "");
242        // A different profile.
243        AutofillProfile profile3 = new AutofillProfile(
244                 "" /* guid */, "https://www.example.com" /* origin */,
245                 "Jasper Lundgren", "",
246                 "1500 Second Ave", "California", "Hollywood", "",
247                 "90068", "",
248                 "US", "555 123-9876", "jasperl@example.com", "");
249        // A profile where a lot of stuff is missing.
250        AutofillProfile profile4 = new AutofillProfile(
251                 "" /* guid */, "https://www.example.com" /* origin */,
252                 "Joe Sergeant", "",
253                 "", "Texas", "Fort Worth", "",
254                 "", "",
255                 "US", "", "", "");
256
257        mHelper.setProfile(profile1);
258        mHelper.setProfile(profile2);
259        mHelper.setProfile(profile3);
260        mHelper.setProfile(profile4);
261
262        List<String> expectedLabels = new LinkedList<String>();
263        expectedLabels.add("123 Main, Los Angeles, jm@example.com");
264        expectedLabels.add("123 Main, Los Angeles, jm-work@example.com");
265        expectedLabels.add("1500 Second Ave, Hollywood");
266        expectedLabels.add("Fort Worth, Texas");
267
268        List<AutofillProfile> profiles = mHelper.getProfiles();
269        assertEquals(expectedLabels.size(), profiles.size());
270        for (int i = 0; i < profiles.size(); ++i) {
271            int idx = expectedLabels.indexOf(profiles.get(i).getLabel());
272            assertFalse(-1 == idx);
273            expectedLabels.remove(idx);
274        }
275    }
276}
277