File: button_test.js

package info (click to toggle)
aseba-plugin-blockly 20180211%2Bgit-2
  • links: PTS
  • area: non-free
  • in suites: buster
  • size: 64,472 kB
  • sloc: xml: 7,976; python: 2,314; sh: 261; lisp: 24; makefile: 10
file content (291 lines) | stat: -rw-r--r-- 10,234 bytes parent folder | download | duplicates (2)
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
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

goog.provide('goog.ui.ButtonTest');
goog.setTestOnly('goog.ui.ButtonTest');

goog.require('goog.dom');
goog.require('goog.dom.classlist');
goog.require('goog.events');
goog.require('goog.events.Event');
goog.require('goog.events.EventType');
goog.require('goog.events.KeyCodes');
goog.require('goog.events.KeyHandler');
goog.require('goog.testing.events');
goog.require('goog.testing.jsunit');
goog.require('goog.ui.Button');
goog.require('goog.ui.ButtonRenderer');
goog.require('goog.ui.ButtonSide');
goog.require('goog.ui.Component');
goog.require('goog.ui.NativeButtonRenderer');

var sandbox;
var button;
var clonedButtonDom;
var demoButtonElement;

function setUp() {
  sandbox = goog.dom.getElement('sandbox');
  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.classlist.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.classlist.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);
}

function testSetAriaLabel() {
  assertNull(
      'Button must not have aria label by default', button.getAriaLabel());
  button.setAriaLabel('Button 1');
  button.render();
  assertEquals(
      'Button element must have expected aria-label', 'Button 1',
      button.getElement().getAttribute('aria-label'));
  button.setAriaLabel('Button 2');
  assertEquals(
      'Button element must have updated aria-label', 'Button 2',
      button.getElement().getAttribute('aria-label'));
}

function testSetAriaLabel_decorate() {
  assertNull(
      'Button must not have aria label by default', button.getAriaLabel());
  button.setAriaLabel('Button 1');
  button.decorate(demoButtonElement);
  var el = button.getElementStrict();
  assertEquals(
      'Button element must have expected aria-label', 'Button 1',
      el.getAttribute('aria-label'));
  assertEquals(
      'Button element must have expected aria-role', 'button',
      el.getAttribute('role'));
  button.setAriaLabel('Button 2');
  assertEquals(
      'Button element must have updated aria-label', 'Button 2',
      el.getAttribute('aria-label'));
  assertEquals(
      'Button element must have expected aria-role', 'button',
      el.getAttribute('role'));
}