| 1 | // Copyright (c) 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.testshell.ChromiumTestShellTestBase; |
| 13 | |
| 14 | import java.util.concurrent.ExecutionException; |
| 15 | |
| 16 | /** |
| 17 | * Tests for Chrome on Android's usage of the PersonalDataManager API. |
| 18 | */ |
| 19 | public class PersonalDataManagerTest extends ChromiumTestShellTestBase { |
| 20 | |
| 21 | private AutofillTestHelper mHelper; |
| 22 | |
| 23 | @Override |
| 24 | public void setUp() throws Exception { |
| 25 | super.setUp(); |
| 26 | clearAppData(); |
| 27 | launchChromiumTestShellWithBlankPage(); |
| 28 | assertTrue(waitForActiveShellToBeDoneLoading()); |
| 29 | |
| 30 | mHelper = new AutofillTestHelper(); |
| 31 | } |
| 32 | |
| 33 | @SmallTest |
| 34 | @Feature({"Autofill"}) |
| 35 | public void testAddAndEditProfiles() throws InterruptedException, ExecutionException { |
| 36 | AutofillProfile profile = new AutofillProfile( |
| 37 | "" /* guid */, "https://www.example.com" /* origin */, |
| 38 | "John Smith", "Acme Inc.", "1 Main", "Apt A", "San Francisco", "CA", |
| 39 | "94102", "US", "4158889999", "john@acme.inc"); |
| 40 | String profileOneGUID = mHelper.setProfile(profile); |
| 41 | assertEquals(1, mHelper.getNumberOfProfiles()); |
| 42 | |
| 43 | AutofillProfile profile2 = new AutofillProfile( |
| 44 | "" /* guid */, "http://www.example.com" /* origin */, |
| 45 | "John Hackock", "Acme Inc.", "1 Main", "Apt A", "San Francisco", "CA", |
| 46 | "94102", "US", "4158889999", "john@acme.inc"); |
| 47 | String profileTwoGUID = mHelper.setProfile(profile2); |
| 48 | assertEquals(2, mHelper.getNumberOfProfiles()); |
| 49 | |
| 50 | profile.setGUID(profileOneGUID); |
| 51 | profile.setCountry("Canada"); |
| 52 | mHelper.setProfile(profile); |
| 53 | assertEquals("Should still have only two profile", 2, mHelper.getNumberOfProfiles()); |
| 54 | |
| 55 | AutofillProfile storedProfile = mHelper.getProfile(profileOneGUID); |
| 56 | assertEquals(profileOneGUID, storedProfile.getGUID()); |
| 57 | assertEquals("https://www.example.com", storedProfile.getOrigin()); |
| 58 | assertEquals("CA", storedProfile.getCountryCode()); |
| 59 | assertEquals("San Francisco", storedProfile.getCity()); |
| 60 | assertNotNull(mHelper.getProfile(profileTwoGUID)); |
| 61 | } |
| 62 | |
| 63 | @SmallTest |
| 64 | @Feature({"Autofill"}) |
| 65 | public void testAddAndDeleteProfile() throws InterruptedException, ExecutionException { |
| 66 | AutofillProfile profile = new AutofillProfile( |
| 67 | "" /* guid */, "Chrome settings" /* origin */, |
| 68 | "John Smith", "Acme Inc.", "1 Main", "Apt A", "San Francisco", "CA", |
| 69 | "94102", "US", "4158889999", "john@acme.inc"); |
| 70 | String profileOneGUID = mHelper.setProfile(profile); |
| 71 | assertEquals(1, mHelper.getNumberOfProfiles()); |
| 72 | |
| 73 | mHelper.deleteProfile(profileOneGUID); |
| 74 | assertEquals(0, mHelper.getNumberOfProfiles()); |
| 75 | } |
| 76 | |
| 77 | @SmallTest |
| 78 | @Feature({"Autofill"}) |
| 79 | public void testAddAndEditCreditCards() throws InterruptedException, ExecutionException { |
| 80 | CreditCard card = new CreditCard( |
| 81 | "" /* guid */, "https://www.example.com" /* origin */, |
| 82 | "Visa", "1234123412341234", "", "5", "2020"); |
| 83 | String cardOneGUID = mHelper.setCreditCard(card); |
| 84 | assertEquals(1, mHelper.getNumberOfCreditCards()); |
| 85 | |
| 86 | CreditCard card2 = new CreditCard( |
| 87 | "" /* guid */, "http://www.example.com" /* origin */, |
| 88 | "American Express", "1234123412341234", "", "8", "2020"); |
| 89 | String cardTwoGUID = mHelper.setCreditCard(card2); |
| 90 | assertEquals(2, mHelper.getNumberOfCreditCards()); |
| 91 | |
| 92 | card.setGUID(cardOneGUID); |
| 93 | card.setMonth("10"); |
| 94 | card.setNumber("5678567856785678"); |
| 95 | mHelper.setCreditCard(card); |
| 96 | assertEquals("Should still have only two cards", 2, mHelper.getNumberOfCreditCards()); |
| 97 | |
| 98 | CreditCard storedCard = mHelper.getCreditCard(cardOneGUID); |
| 99 | assertEquals(cardOneGUID, storedCard.getGUID()); |
| 100 | assertEquals("https://www.example.com", storedCard.getOrigin()); |
| 101 | assertEquals("Visa", storedCard.getName()); |
| 102 | assertEquals("10", storedCard.getMonth()); |
| 103 | assertEquals("5678567856785678", storedCard.getNumber()); |
| 104 | assertEquals("************5678", storedCard.getObfuscatedNumber()); |
| 105 | assertNotNull(mHelper.getCreditCard(cardTwoGUID)); |
| 106 | } |
| 107 | |
| 108 | @SmallTest |
| 109 | @Feature({"Autofill"}) |
| 110 | public void testAddAndDeleteCreditCard() throws InterruptedException, ExecutionException { |
| 111 | CreditCard card = new CreditCard( |
| 112 | "" /* guid */, "Chrome settings" /* origin */, |
| 113 | "Visa", "1234123412341234", "", "5", "2020"); |
| 114 | String cardOneGUID = mHelper.setCreditCard(card); |
| 115 | assertEquals(1, mHelper.getNumberOfCreditCards()); |
| 116 | |
| 117 | mHelper.deleteCreditCard(cardOneGUID); |
| 118 | assertEquals(0, mHelper.getNumberOfCreditCards()); |
| 119 | } |
| 120 | |
| 121 | @SmallTest |
| 122 | @Feature({"Autofill"}) |
| 123 | public void testRespectCountryCodes() throws InterruptedException, ExecutionException { |
| 124 | // The constructor should accept country names and ISO 3166-1-alpha-2 country codes. |
| 125 | // getCountryCode() should return a country code. |
| 126 | AutofillProfile profile1 = new AutofillProfile( |
| 127 | "" /* guid */, "https://www.example.com" /* origin */, |
| 128 | "John Smith", "Acme Inc.", "1 Main", "Apt A", "Montreal", "Quebec", |
| 129 | "H3B 2Y5", "Canada", "514-670-1234", "john@acme.inc"); |
| 130 | String profileGuid1 = mHelper.setProfile(profile1); |
| 131 | |
| 132 | AutofillProfile profile2 = new AutofillProfile( |
| 133 | "" /* guid */, "https://www.example.com" /* origin */, |
| 134 | "Greg Smith", "Ucme Inc.", "123 Bush", "Apt 125", "Montreal", "Quebec", |
| 135 | "H3B 2Y5", "CA", "514-670-4321", "greg@ucme.inc"); |
| 136 | String profileGuid2 = mHelper.setProfile(profile2); |
| 137 | |
| 138 | assertEquals(2, mHelper.getNumberOfProfiles()); |
| 139 | |
| 140 | AutofillProfile storedProfile1 = mHelper.getProfile(profileGuid1); |
| 141 | assertEquals("CA", storedProfile1.getCountryCode()); |
| 142 | |
| 143 | AutofillProfile storedProfile2 = mHelper.getProfile(profileGuid2); |
| 144 | assertEquals("CA", storedProfile2.getCountryCode()); |
| 145 | } |
| 146 | |
| 147 | } |