aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/textarea_test.html
blob: aebc733ccf2e2b6188f039c1e7fba0862398c519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html>
<!--
Copyright 2010 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.ui.Textarea</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.classes');
    goog.require('goog.debug.DivConsole');
    goog.require('goog.debug.LogManager');
    goog.require('goog.debug.Logger');
    goog.require('goog.events');
    goog.require('goog.testing.ExpectedFailures');
    goog.require('goog.testing.events');
    goog.require('goog.testing.events.EventObserver');
    goog.require('goog.testing.jsunit');
    goog.require('goog.object');
    goog.require('goog.ui.Control');
    goog.require('goog.ui.ControlRenderer');
    goog.require('goog.ui.Textarea');
    goog.require('goog.ui.TextareaRenderer');
    goog.require('goog.userAgent');
    goog.require('goog.userAgent.product');
    goog.require('goog.userAgent.product.isVersion');
  </script>
  <style>
    textarea {
      height: 25px; /* Need to force an initial height < our minHeight. */
      width: 150px;
      padding: 2px;
      margin: 0;
      border: 1px solid #000;
    }
    .drop-shadowed {
      filter:progid:DXImageTransform.Microsoft.DropShadow(color='#e7e7e7',
          offX='5',offY='5');
      box-shadow: 5px 5px 0 #e7e7e7;
      -moz-box-shadow: 5px 5px 0 #e7e7e7;
      -webkit-box-shadow: 5px 5px 0 #e7e7e7;
    }
  </style>
