aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/button_test.html
blob: d0737fb3169e0266ad15ac4c83f6c316db112792 (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
<!DOCTYPE html>
<html>
<!--
Copyright 2008 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.
-->
<!--
Author: nicksantos@google.com (Nick Santos)
Author: attila@google.com (Attila Bodis)
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.ui.Button</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.events');
    goog.require('goog.testing.events');
    goog.require('goog.testing.jsunit');
    goog.require('goog.ui.Button');
    goog.require('goog.ui.ButtonSide');
    goog.require('goog.ui.ButtonRenderer');
    goog.require('goog.ui.NativeButtonRenderer');
  </script>
</head>
<body>
  <p>Here's a button defined in markup:</p>
  <button id="demoButton">Foo</button>
  <div id="sandbox"></div>
  <script>
    var sandbox = goog.dom.getElement('sandbox');
    var button;
    var clonedButtonDom;
    var demoButtonElement;

    function setUp() {
      button = new goog.ui.Button();
      demoButtonElement = goog.dom.getElement('demoButton');
      clonedButtonDom = demoButtonElement.cloneNode(true);
    }

    function tearDown() {
      button.dispose();
      demoButtonElement.parentNode.replaceChild(clonedButtonDom,
          demoButtonElement);
      goog.dom.removeChildren(sandbox);
    }

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

      var fakeDomHelper = {};
      var testButton = new goog.ui.Button('Hello',
          goog.ui.ButtonRenderer.getInstance(), fakeDomHelper);
      assertEquals('Content must have expected value', 'Hello',
          testButton.getContent());
      assertEquals('Renderer must have expected value',
          goog.ui.ButtonRenderer.getInstance(), testButton.getRenderer());
      assertEquals('DOM helper must have expected value', fakeDomHelper,
          testButton.getDomHelper());
      testButton.dispose();
    }

    function testGetSetValue() {
      assertUndefined('Button\'s value must default to undefined',
          button.getValue());
      button.setValue(17);
      assertEquals('Button must have expected value', 17, button.getValue());
      button.render(sandbox);
      assertEquals('Button element must have expected value', '17',
          button.getElement().value);
      button.setValue('foo');
      assertEquals('Button element must have updated value', 'foo',
          button.getElement().value);
      button.setValueInternal('bar');
      assertEquals('Button must have new internal value', 'bar',
          button.getValue());
      assertEquals('Button element must be unchanged', 'foo',
          button.getElement().value);
    }

    function testGetSetTooltip() {
      assertUndefined('Button\'s tooltip must default to undefined',
          button.getTooltip());
      button.setTooltip('Hello');
      assertEquals('Button must have expected tooltip', 'Hello',
          button.getTooltip());
      button.render(sandbox);
      assertEquals('Button element must have expected title', 'Hello',
          button.getElement().title);
      button.setTooltip('Goodbye');
      assertEquals('Button element must have updated title', 'Goodbye',
          button.getElement().title);
      button.setTooltipInternal('World');
      assertEquals('Button must have new internal tooltip', 'World',
          button.getTooltip());
      assertEquals('Button element must be unchanged', 'Goodbye',
          button.getElement().title);
    }

    function testSetCollapsed() {
      assertNull('Button must not have any collapsed styling by default',
          button.getExtraClassNames());
      button.setCollapsed(goog.ui.ButtonSide.START);
      assertSameElements('Button must have the start side collapsed',
          ['goog-button-collapse-left'], button.getExtraClassNames());
      button.render(sandbox);
      assertSameElements('Button element must have the start side collapsed',
          ['goog-button', 'goog-button-collapse-left'],
          goog.dom.classes.get(button.getElement()));
      button.setCollapsed(goog.ui.ButtonSide.BOTH);
      assertSameElements('Button must have both sides collapsed',
          ['goog-button-collapse-left', 'goog-button-collapse-right'],
          button.getExtraClassNames());
      assertSameElements('Button element must have both sides collapsed', [
        'goog-button',
        'goog-button-collapse-left',
        'goog-button-collapse-right'
      ], goog.dom.classes.get(button.getElement()));
    }

    function testDispose() {
      assertFalse('Button must not have been disposed of', button.isDisposed());
      button.render(sandbox);
      button.setValue('foo');
      button.setTooltip('bar');
      button.dispose();
      assertTrue('Button must have been disposed of', button.isDisposed());
      assertUndefined('Button\'s value must have been deleted',
          button.getValue());
      assertUndefined('Button\'s tooltip must have been deleted',
          button.getTooltip());
    }

    function testBasicButtonBehavior() {
      var dispatchedActionCount = 0;
      var handleAction = function() {
        dispatchedActionCount++;
      };
      goog.events.listen(button, goog.ui.Component.EventType.ACTION,
          handleAction);

      button.decorate(demoButtonElement);
      goog.testing.events.fireClickSequence(demoButtonElement);
      assertEquals('Button must have dispatched ACTION on click', 1,
          dispatchedActionCount);

      dispatchedActionCount = 0;
      var e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY,
          button);
      e.keyCode = goog.events.KeyCodes.ENTER;
      button.handleKeyEvent(e);
      assertEquals('Enabled button must have dispatched ACTION on Enter key', 1,
          dispatchedActionCount);

      dispatchedActionCount = 0;
      e = new goog.events.Event(goog.events.EventType.KEYUP, button);
      e.keyCode = goog.events.KeyCodes.SPACE;
      button.handleKeyEvent(e);
      assertEquals('Enabled button must have dispatched ACTION on Space key', 1,
          dispatchedActionCount);

      goog.events.unlisten(button, goog.ui.Component.EventType.ACTION,
          handleAction);
    }

    function testDisabledButtonBehavior() {
      var dispatchedActionCount = 0;
      var handleAction = function() {
        dispatchedActionCount++;
      };
      goog.events.listen(button, goog.ui.Component.EventType.ACTION,
          handleAction);

      button.setEnabled(false);

      dispatchedActionCount = 0;
      button.handleKeyEvent({keyCode: goog.events.KeyCodes.ENTER});
      assertEquals('Disabled button must not dispatch ACTION on Enter key',
          0, dispatchedActionCount);

      dispatchedActionCount = 0;
      button.handleKeyEvent({
        keyCode: goog.events.KeyCodes.SPACE,
        type: goog.events.EventType.KEYUP
      });
      assertEquals('Disabled button must not have dispatched ACTION on Space',
          0, dispatchedActionCount);

      goog.events.unlisten(button, goog.ui.Component.EventType.ACTION,
          handleAction);
    }

    function testSpaceFireActionOnKeyUp() {
      var dispatchedActionCount = 0;
      var handleAction = function() {
        dispatchedActionCount++;
      };
      goog.events.listen(button, goog.ui.Component.EventType.ACTION,
          handleAction);

      dispatchedActionCount = 0;
      e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
      e.keyCode = goog.events.KeyCodes.SPACE;
      button.handleKeyEvent(e);
      assertEquals('Button must not have dispatched ACTION on Space keypress',
          0, dispatchedActionCount);
      assertEquals('The default action (scrolling) must have been prevented ' +
                   'for Space keypress',
                   false,
                   e.returnValue_);


      dispatchedActionCount = 0;
      e = new goog.events.Event(goog.events.EventType.KEYUP, button);
      e.keyCode = goog.events.KeyCodes.SPACE;
      button.handleKeyEvent(e);
      assertEquals('Button must have dispatched ACTION on Space keyup',
          1, dispatchedActionCount);

      goog.events.unlisten(button, goog.ui.Component.EventType.ACTION,
          handleAction);
    }

    function testEnterFireActionOnKeyPress() {
      var dispatchedActionCount = 0;
      var handleAction = function() {
        dispatchedActionCount++;
      };
      goog.events.listen(button, goog.ui.Component.EventType.ACTION,
          handleAction);

      dispatchedActionCount = 0;
      e = new goog.events.Event(goog.events.KeyHandler.EventType.KEY, button);
      e.keyCode = goog.events.KeyCodes.ENTER;
      button.handleKeyEvent(e);
      assertEquals('Button must have dispatched ACTION on Enter keypress',
          1, dispatchedActionCount);

      dispatchedActionCount = 0;
      e = new goog.events.Event(goog.events.EventType.KEYUP, button);
      e.keyCode = goog.events.KeyCodes.ENTER;
      button.handleKeyEvent(e);
      assertEquals('Button must not have dispatched ACTION on Enter keyup',
          0, dispatchedActionCount);

      goog.events.unlisten(button, goog.ui.Component.EventType.ACTION,
          handleAction);
    }

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