Debian Codegraph @

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

raw
1 // Copyright 2008 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.ButtonTest');
16 goog.setTestOnly('goog.ui.ButtonTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.classlist');
20 goog.require('goog.events');
21 goog.require('goog.events.Event');
22 goog.require('goog.events.EventType');
23 goog.require('goog.events.KeyCodes');
24 goog.require('goog.events.KeyHandler');
25 goog.require('goog.testing.events');
26 goog.require('goog.testing.jsunit');
27 goog.require('goog.ui.Button');
28 goog.require('goog.ui.ButtonRenderer');
29 goog.require('goog.ui.ButtonSide');
30 goog.require('goog.ui.Component');
31 goog.require('goog.ui.NativeButtonRenderer');
32
33 var sandbox;
34 var button;
35 var clonedButtonDom;
36 var demoButtonElement;
37
38 function setUp() {
39 sandbox = goog.dom.getElement('sandbox');
40 button = new goog.ui.Button();
41 demoButtonElement = goog.dom.getElement('demoButton');
42 clonedButtonDom = demoButtonElement.cloneNode(true);
43 }
44
45 function tearDown() {
46 button.dispose();
47 demoButtonElement.parentNode.replaceChild(clonedButtonDom, demoButtonElement);
48 goog.dom.removeChildren(sandbox);
49 }
50
51 function testConstructor() {
52 assertNotNull('Button must not be null', button);
53 assertEquals(
54 'Renderer must default to expected value',
55 goog.ui.NativeButtonRenderer.getInstance(), button.getRenderer());
56
57 var fakeDomHelper = {};
58 var testButton = new goog.ui.Button(
59 'Hello', goog.ui.ButtonRenderer.getInstance(), fakeDomHelper);
60 assertEquals(
61 'Content must have expected value', 'Hello', testButton.getContent());
62 assertEquals(
63 'Renderer must have expected value', goog.ui.ButtonRenderer.getInstance(),
64 testButton.getRenderer());
65 assertEquals(
66 'DOM helper must have expected value', fakeDomHelper,
67 testButton.getDomHelper());
68 testButton.dispose();
69 }
70
71 function testGetSetValue() {
72 assertUndefined(
73 'Button\'s value must default to undefined', button.getValue());
74 button.setValue(17);
75 assertEquals('Button must have expected value', 17, button.getValue());
76 button.render(sandbox);
77 assertEquals(
78 'Button element must have expected value', '17',
79 button.getElement().value);
80 button.setValue('foo');
81 assertEquals(
82 'Button element must have updated value', 'foo',
83 button.getElement().value);
84 button.setValueInternal('bar');
85 assertEquals('Button must have new internal value', 'bar', button.getValue());
86 assertEquals(
87 'Button element must be unchanged', 'foo', button.getElement().value);
88 }
89
90 function testGetSetTooltip() {
91 assertUndefined(
92 'Button\'s tooltip must default to undefined', button.getTooltip());
93 button.setTooltip('Hello');
94 assertEquals(
95 'Button must have expected tooltip', 'Hello', button.getTooltip());
96 button.render(sandbox);
97 assertEquals(
98 'Button element must have expected title', 'Hello',
99 button.getElement().title);
100 button.setTooltip('Goodbye');
101 assertEquals(
102 'Button element must have updated title', 'Goodbye',
103 button.getElement().title);
104 button.setTooltipInternal('World');
105 assertEquals(
106 'Button must have new internal tooltip', 'World', button.getTooltip());
107 assertEquals(
108 'Button element must be unchanged', 'Goodbye', button.getElement().title);
109 }
110
111 function testSetCollapsed() {
112 assertNull(
113 'Button must not have any collapsed styling by default',
114 button.getExtraClassNames());
115 button.setCollapsed(goog.ui.ButtonSide.START);
116 assertSameElements(
117 'Button must have the start side collapsed',
118 ['goog-button-collapse-left'], button.getExtraClassNames());
119 button.render(sandbox);
120 assertSameElements(
121 'Button element must have the start side collapsed',
122 ['goog-button', 'goog-button-collapse-left'],
123 goog.dom.classlist.get(button.getElement()));
124 button.setCollapsed(goog.ui.ButtonSide.BOTH);
125 assertSameElements(
126 'Button must have both sides collapsed',
127 ['goog-button-collapse-left', 'goog-button-collapse-right'],
128 button.getExtraClassNames());
129 assertSameElements(
130 'Button element must have both sides collapsed',
131 [
132 'goog-button', 'goog-button-collapse-left', 'goog-button-collapse-right'
133 ],
134 goog.dom.classlist.get(button.getElement()));
135 }
136
137 function testDispose() {
138 assertFalse('Button must not have been disposed of', button.isDisposed());
139 button.render(sandbox);
140 button.setValue('foo');
141 button.setTooltip('bar');
142 button.dispose();
143 assertTrue('Button must have been disposed of', button.isDisposed());
144 assertUndefined('Button\'s value must have been deleted', button.getValue());
145 assertUndefined(
146 'Button\'s tooltip must have been deleted', button.getTooltip());
147 }
148
149 function testBasicButtonBehavior() {
150 var dispatchedActionCount = 0;
151 var handleAction = function() { dispatchedActionCount++; };
152 goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
153
154 button.decorate(demoButtonElement);
155 goog.testing.events.fireClickSequence(demoButtonElement);
156 assertEquals(
157 'Button must have dispatched ACTION on click', 1, dispatchedActionCount);
158
159 dispatchedActionCount = 0;
160 var e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
161 e.keyCode = goog.events.KeyCodes.ENTER;
162 button.handleKeyEvent(e);
163 assertEquals(
164 'Enabled button must have dispatched ACTION on Enter key', 1,
165 dispatchedActionCount);
166
167 dispatchedActionCount = 0;
168 e = new goog.events.Event(goog.events.EventType.KEYUP, button);
169 e.keyCode = goog.events.KeyCodes.SPACE;
170 button.handleKeyEvent(e);
171 assertEquals(
172 'Enabled button must have dispatched ACTION on Space key', 1,
173 dispatchedActionCount);
174
175 goog.events.unlisten(
176 button, goog.ui.Component.EventType.ACTION, handleAction);
177 }
178
179 function testDisabledButtonBehavior() {
180 var dispatchedActionCount = 0;
181 var handleAction = function() { dispatchedActionCount++; };
182 goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
183
184 button.setEnabled(false);
185
186 dispatchedActionCount = 0;
187 button.handleKeyEvent({keyCode: goog.events.KeyCodes.ENTER});
188 assertEquals(
189 'Disabled button must not dispatch ACTION on Enter key', 0,
190 dispatchedActionCount);
191
192 dispatchedActionCount = 0;
193 button.handleKeyEvent(
194 {keyCode: goog.events.KeyCodes.SPACE, type: goog.events.EventType.KEYUP});
195 assertEquals(
196 'Disabled button must not have dispatched ACTION on Space', 0,
197 dispatchedActionCount);
198
199 goog.events.unlisten(
200 button, goog.ui.Component.EventType.ACTION, handleAction);
201 }
202
203 function testSpaceFireActionOnKeyUp() {
204 var dispatchedActionCount = 0;
205 var handleAction = function() { dispatchedActionCount++; };
206 goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
207
208 dispatchedActionCount = 0;
209 e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
210 e.keyCode = goog.events.KeyCodes.SPACE;
211 button.handleKeyEvent(e);
212 assertEquals(
213 'Button must not have dispatched ACTION on Space keypress', 0,
214 dispatchedActionCount);
215 assertEquals(
216 'The default action (scrolling) must have been prevented ' +
217 'for Space keypress',
218 false, e.returnValue_);
219
220
221 dispatchedActionCount = 0;
222 e = new goog.events.Event(goog.events.EventType.KEYUP, button);
223 e.keyCode = goog.events.KeyCodes.SPACE;
224 button.handleKeyEvent(e);
225 assertEquals(
226 'Button must have dispatched ACTION on Space keyup', 1,
227 dispatchedActionCount);
228
229 goog.events.unlisten(
230 button, goog.ui.Component.EventType.ACTION, handleAction);
231 }
232
233 function testEnterFireActionOnKeyPress() {
234 var dispatchedActionCount = 0;
235 var handleAction = function() { dispatchedActionCount++; };
236 goog.events.listen(button, goog.ui.Component.EventType.ACTION, handleAction);
237
238 dispatchedActionCount = 0;
239 e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
240 e.keyCode = goog.events.KeyCodes.ENTER;
241 button.handleKeyEvent(e);
242 assertEquals(
243 'Button must have dispatched ACTION on Enter keypress', 1,
244 dispatchedActionCount);
245
246 dispatchedActionCount = 0;
247 e = new goog.events.Event(goog.events.EventType.KEYUP, button);
248 e.keyCode = goog.events.KeyCodes.ENTER;
249 button.handleKeyEvent(e);
250 assertEquals(
251 'Button must not have dispatched ACTION on Enter keyup', 0,
252 dispatchedActionCount);
253
254 goog.events.unlisten(
255 button, goog.ui.Component.EventType.ACTION, handleAction);
256 }
257
258 function testSetAriaLabel() {
259 assertNull(
260 'Button must not have aria label by default', button.getAriaLabel());
261 button.setAriaLabel('Button 1');
262 button.render();
263 assertEquals(
264 'Button element must have expected aria-label', 'Button 1',
265 button.getElement().getAttribute('aria-label'));
266 button.setAriaLabel('Button 2');
267 assertEquals(
268 'Button element must have updated aria-label', 'Button 2',
269 button.getElement().getAttribute('aria-label'));
270 }
271
272 function testSetAriaLabel_decorate() {
273 assertNull(
274 'Button must not have aria label by default', button.getAriaLabel());
275 button.setAriaLabel('Button 1');
276 button.decorate(demoButtonElement);
277 var el = button.getElementStrict();
278 assertEquals(
279 'Button element must have expected aria-label', 'Button 1',
280 el.getAttribute('aria-label'));
281 assertEquals(
282 'Button element must have expected aria-role', 'button',
283 el.getAttribute('role'));
284 button.setAriaLabel('Button 2');
285 assertEquals(
286 'Button element must have updated aria-label', 'Button 2',
287 el.getAttribute('aria-label'));
288 assertEquals(
289 'Button element must have expected aria-role', 'button',
290 el.getAttribute('role'));
291 }