Debian Codegraph @

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

raw
1 // Copyright 2010 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.TextareaTest');
16 goog.setTestOnly('goog.ui.TextareaTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.classlist');
20 goog.require('goog.events');
21 goog.require('goog.style');
22 goog.require('goog.testing.ExpectedFailures');
23 goog.require('goog.testing.events.EventObserver');
24 goog.require('goog.testing.jsunit');
25 goog.require('goog.ui.Textarea');
26 goog.require('goog.ui.TextareaRenderer');
27 goog.require('goog.userAgent');
28 goog.require('goog.userAgent.product');
29
30 var sandbox;
31 var textarea;
32 var demoTextareaElement;
33 var expectedFailures;
34
35 function setUp() {
36 sandbox = goog.dom.getElement('sandbox');
37 textarea = new goog.ui.Textarea();
38 demoTextareaElement = goog.dom.getElement('demo-textarea');
39 expectedFailures = new goog.testing.ExpectedFailures();
40 }
41
42 function tearDown() {
43 expectedFailures.handleTearDown();
44 textarea.dispose();
45 goog.dom.removeChildren(sandbox);
46 }
47
48
49 /**
50 * @return {boolean} Whether we're on Mac Safari 3.x.
51 */
52 function isMacSafari3() {
53 return goog.userAgent.WEBKIT && goog.userAgent.MAC &&
54 !goog.userAgent.isVersionOrHigher('527');
55 }
56
57
58 /**
59 * @return {boolean} Whether we're on Linux Firefox 3.6.3.
60 */
61 function isLinuxFirefox() {
62 return goog.userAgent.product.FIREFOX && goog.userAgent.LINUX;
63 }
64
65
66 /**
67 * @return {boolean} Whether we're on Firefox 3.0.
68 */
69 function isFirefox3() {
70 return goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9.1');
71 }
72
73 function testConstructor() {
74 assertNotNull('Textarea must not be null', textarea);
75 assertEquals(
76 'Renderer must default to expected value',
77 goog.ui.TextareaRenderer.getInstance(), textarea.getRenderer());
78
79 var fakeDomHelper = {'getDocument': function() { return true; }};
80 var testTextarea = new goog.ui.Textarea(
81 'Hello', goog.ui.TextareaRenderer.getInstance(), fakeDomHelper);
82 assertEquals(
83 'Content must have expected content', 'Hello', testTextarea.getContent());
84 assertEquals(
85 'Renderer must have expected value',
86 goog.ui.TextareaRenderer.getInstance(), testTextarea.getRenderer());
87 assertEquals(
88 'DOM helper must have expected value', fakeDomHelper,
89 testTextarea.getDomHelper());
90 testTextarea.dispose();
91 }
92
93 function testConstructorWithDecorator() {
94 var decoratedTextarea = new goog.ui.Textarea();
95 decoratedTextarea.decorate(demoTextareaElement);
96 assertEquals(
97 'Textarea should have current content after decoration', 'Foo',
98 decoratedTextarea.getContent());
99 var initialHeight = decoratedTextarea.getHeight_();
100 var initialOffsetHeight = decoratedTextarea.getElement().offsetHeight;
101 // focus() will trigger the grow/shrink flow.
102 decoratedTextarea.getElement().focus();
103 assertEquals(
104 'Height should not have changed without content change', initialHeight,
105 decoratedTextarea.getHeight_());
106 assertEquals(
107 'offsetHeight should not have changed without content ' +
108 'change',
109 initialOffsetHeight, decoratedTextarea.getElement().offsetHeight);
110 decoratedTextarea.dispose();
111 }
112
113 function testGetSetContent() {
114 textarea.render(sandbox);
115 assertEquals(
116 'Textarea\'s content must default to an empty string', '',
117 textarea.getContent());
118 textarea.setContent(17);
119 assertEquals(
120 'Textarea element must have expected content', '17',
121 textarea.getElement().value);
122 textarea.setContent('foo');
123 assertEquals(
124 'Textarea element must have updated content', 'foo',
125 textarea.getElement().value);
126 }
127
128 function testGetSetValue() {
129 textarea.render(sandbox);
130 assertEquals(
131 'Textarea\'s content must default to an empty string', '',
132 textarea.getValue());
133 textarea.setValue(17);
134 assertEquals(
135 'Textarea element must have expected content', '17', textarea.getValue());
136 textarea.setValue('17');
137 assertEquals(
138 'Textarea element must have expected content', '17', textarea.getValue());
139 }
140
141 function testBasicTextareaBehavior() {
142 var observer = new goog.testing.events.EventObserver();
143 goog.events.listen(textarea, goog.ui.Textarea.EventType.RESIZE, observer);
144 textarea.render(sandbox);
145 var el = textarea.getElement();
146 var heightBefore = textarea.getHeight_();
147 assertTrue(
148 'One resize event should be fired during render',
149 observer.getEvents().length == 1);
150 textarea.setContent(
151 'Lorem ipsum dolor sit amet, consectetuer ' +
152 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
153 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
154 'lorem, in fermentum purus ligula quis nunc.');
155 var heightAfter = textarea.getHeight_();
156 assertTrue(
157 'With this much content, height should have grown.',
158 heightAfter > heightBefore);
159 assertTrue(
160 'With a height change, a resize event should have fired.',
161 observer.getEvents().length == 2);
162 textarea.setContent('');
163 heightAfter = textarea.getHeight_();
164 assertTrue(
165 'Textarea should shrink with no content.', heightAfter <= heightBefore);
166 assertTrue(
167 'With a height change, a resize event should have fired.',
168 observer.getEvents().length == 3);
169 goog.events.unlisten(textarea, goog.ui.Textarea.EventType.RESIZE, observer);
170 }
171
172 function testMinHeight() {
173 textarea.render(sandbox);
174 textarea.setMinHeight(50);
175 assertEquals(
176 'offsetHeight should be 50 initially', 50,
177 textarea.getElement().offsetHeight);
178 textarea.setContent(
179 'Lorem ipsum dolor sit amet, consectetuer ' +
180 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
181 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
182 'lorem, in fermentum purus ligula quis nunc.');
183 assertTrue('getHeight_() should be > 50', textarea.getHeight_() > 50);
184
185 textarea.setContent('');
186 assertEquals(
187 'With no content, offsetHeight should go back to 50, ' +
188 'the minHeight.',
189 50, textarea.getElement().offsetHeight);
190
191 expectedFailures.expectFailureFor(isMacSafari3());
192 try {
193 textarea.setMinHeight(0);
194 assertTrue(
195 'After setting minHeight to 0, offsetHeight should ' +
196 'now be < 50, but it is ' + textarea.getElement().offsetHeight,
197 textarea.getElement().offsetHeight < 50);
198 } catch (e) {
199 expectedFailures.handleException(e);
200 }
201 }
202
203 function testMouseUpListener() {
204 textarea.render(sandbox);
205 textarea.setMinHeight(100);
206 textarea.setMaxHeight(200);
207 textarea.mouseUpListener_({});
208 assertEquals(
209 'After a mouseup which is not a resize, minHeight should ' +
210 'still be 100',
211 100, textarea.minHeight_);
212
213 // We need to test how CSS drop shadows effect this too.
214 goog.dom.classlist.add(textarea.getElement(), 'drop-shadowed');
215 textarea.mouseUpListener_({});
216 assertEquals(
217 'After a mouseup which is not a resize, minHeight should ' +
218 'still be 100 even with a shadow',
219 100, textarea.minHeight_);
220 }
221
222 function testMaxHeight() {
223 textarea.render(sandbox);
224 textarea.setMaxHeight(50);
225 assertTrue(
226 'Initial offsetHeight should be less than 50',
227 textarea.getElement().offsetHeight < 50);
228 var newContent = 'Lorem ipsum dolor sit amet, consectetuer adipiscing ' +
229 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
230 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
231 'lorem, in fermentum purus ligula quis nunc.';
232 textarea.setContent(newContent);
233
234 assertTrue(
235 'With lots of content, getHeight_() should be > 50',
236 textarea.getHeight_() > 50);
237 assertEquals(
238 'Even with lots of content, offsetHeight should be 50 ' +
239 'with maxHeight set',
240 50, textarea.getElement().offsetHeight);
241 textarea.setMaxHeight(0);
242 assertTrue(
243 'After setting maxHeight to 0, offsetHeight should now ' +
244 'be > 50',
245 textarea.getElement().offsetHeight > 50);
246 }
247
248 function testMaxHeight_canShrink() {
249 textarea.render(sandbox);
250 textarea.setMaxHeight(50);
251 assertTrue(
252 'Initial offsetHeight should be less than 50',
253 textarea.getElement().offsetHeight < 50);
254 var newContent = 'Lorem ipsum dolor sit amet, consectetuer adipiscing ' +
255 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
256 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
257 'lorem, in fermentum purus ligula quis nunc.';
258 textarea.setContent(newContent);
259
260 assertEquals(
261 'Even with lots of content, offsetHeight should be 50 ' +
262 'with maxHeight set',
263 50, textarea.getElement().offsetHeight);
264 textarea.setContent('');
265 assertTrue(
266 'With no content, offsetHeight should be back to < 50',
267 textarea.getElement().offsetHeight < 50);
268 }
269
270 function testSetPlaceholder() {
271 textarea.setPlaceholder('Some default text here.');
272 textarea.setPlaceholder('new default text...');
273 textarea.render(sandbox);
274 if (textarea.supportsNativePlaceholder_()) {
275 assertEquals('new default text...', textarea.getElement().placeholder);
276 }
277 assertEquals('', textarea.getValue());
278 textarea.setValue('some value');
279 assertEquals('some value', textarea.getValue());
280 // ensure setting a new placeholder doesn't replace the value.
281 textarea.setPlaceholder('some new placeholder');
282 assertEquals('some value', textarea.getValue());
283 }
284
285 function testSetPlaceholderForInitialContent() {
286 var testTextarea = new goog.ui.Textarea('initial content');
287 testTextarea.render(sandbox);
288 assertEquals('initial content', testTextarea.getValue());
289 testTextarea.setPlaceholder('new default text...');
290 assertEquals('initial content', testTextarea.getValue());
291 testTextarea.setValue('new content');
292 assertEquals('new content', testTextarea.getValue());
293 testTextarea.setValue('');
294 assertEquals('', testTextarea.getValue());
295 if (!testTextarea.supportsNativePlaceholder_()) {
296 // Pretend we leave the textarea. When that happens, the
297 // placeholder text should appear.
298 assertEquals('', testTextarea.getElement().value);
299 testTextarea.blur_();
300 assertEquals('new default text...', testTextarea.getElement().value);
301 }
302 }
303
304 function testMinAndMaxHeight() {
305 textarea.render(sandbox);
306 textarea.setMinHeight(50);
307 textarea.setMaxHeight(150);
308 assertEquals(
309 'offsetHeight should be 50 initially', 50,
310 textarea.getElement().offsetHeight);
311
312 textarea.setContent(
313 'Lorem ipsum dolor sit amet, consectetuer ' +
314 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
315 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
316 'lorem, in fermentum purus ligula quis nunc.');
317
318 var height = textarea.getHeight_();
319 // For some reason Mac Safari 3 has 136 and Linux FF has 146 here.
320 expectedFailures.expectFailureFor(isMacSafari3() || isLinuxFirefox());
321 try {
322 assertTrue(
323 'With lots of content, getHeight_() should be > 150 ' +
324 '(it is ' + height + ')',
325 height > 150);
326 assertEquals(
327 'Even with lots of content, offsetHeight should be 150 ' +
328 'with maxHeight set',
329 150, textarea.getElement().offsetHeight);
330
331 textarea.setMaxHeight(0);
332 assertTrue(
333 'After setting maxHeight to 0, offsetHeight should now ' +
334 'be > 150 (it is ' + textarea.getElement().offsetHeight + ')',
335 textarea.getElement().offsetHeight > 150);
336
337 textarea.setContent('');
338 textarea.setMinHeight(0);
339 assertTrue(
340 'After setting minHeight to 0, with no contents, ' +
341 'offsetHeight should now be < 50',
342 textarea.getElement().offsetHeight < 50);
343 } catch (e) {
344 expectedFailures.handleException(e);
345 }
346 }
347
348 function testSetValueWhenInvisible() {
349 textarea.render(sandbox);
350 var content = 'Lorem ipsum dolor sit amet, consectetuer ' +
351 'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
352 'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
353 'lorem, in fermentum purus ligula quis nunc.';
354 textarea.setValue(content);
355 var height = textarea.getHeight_();
356 var elementHeight = goog.style.getStyle(textarea.getElement(), 'height');
357 assertEquals(height + 'px', elementHeight);
358
359 // Hide the element, height_ should be invalidate when setValue().
360 goog.style.setElementShown(textarea.getElement(), false);
361 textarea.setValue(content);
362
363 // Show the element again.
364 goog.style.setElementShown(textarea.getElement(), true);
365 textarea.setValue(content);
366 height = textarea.getHeight_();
367 elementHeight = goog.style.getStyle(textarea.getElement(), 'height');
368 assertEquals(height + 'px', elementHeight);
369 }
370
371 function testSetAriaLabel() {
372 assertNull(
373 'Textarea must not have aria label by default', textarea.getAriaLabel());
374 textarea.setAriaLabel('My textarea');
375 textarea.render(sandbox);
376 var element = textarea.getElementStrict();
377 assertNotNull('Element must not be null', element);
378 assertEquals(
379 'Item element must have expected aria-label', 'My textarea',
380 element.getAttribute('aria-label'));
381 textarea.setAriaLabel('My new textarea');
382 assertEquals(
383 'Item element must have updated aria-label', 'My new textarea',
384 element.getAttribute('aria-label'));
385 }