File: combobox_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,488 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.ComboBoxTest');
goog.setTestOnly('goog.ui.ComboBoxTest');

goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.dom.classlist');
goog.require('goog.events.KeyCodes');
goog.require('goog.testing.MockClock');
goog.require('goog.testing.events');
goog.require('goog.testing.jsunit');
goog.require('goog.ui.ComboBox');
goog.require('goog.ui.ComboBoxItem');
goog.require('goog.ui.Component');
goog.require('goog.ui.ControlRenderer');
goog.require('goog.ui.LabelInput');
goog.require('goog.ui.Menu');
goog.require('goog.ui.MenuItem');

var comboBox;
var input;

function setUp() {
  goog.dom.removeChildren(goog.dom.getElement('combo'));

  comboBox = new goog.ui.ComboBox();
  comboBox.setDefaultText('Select a color...');
  comboBox.addItem(new goog.ui.ComboBoxItem('Red'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Maroon'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Gre<en'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Blue'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Royal Blue'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Yellow'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Magenta'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Mouve'));
  comboBox.addItem(new goog.ui.ComboBoxItem('Grey'));
  comboBox.render(goog.dom.getElement('combo'));

  input = comboBox.getInputElement();
}

function tearDown() {
  comboBox.dispose();
}

function testInputElementAttributes() {
  var comboBox = new goog.ui.ComboBox();
  comboBox.setFieldName('a_form_field');
  comboBox.createDom();
  var inputElement = comboBox.getInputElement();
  assertEquals('text', inputElement.type);
  assertEquals('a_form_field', inputElement.name);
  assertEquals('off', inputElement.autocomplete);
  comboBox.dispose();
}

function testSetDefaultText() {
  assertEquals('Select a color...', comboBox.getDefaultText());
  comboBox.setDefaultText('new default text...');
  assertEquals('new default text...', comboBox.getDefaultText());
  assertEquals('new default text...', comboBox.labelInput_.getLabel());
}

function testGetMenu() {
  assertTrue(
      'Menu should be instance of goog.ui.Menu',
      comboBox.getMenu() instanceof goog.ui.Menu);
  assertEquals(
      'Menu should have correct number of children', 9,
      comboBox.getMenu().getChildCount());
}

function testMenuBeginsInvisible() {
  assertFalse('Menu should begin invisible', comboBox.getMenu().isVisible());
}

function testClickCausesPopup() {
  goog.testing.events.fireClickSequence(input);
  assertTrue(
      'Menu becomes visible after click', comboBox.getMenu().isVisible());
}

function testUpKeyCausesPopup() {
  goog.testing.events.fireKeySequence(input, goog.events.KeyCodes.UP);
  assertTrue(
      'Menu becomes visible after UP key', comboBox.getMenu().isVisible());
}

function testActionSelectsItem() {
  comboBox.getMenu().getItemAt(2).dispatchEvent(
      goog.ui.Component.EventType.ACTION);
  assertEquals('Gre<en', input.value);
}

function testActionSelectsItemWithModel() {
  var itemWithModel = new goog.ui.MenuItem('one', 1);
  comboBox.addItem(itemWithModel);
  itemWithModel.dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals('one', comboBox.getValue());
}

function testRedisplayMenuAfterBackspace() {
  input.value = 'mx';
  comboBox.onInputEvent_();
  input.value = 'm';
  comboBox.onInputEvent_();
  assertEquals(
      'Three items should be displayed', 3,
      comboBox.getNumberOfVisibleItems_());
}

function testExternallyCreatedMenu() {
  var menu = new goog.ui.Menu();
  menu.decorate(goog.dom.getElement('menu'));
  assertTrue(
      'Menu items should be instances of goog.ui.ComboBoxItem',
      menu.getChildAt(0) instanceof goog.ui.ComboBoxItem);

  comboBox = new goog.ui.ComboBox(null, menu);
  comboBox.render(goog.dom.getElement('combo'));

  input = goog.dom.getElementsByTagName(
      goog.dom.TagName.INPUT, comboBox.getElement())[0];
  menu.getItemAt(2).dispatchEvent(goog.ui.Component.EventType.ACTION);
  assertEquals('Blue', input.value);
}

function testRecomputeVisibleCountAfterChangingItems() {
  input.value = 'Black';
  comboBox.onInputEvent_();
  assertEquals(
      'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_());
  comboBox.addItem(new goog.ui.ComboBoxItem('Black'));
  assertEquals(
      'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_());

  input.value = 'Red';
  comboBox.onInputEvent_();
  assertEquals(
      'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_());
  comboBox.removeItemAt(0);  // Red
  assertEquals(
      'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_());
}

function testSetEnabled() {
  // By default, everything should be enabled.
  assertFalse('Text input should initially not be disabled', input.disabled);
  assertFalse(
      'Text input should initially not look disabled',
      goog.dom.classlist.contains(
          input,
          goog.getCssName(
              goog.ui.LabelInput.prototype.labelCssClassName, 'disabled')));
  assertFalse(
      'Combo box should initially not look disabled',
      goog.dom.classlist.contains(
          comboBox.getElement(), goog.getCssName('goog-combobox-disabled')));
  goog.testing.events.fireClickSequence(comboBox.getElement());
  assertTrue(
      'Menu initially becomes visible after click',
      comboBox.getMenu().isVisible());
  goog.testing.events.fireClickSequence(document);
  assertFalse(
      'Menu initially becomes invisible after document click',
      comboBox.getMenu().isVisible());

  assertTrue(comboBox.isEnabled());
  comboBox.setEnabled(false);
  assertFalse(comboBox.isEnabled());
  assertTrue(
      'Text input should be disabled after being disabled', input.disabled);
  assertTrue(
      'Text input should appear disabled after being disabled',
      goog.dom.classlist.contains(
          input,
          goog.getCssName(
              goog.ui.LabelInput.prototype.labelCssClassName, 'disabled')));
  assertTrue(
      'Combo box should appear disabled after being disabled',
      goog.dom.classlist.contains(
          comboBox.getElement(), goog.getCssName('goog-combobox-disabled')));
  goog.testing.events.fireClickSequence(comboBox.getElement());
  assertFalse(
      'Menu should not become visible after click if disabled',
      comboBox.getMenu().isVisible());

  comboBox.setEnabled(true);
  assertTrue(comboBox.isEnabled());
  assertFalse(
      'Text input should not be disabled after being re-enabled',
      input.disabled);
  assertFalse(
      'Text input should not appear disabled after being re-enabled',
      goog.dom.classlist.contains(
          input,
          goog.getCssName(
              goog.ui.LabelInput.prototype.labelCssClassName, 'disabled')));
  assertFalse(
      'Combo box should not appear disabled after being re-enabled',
      goog.dom.classlist.contains(
          comboBox.getElement(), goog.getCssName('goog-combobox-disabled')));
  goog.testing.events.fireClickSequence(comboBox.getElement());
  assertTrue(
      'Menu becomes visible after click when re-enabled',
      comboBox.getMenu().isVisible());
  goog.testing.events.fireClickSequence(document);
  assertFalse(
      'Menu becomes invisible after document click when re-enabled',
      comboBox.getMenu().isVisible());
}

function testSetFormatFromToken() {
  var item = new goog.ui.ComboBoxItem('ABc');
  item.setFormatFromToken('b');
  var div = goog.dom.createDom(goog.dom.TagName.DIV);
  new goog.ui.ControlRenderer().setContent(div, item.getContent());
  assertTrue(div.innerHTML == 'A<b>B</b>c' || div.innerHTML == 'A<B>B</B>c');
}

function testSetValue() {
  var clock = new goog.testing.MockClock(/* autoInstall */ true);

  // Get the input focus. Note that both calls are needed to correctly
  // simulate the focus (and setting document.activeElement) across all
  // browsers.
  input.focus();
  goog.testing.events.fireClickSequence(input);

  // Simulate text input.
  input.value = 'Black';
  comboBox.onInputEvent_();
  clock.tick();
  assertEquals(
      'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_());
  assertFalse('Menu should be invisible', comboBox.getMenu().isVisible());

  // Programmatic change with the input focus causes the menu visibility to
  // change if needed.
  comboBox.setValue('Blue');
  clock.tick();
  assertTrue('Menu should be visible1', comboBox.getMenu().isVisible());
  assertEquals(
      'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_());

  // Simulate user input to ensure all the items are invisible again, then
  // blur away.
  input.value = 'Black';
  comboBox.onInputEvent_();
  clock.tick();
  input.blur();
  document.body.focus();
  clock.tick(goog.ui.ComboBox.BLUR_DISMISS_TIMER_MS);
  assertEquals(
      'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_());
  assertFalse('Menu should be invisible', comboBox.getMenu().isVisible());

  // Programmatic change without the input focus does not pop up the menu,
  // but still updates the list of visible items within it.
  comboBox.setValue('Blue');
  clock.tick();
  assertFalse('Menu should be invisible', comboBox.getMenu().isVisible());
  assertEquals(
      'Menu should contain one item', 1, comboBox.getNumberOfVisibleItems_());

  // Click on the combobox. The entire menu becomes visible, the last item
  // (programmatically) set is highlighted.
  goog.testing.events.fireClickSequence(comboBox.getElement());
  assertTrue('Menu should be visible2', comboBox.getMenu().isVisible());
  assertEquals(
      'All items should be displayed', comboBox.getMenu().getItemCount(),
      comboBox.getNumberOfVisibleItems_());
  assertEquals(
      'The last item set should be highlighted',
      /* Blue= */ 3, comboBox.getMenu().getHighlightedIndex());

  clock.uninstall();
}