1 description("This test performs a check that :valid/:invalid CSS psudo selectors are lively applied.");
3 // Setup for static elements.
4 var form = document.createElement('form');
5 document.body.appendChild(form);
6 var nonForm = document.createElement('div');
7 document.body.appendChild(nonForm);
9 function mouseDownOnSelect(selId, index, modifier) {
10 var sl = document.getElementById(selId);
11 var itemHeight = Math.floor(sl.offsetHeight / sl.size);
13 var y = border + index * itemHeight;
16 if (window.eventSender) {
17 eventSender.mouseMoveTo(sl.offsetLeft + border, sl.offsetTop + y - window.pageYOffset);
18 eventSender.mouseDown(0, [modifier]);
19 eventSender.mouseUp(0, [modifier]);
23 function makeInvalid() {
24 var select = document.createElement('select');
26 select.required = true;
28 form.appendChild(select);
32 function appendOption(value, select) {
33 var option = document.createElement('option');
35 option.innerText = value;
40 function insertOptionBefore(value, select, before) {
41 var option = document.createElement('option');
43 option.innerText = value;
44 select.add(option, before);
48 function removeOption(option, select) {
49 select.remove(option);
53 function backgroundOf(el) {
54 if (typeof(el) == 'string')
55 el = document.getElementById(el);
56 return document.defaultView.getComputedStyle(el, null).getPropertyValue('background-color');
59 var elBackground = 'backgroundOf(el)';
60 var invalidColor = 'rgb(255, 0, 0)';
61 var normalColor = 'rgb(255, 255, 255)';
62 var disabledColor = 'rgb(0, 0, 0)';
63 var transparentColor = 'rgba(0, 0, 0, 0)';
64 var validColor = 'rgb(0, 0, 255)';
66 // --------------------------------
67 // willValidate change
68 // --------------------------------
69 var el = makeInvalid();
70 var o1 = appendOption('', el);
71 var o2 = appendOption('X', el);
73 // Confirm this element is invalid.
74 debug('Check the initial state:');
75 shouldBe(elBackground, 'invalidColor');
77 debug('Change name:');
79 shouldBe(elBackground, 'invalidColor');
81 shouldBe(elBackground, 'invalidColor');
83 debug('Change disabled:');
85 o1 = appendOption('', el);
86 o2 = appendOption('X', el);
89 shouldBe(elBackground, 'disabledColor');
91 shouldBe(elBackground, 'invalidColor');
93 debug('Inside/outside of a form:');
95 o1 = appendOption('', el);
96 o2 = appendOption('X', el);
98 nonForm.appendChild(el);
99 shouldBe(elBackground, 'invalidColor');
100 form.appendChild(el);
101 shouldBe(elBackground, 'invalidColor');
103 // --------------------------------
105 // --------------------------------
107 debug('Change the values of select elements without explicit initializing values by clicking:');
108 form.innerHTML = '<select id="select-multiple" multiple required size="4">' +
109 ' <option id="multiple-empty">empty</option>' +
110 ' <option id="multiple-another">another</option>' +
112 '<select id="select-size4" size="4" required>' +
113 ' <option id="size4-empty">empty</option>' +
114 ' <option id="size4-another">another</option>' +
116 mouseDownOnSelect('select-multiple', 0);
117 mouseDownOnSelect('select-size4', 0);
118 shouldBe('backgroundOf("select-multiple")', 'validColor');
119 shouldBe('backgroundOf("select-size4")', 'validColor');
121 debug('Change the value with a placeholder label option:');
123 o1 = appendOption('', el);
124 o2 = appendOption('X', el);
126 shouldBe(elBackground, 'validColor');
128 shouldBe(elBackground, 'invalidColor');
130 debug('Change the value without a placeholder label option:');
132 o1 = appendOption('X', el);
133 o2 = appendOption('', el);
135 shouldBe(elBackground, 'validColor');
137 shouldBe(elBackground, 'validColor');
139 debug('Insert/remove options:');
141 o1 = appendOption('', el);
142 o2 = appendOption('X', el);
144 shouldBe(elBackground, 'invalidColor');
145 o3 = insertOptionBefore('Y', el, el.firstChild);
146 shouldBe(elBackground, 'validColor');
147 removeOption(o3, el);
148 shouldBe(elBackground, 'invalidColor');
149 o3 = appendOption('Z', el);
151 shouldBe(elBackground, 'validColor');
153 shouldBe(elBackground, 'invalidColor');
155 debug('Set an attribute: multiple:');
157 o1 = appendOption('', el);
158 o2 = appendOption('X', el);
160 shouldBe(elBackground, 'invalidColor');
162 shouldBe(elBackground, 'validColor');
163 el.removeAttribute('multiple');
164 shouldBe(elBackground, 'invalidColor');
166 debug('Set an attribute: size:');
168 o1 = appendOption('', el);
169 o2 = appendOption('X', el);
171 shouldBe(elBackground, 'invalidColor');
173 shouldBe(elBackground, 'validColor');
174 el.removeAttribute('size');
175 shouldBe(elBackground, 'invalidColor');
179 o1 = appendOption('', el);
180 o2 = appendOption('X', el);
184 shouldBe(elBackground, 'invalidColor');
186 document.execCommand("SelectAll");
187 shouldBe(elBackground, 'validColor');
194 o1 = appendOption('', el);
195 o2 = appendOption('X', el);
197 shouldBe(elBackground, 'validColor');
199 shouldBe(elBackground, 'invalidColor');
201 // --------------------------------
202 // Constraints change
203 // --------------------------------
204 debug('Change required:');
206 o1 = appendOption('', el);
207 o2 = appendOption('X', el);
210 shouldBe(elBackground, 'validColor');
212 shouldBe(elBackground, 'invalidColor');