// 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('GreBc' || div.innerHTML == 'ABc'); } 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(); }