aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/ui/select_test.html
blob: 3ae3d79bb66a7f8a6510582cbbe9653eba85b26b (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
<!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.Select</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.a11y');
    goog.require('goog.dom.a11y.Role');
    goog.require('goog.dom.a11y.State');
    goog.require('goog.testing.jsunit');
    goog.require('goog.testing.recordFunction');
    goog.require('goog.ui.Component.EventType');
    goog.require('goog.ui.MenuItem');
    goog.require('goog.ui.Select');
  </script>
</head>
<body>

<div id="sandbox"></div>
<script>

var defaultCaption = 'initial caption';
var sandbox = goog.dom.getElement('sandbox');
var select;

function setUp() {
  select = new goog.ui.Select(defaultCaption);
}

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

/**
 * 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(sandbox);
  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());
}

// 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(sandbox);
  var item1 = new goog.ui.MenuItem("item 1");
  select.addItem(item1);
  var item2 = new goog.ui.MenuItem("item 2");
  select.addItem(item2);

  assertEquals(goog.dom.a11y.Role.LISTBOX,
      goog.dom.a11y.getRole(select.getElement()));
  assertEquals('false', goog.dom.a11y.getState(select.getElement(),
          goog.dom.a11y.State.HASPOPUP));
  assertEquals(goog.dom.a11y.Role.OPTION,
      goog.dom.a11y.getRole(item1.getElement()));
  assertEquals(goog.dom.a11y.Role.OPTION,
      goog.dom.a11y.getRole(item2.getElement()));
}

/**
 * Checks that the select control handles ACTION events from its items.
 */
function testHandlesItemActions() {
  select.render(sandbox);
  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(sandbox);
  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(sandbox);
  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(sandbox);

  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());
}
</script>

</body>
</html>