File: select_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 (347 lines) | stat: -rw-r--r-- 10,946 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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2010 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.SelectTest');
goog.setTestOnly('goog.ui.SelectTest');

goog.require('goog.a11y.aria');
goog.require('goog.a11y.aria.Role');
goog.require('goog.a11y.aria.State');
goog.require('goog.dom');
goog.require('goog.events');
goog.require('goog.testing.jsunit');
goog.require('goog.testing.recordFunction');
goog.require('goog.ui.Component');
goog.require('goog.ui.CustomButtonRenderer');
goog.require('goog.ui.Menu');
goog.require('goog.ui.MenuItem');
goog.require('goog.ui.Select');
goog.require('goog.ui.Separator');

var defaultCaption = 'initial caption';
var sandboxEl;
var select;

function setUp() {
  sandboxEl = goog.dom.getElement('sandbox');
  select = new goog.ui.Select(defaultCaption);
}

function tearDown() {
  select.dispose();
  goog.dom.removeChildren(sandboxEl);
}


/**
 * Checks that the default caption passed in the constructor and in the setter
 * is returned by getDefaultCaption, and acts as a default caption, i.e. is
 * shown as a caption when no items are selected.
 */
function testDefaultCaption() {
  select.render(sandboxEl);
  var item1 = new goog.ui.MenuItem('item 1');
  select.addItem(item1);
  select.addItem(new goog.ui.MenuItem('item 2'));
  assertEquals(defaultCaption, select.getDefaultCaption());
  assertEquals(defaultCaption, select.getCaption());

  var newCaption = 'new caption';
  select.setDefaultCaption(newCaption);
  assertEquals(newCaption, select.getDefaultCaption());
  assertEquals(newCaption, select.getCaption());

  select.setSelectedItem(item1);
  assertNotEquals(newCaption, select.getCaption());

  select.setSelectedItem(null);
  assertEquals(newCaption, select.getCaption());
}

function testNoDefaultCaption() {
  assertNull(new goog.ui.Select().getDefaultCaption());
  assertEquals('', new goog.ui.Select('').getDefaultCaption());
}

// Confirms that aria roles for select conform to spec:
// http://www.w3.org/TR/wai-aria/roles#listbox
// Basically the select should have a role of LISTBOX and all the items should
// have a role of OPTION.
function testAriaRoles() {
  select.render(sandboxEl);
  var item1 = new goog.ui.MenuItem('item 1');
  select.addItem(item1);
  // Added a separator to make sure that the SETSIZE ignores the separator
  // items.
  var separator = new goog.ui.Separator();
  select.addItem(separator);
  var item2 = new goog.ui.MenuItem('item 2');
  select.addItem(item2);
  assertNotNull(select.getElement());
  assertNotNull(item1.getElement());
  assertNotNull(item2.getElement());
  assertEquals(
      goog.a11y.aria.Role.LISTBOX, goog.a11y.aria.getRole(select.getElement()));
  assertEquals(
      goog.a11y.aria.Role.OPTION, goog.a11y.aria.getRole(item1.getElement()));
  assertEquals(
      goog.a11y.aria.Role.OPTION, goog.a11y.aria.getRole(item2.getElement()));
  assertNotNull(
      goog.a11y.aria.getState(
          select.getElement(), goog.a11y.aria.State.ACTIVEDESCENDANT));
  var contentElement =
      select.getRenderer().getContentElement(select.getElement());
  assertEquals(
      '2',
      goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.SETSIZE));
  assertEquals(
      '0',
      goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET));
  select.setSelectedItem(item1);
  assertEquals(
      '1',
      goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET));
  select.setSelectedItem(item2);
  assertEquals(
      '2',
      goog.a11y.aria.getState(contentElement, goog.a11y.aria.State.POSINSET));
}


/**
 * Checks that the select control handles ACTION events from its items.
 */
function testHandlesItemActions() {
  select.render(sandboxEl);
  var item1 = new goog.ui.MenuItem('item 1');
  var item2 = new goog.ui.MenuItem('item 2');
  select.addItem(item1);
  select.addItem(item2);

  item1.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(item1, select.getSelectedItem());
  assertEquals(item1.getCaption(), select.getCaption());

  item2.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(item2, select.getSelectedItem());
  assertEquals(item2.getCaption(), select.getCaption());
}


/**
 * Tests goog.ui.Select.prototype.setValue.
 */
function testSetValue() {
  select.render(sandboxEl);
  var item1 = new goog.ui.MenuItem('item 1', 1);
  var item2 = new goog.ui.MenuItem('item 2', 2);
  select.addItem(item1);
  select.addItem(item2);

  select.setValue(1);
  assertEquals(item1, select.getSelectedItem());

  select.setValue(2);
  assertEquals(item2, select.getSelectedItem());

  select.setValue(3);
  assertNull(select.getSelectedItem());
}


/**
 * Checks that the current selection is cleared when the selected item is
 * removed.
 */
function testSelectionIsClearedWhenSelectedItemIsRemoved() {
  select.render(sandboxEl);
  var item1 = new goog.ui.MenuItem('item 1');
  select.addItem(item1);
  select.addItem(new goog.ui.MenuItem('item 2'));

  select.setSelectedItem(item1);
  select.removeItem(item1);
  assertNull(select.getSelectedItem());
}


