| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.ComboBoxTest'); |
| 16 |
goog.setTestOnly('goog.ui.ComboBoxTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.dom.classlist'); |
| 21 |
goog.require('goog.events.KeyCodes'); |
| 22 |
goog.require('goog.testing.MockClock'); |
| 23 |
goog.require('goog.testing.events'); |
| 24 |
goog.require('goog.testing.jsunit'); |
| 25 |
goog.require('goog.ui.ComboBox'); |
| 26 |
goog.require('goog.ui.ComboBoxItem'); |
| 27 |
goog.require('goog.ui.Component'); |
| 28 |
goog.require('goog.ui.ControlRenderer'); |
| 29 |
goog.require('goog.ui.LabelInput'); |
| 30 |
goog.require('goog.ui.Menu'); |
| 31 |
goog.require('goog.ui.MenuItem'); |
| 32 |
|
| 33 |
var comboBox; |
| 34 |
var input; |
| 35 |
|
| 36 |
function setUp() { |
| 37 |
goog.dom.removeChildren(goog.dom.getElement('combo')); |
| 38 |
|
| 39 |
comboBox = new goog.ui.ComboBox(); |
| 40 |
comboBox.setDefaultText('Select a color...'); |
| 41 |
comboBox.addItem(new goog.ui.ComboBoxItem('Red')); |
| 42 |
comboBox.addItem(new goog.ui.ComboBoxItem('Maroon')); |
| 43 |
comboBox.addItem(new goog.ui.ComboBoxItem('Gre<en')); |
| 44 |
comboBox.addItem(new goog.ui.ComboBoxItem('Blue')); |
| 45 |
comboBox.addItem(new goog.ui.ComboBoxItem('Royal Blue')); |
| 46 |
comboBox.addItem(new goog.ui.ComboBoxItem('Yellow')); |
| 47 |
comboBox.addItem(new goog.ui.ComboBoxItem('Magenta')); |
| 48 |
comboBox.addItem(new goog.ui.ComboBoxItem('Mouve')); |
| 49 |
comboBox.addItem(new goog.ui.ComboBoxItem('Grey')); |
| 50 |
comboBox.render(goog.dom.getElement('combo')); |
| 51 |
|
| 52 |
input = comboBox.getInputElement(); |
| 53 |
} |
| 54 |
|
| 55 |
function tearDown() { |
| 56 |
comboBox.dispose(); |
| 57 |
} |
| 58 |
|
| 59 |
function testInputElementAttributes() { |
| 60 |
var comboBox = new goog.ui.ComboBox(); |
| 61 |
comboBox.setFieldName('a_form_field'); |
| 62 |
comboBox.createDom(); |
| 63 |
var inputElement = comboBox.getInputElement(); |
| 64 |
assertEquals('text', inputElement.type); |
| 65 |
assertEquals('a_form_field', inputElement.name); |
| 66 |
assertEquals('off', inputElement.autocomplete); |
| 67 |
comboBox.dispose(); |
| 68 |
} |
| 69 |
|
| 70 |
function testSetDefaultText() { |
| 71 |
assertEquals('Select a color...', comboBox.getDefaultText()); |
| 72 |
comboBox.setDefaultText('new default text...'); |
| 73 |
assertEquals('new default text...', comboBox.getDefaultText()); |
| 74 |
assertEquals('new default text...', comboBox.labelInput_.getLabel()); |
| 75 |
} |
| 76 |
|
| 77 |
function testGetMenu() { |
| 78 |
assertTrue( |
| 79 |
'Menu should be instance of goog.ui.Menu', |
| 80 |
comboBox.getMenu() instanceof goog.ui.Menu); |
| 81 |
assertEquals( |
| 82 |
'Menu should have correct number of children', 9, |
| 83 |
comboBox.getMenu().getChildCount()); |
| 84 |
} |
| 85 |
|
| 86 |
function testMenuBeginsInvisible() { |
| 87 |
assertFalse('Menu should begin invisible', comboBox.getMenu().isVisible()); |
| 88 |
} |
| 89 |
|
| 90 |
function testClickCausesPopup() { |
| 91 |
goog.testing.events.fireClickSequence(input); |
| 92 |
assertTrue( |
| 93 |
'Menu becomes visible after click', comboBox.getMenu().isVisible()); |
| 94 |
} |
| 95 |
|
| 96 |
function testUpKeyCausesPopup() { |
| 97 |
goog.testing.events.fireKeySequence(input, goog.events.KeyCodes.UP); |
| 98 |
assertTrue( |
| 99 |
'Menu becomes visible after UP key', comboBox.getMenu().isVisible()); |
| 100 |
} |
| 101 |
|
| 102 |
function testActionSelectsItem() { |
| 103 |
comboBox.getMenu().getItemAt(2).dispatchEvent( |
| 104 |
goog.ui.Component.EventType.ACTION); |
| 105 |
assertEquals('Gre<en', input.value); |
| 106 |
} |
| 107 |
|
| 108 |
function testActionSelectsItemWithModel() { |
| 109 |
var itemWithModel = new goog.ui.MenuItem('one', 1); |
| 110 |
comboBox.addItem(itemWithModel); |
| 111 |
itemWithModel.dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 112 |
assertEquals('one', comboBox.getValue()); |
| 113 |
} |
| 114 |
|
| 115 |
function testRedisplayMenuAfterBackspace() { |
| 116 |
input.value = 'mx'; |
| 117 |
comboBox.onInputEvent_(); |
| 118 |
input.value = 'm'; |
| 119 |
comboBox.onInputEvent_(); |
| 120 |
assertEquals( |
| 121 |
'Three items should be displayed', 3, |
| 122 |
comboBox.getNumberOfVisibleItems_()); |
| 123 |
} |
| 124 |
|
| 125 |
function testExternallyCreatedMenu() { |
| 126 |
var menu = new goog.ui.Menu(); |
| 127 |
menu.decorate(goog.dom.getElement('menu')); |
| 128 |
assertTrue( |
| 129 |
'Menu items should be instances of goog.ui.ComboBoxItem', |
| 130 |
menu.getChildAt(0) instanceof goog.ui.ComboBoxItem); |
| 131 |
|
| 132 |
comboBox = new goog.ui.ComboBox(null, menu); |
| 133 |
comboBox.render(goog.dom.getElement('combo')); |
| 134 |
|
| 135 |
input = goog.dom.getElementsByTagName( |
| 136 |
goog.dom.TagName.INPUT, comboBox.getElement())[0]; |
| 137 |
menu.getItemAt(2).dispatchEvent(goog.ui.Component.EventType.ACTION); |
| 138 |
assertEquals('Blue', input.value); |
| 139 |
} |
| 140 |
|
| 141 |
function testRecomputeVisibleCountAfterChangingItems() { |
| 142 |
input.value = 'Black'; |
| 143 |
comboBox.onInputEvent_(); |
| 144 |
assertEquals( |
| 145 |
'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_()); |
| 146 |
comboBox.addItem(new goog.ui.ComboBoxItem('Black')); |
| 147 |
assertEquals( |
| 148 |
'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_()); |
| 149 |
|
| 150 |
input.value = 'Red'; |
| 151 |
comboBox.onInputEvent_(); |
| 152 |
assertEquals( |
| 153 |
'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_()); |
| 154 |
comboBox.removeItemAt(0); // Red |
| 155 |
assertEquals( |
| 156 |
'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_()); |
| 157 |
} |
| 158 |
|
| 159 |
function testSetEnabled() { |
| 160 |
// By default, everything should be enabled. |
| 161 |
assertFalse('Text input should initially not be disabled', input.disabled); |
| 162 |
assertFalse( |
| 163 |
'Text input should initially not look disabled', |
| 164 |
goog.dom.classlist.contains( |
| 165 |
input, |
| 166 |
goog.getCssName( |
| 167 |
goog.ui.LabelInput.prototype.labelCssClassName, 'disabled'))); |
| 168 |
assertFalse( |
| 169 |
'Combo box should initially not look disabled', |
| 170 |
goog.dom.classlist.contains( |
| 171 |
comboBox.getElement(), goog.getCssName('goog-combobox-disabled'))); |
| 172 |
goog.testing.events.fireClickSequence(comboBox.getElement()); |
| 173 |
assertTrue( |
| 174 |
'Menu initially becomes visible after click', |
| 175 |
comboBox.getMenu().isVisible()); |
| 176 |
goog.testing.events.fireClickSequence(document); |
| 177 |
assertFalse( |
| 178 |
'Menu initially becomes invisible after document click', |
| 179 |
comboBox.getMenu().isVisible()); |
| 180 |
|
| 181 |
assertTrue(comboBox.isEnabled()); |
| 182 |
comboBox.setEnabled(false); |
| 183 |
assertFalse(comboBox.isEnabled()); |
| 184 |
assertTrue( |
| 185 |
'Text input should be disabled after being disabled', input.disabled); |
| 186 |
assertTrue( |
| 187 |
'Text input should appear disabled after being disabled', |
| 188 |
goog.dom.classlist.contains( |
| 189 |
input, |
| 190 |
goog.getCssName( |
| 191 |
goog.ui.LabelInput.prototype.labelCssClassName, 'disabled'))); |
| 192 |
assertTrue( |
| 193 |
'Combo box should appear disabled after being disabled', |
| 194 |
goog.dom.classlist.contains( |
| 195 |
comboBox.getElement(), goog.getCssName('goog-combobox-disabled'))); |
| 196 |
goog.testing.events.fireClickSequence(comboBox.getElement()); |
| 197 |
assertFalse( |
| 198 |
'Menu should not become visible after click if disabled', |
| 199 |
comboBox.getMenu().isVisible()); |
| 200 |
|
| 201 |
comboBox.setEnabled(true); |
| 202 |
assertTrue(comboBox.isEnabled()); |
| 203 |
assertFalse( |
| 204 |
'Text input should not be disabled after being re-enabled', |
| 205 |
input.disabled); |
| 206 |
assertFalse( |
| 207 |
'Text input should not appear disabled after being re-enabled', |
| 208 |
goog.dom.classlist.contains( |
| 209 |
input, |
| 210 |
goog.getCssName( |
| 211 |
goog.ui.LabelInput.prototype.labelCssClassName, 'disabled'))); |
| 212 |
assertFalse( |
| 213 |
'Combo box should not appear disabled after being re-enabled', |
| 214 |
goog.dom.classlist.contains( |
| 215 |
comboBox.getElement(), goog.getCssName('goog-combobox-disabled'))); |
| 216 |
goog.testing.events.fireClickSequence(comboBox.getElement()); |
| 217 |
assertTrue( |
| 218 |
'Menu becomes visible after click when re-enabled', |
| 219 |
comboBox.getMenu().isVisible()); |
| 220 |
goog.testing.events.fireClickSequence(document); |
| 221 |
assertFalse( |
| 222 |
'Menu becomes invisible after document click when re-enabled', |
| 223 |
comboBox.getMenu().isVisible()); |
| 224 |
} |
| 225 |
|
| 226 |
function testSetFormatFromToken() { |
| 227 |
var item = new goog.ui.ComboBoxItem('ABc'); |
| 228 |
item.setFormatFromToken('b'); |
| 229 |
var div = goog.dom.createDom(goog.dom.TagName.DIV); |
| 230 |
new goog.ui.ControlRenderer().setContent(div, item.getContent()); |
| 231 |
assertTrue(div.innerHTML == 'A<b>B</b>c' || div.innerHTML == 'A<B>B</B>c'); |
| 232 |
} |
| 233 |
|
| 234 |
function testSetValue() { |
| 235 |
var clock = new goog.testing.MockClock(/* autoInstall */ true); |
| 236 |
|
| 237 |
// Get the input focus. Note that both calls are needed to correctly |
| 238 |
// simulate the focus (and setting document.activeElement) across all |
| 239 |
// browsers. |
| 240 |
input.focus(); |
| 241 |
goog.testing.events.fireClickSequence(input); |
| 242 |
|
| 243 |
// Simulate text input. |
| 244 |
input.value = 'Black'; |
| 245 |
comboBox.onInputEvent_(); |
| 246 |
clock.tick(); |
| 247 |
assertEquals( |
| 248 |
'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_()); |
| 249 |
assertFalse('Menu should be invisible', comboBox.getMenu().isVisible()); |
| 250 |
|
| 251 |
// Programmatic change with the input focus causes the menu visibility to |
| 252 |
// change if needed. |
| 253 |
comboBox.setValue('Blue'); |
| 254 |
clock.tick(); |
| 255 |
assertTrue('Menu should be visible1', comboBox.getMenu().isVisible()); |
| 256 |
assertEquals( |
| 257 |
'One item should be displayed', 1, comboBox.getNumberOfVisibleItems_()); |
| 258 |
|
| 259 |
// Simulate user input to ensure all the items are invisible again, then |
| 260 |
// blur away. |
| 261 |
input.value = 'Black'; |
| 262 |
comboBox.onInputEvent_(); |
| 263 |
clock.tick(); |
| 264 |
input.blur(); |
| 265 |
document.body.focus(); |
| 266 |
clock.tick(goog.ui.ComboBox.BLUR_DISMISS_TIMER_MS); |
| 267 |
assertEquals( |
| 268 |
'No items should be displayed', 0, comboBox.getNumberOfVisibleItems_()); |
| 269 |
assertFalse('Menu should be invisible', comboBox.getMenu().isVisible()); |
| 270 |
|
| 271 |
// Programmatic change without the input focus does not pop up the menu, |
| 272 |
// but still updates the list of visible items within it. |
| 273 |
comboBox.setValue('Blue'); |
| 274 |
clock.tick(); |
| 275 |
assertFalse('Menu should be invisible', comboBox.getMenu().isVisible()); |
| 276 |
assertEquals( |
| 277 |
'Menu should contain one item', 1, comboBox.getNumberOfVisibleItems_()); |
| 278 |
|
| 279 |
// Click on the combobox. The entire menu becomes visible, the last item |
| 280 |
// (programmatically) set is highlighted. |
| 281 |
goog.testing.events.fireClickSequence(comboBox.getElement()); |
| 282 |
assertTrue('Menu should be visible2', comboBox.getMenu().isVisible()); |
| 283 |
assertEquals( |
| 284 |
'All items should be displayed', comboBox.getMenu().getItemCount(), |
| 285 |
comboBox.getNumberOfVisibleItems_()); |
| 286 |
assertEquals( |
| 287 |
'The last item set should be highlighted', |
| 288 |
/* Blue= */ 3, comboBox.getMenu().getHighlightedIndex()); |
| 289 |
|
| 290 |
clock.uninstall(); |
| 291 |
} |