</head>
<body>
  <h1>goog.ui.Textarea tests</h1>
  <p>Here's a textarea defined in markup:</p>
  <textarea id="demo-textarea">Foo</textarea>
  <div id="sandbox"></div>
  <script>
    var sandbox = goog.dom.getElement('sandbox');
    var textarea;
    var demoTextareaElement = goog.dom.getElement('demo-textarea');
    var expectedFailures = new goog.testing.ExpectedFailures();

    function setUp() {
      textarea = new goog.ui.Textarea();
    }

    function tearDown() {
      expectedFailures.handleTearDown();
      textarea.dispose();
      goog.dom.removeChildren(sandbox);
    }

    /**
     * @return {boolean} Whether we're on Mac Safari 3.x.
     */
    function isMacSafari3() {
      return goog.userAgent.WEBKIT && goog.userAgent.MAC &&
          !goog.userAgent.isVersion('527');
    }

    /**
     * @return {boolean} Whether we're on Linux Firefox 3.6.3.
     */
    function isLinuxFirefox363() {
      return goog.userAgent.product.FIREFOX && goog.userAgent.LINUX &&
          goog.userAgent.product.isVersion('3.6.3') &&
          !goog.userAgent.product.isVersion('3.6.4');
    }

    /**
     * @return {boolean} Whether we're on Firefox 3.0.
     */
    function isFirefox3() {
      return goog.userAgent.GECKO &&
          !goog.userAgent.isVersion('1.9.1');
    }

    function testConstructor() {
      assertNotNull('Textarea must not be null', textarea);
      assertEquals('Renderer must default to expected value',
          goog.ui.TextareaRenderer.getInstance(), textarea.getRenderer());

      var fakeDomHelper = {
        'getDocument': function() { return true; }
      };
      var testTextarea = new goog.ui.Textarea('Hello',
          goog.ui.TextareaRenderer.getInstance(), fakeDomHelper);
      assertEquals('Content must have expected content', 'Hello',
          testTextarea.getContent());
      assertEquals('Renderer must have expected value',
          goog.ui.TextareaRenderer.getInstance(), testTextarea.getRenderer());
      assertEquals('DOM helper must have expected value', fakeDomHelper,
          testTextarea.getDomHelper());
      testTextarea.dispose();
    }

    function testConstructorWithDecorator() {
      var decoratedTextarea = new goog.ui.Textarea();
      decoratedTextarea.decorate(demoTextareaElement);
      assertEquals('Textarea should have current content after decoration',
          'Foo', decoratedTextarea.getContent());
      var initialHeight = decoratedTextarea.getHeight_();
      var initialOffsetHeight = decoratedTextarea.getElement().offsetHeight;
      // focus() will trigger the grow/shrink flow.
      decoratedTextarea.getElement().focus();
      assertEquals('Height should not have changed without content change',
           initialHeight, decoratedTextarea.getHeight_());
      assertEquals('offsetHeight should not have changed without content ' +
          'change', initialOffsetHeight,
          decoratedTextarea.getElement().offsetHeight);
      decoratedTextarea.dispose();
    }

    function testGetSetContent() {
      textarea.render(sandbox);
      assertEquals('Textarea\'s content must default to an empty string',
          '', textarea.getContent());
      textarea.setContent(17);
      assertEquals('Textarea element must have expected content', '17',
          textarea.getElement().value);
      textarea.setContent('foo');
      assertEquals('Textarea element must have updated content', 'foo',
          textarea.getElement().value);
    }

    function testGetSetValue() {
      textarea.render(sandbox);
      assertEquals('Textarea\'s content must default to an empty string',
          '', textarea.getValue());
      textarea.setValue(17);
      assertEquals('Textarea element must have expected content', '17',
          textarea.getValue());
      textarea.setValue('17');
      assertEquals('Textarea element must have expected content', '17',
          textarea.getValue());
    }

    function testBasicTextareaBehavior() {
      var observer = new goog.testing.events.EventObserver();
      goog.events.listen(textarea, goog.ui.Textarea.EventType.RESIZE, observer);
      textarea.render(sandbox);
      var el = textarea.getElement();
      var heightBefore = textarea.getHeight_();
      assertTrue('One resize event should be fired during render',
          observer.getEvents().length == 1);
      textarea.setContent('Lorem ipsum dolor sit amet, consectetuer ' +
          'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
          'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
          'lorem, in fermentum purus ligula quis nunc.');
      var heightAfter = textarea.getHeight_();
      assertTrue('With this much content, height should have grown.',
          heightAfter > heightBefore);
      assertTrue('With a height change, a resize event should have fired.',
          observer.getEvents().length == 2);

      textarea.setContent('');
      heightAfter = textarea.getHeight_();
      assertTrue('Textarea should shrink with no content.',
          heightAfter <= heightBefore);
      assertTrue('With a height change, a resize event should have fired.',
          observer.getEvents().length == 3);
      goog.events.unlisten(textarea, goog.ui.Textarea.EventType.RESIZE,
          observer);
    }

    function testGetHorizontalScrollBarHeight() {
      textarea.render(sandbox);
      var textareaEl = textarea.getElement();
      var scrollHeightWithoutScrollBar = textareaEl.scrollHeight;
      var withoutScrollBarHeight =
          textarea.getHorizontalScrollBarHeight_();
      assertEquals('ScrollbarHeight should be 0 with no scrollbar.',
          0, withoutScrollBarHeight);

      textarea.setContent('Onereallylongstringoftextlongenoughto' +
          'justifyascrollbar');
      textarea.getElement().style.overflow = 'scroll';
      textarea.getElement().style.overflowX = 'scroll';
      var scrollBarHeight =
          textarea.getHorizontalScrollBarHeight_();
      var scrollHeight = textareaEl.scrollHeight;

      // Opera & Safari3 include scrollbars in scrollHeight.
      expectedFailures.expectFailureFor(goog.userAgent.OPERA ||
          isMacSafari3() || isFirefox3());
      try {
        assertTrue('ScrollbarHeight should be something > 0 (it is ' +
          scrollBarHeight + ') and isFirefox3():' + isFirefox3() + ', ' +
          goog.userAgent.VERSION, scrollBarHeight > 0);
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testMinHeight() {
      textarea.render(sandbox);
      textarea.setMinHeight(50);
      assertEquals('offsetHeight should be 50 initially', 50,
          textarea.getElement().offsetHeight);
      textarea.setContent('Lorem ipsum dolor sit amet, consectetuer  ' +
          'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
          'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
          'lorem, in fermentum purus ligula quis nunc.');
      assertTrue('getHeight_() should be > 50',
          textarea.getHeight_() > 50);

      textarea.setContent('');
      assertEquals('With no content, offsetHeight should go back to 50, ' +
          'the minHeight.', 50, textarea.getElement().offsetHeight);

      expectedFailures.expectFailureFor(isMacSafari3());
      try {
        textarea.setMinHeight(0);
        assertTrue('After setting minHeight to 0, offsetHeight should ' +
            'now be < 50, but it is ' + textarea.getElement().offsetHeight,
            textarea.getElement().offsetHeight < 50);
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

    function testMouseUpListener() {
      textarea.render(sandbox);
      textarea.setMinHeight(100);
      textarea.setMaxHeight(200);
      textarea.mouseUpListener_({});
      assertEquals('After a mouseup which is not a resize, minHeight should ' +
          'still be 100', 100, textarea.minHeight_);

      // We need to test how CSS drop shadows effect this too.
      goog.dom.classes.add(textarea.getElement(), 'drop-shadowed');
      textarea.mouseUpListener_({});
      assertEquals('After a mouseup which is not a resize, minHeight should ' +
          'still be 100 even with a shadow', 100, textarea.minHeight_);

    }

    function testMaxHeight() {
      textarea.render(sandbox);
      textarea.setMaxHeight(50);
      assertTrue('Initial offsetHeight should be less than 50',
          textarea.getElement().offsetHeight < 50);
      var newContent = 'Lorem ipsum dolor sit amet, consectetuer adipiscing ' +
          'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
          'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
          'lorem, in fermentum purus ligula quis nunc.'
      textarea.setContent(newContent);

      assertTrue('With lots of content, getHeight_() should be > 50',
          textarea.getHeight_() > 50);
      assertEquals('Even with lots of content, offsetHeight should be 50 ' +
          'with maxHeight set', 50, textarea.getElement().offsetHeight);
      textarea.setMaxHeight(0);
      assertTrue('After setting maxHeight to 0, offsetHeight should now ' +
          'be > 50', textarea.getElement().offsetHeight > 50);
    }

    function testMinAndMaxHeight() {
      textarea.render(sandbox);
      textarea.setMinHeight(50);
      textarea.setMaxHeight(150);
      assertEquals('offsetHeight should be 50 initially', 50,
          textarea.getElement().offsetHeight);

      textarea.setContent('Lorem ipsum dolor sit amet, consectetuer  ' +
          'elit. Aenean sollicitudin ultrices urna. Proin vehicula mauris ac ' +
          'est. Ut scelerisque, risus ut facilisis dictum, est massa lacinia ' +
          'lorem, in fermentum purus ligula quis nunc.');


      var height = textarea.getHeight_();
      // For some reason Mac Safari 3 has 136 and Linux FF 3.6.3 has 146 here.
      expectedFailures.expectFailureFor(isMacSafari3() || isLinuxFirefox363());
      try {
        assertTrue('With lots of content, getHeight_() should be > 150 ' +
          '(it is ' + height + ')', height > 150);
        assertEquals('Even with lots of content, offsetHeight should be 150 ' +
            'with maxHeight set', 150,
            textarea.getElement().offsetHeight);

        textarea.setMaxHeight(0);
        assertTrue('After setting maxHeight to 0, offsetHeight should now ' +
            'be > 150 (it is ' + textarea.getElement().offsetHeight + ')',
            textarea.getElement().offsetHeight > 150);

        textarea.setContent('');
        textarea.setMinHeight(0);
        assertTrue('After setting minHeight to 0, with no contents, ' +
            'offsetHeight should now be < 50',
            textarea.getElement().offsetHeight < 50);
      } catch (e) {
        expectedFailures.handleException(e);
      }
    }

  </script>
</body>
</html>