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.
6 * TestFixture for testing the formatting of settings pages.
7 * @extends {testing.Test}
10 function SettingsFormatWebUITest() {}
13 * Map of rule exemptions grouped by test.
16 SettingsFormatWebUITest.Filters = {
18 * Exemption for checkboxes that do not require an id or pref property.
19 * Input methods use inputMethodId instead of id for unique identification.
21 'pref': ['language-options-input-method-template',
22 'language-options-input-method-list']
26 * Collection of error messages.
29 SettingsFormatWebUITest.Messages = {
30 MISSING_CHECK_WRAPPER: 'Element $1 should be enclosed in <div class="$2">',
31 MISSING_ID_OR_PREF: 'Missing id or pref preoperty for checkbox $1.',
32 MISSING_RADIO_BUTTON_NAME: 'Radio button $1 is missing the name property',
33 MISSING_RADIO_BUTTON_VALUE: 'Radio button $1 is missing the value property',
36 SettingsFormatWebUITest.prototype = {
37 __proto__: testing.Test.prototype,
40 * Navigate to browser settings.
42 browsePreload: 'chrome://settings-frame/',
45 * List of errors generated during a test. Used instead of expect* functions
46 * to suppress verbosity. The implementation of errorsToMessage in the
47 * testing API generates a call stack for each error produced which greatly
48 * reduces readability.
49 * @type {Array<string>}
57 tearDown: function() {
58 assertTrue(this.errors.length == 0, '\n' + this.errors.join('\n'));
62 * Generates a failure message. During tear down of the test, the accumulation
63 * of pending messages triggers a test failure.
64 * @param {string} key Label of the message formatting string.
65 * @param {!Element} element The element that triggered the failure.
66 * @param {...string} args Additional arguments for formatting the message.
68 fail: function(key, element, args) {
69 var subs = [this.getLabel(element)].concat(
70 Array.prototype.slice.call(arguments, 2));
71 var message = SettingsFormatWebUITest.Messages[key].replace(
74 return subs[m[1] - 1] || '$' + m[1];
76 assertFalse(/\$\d/.test(message), 'found unreplaced subs');
77 this.errors.push(message);
81 * String for identifying a node within an error message.
82 * @param {!Element} element The target element to identify.
83 * @return {string} Name to facilitate tracking down the element.
85 getLabel: function(element) {
92 if (element.name && element.value)
93 return element.name + '-' + element.value;
95 return this.getLabel(element.parentNode);
100 * Checks if the node is exempt from following the formatting rule.
101 * @param {!Element} element The candidate element.
102 * @param {Array<string>} filters List of exemptions.
103 * @return {boolean} True if the element is exempt.
105 isExempt: function(element, filters) {
106 var target = this.getLabel(element);
107 for (var i = 0; i < filters.length; i++) {
108 if (filters[i] == target)
116 * Ensure that radio and checkbox buttons have consistent layout.
118 TEST_F('SettingsFormatWebUITest', 'RadioCheckboxStyleCheck', function() {
119 var settings = $('settings');
120 assertTrue(settings != null, 'Unable to access settings');
121 var query = 'input[type=checkbox], input[type=radio]';
122 var elements = document.querySelectorAll(query);
123 assertTrue(elements.length > 0);
124 for (var i = 0; i < elements.length; i++) {
125 var element = elements[i];
126 if (!findAncestorByClass(element, element.type))
127 this.fail('MISSING_CHECK_WRAPPER', element, element.type);
132 * Each checkbox requires an id or pref property.
134 TEST_F('SettingsFormatWebUITest', 'CheckboxIdOrPrefCheck', function() {
136 'input[type=checkbox]:not([pref]):not([id]):not(.spacer-checkbox)';
137 var elements = document.querySelectorAll(query);
138 for (var i = 0; i < elements.length; i++) {
139 var element = elements[i];
140 if (!this.isExempt(element, SettingsFormatWebUITest.Filters['pref']))
141 this.fail('MISSING_ID_OR_PREF', element);
146 * Each radio button requires name and value properties.
148 TEST_F('SettingsFormatWebUITest', 'RadioButtonNameValueCheck', function() {
149 var elements = document.querySelectorAll('input[type=radio]');
150 for (var i = 0; i < elements.length; i++) {
151 var element = elements[i];
153 this.fail('MISSING_RADIO_BUTTON_NAME', element);
155 if (!element.getAttribute('value'))
156 this.fail('MISSING_RADIO_BUTTON_VALUE', element);