1// Copyright (c) 2012 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/**
6 * Returns the HTML element for the |field|.
7 * @param {string} field The field name for the element.
8 * @return {HTMLElement} The HTML element.
9 */
10function getField(field) {
11  return document.querySelector(
12      '#autofill-edit-address-overlay [field=' + field + ']');
13}
14
15/**
16 * Returns the size of the |list|.
17 * @param {HTMLElement} list The list to check.
18 * @return {number} The size of the list.
19 */
20function getListSize(list) {
21  // Remove 1 for placeholder input field.
22  return list.items.length - 1;
23}
24
25/**
26 * TestFixture for autofill options WebUI testing.
27 * @extends {testing.Test}
28 * @constructor
29 */
30function AutofillOptionsWebUITest() {}
31
32AutofillOptionsWebUITest.prototype = {
33  __proto__: testing.Test.prototype,
34
35  /**
36   * Browse to autofill options.
37   */
38  browsePreload: 'chrome://settings-frame/autofill',
39};
40
41// Test opening the autofill options has correct location.
42TEST_F('AutofillOptionsWebUITest', 'testOpenAutofillOptions', function() {
43  assertEquals(this.browsePreload, document.location.href);
44});
45
46/**
47 * TestFixture for autofill edit address overlay WebUI testing.
48 * @extends {testing.Test}
49 * @constructor
50 */
51function AutofillEditAddressWebUITest() {}
52
53AutofillEditAddressWebUITest.prototype = {
54  __proto__: testing.Test.prototype,
55
56  /** @override  */
57  browsePreload: 'chrome://settings-frame/autofillEditAddress',
58};
59
60TEST_F('AutofillEditAddressWebUITest', 'testInitialFormLayout', function() {
61  assertEquals(this.browsePreload, document.location.href);
62
63  assertEquals(getField('country').value, '');
64  assertEquals(0, getListSize(getField('phone')));
65  assertEquals(0, getListSize(getField('email')));
66  assertEquals(0, getListSize(getField('fullName')));
67  assertEquals('', getField('city').value);
68
69  testDone();
70});
71
72TEST_F('AutofillEditAddressWebUITest', 'testLoadAddress', function() {
73  assertEquals(this.browsePreload, document.location.href);
74
75  var testAddress = {
76    guid: 'GUID Value',
77    fullName: ['Full Name 1', 'Full Name 2'],
78    companyName: 'Company Name Value',
79    addrLines: 'First Line Value\nSecond Line Value',
80    dependentLocality: 'Dependent Locality Value',
81    city: 'City Value',
82    state: 'State Value',
83    postalCode: 'Postal Code Value',
84    sortingCode: 'Sorting Code Value',
85    country: 'CH',
86    phone: ['123', '456'],
87    email: ['a@b.c', 'x@y.z'],
88    languageCode: 'de',
89    components: [[
90       {field: 'postalCode', length: 'short'},
91       {field: 'sortingCode', length: 'short'},
92       {field: 'dependentLocality', length: 'short'},
93       {field: 'city', length: 'short'},
94       {field: 'state', length: 'short'},
95       {field: 'addrLines', length: 'long'},
96       {field: 'companyName', length: 'long'},
97       {field: 'country', length: 'long'},
98       {field: 'fullName', length: 'long', placeholder: 'Add name'}
99    ]]
100  };
101  AutofillEditAddressOverlay.loadAddress(testAddress);
102
103  var overlay = AutofillEditAddressOverlay.getInstance();
104  assertEquals(testAddress.guid, overlay.guid_);
105  assertEquals(testAddress.languageCode, overlay.languageCode_);
106
107  var lists = ['fullName', 'email', 'phone'];
108  for (var i in lists) {
109    var field = getField(lists[i]);
110    assertEquals(testAddress[lists[i]].length, getListSize(field));
111    assertTrue(field.getAttribute('placeholder').length > 0);
112    assertTrue(field instanceof cr.ui.List);
113  }
114
115  var inputs = ['companyName', 'dependentLocality', 'city', 'state',
116                'postalCode', 'sortingCode'];
117  for (var i in inputs) {
118    var field = getField(inputs[i]);
119    assertEquals(testAddress[inputs[i]], field.value);
120    assertTrue(field instanceof HTMLInputElement);
121  }
122
123  var addrLines = getField('addrLines');
124  assertEquals(testAddress.addrLines, addrLines.value);
125  assertTrue(addrLines instanceof HTMLTextAreaElement);
126
127  var country = getField('country');
128  assertEquals(testAddress.country, country.value);
129  assertTrue(country instanceof HTMLSelectElement);
130});
131
132TEST_F('AutofillEditAddressWebUITest', 'testLoadAddressComponents', function() {
133  assertEquals(this.browsePreload, document.location.href);
134
135  var testInput = {
136    languageCode: 'fr',
137    components: [[{field: 'city'}],
138                 [{field: 'state'}]]
139  };
140  AutofillEditAddressOverlay.loadAddressComponents(testInput);
141
142  assertEquals('fr', AutofillEditAddressOverlay.getInstance().languageCode_);
143  expectEquals(2, $('autofill-edit-address-fields').children.length);
144});
145
146TEST_F('AutofillEditAddressWebUITest', 'testFieldValuesSaved', function() {
147  assertEquals(this.browsePreload, document.location.href);
148
149  AutofillEditAddressOverlay.loadAddressComponents({
150    languageCode: 'en',
151    components: [[{field: 'city'}]]
152  });
153  getField('city').value = 'New York';
154
155  AutofillEditAddressOverlay.loadAddressComponents({
156    languageCode: 'en',
157    components: [[{field: 'state'}]]
158  });
159  assertEquals(null, getField('city'));
160
161  AutofillEditAddressOverlay.loadAddressComponents({
162    languageCode: 'en',
163    components: [[{field: 'city'}]]
164  });
165  assertEquals('New York', getField('city').value);
166});
167
168/**
169 * Class to test the autofill edit address overlay asynchronously.
170 * @extends {testing.Test}
171 * @constructor
172 */
173function AutofillEditAddressAsyncWebUITest() {}
174
175AutofillEditAddressAsyncWebUITest.prototype = {
176  __proto__: testing.Test.prototype,
177
178  /** @override */
179  browsePreload: 'chrome://settings-frame/autofillEditAddress',
180
181  /** @override */
182  isAsync: true,
183};
184
185TEST_F('AutofillEditAddressAsyncWebUITest',
186       'testAutofillPhoneValueListDoneValidating',
187       function() {
188  assertEquals(this.browsePreload, document.location.href);
189
190  var phoneList = getField('phone');
191  expectEquals(0, phoneList.validationRequests_);
192  phoneList.doneValidating().then(function() {
193    phoneList.focus();
194    var input = phoneList.querySelector('input');
195    input.focus();
196    document.execCommand('insertText', false, '111-222-333');
197    assertEquals('111-222-333', input.value);
198    input.blur();
199    phoneList.doneValidating().then(testDone);
200  });
201});
202