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