| 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.ac.InputHandlerTest'); |
| 16 |
goog.setTestOnly('goog.ui.ac.InputHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria'); |
| 19 |
goog.require('goog.a11y.aria.Role'); |
| 20 |
goog.require('goog.a11y.aria.State'); |
| 21 |
goog.require('goog.dom'); |
| 22 |
goog.require('goog.dom.TagName'); |
| 23 |
goog.require('goog.dom.selection'); |
| 24 |
goog.require('goog.events.BrowserEvent'); |
| 25 |
goog.require('goog.events.Event'); |
| 26 |
goog.require('goog.events.EventTarget'); |
| 27 |
goog.require('goog.events.KeyCodes'); |
| 28 |
goog.require('goog.functions'); |
| 29 |
goog.require('goog.object'); |
| 30 |
goog.require('goog.testing.MockClock'); |
| 31 |
goog.require('goog.testing.jsunit'); |
| 32 |
goog.require('goog.ui.ac.InputHandler'); |
| 33 |
goog.require('goog.userAgent'); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Mock out the input element. |
| 39 |
* @constructor |
| 40 |
*/ |
| 41 |
function MockElement() { |
| 42 |
goog.events.EventTarget.call(this); |
| 43 |
this.setAttributeNS = function() {}; |
| 44 |
this.setAttribute = function(key, value) { this[key] = value; }; |
| 45 |
this.focus = function() {}; |
| 46 |
this.blur = function() {}; |
| 47 |
this.ownerDocument = document; |
| 48 |
this.selectionStart = 0; |
| 49 |
} |
| 50 |
goog.inherits(MockElement, goog.events.EventTarget); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* @constructor |
| 56 |
*/ |
| 57 |
function MockAutoCompleter() { |
| 58 |
this.setToken = null; |
| 59 |
this.setTokenWasCalled = false; |
| 60 |
this.selectHilitedWasCalled = false; |
| 61 |
this.dismissWasCalled = false; |
| 62 |
this.getTarget = function() { return mockElement }; |
| 63 |
this.setTarget = function() {}; |
| 64 |
this.setToken = function(token) { |
| 65 |
this.setTokenWasCalled = true; |
| 66 |
this.setToken = token; |
| 67 |
}; |
| 68 |
this.selectHilited = function() { |
| 69 |
this.selectHilitedWasCalled = true; |
| 70 |
return true; // Success. |
| 71 |
}; |
| 72 |
this.cancelDelayedDismiss = function() {}; |
| 73 |
this.dismissOnDelay = function() {}; |
| 74 |
this.dismiss = function() { this.dismissWasCalled = true; }; |
| 75 |
this.isOpen = goog.functions.TRUE; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* MockInputHandler simulates key events for testing the IME behavior of |
| 82 |
* InputHandler. |
| 83 |
* @constructor |
| 84 |
*/ |
| 85 |
function MockInputHandler() { |
| 86 |
goog.ui.ac.InputHandler.call(this); |
| 87 |
|
| 88 |
this.ac_ = new MockAutoCompleter(); |
| 89 |
this.cursorPosition_ = 0; |
| 90 |
|
| 91 |
this.attachInput(mockElement); |
| 92 |
} |
| 93 |
goog.inherits(MockInputHandler, goog.ui.ac.InputHandler); |
| 94 |
|
| 95 |
|
| 96 |
/** Checks for updates to the text area, should not happen during IME. */ |
| 97 |
MockInputHandler.prototype.update = function() { |
| 98 |
this.updates++; |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
/** Simulates key events. */ |
| 103 |
MockInputHandler.prototype.fireKeyEvents = function( |
| 104 |
keyCode, down, press, up, opt_properties) { |
| 105 |
if (down) this.fireEvent('keydown', keyCode, opt_properties); |
| 106 |
if (press) this.fireEvent('keypress', keyCode, opt_properties); |
| 107 |
if (up) this.fireEvent('keyup', keyCode, opt_properties); |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
/** Simulates an event. */ |
| 112 |
MockInputHandler.prototype.fireEvent = function(type, keyCode, opt_properties) { |
| 113 |
var e = {}; |
| 114 |
e.type = type; |
| 115 |
e.keyCode = keyCode; |
| 116 |
e.preventDefault = function() {}; |
| 117 |
if (!goog.userAgent.IE) { |
| 118 |
e.which = type == 'keydown' ? keyCode : 0; |
| 119 |
} |
| 120 |
if (opt_properties) { |
| 121 |
goog.object.extend(e, opt_properties); |
| 122 |
} |
| 123 |
e = new goog.events.BrowserEvent(e); |
| 124 |
mockElement.dispatchEvent(e); |
| 125 |
}; |
| 126 |
|
| 127 |
MockInputHandler.prototype.setCursorPosition = function(cursorPosition) { |
| 128 |
this.cursorPosition_ = cursorPosition; |
| 129 |
}; |
| 130 |
|
| 131 |
MockInputHandler.prototype.getCursorPosition = function() { |
| 132 |
return this.cursorPosition_; |
| 133 |
}; |
| 134 |
|
| 135 |
// Variables used by all test |
| 136 |
var mh = null; |
| 137 |
var oldMac, oldWin, oldLinux, oldIe, oldFf, oldWebkit, oldVersion; |
| 138 |
var oldUsesKeyDown; |
| 139 |
var mockElement; |
| 140 |
var mockClock; |
| 141 |
|
| 142 |
function setUp() { |
| 143 |
oldMac = goog.userAgent.MAC; |
| 144 |
oldWin = goog.userAgent.WINDOWS; |
| 145 |
oldLinux = goog.userAgent.LINUX; |
| 146 |
oldIe = goog.userAgent.IE; |
| 147 |
oldFf = goog.userAgent.GECKO; |
| 148 |
oldWebkit = goog.userAgent.WEBKIT; |
| 149 |
oldVersion = goog.userAgent.VERSION; |
| 150 |
oldUsesKeyDown = goog.events.KeyHandler.USES_KEYDOWN_; |
| 151 |
mockClock = new goog.testing.MockClock(true); |
| 152 |
mockElement = new MockElement; |
| 153 |
mh = new MockInputHandler; |
| 154 |
} |
| 155 |
|
| 156 |
function tearDown() { |
| 157 |
goog.userAgent.MAC = oldMac; |
| 158 |
goog.userAgent.WINDOWS = oldWin; |
| 159 |
goog.userAgent.LINUX = oldLinux; |
| 160 |
goog.userAgent.IE = oldIe; |
| 161 |
goog.userAgent.GECKO = oldFf; |
| 162 |
goog.userAgent.WEBKIT = oldWebkit; |
| 163 |
goog.userAgent.VERSION = oldVersion; |
| 164 |
goog.events.KeyHandler.USES_KEYDOWN_ = oldUsesKeyDown; |
| 165 |
mockClock.dispose(); |
| 166 |
mockElement.dispose(); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** Used to simulate behavior of Windows/Firefox3 */ |
| 171 |
function simulateWinFirefox3() { |
| 172 |
goog.userAgent.MAC = false; |
| 173 |
goog.userAgent.WINDOWS = true; |
| 174 |
goog.userAgent.LINUX = false; |
| 175 |
goog.userAgent.IE = false; |
| 176 |
goog.userAgent.EDGE = false; |
| 177 |
goog.userAgent.EDGE_OR_IE = false; |
| 178 |
goog.userAgent.GECKO = true; |
| 179 |
goog.userAgent.WEBKIT = false; |
| 180 |
goog.events.KeyHandler.USES_KEYDOWN_ = false; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** Used to simulate behavior of Windows/InternetExplorer7 */ |
| 185 |
function simulateWinIe7() { |
| 186 |
goog.userAgent.MAC = false; |
| 187 |
goog.userAgent.WINDOWS = true; |
| 188 |
goog.userAgent.LINUX = false; |
| 189 |
goog.userAgent.IE = true; |
| 190 |
goog.userAgent.EDGE = false; |
| 191 |
goog.userAgent.EDGE_OR_IE = true; |
| 192 |
goog.userAgent.DOCUMENT_MODE = 7; |
| 193 |
goog.userAgent.GECKO = false; |
| 194 |
goog.userAgent.WEBKIT = false; |
| 195 |
goog.events.KeyHandler.USES_KEYDOWN_ = true; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
/** Used to simulate behavior of Windows/Chrome */ |
| 200 |
function simulateWinChrome() { |
| 201 |
goog.userAgent.MAC = false; |
| 202 |
goog.userAgent.WINDOWS = true; |
| 203 |
goog.userAgent.LINUX = false; |
| 204 |
goog.userAgent.IE = false; |
| 205 |
goog.userAgent.EDGE = false; |
| 206 |
goog.userAgent.EDGE_OR_IE = false; |
| 207 |
goog.userAgent.GECKO = false; |
| 208 |
goog.userAgent.WEBKIT = true; |
| 209 |
goog.userAgent.VERSION = '525'; |
| 210 |
goog.events.KeyHandler.USES_KEYDOWN_ = true; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
/** Used to simulate behavior of Mac/Firefox3 */ |
| 215 |
function simulateMacFirefox3() { |
| 216 |
goog.userAgent.MAC = true; |
| 217 |
goog.userAgent.WINDOWS = false; |
| 218 |
goog.userAgent.LINUX = false; |
| 219 |
goog.userAgent.IE = false; |
| 220 |
goog.userAgent.EDGE = false; |
| 221 |
goog.userAgent.EDGE_OR_IE = false; |
| 222 |
goog.userAgent.GECKO = true; |
| 223 |
goog.userAgent.WEBKIT = false; |
| 224 |
goog.events.KeyHandler.USES_KEYDOWN_ = true; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/** Used to simulate behavior of Mac/Safari3 */ |
| 229 |
function simulateMacSafari3() { |
| 230 |
goog.userAgent.MAC = true; |
| 231 |
goog.userAgent.WINDOWS = false; |
| 232 |
goog.userAgent.LINUX = false; |
| 233 |
goog.userAgent.IE = false; |
| 234 |
goog.userAgent.EDGE = false; |
| 235 |
goog.userAgent.EDGE_OR_IE = false; |
| 236 |
goog.userAgent.GECKO = false; |
| 237 |
goog.userAgent.WEBKIT = true; |
| 238 |
goog.userAgent.VERSION = '525'; |
| 239 |
goog.events.KeyHandler.USES_KEYDOWN_ = true; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/** Used to simulate behavior of Linux/Firefox3 */ |
| 244 |
function simulateLinuxFirefox3() { |
| 245 |
goog.userAgent.MAC = false; |
| 246 |
goog.userAgent.WINDOWS = false; |
| 247 |
goog.userAgent.LINUX = true; |
| 248 |
goog.userAgent.IE = false; |
| 249 |
goog.userAgent.EDGE = false; |
| 250 |
goog.userAgent.EDGE_OR_IE = false; |
| 251 |
goog.userAgent.GECKO = true; |
| 252 |
goog.userAgent.WEBKIT = false; |
| 253 |
goog.events.KeyHandler.USES_KEYDOWN_ = true; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/** Test the normal, non-IME case */ |
| 258 |
function testRegularKey() { |
| 259 |
// Each key fires down, press, and up in that order, and each should |
| 260 |
// trigger an autocomplete update |
| 261 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 262 |
|
| 263 |
mh.fireKeyEvents(goog.events.KeyCodes.K, true, true, true); |
| 264 |
assertFalse('IME should not be triggered by K', mh.waitingForIme_); |
| 265 |
|
| 266 |
mh.fireKeyEvents(goog.events.KeyCodes.A, true, true, true); |
| 267 |
assertFalse('IME should not be triggered by A', mh.waitingForIme_); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* This test simulates the key inputs generated by pressing |
| 273 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 274 |
* on Windows/Firefox3. |
| 275 |
*/ |
| 276 |
function testImeWinFirefox3() { |
| 277 |
simulateWinFirefox3(); |
| 278 |
mh.fireEvent('focus', ''); |
| 279 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 280 |
|
| 281 |
// ime_on |
| 282 |
|
| 283 |
// a |
| 284 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 285 |
// Event is not generated for key code a. |
| 286 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 287 |
|
| 288 |
// enter |
| 289 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, false, false, true); |
| 290 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 291 |
|
| 292 |
// i |
| 293 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 294 |
// Event is not generated for key code i. |
| 295 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 296 |
|
| 297 |
// ime_off |
| 298 |
|
| 299 |
// u |
| 300 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 301 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 302 |
|
| 303 |
mh.fireEvent('blur', ''); |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
/** |
| 308 |
* This test simulates the key inputs generated by pressing |
| 309 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 310 |
* on Windows/InternetExplorer7. |
| 311 |
*/ |
| 312 |
function testImeWinIe7() { |
| 313 |
simulateWinIe7(); |
| 314 |
mh.fireEvent('focus', ''); |
| 315 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 316 |
|
| 317 |
// ime_on |
| 318 |
|
| 319 |
// a |
| 320 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 321 |
mh.fireKeyEvents(goog.events.KeyCodes.A, false, false, true); |
| 322 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 323 |
|
| 324 |
// enter |
| 325 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 326 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, false, false, true); |
| 327 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 328 |
|
| 329 |
// i |
| 330 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 331 |
mh.fireKeyEvents(goog.events.KeyCodes.I, false, false, true); |
| 332 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 333 |
|
| 334 |
// ime_off |
| 335 |
|
| 336 |
// u |
| 337 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 338 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 339 |
|
| 340 |
mh.fireEvent('blur', ''); |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
/** |
| 345 |
* This test simulates the key inputs generated by pressing |
| 346 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 347 |
* on Windows/Chrome. |
| 348 |
*/ |
| 349 |
function testImeWinChrome() { |
| 350 |
simulateWinChrome(); |
| 351 |
mh.fireEvent('focus', ''); |
| 352 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 353 |
|
| 354 |
// ime_on |
| 355 |
|
| 356 |
// a |
| 357 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 358 |
mh.fireKeyEvents(goog.events.KeyCodes.A, false, false, true); |
| 359 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 360 |
|
| 361 |
// enter |
| 362 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 363 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, false, false, true); |
| 364 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 365 |
|
| 366 |
// i |
| 367 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 368 |
mh.fireKeyEvents(goog.events.KeyCodes.I, false, false, true); |
| 369 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 370 |
|
| 371 |
// ime_off |
| 372 |
|
| 373 |
// u |
| 374 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 375 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 376 |
|
| 377 |
mh.fireEvent('blur', ''); |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
/** |
| 382 |
* This test simulates the key inputs generated by pressing |
| 383 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 384 |
* on Mac/Firefox3. |
| 385 |
*/ |
| 386 |
function testImeMacFirefox3() { |
| 387 |
// TODO(user): Currently our code cannot distinguish preedit characters |
| 388 |
// from normal ones for Mac/Firefox3. |
| 389 |
// Enable this test after we fix it. |
| 390 |
|
| 391 |
simulateMacFirefox3(); |
| 392 |
mh.fireEvent('focus', ''); |
| 393 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 394 |
|
| 395 |
// ime_on |
| 396 |
|
| 397 |
// a |
| 398 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 399 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 400 |
mh.fireKeyEvents(goog.events.KeyCodes.A, true, false, true); |
| 401 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 402 |
|
| 403 |
// enter |
| 404 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, true, true, true); |
| 405 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 406 |
|
| 407 |
// i |
| 408 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 409 |
mh.fireKeyEvents(goog.events.KeyCodes.I, true, false, true); |
| 410 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 411 |
|
| 412 |
// ime_off |
| 413 |
|
| 414 |
// u |
| 415 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 416 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 417 |
|
| 418 |
mh.fireEvent('blur', ''); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
/** |
| 423 |
* This test simulates the key inputs generated by pressing |
| 424 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 425 |
* on Mac/Safari3. |
| 426 |
*/ |
| 427 |
function testImeMacSafari3() { |
| 428 |
simulateMacSafari3(); |
| 429 |
mh.fireEvent('focus', ''); |
| 430 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 431 |
|
| 432 |
// ime_on |
| 433 |
|
| 434 |
// a |
| 435 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 436 |
mh.fireKeyEvents(goog.events.KeyCodes.A, false, false, true); |
| 437 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 438 |
|
| 439 |
// enter |
| 440 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 441 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, false, false, true); |
| 442 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 443 |
|
| 444 |
// i |
| 445 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, false, false); |
| 446 |
mh.fireKeyEvents(goog.events.KeyCodes.I, false, false, true); |
| 447 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 448 |
|
| 449 |
// ime_off |
| 450 |
|
| 451 |
// u |
| 452 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 453 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 454 |
|
| 455 |
mh.fireEvent('blur', ''); |
| 456 |
} |
| 457 |
|
| 458 |
|
| 459 |
/** |
| 460 |
* This test simulates the key inputs generated by pressing |
| 461 |
* '<ime_on>a<enter>i<ime_off>u' using the Japanese IME |
| 462 |
* on Linux/Firefox3. |
| 463 |
*/ |
| 464 |
function testImeLinuxFirefox3() { |
| 465 |
// TODO(user): Currently our code cannot distinguish preedit characters |
| 466 |
// from normal ones for Linux/Firefox3. |
| 467 |
// Enable this test after we fix it. |
| 468 |
|
| 469 |
|
| 470 |
simulateLinuxFirefox3(); |
| 471 |
mh.fireEvent('focus', ''); |
| 472 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 473 |
|
| 474 |
// ime_on |
| 475 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 476 |
|
| 477 |
// a |
| 478 |
mh.fireKeyEvents(goog.events.KeyCodes.A, true, false, true); |
| 479 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 480 |
|
| 481 |
// enter |
| 482 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, true, true, true); |
| 483 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 484 |
|
| 485 |
// i |
| 486 |
mh.fireKeyEvents(goog.events.KeyCodes.WIN_IME, true, true, false); |
| 487 |
mh.fireKeyEvents(goog.events.KeyCodes.I, true, false, true); |
| 488 |
assertTrue('IME should be triggered', mh.waitingForIme_); |
| 489 |
|
| 490 |
// ime_off |
| 491 |
|
| 492 |
// u |
| 493 |
mh.fireKeyEvents(goog.events.KeyCodes.U, true, true, true); |
| 494 |
assertFalse('IME should not be triggered', mh.waitingForIme_); |
| 495 |
|
| 496 |
mh.fireEvent('blur', ''); |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
/** |
| 501 |
* Check attaching to an EventTarget instead of an element. |
| 502 |
*/ |
| 503 |
function testAttachEventTarget() { |
| 504 |
var target = new goog.events.EventTarget(); |
| 505 |
|
| 506 |
assertNull(mh.activeElement_); |
| 507 |
mh.attachInput(target); |
| 508 |
assertNull(mh.activeElement_); |
| 509 |
|
| 510 |
mockElement.dispatchEvent(new goog.events.Event('focus', mockElement)); |
| 511 |
assertEquals(mockElement, mh.activeElement_); |
| 512 |
|
| 513 |
mh.detachInput(target); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
/** |
| 518 |
* Make sure that the active element handling works. |
| 519 |
*/ |
| 520 |
function testActiveElement() { |
| 521 |
assertNull(mh.activeElement_); |
| 522 |
|
| 523 |
mockElement.dispatchEvent('keydown'); |
| 524 |
assertEquals(mockElement, mh.activeElement_); |
| 525 |
|
| 526 |
mockElement.dispatchEvent('blur'); |
| 527 |
assertNull(mh.activeElement_); |
| 528 |
|
| 529 |
mockElement.dispatchEvent('focus'); |
| 530 |
assertEquals(mockElement, mh.activeElement_); |
| 531 |
|
| 532 |
mh.detachInput(mockElement); |
| 533 |
assertNull(mh.activeElement_); |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/** |
| 538 |
* We can attach an EventTarget that isn't an element. |
| 539 |
*/ |
| 540 |
function testAttachEventTarget() { |
| 541 |
var target = new goog.events.EventTarget(); |
| 542 |
|
| 543 |
assertNull(mh.activeElement_); |
| 544 |
mh.attachInput(target); |
| 545 |
assertNull(mh.activeElement_); |
| 546 |
|
| 547 |
target.dispatchEvent(new goog.events.Event('focus', mockElement)); |
| 548 |
assertEquals(mockElement, mh.activeElement_); |
| 549 |
|
| 550 |
mh.detachInput(target); |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
/** |
| 555 |
* Make sure an already-focused element becomes active immediately. |
| 556 |
*/ |
| 557 |
function testActiveElementAlreadyFocused() { |
| 558 |
var element = document.getElementById('textInput'); |
| 559 |
element.style.display = ''; |
| 560 |
element.focus(); |
| 561 |
|
| 562 |
assertNull(mh.activeElement_); |
| 563 |
|
| 564 |
mh.attachInput(element); |
| 565 |
assertEquals(element, mh.activeElement_); |
| 566 |
|
| 567 |
mh.detachInput(element); |
| 568 |
element.style.display = 'none'; |
| 569 |
} |
| 570 |
|
| 571 |
function testUpdateDoesNotTriggerSetTokenForSelectRow() { |
| 572 |
var ih = new goog.ui.ac.InputHandler(); |
| 573 |
|
| 574 |
// Set up our input handler with the necessary mocks |
| 575 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 576 |
ih.ac_ = mockAutoCompleter; |
| 577 |
ih.activeElement_ = mockElement; |
| 578 |
|
| 579 |
var row = {}; |
| 580 |
ih.selectRow(row, false); |
| 581 |
|
| 582 |
ih.update(); |
| 583 |
assertFalse( |
| 584 |
'update should not call setToken on selectRow', |
| 585 |
mockAutoCompleter.setTokenWasCalled); |
| 586 |
|
| 587 |
ih.update(); |
| 588 |
assertFalse( |
| 589 |
'update should not call setToken on selectRow', |
| 590 |
mockAutoCompleter.setTokenWasCalled); |
| 591 |
} |
| 592 |
|
| 593 |
function testSetTokenText() { |
| 594 |
var ih = new MockInputHandler(); |
| 595 |
|
| 596 |
// Set up our input handler with the necessary mocks |
| 597 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 598 |
ih.ac_ = mockAutoCompleter; |
| 599 |
ih.activeElement_ = mockElement; |
| 600 |
mockElement.value = 'bob, wal, joey'; |
| 601 |
ih.setCursorPosition(8); |
| 602 |
|
| 603 |
ih.setTokenText('waldo', true /* multi-row */); |
| 604 |
|
| 605 |
assertEquals('bob, waldo, joey', mockElement.value); |
| 606 |
} |
| 607 |
|
| 608 |
function testSetTokenTextLeftHandSideOfToken() { |
| 609 |
var ih = new MockInputHandler(); |
| 610 |
ih.setSeparators(' '); |
| 611 |
ih.setWhitespaceWrapEntries(false); |
| 612 |
|
| 613 |
// Set up our input handler with the necessary mocks |
| 614 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 615 |
ih.ac_ = mockAutoCompleter; |
| 616 |
ih.activeElement_ = mockElement; |
| 617 |
mockElement.value = 'foo bar'; |
| 618 |
// Sets cursor position right before 'bar' |
| 619 |
ih.setCursorPosition(4); |
| 620 |
|
| 621 |
ih.setTokenText('bar', true /* multi-row */); |
| 622 |
|
| 623 |
assertEquals('foo bar ', mockElement.value); |
| 624 |
} |
| 625 |
|
| 626 |
function testEmptyTokenWithSeparator() { |
| 627 |
var ih = new goog.ui.ac.InputHandler(); |
| 628 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 629 |
ih.ac_ = mockAutoCompleter; |
| 630 |
ih.activeElement_ = mockElement; |
| 631 |
mockElement.value = ', ,'; |
| 632 |
// Sets cursor position before the second comma |
| 633 |
goog.dom.selection.setStart(mockElement, 2); |
| 634 |
|
| 635 |
ih.update(); |
| 636 |
assertTrue( |
| 637 |
'update should call setToken on selectRow', |
| 638 |
mockAutoCompleter.setTokenWasCalled); |
| 639 |
assertEquals( |
| 640 |
'update should be called with empty string', '', |
| 641 |
mockAutoCompleter.setToken); |
| 642 |
} |
| 643 |
|
| 644 |
function testNonEmptyTokenWithSeparator() { |
| 645 |
var ih = new goog.ui.ac.InputHandler(); |
| 646 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 647 |
ih.ac_ = mockAutoCompleter; |
| 648 |
ih.activeElement_ = mockElement; |
| 649 |
mockElement.value = ', joe ,'; |
| 650 |
// Sets cursor position before the second comma |
| 651 |
goog.dom.selection.setStart(mockElement, 5); |
| 652 |
|
| 653 |
ih.update(); |
| 654 |
assertTrue( |
| 655 |
'update should call setToken on selectRow', |
| 656 |
mockAutoCompleter.setTokenWasCalled); |
| 657 |
assertEquals( |
| 658 |
'update should be called with expected string', 'joe', |
| 659 |
mockAutoCompleter.setToken); |
| 660 |
} |
| 661 |
|
| 662 |
function testGetThrottleTime() { |
| 663 |
var ih = new goog.ui.ac.InputHandler(); |
| 664 |
ih.setThrottleTime(999); |
| 665 |
assertEquals('throttle time set+get', 999, ih.getThrottleTime()); |
| 666 |
} |
| 667 |
|
| 668 |
function testGetUpdateDuringTyping() { |
| 669 |
var ih = new goog.ui.ac.InputHandler(); |
| 670 |
ih.setUpdateDuringTyping(false); |
| 671 |
assertFalse('update during typing set+get', ih.getUpdateDuringTyping()); |
| 672 |
} |
| 673 |
|
| 674 |
function testEnterToSelect() { |
| 675 |
mh.fireEvent('focus', ''); |
| 676 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, true, true, true); |
| 677 |
assertTrue('Should hilite', mh.ac_.selectHilitedWasCalled); |
| 678 |
assertFalse('Should NOT be dismissed', mh.ac_.dismissWasCalled); |
| 679 |
} |
| 680 |
|
| 681 |
function testEnterDoesNotSelectWhenClosed() { |
| 682 |
mh.fireEvent('focus', ''); |
| 683 |
mh.ac_.isOpen = goog.functions.FALSE; |
| 684 |
mh.fireKeyEvents(goog.events.KeyCodes.ENTER, true, true, true); |
| 685 |
assertFalse('Should NOT hilite', mh.ac_.selectHilitedWasCalled); |
| 686 |
assertTrue('Should be dismissed', mh.ac_.dismissWasCalled); |
| 687 |
} |
| 688 |
|
| 689 |
function testTabToSelect() { |
| 690 |
mh.fireEvent('focus', ''); |
| 691 |
mh.fireKeyEvents(goog.events.KeyCodes.TAB, true, true, true); |
| 692 |
assertTrue('Should hilite', mh.ac_.selectHilitedWasCalled); |
| 693 |
assertFalse('Should NOT be dismissed', mh.ac_.dismissWasCalled); |
| 694 |
} |
| 695 |
|
| 696 |
function testTabDoesNotSelectWhenClosed() { |
| 697 |
mh.fireEvent('focus', ''); |
| 698 |
mh.ac_.isOpen = goog.functions.FALSE; |
| 699 |
mh.fireKeyEvents(goog.events.KeyCodes.TAB, true, true, true); |
| 700 |
assertFalse('Should NOT hilite', mh.ac_.selectHilitedWasCalled); |
| 701 |
assertTrue('Should be dismissed', mh.ac_.dismissWasCalled); |
| 702 |
} |
| 703 |
|
| 704 |
function testShiftTabDoesNotSelect() { |
| 705 |
mh.fireEvent('focus', ''); |
| 706 |
mh.ac_.isOpen = goog.functions.TRUE; |
| 707 |
mh.fireKeyEvents( |
| 708 |
goog.events.KeyCodes.TAB, true, true, true, {shiftKey: true}); |
| 709 |
assertFalse('Should NOT hilite', mh.ac_.selectHilitedWasCalled); |
| 710 |
assertTrue('Should be dismissed', mh.ac_.dismissWasCalled); |
| 711 |
} |
| 712 |
|
| 713 |
function testEmptySeparatorUsesDefaults() { |
| 714 |
var inputHandler = new goog.ui.ac.InputHandler(''); |
| 715 |
assertFalse(inputHandler.separatorCheck_.test('')); |
| 716 |
assertFalse(inputHandler.separatorCheck_.test('x')); |
| 717 |
assertTrue(inputHandler.separatorCheck_.test(',')); |
| 718 |
} |
| 719 |
|
| 720 |
function testMultipleSeparatorUsesEmptyDefaults() { |
| 721 |
var inputHandler = new goog.ui.ac.InputHandler(',\n', null, true); |
| 722 |
inputHandler.setWhitespaceWrapEntries(false); |
| 723 |
inputHandler.setSeparators(',\n', ''); |
| 724 |
|
| 725 |
// Set up our input handler with the necessary mocks |
| 726 |
var mockAutoCompleter = new MockAutoCompleter(); |
| 727 |
inputHandler.ac_ = mockAutoCompleter; |
| 728 |
inputHandler.activeElement_ = mockElement; |
| 729 |
mockElement.value = 'bob,wal'; |
| 730 |
inputHandler.setCursorPosition(8); |
| 731 |
|
| 732 |
inputHandler.setTokenText('waldo', true /* multi-row */); |
| 733 |
|
| 734 |
assertEquals('bob,waldo', mockElement.value); |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
function testAriaTags() { |
| 739 |
var target = goog.dom.createDom(goog.dom.TagName.DIV); |
| 740 |
mh.attachInput(target); |
| 741 |
|
| 742 |
assertEquals(goog.a11y.aria.Role.COMBOBOX, goog.a11y.aria.getRole(target)); |
| 743 |
assertEquals( |
| 744 |
'list', |
| 745 |
goog.a11y.aria.getState(target, goog.a11y.aria.State.AUTOCOMPLETE)); |
| 746 |
|
| 747 |
mh.detachInput(target); |
| 748 |
|
| 749 |
assertNull(goog.a11y.aria.getRole(target)); |
| 750 |
assertEquals( |
| 751 |
'', goog.a11y.aria.getState(target, goog.a11y.aria.State.AUTOCOMPLETE)); |
| 752 |
} |