| 1 |
// Copyright 2007 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.acTest'); |
| 16 |
goog.setTestOnly('goog.ui.acTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.asserts'); |
| 20 |
goog.require('goog.dom'); |
| 21 |
goog.require('goog.dom.NodeType'); |
| 22 |
goog.require('goog.dom.classlist'); |
| 23 |
goog.require('goog.dom.selection'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.BrowserEvent'); |
| 26 |
goog.require('goog.events.Event'); |
| 27 |
goog.require('goog.events.EventType'); |
| 28 |
goog.require('goog.events.KeyCodes'); |
| 29 |
goog.require('goog.style'); |
| 30 |
goog.require('goog.testing.MockClock'); |
| 31 |
goog.require('goog.testing.jsunit'); |
| 32 |
goog.require('goog.ui.ac'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
var autocomplete; |
| 35 |
var data = ['ab', 'aab', 'aaab']; |
| 36 |
var input; |
| 37 |
var mockClock; |
| 38 |
|
| 39 |
function setUpPage() { |
| 40 |
goog.ui.ac.createSimpleAutoComplete( |
| 41 |
data, goog.dom.getElement('user'), true, false); |
| 42 |
} |
| 43 |
|
| 44 |
function setUp() { |
| 45 |
mockClock = new goog.testing.MockClock(true); |
| 46 |
input = goog.dom.getElement('input'); |
| 47 |
input.value = ''; |
| 48 |
autocomplete = goog.ui.ac.createSimpleAutoComplete(data, input, true, false); |
| 49 |
} |
| 50 |
|
| 51 |
function tearDown() { |
| 52 |
autocomplete.dispose(); |
| 53 |
mockClock.dispose(); |
| 54 |
} |
| 55 |
|
| 56 |
//========================================================================= |
| 57 |
// Utility methods |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Fire listeners of a given type that are listening to the event's |
| 62 |
* currentTarget. |
| 63 |
* |
| 64 |
* @param {goog.events.BrowserEvent} event |
| 65 |
*/ |
| 66 |
function simulateEvent(event) { |
| 67 |
goog.events.fireListeners(event.currentTarget, event.type, true, event); |
| 68 |
goog.events.fireListeners(event.currentTarget, event.type, false, event); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
/** |
| 73 |
* Fire all key event listeners that are listening to the input element. |
| 74 |
* |
| 75 |
* @param {number} keyCode The key code. |
| 76 |
*/ |
| 77 |
function simulateAllKeyEventsOnInput(keyCode) { |
| 78 |
var eventTypes = [ |
| 79 |
goog.events.EventType.KEYDOWN, goog.events.EventType.KEYPRESS, |
| 80 |
goog.events.EventType.KEYUP |
| 81 |
]; |
| 82 |
|
| 83 |
goog.array.forEach(eventTypes, function(type) { |
| 84 |
var event = new goog.events.Event(type, input); |
| 85 |
event.keyCode = keyCode; |
| 86 |
simulateEvent(new goog.events.BrowserEvent(event, input)); |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* @param {string} text |
| 93 |
* @return {Node} Node whose inner text maches the given text. |
| 94 |
*/ |
| 95 |
function findNodeByInnerText(text) { |
| 96 |
return goog.dom.findNode(document.body, function(node) { |
| 97 |
try { |
| 98 |
var display = goog.userAgent.IE ? |
| 99 |
goog.style.getCascadedStyle(node, 'display') : |
| 100 |
goog.style.getComputedStyle(node, 'display'); |
| 101 |
|
| 102 |
return goog.dom.getRawTextContent(node) == text && 'none' != display && |
| 103 |
node.nodeType == goog.dom.NodeType.ELEMENT; |
| 104 |
} catch (e) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
}); |
| 108 |
} |
| 109 |
|
| 110 |
//========================================================================= |
| 111 |
// Tests |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Ensure that the display of the autocompleter works. |
| 116 |
*/ |
| 117 |
function testBasicDisplay() { |
| 118 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.DOWN); |
| 119 |
|
| 120 |
input.value = 'a'; |
| 121 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.A); |
| 122 |
mockClock.tick(500); |
| 123 |
|
| 124 |
var nodes = [ |
| 125 |
findNodeByInnerText(data[0]), findNodeByInnerText(data[1]), |
| 126 |
findNodeByInnerText(data[2]) |
| 127 |
]; |
| 128 |
assert(!!nodes[0]); |
| 129 |
assert(!!nodes[1]); |
| 130 |
assert(!!nodes[2]); |
| 131 |
assert(goog.style.isUnselectable(nodes[0])); |
| 132 |
assert(goog.style.isUnselectable(nodes[1])); |
| 133 |
assert(goog.style.isUnselectable(nodes[2])); |
| 134 |
|
| 135 |
input.value = 'aa'; |
| 136 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.A); |
| 137 |
mockClock.tick(500); |
| 138 |
|
| 139 |
assertFalse(!!findNodeByInnerText(data[0])); |
| 140 |
assert(!!findNodeByInnerText(data[1])); |
| 141 |
assert(!!findNodeByInnerText(data[2])); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Ensure that key navigation with multiple inputs work |
| 147 |
*/ |
| 148 |
function testKeyNavigation() { |
| 149 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.DOWN); |
| 150 |
|
| 151 |
input.value = 'c, a'; |
| 152 |
goog.dom.selection.setCursorPosition(input, 'c, a'.length); |
| 153 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.A); |
| 154 |
mockClock.tick(500); |
| 155 |
|
| 156 |
assert(document.body.innerHTML, !!findNodeByInnerText(data[1])); |
| 157 |
assert(!!findNodeByInnerText(data[2])); |
| 158 |
|
| 159 |
var selected = goog.asserts.assertElement(findNodeByInnerText(data[0])); |
| 160 |
assertTrue( |
| 161 |
'Should have new standard active class', |
| 162 |
goog.dom.classlist.contains(selected, 'ac-active')); |
| 163 |
assertTrue( |
| 164 |
'Should have legacy active class', |
| 165 |
goog.dom.classlist.contains(selected, 'active')); |
| 166 |
|
| 167 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.DOWN); |
| 168 |
assertFalse( |
| 169 |
goog.dom.classlist.contains( |
| 170 |
goog.asserts.assertElement(findNodeByInnerText(data[0])), |
| 171 |
'ac-active')); |
| 172 |
assert( |
| 173 |
goog.dom.classlist.contains( |
| 174 |
goog.asserts.assertElement(findNodeByInnerText(data[1])), |
| 175 |
'ac-active')); |
| 176 |
|
| 177 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.ENTER); |
| 178 |
assertEquals('c, aab, ', input.value); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* Ensure that mouse navigation with multiple inputs works. |
| 184 |
*/ |
| 185 |
function testMouseNavigation() { |
| 186 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.DOWN); |
| 187 |
|
| 188 |
input.value = 'c, a'; |
| 189 |
goog.dom.selection.setCursorPosition(input, 'c, a'.length); |
| 190 |
simulateAllKeyEventsOnInput(goog.events.KeyCodes.A); |
| 191 |
mockClock.tick(500); |
| 192 |
|
| 193 |
var secondOption = goog.asserts.assertElement(findNodeByInnerText(data[1])); |
| 194 |
var parent = secondOption.parentNode; |
| 195 |
assertFalse(goog.dom.classlist.contains(secondOption, 'ac-active')); |
| 196 |
|
| 197 |
var mouseOver = |
| 198 |
new goog.events.Event(goog.events.EventType.MOUSEOVER, secondOption); |
| 199 |
simulateEvent(new goog.events.BrowserEvent(mouseOver, parent)); |
| 200 |
assert(goog.dom.classlist.contains(secondOption, 'ac-active')); |
| 201 |
|
| 202 |
var mouseDown = |
| 203 |
new goog.events.Event(goog.events.EventType.MOUSEDOWN, secondOption); |
| 204 |
simulateEvent(new goog.events.BrowserEvent(mouseDown, parent)); |
| 205 |
var mouseClick = |
| 206 |
new goog.events.Event(goog.events.EventType.CLICK, secondOption); |
| 207 |
simulateEvent(new goog.events.BrowserEvent(mouseClick, parent)); |
| 208 |
|
| 209 |
assertEquals('c, aab, ', input.value); |
| 210 |
} |