Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / options / autofill_options_browsertest.js
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  */
10 function 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 {int} The size of the list.
19  */
20 function 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  */
30 function AutofillOptionsWebUITest() {}
31
32 AutofillOptionsWebUITest.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.
42 TEST_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  */
51 function AutofillEditAddressWebUITest() {}
52
53 AutofillEditAddressWebUITest.prototype = {
54   __proto__: testing.Test.prototype,
55
56   /**
57    * Browse to autofill edit address overlay.
58    */
59   browsePreload: 'chrome://settings-frame/autofillEditAddress',
60
61   /** @override  */
62   isAsync: true,
63 };
64
65 TEST_F('AutofillEditAddressWebUITest',
66        'testAutofillPhoneValueListDoneValidating',
67        function() {
68   assertEquals(this.browsePreload, document.location.href);
69
70   var phoneList = getField('phone');
71   expectEquals(0, phoneList.validationRequests_);
72   phoneList.doneValidating().then(function() {
73     phoneList.focus();
74     var input = phoneList.querySelector('input');
75     input.focus();
76     document.execCommand('insertText', false, '111-222-333');
77     assertEquals('111-222-333', input.value);
78     input.blur();
79     phoneList.doneValidating().then(function() {
80       testDone();
81     });
82   });
83 });
84
85 TEST_F('AutofillEditAddressWebUITest',
86        'testInitialFormLayout',
87        function() {
88   assertEquals(this.browsePreload, document.location.href);
89
90   assertEquals(getField('country').value, '');
91   assertEquals(0, getListSize(getField('phone')));
92   assertEquals(0, getListSize(getField('email')));
93   assertEquals(0, getListSize(getField('fullName')));
94   assertEquals('', getField('city').value);
95
96   testDone();
97 });
98
99 TEST_F('AutofillEditAddressWebUITest',
100        'testLoadAddress',
101        function() {
102   assertEquals(this.browsePreload, document.location.href);
103
104   var testAddress = {
105     guid: 'GUID Value',
106     fullName: ['Full Name 1', 'Full Name 2'],
107     companyName: 'Company Name Value',
108     addrLines: 'First Line Value\nSecond Line Value',
109     dependentLocality: 'Dependent Locality Value',
110     city: 'City Value',
111     state: 'State Value',
112     postalCode: 'Postal Code Value',
113     sortingCode: 'Sorting Code Value',
114     country: 'CH',
115     phone: ['123', '456'],
116     email: ['a@b.c', 'x@y.z'],
117     languageCode: 'de',
118     components: [[
119        {field: 'postalCode', length: 'short'},
120        {field: 'sortingCode', length: 'short'},
121        {field: 'dependentLocality', length: 'short'},
122        {field: 'city', length: 'short'},
123        {field: 'state', length: 'short'},
124        {field: 'addrLines', length: 'long'},
125        {field: 'companyName', length: 'long'},
126        {field: 'country', length: 'long'},
127        {field: 'fullName', length: 'long', placeholder: 'Add name'}
128     ]]
129   };
130   AutofillEditAddressOverlay.loadAddress(testAddress);
131
132   assertEquals(testAddress.guid, AutofillEditAddressOverlay.getInstance().guid);
133   assertEquals(testAddress.languageCode,
134                AutofillEditAddressOverlay.getInstance().languageCode);
135
136   var lists = ['fullName', 'email', 'phone'];
137   for (var i in lists) {
138     var field = getField(lists[i]);
139     assertEquals(testAddress[lists[i]].length, getListSize(field));
140     assertTrue(field.getAttribute('placeholder').length > 0);
141     assertTrue(field instanceof cr.ui.List);
142   }
143
144   var inputs = ['companyName', 'dependentLocality', 'city', 'state',
145                 'postalCode', 'sortingCode'];
146   for (var i in inputs) {
147     var field = getField(inputs[i]);
148     assertEquals(testAddress[inputs[i]], field.value);
149     assertTrue(field instanceof HTMLInputElement);
150   }
151
152   var addrLines = getField('addrLines');
153   assertEquals(testAddress.addrLines, addrLines.value);
154   assertTrue(addrLines instanceof HTMLTextAreaElement);
155
156   var country = getField('country');
157   assertEquals(testAddress.country, country.value);
158   assertTrue(country instanceof HTMLSelectElement);
159
160   testDone();
161 });
162
163 TEST_F('AutofillEditAddressWebUITest',
164        'testLoadAddressComponents',
165        function() {
166   assertEquals(this.browsePreload, document.location.href);
167
168   var testInput = {
169     languageCode: 'fr',
170     components: [[{field: 'city'}],
171                  [{field: 'state'}]]
172   };
173   AutofillEditAddressOverlay.loadAddressComponents(testInput);
174
175   assertEquals('fr', AutofillEditAddressOverlay.getInstance().languageCode);
176   expectEquals(2, $('autofill-edit-address-fields').children.length);
177
178   testDone();
179 });