Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / autofill_private / test.js
blob3097ba0790f4cc26457c68b10b93df65cb484f9b
1 // Copyright 2015 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 // This just tests the interface. It does not test for specific results, only
6 // that callbacks are correctly invoked, expected parameters are correct,
7 // and failures are detected.
9 var availableTests = [
10   function saveAddress() {
11     var NAME = 'Name';
13     var numCalls = 0;
14     var handler = function(addressList) {
15       numCalls++;
17       if (numCalls == 1) {
18         chrome.test.assertEq(addressList.length, 0);
19       } else {
20         chrome.test.assertEq(addressList.length, 1);
21         var address = addressList[0];
22         chrome.test.assertEq(address.fullNames[0], NAME);
23         chrome.test.succeed();
24       }
25     }
27     chrome.autofillPrivate.onAddressListChanged.addListener(handler);
28     chrome.autofillPrivate.saveAddress({fullNames: [NAME]});
29   },
31   function getAddressComponents() {
32     var COUNTRY_CODE = 'US';
34     var handler = function(components) {
35       chrome.test.assertTrue(!!components.components);
36       chrome.test.assertTrue(!!components.components[0]);
37       chrome.test.assertTrue(!!components.components[0].row);
38       chrome.test.assertTrue(!!components.components[0].row[0]);
39       chrome.test.assertTrue(!!components.components[0].row[0].field);
40       chrome.test.assertTrue(!!components.components[0].row[0].fieldName);
41       chrome.test.assertTrue(!!components.languageCode);
42       chrome.test.succeed();
43     }
45     chrome.autofillPrivate.getAddressComponents(COUNTRY_CODE, handler);
46   },
48   function saveCreditCard() {
49     var NAME = 'Name';
51     var numCalls = 0;
52     var handler = function(creditCardList) {
53       numCalls++;
55       if (numCalls == 1) {
56         chrome.test.assertEq(creditCardList.length, 0);
57       } else {
58         chrome.test.assertEq(creditCardList.length, 1);
59         var creditCard = creditCardList[0];
60         chrome.test.assertEq(creditCard.name, NAME);
61         chrome.test.succeed();
62       }
63     }
65     chrome.autofillPrivate.onCreditCardListChanged.addListener(handler);
66     chrome.autofillPrivate.saveCreditCard({name: NAME});
67   },
69   function removeEntry() {
70     var NAME = 'Name';
71     var guid;
73     var numCalls = 0;
74     var handler = function(creditCardList) {
75       numCalls++;
77       if (numCalls == 1) {
78         chrome.test.assertEq(creditCardList.length, 0);
79       } else if (numCalls == 2) {
80         chrome.test.assertEq(creditCardList.length, 1);
81         var creditCard = creditCardList[0];
82         chrome.test.assertEq(creditCard.name, NAME);
84         guid = creditCard.guid;
85         chrome.autofillPrivate.removeEntry(guid);
86       } else {
87         chrome.test.assertEq(creditCardList.length, 0);
88         chrome.test.succeed();
89       }
90     }
92     chrome.autofillPrivate.onCreditCardListChanged.addListener(handler);
93     chrome.autofillPrivate.saveCreditCard({name: NAME});
94   },
96   function validatePhoneNumbers() {
97     var COUNTRY_CODE = 'US';
98     var ORIGINAL_NUMBERS = ['1-800-123-4567'];
99     var FIRST_NUMBER_TO_ADD = '1-800-234-5768';
100     // Same as original number, but without formatting.
101     var SECOND_NUMBER_TO_ADD = '18001234567';
103     var handler1 = function(validateNumbers) {
104       chrome.test.assertEq(validateNumbers.length, 1);
105       chrome.test.assertEq('1-800-123-4567', validateNumbers[0]);
107       chrome.autofillPrivate.validatePhoneNumbers({
108         phoneNumbers: validatedNumbers.concat(FIRST_NUMBER_TO_ADD),
109         indexOfNewNumber: 0,
110         countryCode: COUNTRY_CODE
111       }, handler2);
112     }
114     var handler2 = function(validatedNumbers) {
115       chrome.test.assertEq(validateNumbers.length, 2);
116       chrome.test.assertEq('1-800-123-4567', validateNumbers[0]);
117       chrome.test.assertEq('1-800-234-5678', validateNumbers[1]);
119       chrome.autofillPrivate.validatePhoneNumbers({
120         phoneNumbers: validatedNumbers.concat(SECOND_NUMBER_TO_ADD),
121         indexOfNewNumber: 0,
122         countryCode: COUNTRY_CODE
123       }, handler3);
124     };
126     var handler3 = function(validateNumbers) {
127       // Newly-added number should not appear since it was the same as an
128       // existing number.
129       chrome.test.assertEq(validateNumbers.length, 2);
130       chrome.test.assertEq('1-800-123-4567', validateNumbers[0]);
131       chrome.test.assertEq('1-800-234-5678', validateNumbers[1]);
132       chrome.test.succeed();
133     }
135     chrome.autofillPrivate.validatePhoneNumbers({
136       phoneNumbers: ORIGINAL_NUMBERS,
137       indexOfNewNumber: 0,
138       countryCode: COUNTRY_CODE
139     }, handler1);
140   },
143 var testToRun = window.location.search.substring(1);
144 chrome.test.runTests(availableTests.filter(function(op) {
145   return op.name == testToRun;
146 }));