Debian Codegraph @

closure-library/closure/goog/ui/checkbox_test.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.ui.CheckboxTest');
16 goog.setTestOnly('goog.ui.CheckboxTest');
17
18 goog.require('goog.a11y.aria');
19 goog.require('goog.a11y.aria.Role');
20 goog.require('goog.a11y.aria.State');
21 goog.require('goog.dom');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.dom.classlist');
24 goog.require('goog.events');
25 goog.require('goog.events.KeyCodes');
26 goog.require('goog.testing.events');
27 goog.require('goog.testing.jsunit');
28 goog.require('goog.ui.Checkbox');
29 goog.require('goog.ui.CheckboxRenderer');
30 goog.require('goog.ui.Component');
31 goog.require('goog.ui.ControlRenderer');
32 goog.require('goog.ui.decorate');
33
34 var checkbox;
35
36 function setUp() {
37 checkbox = new goog.ui.Checkbox();
38 }
39
40 function tearDown() {
41 checkbox.dispose();
42 }
43
44 function testClassNames() {
45 checkbox.createDom();
46
47 checkbox.setChecked(false);
48 assertSameElements(
49 'classnames of unchecked checkbox',
50 ['goog-checkbox', 'goog-checkbox-unchecked'],
51 goog.dom.classlist.get(checkbox.getElement()));
52
53 checkbox.setChecked(true);
54 assertSameElements(
55 'classnames of checked checkbox',
56 ['goog-checkbox', 'goog-checkbox-checked'],
57 goog.dom.classlist.get(checkbox.getElement()));
58
59 checkbox.setChecked(null);
60 assertSameElements(
61 'classnames of partially checked checkbox',
62 ['goog-checkbox', 'goog-checkbox-undetermined'],
63 goog.dom.classlist.get(checkbox.getElement()));
64
65 checkbox.setEnabled(false);
66 assertSameElements(
67 'classnames of partially checked disabled checkbox',
68 ['goog-checkbox', 'goog-checkbox-undetermined', 'goog-checkbox-disabled'],
69 goog.dom.classlist.get(checkbox.getElement()));
70 }
71
72 function testIsEnabled() {
73 assertTrue('enabled by default', checkbox.isEnabled());
74 checkbox.setEnabled(false);
75 assertFalse('has been disabled', checkbox.isEnabled());
76 }
77
78 function testCheckedState() {
79 assertTrue(
80 'unchecked by default', !checkbox.isChecked() && checkbox.isUnchecked() &&
81 !checkbox.isUndetermined());
82
83 checkbox.setChecked(true);
84 assertTrue(
85 'set to checked', checkbox.isChecked() && !checkbox.isUnchecked() &&
86 !checkbox.isUndetermined());
87
88 checkbox.setChecked(null);
89 assertTrue(
90 'set to partially checked', !checkbox.isChecked() &&
91 !checkbox.isUnchecked() && checkbox.isUndetermined());
92 }
93
94 function testToggle() {
95 checkbox.setChecked(null);
96 checkbox.toggle();
97 assertTrue('undetermined -> checked', checkbox.getChecked());
98 checkbox.toggle();
99 assertFalse('checked -> unchecked', checkbox.getChecked());
100 checkbox.toggle();
101 assertTrue('unchecked -> checked', checkbox.getChecked());
102 }
103
104 function testEvents() {
105 checkbox.render();
106
107 var events = [];
108 goog.events.listen(
109 checkbox,
110 [
111 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK,
112 goog.ui.Component.EventType.UNCHECK, goog.ui.Component.EventType.CHANGE
113 ],
114 function(e) { events.push(e.type); });
115
116 checkbox.setEnabled(false);
117 goog.testing.events.fireClickSequence(checkbox.getElement());
118 assertArrayEquals('disabled => no events', [], events);
119 assertFalse('checked state did not change', checkbox.getChecked());
120 events = [];
121
122 checkbox.setEnabled(true);
123 goog.testing.events.fireClickSequence(checkbox.getElement());
124 assertArrayEquals(
125 'ACTION+CHECK+CHANGE fired',
126 [
127 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK,
128 goog.ui.Component.EventType.CHANGE
129 ],
130 events);
131 assertTrue('checkbox became checked', checkbox.getChecked());
132 events = [];
133
134 goog.testing.events.fireClickSequence(checkbox.getElement());
135 assertArrayEquals(
136 'ACTION+UNCHECK+CHANGE fired',
137 [
138 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.UNCHECK,
139 goog.ui.Component.EventType.CHANGE
140 ],
141 events);
142 assertFalse('checkbox became unchecked', checkbox.getChecked());
143 events = [];
144
145 goog.events.listen(checkbox, goog.ui.Component.EventType.CHECK, function(e) {
146 e.preventDefault();
147 });
148 goog.testing.events.fireClickSequence(checkbox.getElement());
149 assertArrayEquals(
150 'ACTION+CHECK fired',
151 [goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK],
152 events);
153 assertFalse('toggling has been prevented', checkbox.getChecked());
154 }
155
156 function testCheckboxAriaLabelledby() {
157 var label = goog.dom.createElement(goog.dom.TagName.DIV);
158 var label2 = goog.dom.createElement(
159 goog.dom.TagName.DIV, {id: checkbox.makeId('foo')});
160 document.body.appendChild(label);
161 document.body.appendChild(label2);
162 try {
163 checkbox.setChecked(false);
164 checkbox.setLabel(label);
165 checkbox.render(label);
166 assertNotNull(checkbox.getElement());
167 assertEquals(
168 label.id, goog.a11y.aria.getState(
169 checkbox.getElement(), goog.a11y.aria.State.LABELLEDBY));
170
171 checkbox.setLabel(label2);
172 assertEquals(
173 label2.id, goog.a11y.aria.getState(
174 checkbox.getElement(), goog.a11y.aria.State.LABELLEDBY));
175 } finally {
176 document.body.removeChild(label);
177 document.body.removeChild(label2);
178 }
179 }
180
181 function testLabel() {
182 var label = goog.dom.createElement(goog.dom.TagName.DIV);
183 document.body.appendChild(label);
184 try {
185 checkbox.setChecked(false);
186 checkbox.setLabel(label);
187 checkbox.render(label);
188
189 // Clicking on label toggles checkbox.
190 goog.testing.events.fireClickSequence(label);
191 assertTrue(
192 'checkbox toggled if the label is clicked', checkbox.getChecked());
193 goog.testing.events.fireClickSequence(checkbox.getElement());
194 assertFalse('checkbox toggled if it is clicked', checkbox.getChecked());
195
196 // Test that mouse events on the label have the correct effect on the
197 // checkbox state when it is enabled.
198 checkbox.setEnabled(true);
199 goog.testing.events.fireMouseOverEvent(label);
200 assertTrue(checkbox.hasState(goog.ui.Component.State.HOVER));
201 assertContains(
202 'checkbox gets hover state on mouse over', 'goog-checkbox-hover',
203 goog.dom.classlist.get(checkbox.getElement()));
204 goog.testing.events.fireMouseDownEvent(label);
205 assertTrue(checkbox.hasState(goog.ui.Component.State.ACTIVE));
206 assertContains(
207 'checkbox gets active state on label mousedown', 'goog-checkbox-active',
208 goog.dom.classlist.get(checkbox.getElement()));
209 goog.testing.events.fireMouseOutEvent(checkbox.getElement());
210 assertFalse(checkbox.hasState(goog.ui.Component.State.HOVER));
211 assertNotContains(
212 'checkbox does not have hover state after mouse out',
213 'goog-checkbox-hover', goog.dom.classlist.get(checkbox.getElement()));
214 assertFalse(checkbox.hasState(goog.ui.Component.State.ACTIVE));
215 assertNotContains(
216 'checkbox does not have active state after mouse out',
217 'goog-checkbox-active', goog.dom.classlist.get(checkbox.getElement()));
218
219 // Test label mouse events on disabled checkbox.
220 checkbox.setEnabled(false);
221 goog.testing.events.fireMouseOverEvent(label);
222 assertFalse(checkbox.hasState(goog.ui.Component.State.HOVER));
223 assertNotContains(
224 'disabled checkbox does not get hover state on mouseover',
225 'goog-checkbox-hover', goog.dom.classlist.get(checkbox.getElement()));
226 goog.testing.events.fireMouseDownEvent(label);
227 assertFalse(checkbox.hasState(goog.ui.Component.State.ACTIVE));
228 assertNotContains(
229 'disabled checkbox does not get active state mousedown',
230 'goog-checkbox-active', goog.dom.classlist.get(checkbox.getElement()));
231 goog.testing.events.fireMouseOutEvent(checkbox.getElement());
232 assertFalse(checkbox.hasState(goog.ui.Component.State.ACTIVE));
233 assertNotContains(
234 'checkbox does not get stuck in hover state', 'goog-checkbox-hover',
235 goog.dom.classlist.get(checkbox.getElement()));
236
237 // Making the label null prevents it from affecting checkbox state.
238 checkbox.setEnabled(true);
239 checkbox.setLabel(null);
240 goog.testing.events.fireClickSequence(label);
241 assertFalse('label element deactivated', checkbox.getChecked());
242 goog.testing.events.fireClickSequence(checkbox.getElement());
243 assertTrue('checkbox still active', checkbox.getChecked());
244 } finally {
245 document.body.removeChild(label);
246 }
247 }
248
249 function testLabel_setAgain() {
250 var label = goog.dom.createElement(goog.dom.TagName.DIV);
251 document.body.appendChild(label);
252 try {
253 checkbox.setChecked(false);
254 checkbox.setLabel(label);
255 checkbox.render(label);
256
257 checkbox.getElement().focus();
258 checkbox.setLabel(label);
259 assertEquals(
260 'checkbox should not have lost focus', checkbox.getElement(),
261 document.activeElement);
262 } finally {
263 document.body.removeChild(label);
264 }
265 }
266
267 function testConstructor() {
268 assertEquals(
269 'state is unchecked', goog.ui.Checkbox.State.UNCHECKED,
270 checkbox.getChecked());
271
272 var testCheckboxWithState =
273 new goog.ui.Checkbox(goog.ui.Checkbox.State.UNDETERMINED);
274 assertNotNull('checkbox created with custom state', testCheckboxWithState);
275 assertEquals(
276 'checkbox state is undetermined', goog.ui.Checkbox.State.UNDETERMINED,
277 testCheckboxWithState.getChecked());
278 testCheckboxWithState.dispose();
279 }
280
281 function testCustomRenderer() {
282 var cssClass = 'my-custom-checkbox';
283 var renderer = goog.ui.ControlRenderer.getCustomRenderer(
284 goog.ui.CheckboxRenderer, cssClass);
285 var customCheckbox = new goog.ui.Checkbox(undefined, undefined, renderer);
286 customCheckbox.createDom();
287 assertElementsEquals(
288 ['my-custom-checkbox', 'my-custom-checkbox-unchecked'],
289 goog.dom.classlist.get(customCheckbox.getElement()));
290 customCheckbox.setChecked(true);
291 assertElementsEquals(
292 ['my-custom-checkbox', 'my-custom-checkbox-checked'],
293 goog.dom.classlist.get(customCheckbox.getElement()));
294 customCheckbox.setChecked(null);
295 assertElementsEquals(
296 ['my-custom-checkbox', 'my-custom-checkbox-undetermined'],
297 goog.dom.classlist.get(customCheckbox.getElement()));
298 customCheckbox.dispose();
299 }
300
301 function testGetAriaRole() {
302 checkbox.createDom();
303 assertNotNull(checkbox.getElement());
304 assertEquals(
305 "Checkbox's ARIA role should be 'checkbox'", goog.a11y.aria.Role.CHECKBOX,
306 goog.a11y.aria.getRole(checkbox.getElement()));
307 }
308
309 function testCreateDomUpdateAriaState() {
310 checkbox.createDom();
311 assertNotNull(checkbox.getElement());
312 assertEquals(
313 'Checkbox must have default false ARIA state aria-checked', 'false',
314 goog.a11y.aria.getState(
315 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
316
317 checkbox.setChecked(goog.ui.Checkbox.State.CHECKED);
318 assertEquals(
319 'Checkbox must have true ARIA state aria-checked', 'true',
320 goog.a11y.aria.getState(
321 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
322
323 checkbox.setChecked(goog.ui.Checkbox.State.UNCHECKED);
324 assertEquals(
325 'Checkbox must have false ARIA state aria-checked', 'false',
326 goog.a11y.aria.getState(
327 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
328
329 checkbox.setChecked(goog.ui.Checkbox.State.UNDETERMINED);
330 assertEquals(
331 'Checkbox must have mixed ARIA state aria-checked', 'mixed',
332 goog.a11y.aria.getState(
333 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
334 }
335
336 function testDecorateUpdateAriaState() {
337 var decorateSpan = goog.dom.getElement('decorate');
338 checkbox.decorate(decorateSpan);
339
340 assertEquals(
341 'Checkbox must have default false ARIA state aria-checked', 'false',
342 goog.a11y.aria.getState(
343 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
344
345 checkbox.setChecked(goog.ui.Checkbox.State.CHECKED);
346 assertEquals(
347 'Checkbox must have true ARIA state aria-checked', 'true',
348 goog.a11y.aria.getState(
349 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
350
351 checkbox.setChecked(goog.ui.Checkbox.State.UNCHECKED);
352 assertEquals(
353 'Checkbox must have false ARIA state aria-checked', 'false',
354 goog.a11y.aria.getState(
355 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
356
357 checkbox.setChecked(goog.ui.Checkbox.State.UNDETERMINED);
358 assertEquals(
359 'Checkbox must have mixed ARIA state aria-checked', 'mixed',
360 goog.a11y.aria.getState(
361 checkbox.getElement(), goog.a11y.aria.State.CHECKED));
362 }
363
364 function testSpaceKey() {
365 var normalSpan = goog.dom.getElement('normal');
366
367 checkbox.decorate(normalSpan);
368 assertEquals(
369 'default state is unchecked', goog.ui.Checkbox.State.UNCHECKED,
370 checkbox.getChecked());
371 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.SPACE);
372 assertEquals(
373 'SPACE toggles checkbox to be checked', goog.ui.Checkbox.State.CHECKED,
374 checkbox.getChecked());
375 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.SPACE);
376 assertEquals(
377 'another SPACE toggles checkbox to be unchecked',
378 goog.ui.Checkbox.State.UNCHECKED, checkbox.getChecked());
379
380 // Enter for example doesn't work
381 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.ENTER);
382 assertEquals(
383 'Enter leaves checkbox unchecked', goog.ui.Checkbox.State.UNCHECKED,
384 checkbox.getChecked());
385 }
386
387 function testSpaceKeyFiresEvents() {
388 var normalSpan = goog.dom.getElement('normal');
389
390 checkbox.decorate(normalSpan);
391 var events = [];
392 goog.events.listen(
393 checkbox,
394 [
395 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK,
396 goog.ui.Component.EventType.UNCHECK, goog.ui.Component.EventType.CHANGE
397 ],
398 function(e) { events.push(e.type); });
399
400 assertEquals(
401 'Unexpected default state.', goog.ui.Checkbox.State.UNCHECKED,
402 checkbox.getChecked());
403 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.SPACE);
404 assertArrayEquals(
405 'Unexpected events fired when checking with spacebar.',
406 [
407 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK,
408 goog.ui.Component.EventType.CHANGE
409 ],
410 events);
411 assertEquals(
412 'Unexpected state after checking.', goog.ui.Checkbox.State.CHECKED,
413 checkbox.getChecked());
414
415 events = [];
416 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.SPACE);
417 assertArrayEquals(
418 'Unexpected events fired when unchecking with spacebar.',
419 [
420 goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.UNCHECK,
421 goog.ui.Component.EventType.CHANGE
422 ],
423 events);
424 assertEquals(
425 'Unexpected state after unchecking.', goog.ui.Checkbox.State.UNCHECKED,
426 checkbox.getChecked());
427
428 events = [];
429 goog.events.listenOnce(
430 checkbox, goog.ui.Component.EventType.CHECK,
431 function(e) { e.preventDefault(); });
432 goog.testing.events.fireKeySequence(normalSpan, goog.events.KeyCodes.SPACE);
433 assertArrayEquals(
434 'Unexpected events fired when checking with spacebar and ' +
435 'the check event is cancelled.',
436 [goog.ui.Component.EventType.ACTION, goog.ui.Component.EventType.CHECK],
437 events);
438 assertEquals(
439 'Unexpected state after check event is cancelled.',
440 goog.ui.Checkbox.State.UNCHECKED, checkbox.getChecked());
441 }
442
443 function testDecorate() {
444 var normalSpan = goog.dom.getElement('normal');
445 var checkedSpan = goog.dom.getElement('checked');
446 var uncheckedSpan = goog.dom.getElement('unchecked');
447 var undeterminedSpan = goog.dom.getElement('undetermined');
448 var disabledSpan = goog.dom.getElement('disabled');
449
450 validateCheckBox(normalSpan, goog.ui.Checkbox.State.UNCHECKED);
451 validateCheckBox(checkedSpan, goog.ui.Checkbox.State.CHECKED);
452 validateCheckBox(uncheckedSpan, goog.ui.Checkbox.State.UNCHECKED);
453 validateCheckBox(undeterminedSpan, goog.ui.Checkbox.State.UNDETERMINED);
454 validateCheckBox(disabledSpan, goog.ui.Checkbox.State.UNCHECKED, true);
455 }
456
457 function validateCheckBox(span, state, opt_disabled) {
458 var testCheckbox = goog.ui.decorate(span);
459 assertNotNull('checkbox created', testCheckbox);
460 assertEquals(
461 'decorate was successful', goog.ui.Checkbox, testCheckbox.constructor);
462 assertEquals(
463 'checkbox state should be: ' + state, state, testCheckbox.getChecked());
464 assertEquals(
465 'checkbox is ' + (!opt_disabled ? 'enabled' : 'disabled'), !opt_disabled,
466 testCheckbox.isEnabled());
467 testCheckbox.dispose();
468 }
469
470 function testSetAriaLabel() {
471 assertNull(
472 'Checkbox must not have aria label by default', checkbox.getAriaLabel());
473 checkbox.setAriaLabel('Checkbox 1');
474 checkbox.render();
475 var el = checkbox.getElementStrict();
476 assertEquals(
477 'Checkbox element must have expected aria-label', 'Checkbox 1',
478 el.getAttribute('aria-label'));
479 assertEquals(
480 'Checkbox element must have expected aria-role', 'checkbox',
481 el.getAttribute('role'));
482 checkbox.setAriaLabel('Checkbox 2');
483 assertEquals(
484 'Checkbox element must have updated aria-label', 'Checkbox 2',
485 el.getAttribute('aria-label'));
486 assertEquals(
487 'Checkbox element must have expected aria-role', 'checkbox',
488 el.getAttribute('role'));
489 }