/**
 * Check that the select control is subscribed to its selection model events
 * after being added, removed and added back again into the document.
 */
function testExitAndEnterDocument() {
  var component = new goog.ui.Component();
  component.render(sandboxEl);

  var item1 = new goog.ui.MenuItem('item 1');
  var item2 = new goog.ui.MenuItem('item 2');
  var item3 = new goog.ui.MenuItem('item 3');

  select.addItem(item1);
  select.addItem(item2);
  select.addItem(item3);

  component.addChild(select, true);
  item2.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(item2.getCaption(), select.getCaption());

  component.removeChild(select, true);
  item1.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(item2.getCaption(), select.getCaption());

  component.addChild(select, true);
  item3.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(item3.getCaption(), select.getCaption());
}

function testSelectEventFiresForProgrammaticChange() {
  select.render();
  var item1 = new goog.ui.MenuItem('item 1');
  var item2 = new goog.ui.MenuItem('item 2');
  select.addItem(item1);
  select.addItem(item2);

  var recordingHandler = new goog.testing.recordFunction();
  goog.events.listen(
      select, goog.ui.Component.EventType.CHANGE, recordingHandler);

  select.setSelectedItem(item2);
  assertEquals(
      'Selecting new item should fire CHANGE event.', 1,
      recordingHandler.getCallCount());

  select.setSelectedItem(item2);
  assertEquals(
      'Selecting the same item should not fire CHANGE event.', 1,
      recordingHandler.getCallCount());

  select.setSelectedIndex(0);
  assertEquals(
      'Selecting new item should fire CHANGE event.', 2,
      recordingHandler.getCallCount());

  select.setSelectedIndex(0);
  assertEquals(
      'Selecting the same item should not fire CHANGE event.', 2,
      recordingHandler.getCallCount());
}

function testSelectEventFiresForUserInitiatedAction() {
  select.render();
  var item1 = new goog.ui.MenuItem('item 1');
  var item2 = new goog.ui.MenuItem('item 2');
  select.addItem(item1);
  select.addItem(item2);

  var recordingHandler = new goog.testing.recordFunction();
  goog.events.listen(
      select, goog.ui.Component.EventType.CHANGE, recordingHandler);

  select.setOpen(true);

  item2.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(
      'Selecting new item should fire CHANGE event.', 1,
      recordingHandler.getCallCount());
  assertFalse(select.isOpen());

  select.setOpen(true);

  item2.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals(
      'Selecting the same item should not fire CHANGE event.', 1,
      recordingHandler.getCallCount());
  assertFalse(select.isOpen());
}


/**
 * Checks that if an item is selected before decorate is called, the selection
 * is preserved after decorate.
 */
function testSetSelectedItemBeforeRender() {
  select.addItem(new goog.ui.MenuItem('item 1'));
  select.addItem(new goog.ui.MenuItem('item 2'));
  var item3 = new goog.ui.MenuItem('item 3');
  select.addItem(item3);
  select.setSelectedItem(item3);
  assertEquals(2, select.getSelectedIndex());

  select.decorate(sandboxEl);
  assertEquals(2, select.getSelectedIndex());
}


/**
 * Checks that if a value is set before decorate is called, the value is
 * preserved after decorate.
 */
function testSetValueBeforeRender() {
  select.addItem(new goog.ui.MenuItem('item 1', 1));
  select.addItem(new goog.ui.MenuItem('item 2', 2));
  select.setValue(2);
  assertEquals(2, select.getValue());

  select.decorate(sandboxEl);
  assertEquals(2, select.getValue());
}


function testUpdateCaption_aria() {
  select.render(sandboxEl);

  // Verify default state.
  assertEquals(defaultCaption, select.getCaption());
  assertFalse(
      !!goog.a11y.aria.getLabel(
          select.getRenderer().getContentElement(select.getElement())));

  // Add and select an item with aria-label.
  var item1 = new goog.ui.MenuItem();
  select.addItem(item1);
  item1.getElement().setAttribute('aria-label', 'item1');
  select.setSelectedIndex(0);
  assertEquals(
      'item1',
      goog.a11y.aria.getLabel(
          select.getRenderer().getContentElement(select.getElement())));

  // Add and select an item without a label.
  var item2 = new goog.ui.MenuItem();
  select.addItem(item2);
  select.setSelectedIndex(1);
  assertFalse(
      !!goog.a11y.aria.getLabel(
          select.getRenderer().getContentElement(select.getElement())));
}

function testDisposeWhenInnerHTMLHasBeenClearedInIE10() {
  assertNotThrows(function() {
    var customSelect = new goog.ui.Select(
        null /* label */, new goog.ui.Menu(),
        new goog.ui.CustomButtonRenderer());
    customSelect.render(sandboxEl);

    // In IE10 setting the innerHTML of a node invalidates the parent child
    // relation of all its child nodes (unlike removeNode).
    goog.dom.removeChildren(sandboxEl);

    // goog.ui.Select's disposeInternal trigger's goog.ui.Component's
    // disposeInternal, which triggers goog.ui.MenuButton's exitDocument,
    // which closes the associated menu and updates the activeDescendent.
    // In the case of a CustomMenuButton the contentElement is referenced by
    // element.firstChild.firstChild, an invalid relation in IE 10.
    customSelect.dispose();
  });
}