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