Best JavaScript code snippet using wpt
autocomplete_test.js
Source:autocomplete_test.js
1// Copyright 2006 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 at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// 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 and13// limitations under the License.14goog.provide('goog.ui.ac.AutoCompleteTest');15goog.setTestOnly('goog.ui.ac.AutoCompleteTest');16goog.require('goog.a11y.aria');17goog.require('goog.a11y.aria.Role');18goog.require('goog.dom');19goog.require('goog.events.EventHandler');20goog.require('goog.events.EventTarget');21goog.require('goog.string');22goog.require('goog.testing.MockControl');23goog.require('goog.testing.events');24goog.require('goog.testing.jsunit');25goog.require('goog.testing.mockmatchers');26goog.require('goog.ui.ac.AutoComplete');27goog.require('goog.ui.ac.InputHandler');28goog.require('goog.ui.ac.RenderOptions');29goog.require('goog.ui.ac.Renderer');30/**31 * Mock DataStore32 * @constructor33 */34function MockDS(opt_autoHilite) {35 this.autoHilite_ = opt_autoHilite;36 var disabledRow = {37 match: function(str) {return this.text.match(str);},38 rowDisabled: true,39 text: 'hello@u.edu'40 };41 this.rows_ = [42 '"Slartibartfast Theadore" <fjordmaster@magrathea.com>',43 '"Zaphod Beeblebrox" <theprez@universe.gov>',44 '"Ford Prefect" <ford@theguide.com>',45 '"Arthur Dent" <has.no.tea@gmail.com>',46 '"Marvin The Paranoid Android" <marv@googlemail.com>',47 'the.mice@magrathea.com',48 'the.mice@myotherdomain.com',49 'hello@a.com',50 disabledRow,51 'row@u.edu',52 'person@a.edu'53 ];54 this.isRowDisabled = function(row) {55 return !!row.rowDisabled;56 };57}58MockDS.prototype.requestMatchingRows = function(token, maxMatches,59 matchHandler) {60 var escapedToken = goog.string.regExpEscape(token);61 var matcher = new RegExp('(^|\\W+)' + escapedToken);62 var matches = [];63 for (var i = 0; i < this.rows_.length && matches.length < maxMatches; ++i) {64 var row = this.rows_[i];65 if (row.match(matcher)) {66 matches.push(row);67 }68 }69 if (this.autoHilite_ === undefined) {70 matchHandler(token, matches);71 } else {72 var options = new goog.ui.ac.RenderOptions();73 options.setAutoHilite(this.autoHilite_);74 matchHandler(token, matches, options);75 }76};77/**78 * Mock Selection Handler79 */80function MockSelect() {81}82goog.inherits(MockSelect, goog.events.EventTarget);83MockSelect.prototype.selectRow = function(row) {84 this.selectedRow = row;85};86/**87 * Renderer subclass that exposes additional private members for testing.88 * @constructor89 */90function TestRend() {91 goog.ui.ac.Renderer.call(this, goog.dom.getElement('test-area'));92}93goog.inherits(TestRend, goog.ui.ac.Renderer);94TestRend.prototype.getRenderedRows = function() {95 return this.rows_;96};97TestRend.prototype.getHilitedRowIndex = function() {98 return this.hilitedRow_;99};100TestRend.prototype.getHilitedRowDiv = function() {101 return this.rowDivs_[this.hilitedRow_];102};103TestRend.prototype.getRowDiv = function(index) {104 return this.rowDivs_[index];105};106var handler;107var inputElement;108var mockControl;109function setUp() {110 inputElement = goog.dom.createDom('input', {type: 'text'});111 handler = new goog.events.EventHandler();112 mockControl = new goog.testing.MockControl();113}114function tearDown() {115 handler.dispose();116 mockControl.$tearDown();117 goog.dom.removeChildren(goog.dom.getElement('test-area'));118}119/**120 * Make sure results are truncated (or not) by setMaxMatches.121 */122function testMaxMatches() {123 var ds = new MockDS();124 var rend = new TestRend();125 var select = new MockSelect();126 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);127 ac.setMaxMatches(2);128 ac.setToken('the');129 assertEquals(2, rend.getRenderedRows().length);130 ac.setToken('');131 ac.setMaxMatches(3);132 ac.setToken('the');133 assertEquals(3, rend.getRenderedRows().length);134 ac.setToken('');135 ac.setMaxMatches(1000);136 ac.setToken('the');137 assertEquals(4, rend.getRenderedRows().length);138 ac.setToken('');139}140function testHiliteViaMouse() {141 var ds = new MockDS();142 var rend = new TestRend();143 var select = new MockSelect();144 var updates = 0;145 var row = null;146 var rowNode = null;147 handler.listen(rend,148 goog.ui.ac.AutoComplete.EventType.ROW_HILITE,149 function(evt) {150 updates++;151 rowNode = evt.rowNode;152 });153 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);154 ac.setMaxMatches(4);155 ac.setToken('the');156 // Need to set the startRenderingRows_ time to something long ago, otherwise157 // the mouse event will not be fired. (The autocomplete logic waits for some158 // time to pass after rendering before firing mouseover events.)159 rend.startRenderingRows_ = -1;160 var hilitedRowDiv = rend.getRowDiv(3);161 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);162 assertEquals(2, updates);163 assertTrue(goog.string.contains(rowNode.innerHTML, 'mice@myotherdomain.com'));164}165function testMouseClickBeforeHilite() {166 var ds = new MockDS();167 var rend = new TestRend();168 var select = new MockSelect();169 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);170 ac.setMaxMatches(4);171 ac.setToken('the');172 // Need to set the startRenderingRows_ time to something long ago, otherwise173 // the mouse event will not be fired. (The autocomplete logic waits for some174 // time to pass after rendering before firing mouseover events.)175 rend.startRenderingRows_ = -1;176 // hilite row 3...177 var hilitedRowDiv = rend.getRowDiv(3);178 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);179 // but click row 2, to simulate mouse getting ahead of focus.180 var targetRowDiv = rend.getRowDiv(2);181 goog.testing.events.fireClickEvent(targetRowDiv);182 assertEquals('the.mice@magrathea.com', select.selectedRow);183}184function testMouseClickOnFirstRowBeforeHilite() {185 var ds = new MockDS();186 var rend = new TestRend();187 var select = new MockSelect();188 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);189 ac.setAutoHilite(false);190 ac.setMaxMatches(4);191 ac.setToken('the');192 // Click the first row before highlighting it, to simulate mouse getting ahead193 // of focus.194 var targetRowDiv = rend.getRowDiv(0);195 goog.testing.events.fireClickEvent(targetRowDiv);196 assertEquals(197 '"Zaphod Beeblebrox" <theprez@universe.gov>', select.selectedRow);198}199function testMouseClickOnRowAfterBlur() {200 var ds = new MockDS();201 var rend = new TestRend();202 var ih = new goog.ui.ac.InputHandler();203 ih.attachInput(inputElement);204 var ac = new goog.ui.ac.AutoComplete(ds, rend, ih);205 goog.testing.events.fireFocusEvent(inputElement);206 ac.setToken('the');207 var targetRowDiv = rend.getRowDiv(0);208 // Simulate the user clicking on an autocomplete row in the short time between209 // blur and autocomplete dismissal.210 goog.testing.events.fireBlurEvent(inputElement);211 assertNotThrows(function() {212 goog.testing.events.fireClickEvent(targetRowDiv);213 });214}215/*216 * Send AutoComplete a SELECT event with empty string for the row. We can't217 * simulate with a simple mouse click, so we dispatch the event directly.218 */219function testSelectEventEmptyRow() {220 var ds = new MockDS();221 var rend = new TestRend();222 var select = new MockSelect();223 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);224 ac.setMaxMatches(4);225 ac.setToken('the');226 rend.startRenderingRows_ = -1;227 // hilight row 2 ('the.mice@...')228 var hilitedRowDiv = rend.getRowDiv(2);229 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);230 assertUndefined(select.selectedRow);231 // Dispatch an event that does not specify a row.232 rend.dispatchEvent({233 type: goog.ui.ac.AutoComplete.EventType.SELECT,234 row: ''235 });236 assertEquals('the.mice@magrathea.com', select.selectedRow);237}238function testSuggestionsUpdateEvent() {239 var ds = new MockDS();240 var rend = new TestRend();241 var select = new MockSelect();242 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);243 var updates = 0;244 handler.listen(ac,245 goog.ui.ac.AutoComplete.EventType.SUGGESTIONS_UPDATE,246 function() {247 updates++;248 });249 ac.setToken('the');250 assertEquals(1, updates);251 ac.setToken('beeb');252 assertEquals(2, updates);253 ac.setToken('ford');254 assertEquals(3, updates);255 ac.dismiss();256 assertEquals(4, updates);257 ac.setToken('dent');258 assertEquals(5, updates);259}260function checkHilitedIndex(renderer, index) {261 assertEquals(index, renderer.getHilitedRowIndex());262}263function testGetRowCount() {264 var ds = new MockDS();265 var rend = new TestRend();266 var select = new MockSelect();267 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);268 assertEquals(0, ac.getRowCount());269 ac.setToken('Zaphod');270 assertEquals(1, ac.getRowCount());271 ac.setMaxMatches(2);272 ac.setToken('the');273 assertEquals(2, ac.getRowCount());274}275/**276 * Try using next and prev to navigate past the ends with default behavior of277 * allowFreeSelect_ and wrap_.278 */279function testHiliteNextPrev_default() {280 var ds = new MockDS();281 var rend = new TestRend();282 var select = new MockSelect();283 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);284 var updates = 0;285 handler.listen(rend,286 goog.ui.ac.AutoComplete.EventType.ROW_HILITE,287 function() {288 updates++;289 });290 // make sure 'next' and 'prev' don't explode before any token is set291 ac.hiliteNext();292 ac.hilitePrev();293 ac.setMaxMatches(4);294 assertEquals(0, rend.getRenderedRows().length);295 // check a few times296 for (var i = 0; i < 3; ++i) {297 ac.setToken('');298 ac.setToken('the');299 assertEquals(4, rend.getRenderedRows().length);300 // check to see if we can select the last of the 4 items301 checkHilitedIndex(rend, 0);302 ac.hiliteNext();303 checkHilitedIndex(rend, 1);304 ac.hiliteNext();305 checkHilitedIndex(rend, 2);306 ac.hiliteNext();307 checkHilitedIndex(rend, 3);308 // try going over the edge309 ac.hiliteNext();310 checkHilitedIndex(rend, 3);311 // go back down312 ac.hilitePrev();313 checkHilitedIndex(rend, 2);314 ac.hilitePrev();315 checkHilitedIndex(rend, 1);316 ac.hilitePrev();317 checkHilitedIndex(rend, 0);318 ac.hilitePrev();319 checkHilitedIndex(rend, 0);320 }321 // 21 changes in the loop above (3 * 7)322 assertEquals(21, updates);323}324/**325 * Try using next and prev to navigate past the ends with default behavior of326 * allowFreeSelect_ and wrap_ and with a disabled first row.327 */328function testHiliteNextPrevWithDisabledFirstRow_default() {329 var ds = new MockDS();330 var rend = new TestRend();331 var select = new MockSelect();332 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);333 var updates = 0;334 handler.listen(rend,335 goog.ui.ac.AutoComplete.EventType.ROW_HILITE,336 function() {337 updates++;338 });339 // make sure 'next' and 'prev' don't explode before any token is set340 ac.hiliteNext();341 ac.hilitePrev();342 ac.setMaxMatches(3);343 assertEquals(0, rend.getRenderedRows().length);344 // check a few times with disabled first row345 for (var i = 0; i < 3; ++i) {346 ac.setToken('');347 ac.setToken('edu');348 assertEquals(3, rend.getRenderedRows().length);349 // The first row is disabled, second should be highlighted.350 checkHilitedIndex(rend, 1);351 ac.hiliteNext();352 checkHilitedIndex(rend, 2);353 // try going over the edge354 ac.hiliteNext();355 checkHilitedIndex(rend, 2);356 // go back down357 ac.hilitePrev();358 checkHilitedIndex(rend, 1);359 // First row is disabled, make sure we don't highlight it.360 ac.hilitePrev();361 checkHilitedIndex(rend, 1);362 }363 // 9 changes in the loop above (3 * 3)364 assertEquals(9, updates);365}366/**367 * Try using next and prev to navigate past the ends with default behavior of368 * allowFreeSelect_ and wrap_ and with a disabled middle row.369 */370function testHiliteNextPrevWithDisabledMiddleRow_default() {371 var ds = new MockDS();372 var rend = new TestRend();373 var select = new MockSelect();374 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);375 var updates = 0;376 handler.listen(rend,377 goog.ui.ac.AutoComplete.EventType.ROW_HILITE,378 function() {379 updates++;380 });381 // make sure 'next' and 'prev' don't explode before any token is set382 ac.hiliteNext();383 ac.hilitePrev();384 ac.setMaxMatches(3);385 assertEquals(0, rend.getRenderedRows().length);386 // check a few times with disabled middle row387 for (var i = 0; i < 3; ++i) {388 ac.setToken('');389 ac.setToken('u');390 assertEquals(3, rend.getRenderedRows().length);391 checkHilitedIndex(rend, 0);392 ac.hiliteNext();393 // Second row is disabled and should be skipped.394 checkHilitedIndex(rend, 2);395 // try going over the edge396 ac.hiliteNext();397 checkHilitedIndex(rend, 2);398 // go back down399 ac.hilitePrev();400 // Second row is disabled, make sure we don't highlight it.401 checkHilitedIndex(rend, 0);402 ac.hilitePrev();403 checkHilitedIndex(rend, 0);404 }405 // 9 changes in the loop above (3 * 3)406 assertEquals(9, updates);407}408/**409 * Try using next and prev to navigate past the ends with default behavior of410 * allowFreeSelect_ and wrap_ and with a disabled last row.411 */412function testHiliteNextPrevWithDisabledLastRow_default() {413 var ds = new MockDS();414 var rend = new TestRend();415 var select = new MockSelect();416 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);417 var updates = 0;418 handler.listen(rend,419 goog.ui.ac.AutoComplete.EventType.ROW_HILITE,420 function() {421 updates++;422 });423 // make sure 'next' and 'prev' don't explode before any token is set424 ac.hiliteNext();425 ac.hilitePrev();426 ac.setMaxMatches(3);427 assertEquals(0, rend.getRenderedRows().length);428 // check a few times with disabled last row429 for (var i = 0; i < 3; ++i) {430 ac.setToken('');431 ac.setToken('h');432 assertEquals(3, rend.getRenderedRows().length);433 checkHilitedIndex(rend, 0);434 ac.hiliteNext();435 checkHilitedIndex(rend, 1);436 // try going over the edge since last row is disabled437 ac.hiliteNext();438 checkHilitedIndex(rend, 1);439 // go back down440 ac.hilitePrev();441 checkHilitedIndex(rend, 0);442 ac.hilitePrev();443 checkHilitedIndex(rend, 0);444 }445 // 9 changes in the loop above (3 * 3)446 assertEquals(9, updates);447}448/**449 * Try using next and prev to navigate past the ends with wrap_ off and450 * allowFreeSelect_ on.451 */452function testHiliteNextPrev_allowFreeSelect() {453 var ds = new MockDS();454 var rend = new TestRend();455 var select = new MockSelect();456 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);457 ac.setAllowFreeSelect(true);458 // make sure 'next' and 'prev' don't explode before any token is set459 ac.hiliteNext();460 ac.hilitePrev();461 ac.setMaxMatches(4);462 assertEquals(0, rend.getRenderedRows().length);463 // check a few times464 for (var i = 0; i < 3; ++i) {465 ac.setToken('');466 ac.setToken('the');467 assertEquals(4, rend.getRenderedRows().length);468 // check to see if we can select the last of the 4 items469 checkHilitedIndex(rend, 0);470 ac.hiliteNext();471 checkHilitedIndex(rend, 1);472 ac.hiliteNext();473 checkHilitedIndex(rend, 2);474 ac.hiliteNext();475 checkHilitedIndex(rend, 3);476 // try going over the edge. Since allowFreeSelect is on, this will477 // deselect the last row.478 ac.hiliteNext();479 checkHilitedIndex(rend, -1);480 // go back down the list481 ac.hiliteNext();482 checkHilitedIndex(rend, 0);483 ac.hiliteNext();484 checkHilitedIndex(rend, 1);485 // go back up the list.486 ac.hilitePrev();487 checkHilitedIndex(rend, 0);488 // go back above the first, deselects first.489 ac.hilitePrev();490 checkHilitedIndex(rend, -1);491 }492}493/**494 * Try using next and prev to navigate past the ends with wrap_ off and495 * allowFreeSelect_ on, and a disabled first row.496 */497function testHiliteNextPrevWithDisabledFirstRow_allowFreeSelect() {498 var ds = new MockDS();499 var rend = new TestRend();500 var select = new MockSelect();501 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);502 ac.setAllowFreeSelect(true);503 // make sure 'next' and 'prev' don't explode before any token is set504 ac.hiliteNext();505 ac.hilitePrev();506 ac.setMaxMatches(4);507 assertEquals(0, rend.getRenderedRows().length);508 // check a few times with disabled first row509 for (var i = 0; i < 3; ++i) {510 ac.setToken('');511 ac.setToken('edu');512 assertEquals(3, rend.getRenderedRows().length);513 // The first row is disabled, second should be highlighted.514 checkHilitedIndex(rend, 1);515 ac.hiliteNext();516 checkHilitedIndex(rend, 2);517 // Try going over the edge. Since allowFreeSelect is on, this will518 // deselect the last row.519 ac.hiliteNext();520 checkHilitedIndex(rend, -1);521 // go back down the list, first row is disabled522 ac.hiliteNext();523 checkHilitedIndex(rend, 1);524 ac.hiliteNext();525 checkHilitedIndex(rend, 2);526 // go back up the list.527 ac.hilitePrev();528 checkHilitedIndex(rend, 1);529 // first is disabled, so deselect the second.530 ac.hilitePrev();531 checkHilitedIndex(rend, -1);532 }533}534/**535 * Try using next and prev to navigate past the ends with wrap_ off and536 * allowFreeSelect_ on, and a disabled middle row.537 */538function testHiliteNextPrevWithDisabledMiddleRow_allowFreeSelect() {539 var ds = new MockDS();540 var rend = new TestRend();541 var select = new MockSelect();542 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);543 ac.setAllowFreeSelect(true);544 // make sure 'next' and 'prev' don't explode before any token is set545 ac.hiliteNext();546 ac.hilitePrev();547 ac.setMaxMatches(4);548 assertEquals(0, rend.getRenderedRows().length);549 // check a few times with disabled middle row550 for (var i = 0; i < 3; ++i) {551 ac.setToken('');552 ac.setToken('u');553 assertEquals(3, rend.getRenderedRows().length);554 checkHilitedIndex(rend, 0);555 ac.hiliteNext();556 // Second row is disabled and should be skipped.557 checkHilitedIndex(rend, 2);558 // try going over the edge. Since allowFreeSelect is on, this will559 // deselect the last row.560 ac.hiliteNext();561 checkHilitedIndex(rend, -1);562 // go back down the list563 ac.hiliteNext();564 checkHilitedIndex(rend, 0);565 ac.hiliteNext();566 checkHilitedIndex(rend, 2);567 // go back up the list.568 ac.hilitePrev();569 checkHilitedIndex(rend, 0);570 // go back above the first, deselects first.571 ac.hilitePrev();572 checkHilitedIndex(rend, -1);573 }574}575/**576 * Try using next and prev to navigate past the ends with wrap_ off and577 * allowFreeSelect_ on, and a disabled last row.578 */579function testHiliteNextPrevWithDisabledLastRow_allowFreeSelect() {580 var ds = new MockDS();581 var rend = new TestRend();582 var select = new MockSelect();583 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);584 ac.setAllowFreeSelect(true);585 // make sure 'next' and 'prev' don't explode before any token is set586 ac.hiliteNext();587 ac.hilitePrev();588 ac.setMaxMatches(4);589 assertEquals(0, rend.getRenderedRows().length);590 // check a few times with disabled last row591 for (var i = 0; i < 3; ++i) {592 ac.setToken('');593 ac.setToken('h');594 assertEquals(3, rend.getRenderedRows().length);595 checkHilitedIndex(rend, 0);596 ac.hiliteNext();597 checkHilitedIndex(rend, 1);598 // try going over the edge since last row is disabled. Since allowFreeSelect599 // is on, this will deselect the last row.600 ac.hiliteNext();601 checkHilitedIndex(rend, -1);602 // go back down the list603 ac.hiliteNext();604 checkHilitedIndex(rend, 0);605 ac.hiliteNext();606 checkHilitedIndex(rend, 1);607 // go back up the list.608 ac.hilitePrev();609 checkHilitedIndex(rend, 0);610 // go back above the first, deselects first.611 ac.hilitePrev();612 checkHilitedIndex(rend, -1);613 }614}615/**616 * Try using next and prev to navigate past the ends with wrap_ on617 * allowFreeSelect_ off.618 */619function testHiliteNextPrev_wrap() {620 var ds = new MockDS();621 var rend = new TestRend();622 var select = new MockSelect();623 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);624 ac.setWrap(true);625 // make sure 'next' and 'prev' don't explode before any token is set626 ac.hiliteNext();627 ac.hilitePrev();628 ac.setMaxMatches(4);629 assertEquals(0, rend.getRenderedRows().length);630 // check a few times631 for (var i = 0; i < 3; ++i) {632 ac.setToken('');633 ac.setToken('the');634 assertEquals(4, rend.getRenderedRows().length);635 // check to see if we can select the last of the 4 items636 checkHilitedIndex(rend, 0);637 ac.hiliteNext();638 checkHilitedIndex(rend, 1);639 ac.hiliteNext();640 checkHilitedIndex(rend, 2);641 ac.hiliteNext();642 checkHilitedIndex(rend, 3);643 // try going over the edge. Since wrap is on, this will go back to 0.644 ac.hiliteNext();645 checkHilitedIndex(rend, 0);646 // go back down the list647 ac.hiliteNext();648 checkHilitedIndex(rend, 1);649 // go back up the list.650 ac.hilitePrev();651 checkHilitedIndex(rend, 0);652 // go back above the first, selects last.653 ac.hilitePrev();654 checkHilitedIndex(rend, 3);655 }656}657/**658 * Try using next and prev to navigate past the ends with wrap_ on659 * allowFreeSelect_ off and a disabled first row.660 */661function testHiliteNextPrevWithDisabledFirstRow_wrap() {662 var ds = new MockDS();663 var rend = new TestRend();664 var select = new MockSelect();665 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);666 ac.setWrap(true);667 // make sure 'next' and 'prev' don't explode before any token is set668 ac.hiliteNext();669 ac.hilitePrev();670 ac.setMaxMatches(4);671 assertEquals(0, rend.getRenderedRows().length);672 // check a few times with disabled first row673 for (var i = 0; i < 3; ++i) {674 ac.setToken('');675 ac.setToken('edu');676 assertEquals(3, rend.getRenderedRows().length);677 // The first row is disabled, second should be highlighted.678 checkHilitedIndex(rend, 1);679 ac.hiliteNext();680 checkHilitedIndex(rend, 2);681 // try going over the edge. Since wrap is on and first row is disabled,682 // this will go back to 1.683 ac.hiliteNext();684 checkHilitedIndex(rend, 1);685 // go back down the list686 ac.hiliteNext();687 checkHilitedIndex(rend, 2);688 // go back up the list.689 ac.hilitePrev();690 checkHilitedIndex(rend, 1);691 // first is disabled, so wrap and select the last.692 ac.hilitePrev();693 checkHilitedIndex(rend, 2);694 }695}696/**697 * Try using next and prev to navigate past the ends with wrap_ on698 * allowFreeSelect_ off and a disabled middle row.699 */700function testHiliteNextPrevWithDisabledMiddleRow_wrap() {701 var ds = new MockDS();702 var rend = new TestRend();703 var select = new MockSelect();704 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);705 ac.setWrap(true);706 // make sure 'next' and 'prev' don't explode before any token is set707 ac.hiliteNext();708 ac.hilitePrev();709 ac.setMaxMatches(4);710 assertEquals(0, rend.getRenderedRows().length);711 // check a few times with disabled middle row712 for (var i = 0; i < 3; ++i) {713 ac.setToken('');714 ac.setToken('u');715 assertEquals(3, rend.getRenderedRows().length);716 checkHilitedIndex(rend, 0);717 ac.hiliteNext();718 // Second row is disabled and should be skipped.719 checkHilitedIndex(rend, 2);720 // try going over the edge. Since wrap is on, this will go back to 0.721 ac.hiliteNext();722 checkHilitedIndex(rend, 0);723 // go back down the list724 ac.hiliteNext();725 checkHilitedIndex(rend, 2);726 // go back up the list.727 ac.hilitePrev();728 checkHilitedIndex(rend, 0);729 // go back above the first, selects last.730 ac.hilitePrev();731 checkHilitedIndex(rend, 2);732 }733}734/**735 * Try using next and prev to navigate past the ends with wrap_ on736 * allowFreeSelect_ off and a disabled last row.737 */738function testHiliteNextPrevWithDisabledLastRow_wrap() {739 var ds = new MockDS();740 var rend = new TestRend();741 var select = new MockSelect();742 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);743 ac.setWrap(true);744 // make sure 'next' and 'prev' don't explode before any token is set745 ac.hiliteNext();746 ac.hilitePrev();747 ac.setMaxMatches(4);748 assertEquals(0, rend.getRenderedRows().length);749 // check a few times with disabled last row750 for (var i = 0; i < 3; ++i) {751 ac.setToken('');752 ac.setToken('h');753 assertEquals(3, rend.getRenderedRows().length);754 checkHilitedIndex(rend, 0);755 ac.hiliteNext();756 checkHilitedIndex(rend, 1);757 // try going over the edge since last row is disabled. Since wrap is on,758 // this will go back to 0.759 ac.hiliteNext();760 checkHilitedIndex(rend, 0);761 // go back down the list762 ac.hiliteNext();763 checkHilitedIndex(rend, 1);764 // go back up the list.765 ac.hilitePrev();766 checkHilitedIndex(rend, 0);767 // go back above the first, since wrap is on and last row is disabled, this768 // will select the second last.769 ac.hilitePrev();770 checkHilitedIndex(rend, 1);771 }772}773/**774 * Try using next and prev to navigate past the ends with wrap_ on775 * allowFreeSelect_ on.776 */777function testHiliteNextPrev_wrapAndAllowFreeSelect() {778 var ds = new MockDS();779 var rend = new TestRend();780 var select = new MockSelect();781 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);782 ac.setWrap(true);783 ac.setAllowFreeSelect(true);784 // make sure 'next' and 'prev' don't explode before any token is set785 ac.hiliteNext();786 ac.hilitePrev();787 ac.setMaxMatches(4);788 assertEquals(0, rend.getRenderedRows().length);789 // check a few times790 for (var i = 0; i < 3; ++i) {791 ac.setToken('');792 ac.setToken('the');793 assertEquals(4, rend.getRenderedRows().length);794 // check to see if we can select the last of the 4 items795 checkHilitedIndex(rend, 0);796 ac.hiliteNext();797 checkHilitedIndex(rend, 1);798 ac.hiliteNext();799 checkHilitedIndex(rend, 2);800 ac.hiliteNext();801 checkHilitedIndex(rend, 3);802 // try going over the edge. Since free select is on, this should go803 // to -1.804 ac.hiliteNext();805 checkHilitedIndex(rend, -1);806 // go back down the list807 ac.hiliteNext();808 checkHilitedIndex(rend, 0);809 ac.hiliteNext();810 checkHilitedIndex(rend, 1);811 // go back up the list.812 ac.hilitePrev();813 checkHilitedIndex(rend, 0);814 // go back above the first, free select.815 ac.hilitePrev();816 checkHilitedIndex(rend, -1);817 // wrap to last818 ac.hilitePrev();819 checkHilitedIndex(rend, 3);820 }821}822/**823 * Try using next and prev to navigate past the ends with wrap_ on824 * allowFreeSelect_ on and a disabled first row.825 */826function testHiliteNextPrevWithDisabledFirstRow_wrapAndAllowFreeSelect() {827 var ds = new MockDS();828 var rend = new TestRend();829 var select = new MockSelect();830 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);831 ac.setWrap(true);832 ac.setAllowFreeSelect(true);833 // make sure 'next' and 'prev' don't explode before any token is set834 ac.hiliteNext();835 ac.hilitePrev();836 ac.setMaxMatches(4);837 assertEquals(0, rend.getRenderedRows().length);838 // check a few times with disabled first row839 for (var i = 0; i < 3; ++i) {840 ac.setToken('');841 ac.setToken('edu');842 assertEquals(3, rend.getRenderedRows().length);843 // The first row is disabled, second should be highlighted.844 checkHilitedIndex(rend, 1);845 ac.hiliteNext();846 checkHilitedIndex(rend, 2);847 // try going over the edge. Since free select is on, this should go to -1.848 ac.hiliteNext();849 checkHilitedIndex(rend, -1);850 // go back down the list, fist row is disabled851 ac.hiliteNext();852 checkHilitedIndex(rend, 1);853 ac.hiliteNext();854 checkHilitedIndex(rend, 2);855 // go back up the list.856 ac.hilitePrev();857 checkHilitedIndex(rend, 1);858 // go back above the first, free select.859 ac.hilitePrev();860 checkHilitedIndex(rend, -1);861 // wrap to last862 ac.hilitePrev();863 checkHilitedIndex(rend, 2);864 }865}866/**867 * Try using next and prev to navigate past the ends with wrap_ on868 * allowFreeSelect_ on and a disabled middle row.869 */870function testHiliteNextPrevWithDisabledMiddleRow_wrapAndAllowFreeSelect() {871 var ds = new MockDS();872 var rend = new TestRend();873 var select = new MockSelect();874 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);875 ac.setWrap(true);876 ac.setAllowFreeSelect(true);877 // make sure 'next' and 'prev' don't explode before any token is set878 ac.hiliteNext();879 ac.hilitePrev();880 ac.setMaxMatches(4);881 assertEquals(0, rend.getRenderedRows().length);882 // check a few times with disabled middle row883 for (var i = 0; i < 3; ++i) {884 ac.setToken('');885 ac.setToken('u');886 assertEquals(3, rend.getRenderedRows().length);887 checkHilitedIndex(rend, 0);888 ac.hiliteNext();889 // Second row is disabled and should be skipped.890 checkHilitedIndex(rend, 2);891 // try going over the edge. Since free select is on, this should go to -1892 ac.hiliteNext();893 checkHilitedIndex(rend, -1);894 // go back down the list895 ac.hiliteNext();896 checkHilitedIndex(rend, 0);897 ac.hiliteNext();898 checkHilitedIndex(rend, 2);899 // go back up the list.900 ac.hilitePrev();901 checkHilitedIndex(rend, 0);902 // go back above the first, free select.903 ac.hilitePrev();904 checkHilitedIndex(rend, -1);905 // wrap to last906 ac.hilitePrev();907 checkHilitedIndex(rend, 2);908 }909}910/**911 * Try using next and prev to navigate past the ends with wrap_ on912 * allowFreeSelect_ on and a disabled last row.913 */914function testHiliteNextPrevWithDisabledLastRow_wrapAndAllowFreeSelect() {915 var ds = new MockDS();916 var rend = new TestRend();917 var select = new MockSelect();918 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);919 ac.setWrap(true);920 ac.setAllowFreeSelect(true);921 // make sure 'next' and 'prev' don't explode before any token is set922 ac.hiliteNext();923 ac.hilitePrev();924 ac.setMaxMatches(4);925 assertEquals(0, rend.getRenderedRows().length);926 // check a few times with disabled last row927 for (var i = 0; i < 3; ++i) {928 ac.setToken('');929 ac.setToken('h');930 assertEquals(3, rend.getRenderedRows().length);931 checkHilitedIndex(rend, 0);932 ac.hiliteNext();933 checkHilitedIndex(rend, 1);934 // try going over the edge since last row is disabled. Since free select is935 // on, this should go to -1936 ac.hiliteNext();937 checkHilitedIndex(rend, -1);938 // go back down the list939 ac.hiliteNext();940 checkHilitedIndex(rend, 0);941 ac.hiliteNext();942 checkHilitedIndex(rend, 1);943 // go back up the list.944 ac.hilitePrev();945 checkHilitedIndex(rend, 0);946 // go back above the first, free select.947 ac.hilitePrev();948 checkHilitedIndex(rend, -1);949 // wrap to the second last, since last is disabled.950 ac.hilitePrev();951 checkHilitedIndex(rend, 1);952 }953}954/**955 * Try using next and prev to navigate past the ends with wrap_ on956 * allowFreeSelect_ on AND turn autoHilite_ off.957 */958function testHiliteNextPrev_wrapAndAllowFreeSelectNoAutoHilite() {959 var ds = new MockDS();960 var rend = new TestRend();961 var select = new MockSelect();962 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);963 ac.setWrap(true);964 ac.setAllowFreeSelect(true);965 ac.setAutoHilite(false);966 // make sure 'next' and 'prev' don't explode before any token is set967 ac.hiliteNext();968 ac.hilitePrev();969 ac.setMaxMatches(4);970 assertEquals(0, rend.getRenderedRows().length);971 // check a few times972 for (var i = 0; i < 3; ++i) {973 ac.setToken('');974 ac.setToken('the');975 assertEquals(4, rend.getRenderedRows().length);976 // check to see if we can select the last of the 4 items.977 // Initially nothing should be selected since autoHilite_ is off.978 checkHilitedIndex(rend, -1);979 ac.hilitePrev();980 checkHilitedIndex(rend, 3);981 ac.hiliteNext();982 checkHilitedIndex(rend, -1);983 ac.hiliteNext();984 checkHilitedIndex(rend, 0);985 ac.hiliteNext();986 checkHilitedIndex(rend, 1);987 ac.hiliteNext();988 checkHilitedIndex(rend, 2);989 ac.hiliteNext();990 checkHilitedIndex(rend, 3);991 // try going over the edge. Since free select is on, this should go992 // to -1.993 ac.hiliteNext();994 checkHilitedIndex(rend, -1);995 // go back down the list996 ac.hiliteNext();997 checkHilitedIndex(rend, 0);998 ac.hiliteNext();999 checkHilitedIndex(rend, 1);1000 // go back up the list.1001 ac.hilitePrev();1002 checkHilitedIndex(rend, 0);1003 // go back above the first, free select.1004 ac.hilitePrev();1005 checkHilitedIndex(rend, -1);1006 // wrap to last1007 ac.hilitePrev();1008 checkHilitedIndex(rend, 3);1009 }1010}1011/**1012 * Try using next and prev to navigate past the ends with wrap_ on1013 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled first row.1014 */1015function testHiliteNextPrevWithDisabledFirstRow_wrapAndAllowFreeSelectNoAutoHilite() {1016 var ds = new MockDS();1017 var rend = new TestRend();1018 var select = new MockSelect();1019 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1020 ac.setWrap(true);1021 ac.setAllowFreeSelect(true);1022 ac.setAutoHilite(false);1023 // make sure 'next' and 'prev' don't explode before any token is set1024 ac.hiliteNext();1025 ac.hilitePrev();1026 ac.setMaxMatches(4);1027 assertEquals(0, rend.getRenderedRows().length);1028 // check a few times with disabled first row1029 for (var i = 0; i < 3; ++i) {1030 ac.setToken('');1031 ac.setToken('edu');1032 assertEquals(3, rend.getRenderedRows().length);1033 // Initially nothing should be selected since autoHilite_ is off.1034 checkHilitedIndex(rend, -1);1035 ac.hilitePrev();1036 checkHilitedIndex(rend, 2);1037 ac.hiliteNext();1038 checkHilitedIndex(rend, -1);1039 ac.hiliteNext();1040 // First row is disabled.1041 checkHilitedIndex(rend, 1);1042 ac.hiliteNext();1043 checkHilitedIndex(rend, 2);1044 // try going over the edge. Since free select is on, this should go to -11045 ac.hiliteNext();1046 checkHilitedIndex(rend, -1);1047 // go back down the list, first row is disabled1048 ac.hiliteNext();1049 checkHilitedIndex(rend, 1);1050 ac.hiliteNext();1051 checkHilitedIndex(rend, 2);1052 // go back up the list.1053 ac.hilitePrev();1054 checkHilitedIndex(rend, 1);1055 // go back above the first, free select.1056 ac.hilitePrev();1057 checkHilitedIndex(rend, -1);1058 // wrap to last1059 ac.hilitePrev();1060 checkHilitedIndex(rend, 2);1061 }1062}1063/**1064 * Try using next and prev to navigate past the ends with wrap_ on1065 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled middle row.1066 */1067function testHiliteNextPrevWithDisabledMiddleRow_wrapAndAllowFreeSelectNoAutoHilite() {1068 var ds = new MockDS();1069 var rend = new TestRend();1070 var select = new MockSelect();1071 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1072 ac.setWrap(true);1073 ac.setAllowFreeSelect(true);1074 ac.setAutoHilite(false);1075 // make sure 'next' and 'prev' don't explode before any token is set1076 ac.hiliteNext();1077 ac.hilitePrev();1078 ac.setMaxMatches(4);1079 assertEquals(0, rend.getRenderedRows().length);1080 // check a few times with disabled middle row1081 for (var i = 0; i < 3; ++i) {1082 ac.setToken('');1083 ac.setToken('u');1084 assertEquals(3, rend.getRenderedRows().length);1085 // Initially nothing should be selected since autoHilite_ is off.1086 checkHilitedIndex(rend, -1);1087 ac.hilitePrev();1088 checkHilitedIndex(rend, 2);1089 ac.hiliteNext();1090 checkHilitedIndex(rend, -1);1091 ac.hiliteNext();1092 checkHilitedIndex(rend, 0);1093 ac.hiliteNext();1094 // Second row is disabled1095 checkHilitedIndex(rend, 2);1096 // try going over the edge. Since free select is on, this should go to -1.1097 ac.hiliteNext();1098 checkHilitedIndex(rend, -1);1099 // go back down the list1100 ac.hiliteNext();1101 checkHilitedIndex(rend, 0);1102 ac.hiliteNext();1103 // Second row is disabled.1104 checkHilitedIndex(rend, 2);1105 // go back up the list.1106 ac.hilitePrev();1107 checkHilitedIndex(rend, 0);1108 // go back above the first, free select.1109 ac.hilitePrev();1110 checkHilitedIndex(rend, -1);1111 // wrap to last1112 ac.hilitePrev();1113 checkHilitedIndex(rend, 2);1114 }1115}1116/**1117 * Try using next and prev to navigate past the ends with wrap_ on1118 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled last row.1119 */1120function testHiliteNextPrevWithDisabledLastRow_wrapAndAllowFreeSelectNoAutoHilite() {1121 var ds = new MockDS();1122 var rend = new TestRend();1123 var select = new MockSelect();1124 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1125 ac.setWrap(true);1126 ac.setAllowFreeSelect(true);1127 ac.setAutoHilite(false);1128 // make sure 'next' and 'prev' don't explode before any token is set1129 ac.hiliteNext();1130 ac.hilitePrev();1131 ac.setMaxMatches(4);1132 assertEquals(0, rend.getRenderedRows().length);1133 // check a few times with disabled last row1134 for (var i = 0; i < 3; ++i) {1135 ac.setToken('');1136 ac.setToken('h');1137 assertEquals(3, rend.getRenderedRows().length);1138 // Initially nothing should be selected since autoHilite_ is off.1139 checkHilitedIndex(rend, -1);1140 ac.hilitePrev();1141 // Last row is disabled1142 checkHilitedIndex(rend, 1);1143 ac.hiliteNext();1144 checkHilitedIndex(rend, -1);1145 ac.hiliteNext();1146 checkHilitedIndex(rend, 0);1147 ac.hiliteNext();1148 checkHilitedIndex(rend, 1);1149 // try going over the edge. Since free select is on, this should go to -1.1150 ac.hiliteNext();1151 checkHilitedIndex(rend, -1);1152 // go back down the list1153 ac.hiliteNext();1154 checkHilitedIndex(rend, 0);1155 ac.hiliteNext();1156 checkHilitedIndex(rend, 1);1157 // go back up the list.1158 ac.hilitePrev();1159 checkHilitedIndex(rend, 0);1160 // go back above the first, free select.1161 ac.hilitePrev();1162 checkHilitedIndex(rend, -1);1163 // wrap to last1164 ac.hilitePrev();1165 checkHilitedIndex(rend, 1);1166 }1167}1168function testHiliteWithChangingNumberOfRows() {1169 var ds = new MockDS();1170 var rend = new TestRend();1171 var select = new MockSelect();1172 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1173 ac.setAutoHilite(true);1174 ac.setMaxMatches(4);1175 ac.setToken('m');1176 assertEquals(4, rend.getRenderedRows().length);1177 checkHilitedIndex(rend, 0);1178 ac.setToken('ma');1179 assertEquals(3, rend.getRenderedRows().length);1180 checkHilitedIndex(rend, 0);1181 // Hilite the second element1182 var id = rend.getRenderedRows()[1].id;1183 ac.hiliteId(id);1184 ac.setToken('mar');1185 assertEquals(1, rend.getRenderedRows().length);1186 checkHilitedIndex(rend, 0);1187 ac.setToken('ma');1188 assertEquals(3, rend.getRenderedRows().length);1189 checkHilitedIndex(rend, 0);1190 // Hilite the second element1191 var id = rend.getRenderedRows()[1].id;1192 ac.hiliteId(id);1193 ac.setToken('m');1194 assertEquals(4, rend.getRenderedRows().length);1195 checkHilitedIndex(rend, 0);1196}1197/**1198 * Checks that autohilite is disabled when there is no token; this allows the1199 * user to tab out of an empty autocomplete.1200 */1201function testNoAutoHiliteWhenTokenIsEmpty() {1202 var ds = new MockDS();1203 var rend = new TestRend();1204 var select = new MockSelect();1205 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1206 ac.setWrap(true);1207 ac.setAllowFreeSelect(true);1208 ac.setAutoHilite(true);1209 ac.setMaxMatches(4);1210 ac.setToken('');1211 assertEquals(4, rend.getRenderedRows().length);1212 // No token; nothing should be hilited.1213 checkHilitedIndex(rend, -1);1214 ac.setToken('the');1215 assertEquals(4, rend.getRenderedRows().length);1216 // Now there is a token, so the first row should be highlighted.1217 checkHilitedIndex(rend, 0);1218}1219/**1220 * Checks that opt_preserveHilited works.1221 */1222function testPreserveHilitedWithoutAutoHilite() {1223 var ds = new MockDS();1224 var rend = new TestRend();1225 var select = new MockSelect();1226 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1227 ac.setWrap(true);1228 ac.setAllowFreeSelect(true);1229 ac.setMaxMatches(4);1230 ac.setAutoHilite(false);1231 ac.setToken('m');1232 assertEquals(4, rend.getRenderedRows().length);1233 // No token; nothing should be hilited.1234 checkHilitedIndex(rend, -1);1235 // Hilite the second element1236 var id = rend.getRenderedRows()[1].id;1237 ac.hiliteId(id);1238 checkHilitedIndex(rend, 1);1239 // Re-render and check if the second element is still hilited1240 ac.renderRows(rend.getRenderedRows(), true /* preserve hilite */);1241 checkHilitedIndex(rend, 1);1242 // Re-render without preservation1243 ac.renderRows(rend.getRenderedRows());1244 checkHilitedIndex(rend, -1);1245}1246/**1247 * Checks that the autohilite argument "true" of the matcher is used.1248 */1249function testAutoHiliteFromMatcherTrue() {1250 var ds = new MockDS(true);1251 var rend = new TestRend();1252 var select = new MockSelect();1253 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1254 ac.setWrap(true);1255 ac.setAllowFreeSelect(true);1256 ac.setAutoHilite(false); // Will be overruled.1257 ac.setMaxMatches(4);1258 ac.setToken('the');1259 assertEquals(4, rend.getRenderedRows().length);1260 // The first row should be highlighted.1261 checkHilitedIndex(rend, 0);1262}1263/**1264 * Checks that the autohilite argument "false" of the matcher is used.1265 */1266function testAutoHiliteFromMatcherFalse() {1267 var ds = new MockDS(false);1268 var rend = new TestRend();1269 var select = new MockSelect();1270 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1271 ac.setWrap(true);1272 ac.setAllowFreeSelect(true);1273 ac.setAutoHilite(true); // Will be overruled.1274 ac.setMaxMatches(4);1275 ac.setToken('the');1276 assertEquals(4, rend.getRenderedRows().length);1277 // The first row should not be highlighted.1278 checkHilitedIndex(rend, -1);1279}1280/**1281 * Hilite using ids, the way mouse-based hiliting would work.1282 */1283function testHiliteId() {1284 var ds = new MockDS();1285 var rend = new TestRend();1286 var select = new MockSelect();1287 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1288 // check a few times1289 for (var i = 0; i < 3; ++i) {1290 ac.setToken('m');1291 assertEquals(4, rend.getRenderedRows().length);1292 // try hiliting all 31293 for (var x = 0; x < 4; ++x) {1294 var id = rend.getRenderedRows()[x].id;1295 ac.hiliteId(id);1296 assertEquals(ac.getIdOfIndex_(x), id);1297 }1298 }1299}1300/**1301 * Test selecting the hilited row1302 */1303function testSelection() {1304 var ds = new MockDS();1305 var rend = new TestRend();1306 var select = new MockSelect();1307 var ac;1308 // try with default selection1309 ac = new goog.ui.ac.AutoComplete(ds, rend, select);1310 ac.setToken('m');1311 ac.selectHilited();1312 assertEquals('"Slartibartfast Theadore" <fjordmaster@magrathea.com>',1313 select.selectedRow);1314 // try second item1315 ac = new goog.ui.ac.AutoComplete(ds, rend, select);1316 ac.setToken('the');1317 ac.hiliteNext();1318 ac.selectHilited();1319 assertEquals('"Ford Prefect" <ford@theguide.com>',1320 select.selectedRow);1321}1322/**1323 * Dismiss when empty and non-empty1324 */1325function testDismiss() {1326 var ds = new MockDS();1327 var rend = new TestRend();1328 var select = new MockSelect();1329 // dismiss empty1330 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1331 var dismissed = 0;1332 handler.listen(ac,1333 goog.ui.ac.AutoComplete.EventType.DISMISS,1334 function() {1335 dismissed++;1336 });1337 ac.dismiss();1338 assertEquals(1, dismissed);1339 ac = new goog.ui.ac.AutoComplete(ds, rend, select);1340 ac.setToken('sir not seen in this picture');1341 ac.dismiss();1342 // dismiss with contents1343 ac = new goog.ui.ac.AutoComplete(ds, rend, select);1344 ac.setToken('t');1345 ac.dismiss();1346}1347function testTriggerSuggestionsOnUpdate() {1348 var ds = new MockDS();1349 var rend = new TestRend();1350 var select = new MockSelect();1351 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1352 var dismissCalled = 0;1353 rend.dismiss = function() {1354 dismissCalled++;1355 };1356 var updateCalled = 0;1357 select.update = function(opt_force) {1358 updateCalled++;1359 };1360 // Normally, menu is dismissed after selecting row (without updating).1361 ac.setToken('the');1362 ac.selectHilited();1363 assertEquals(1, dismissCalled);1364 assertEquals(0, updateCalled);1365 // But not if we re-trigger on update.1366 ac.setTriggerSuggestionsOnUpdate(true);1367 ac.setToken('the');1368 ac.selectHilited();1369 assertEquals(1, dismissCalled);1370 assertEquals(1, updateCalled);1371}1372function testDispose() {1373 var ds = new MockDS();1374 var rend = new TestRend();1375 var select = new MockSelect();1376 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1377 ac.setToken('the');1378 ac.dispose();1379}1380/**1381 * Ensure that activedescendant is updated properly.1382 */1383function testRolesAndStates() {1384 function checkActiveDescendant(activeDescendant) {1385 assertNotNull(inputElement);1386 assertEquals(1387 goog.a11y.aria.getActiveDescendant(inputElement),1388 activeDescendant);1389 }1390 function checkRole(el, role) {1391 assertNotNull(el);1392 assertEquals(goog.a11y.aria.getRole(el), role);1393 }1394 var ds = new MockDS();1395 var rend = new TestRend();1396 var select = new MockSelect();1397 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);1398 ac.setTarget(inputElement);1399 // initially activedescendant is not set1400 checkActiveDescendant(null);1401 // highlight the matching row and check that activedescendant updates1402 ac.setToken('');1403 ac.setToken('the');1404 ac.hiliteNext();1405 checkActiveDescendant(rend.getHilitedRowDiv());1406 // highligted row should have a role of 'option'1407 checkRole(rend.getHilitedRowDiv(), goog.a11y.aria.Role.OPTION);1408 // closing the autocomplete should clear activedescendant1409 ac.dismiss();1410 checkActiveDescendant(null);1411}1412function testAttachInputWithAnchor() {1413 var anchorElement = goog.dom.createDom('div', null, inputElement);1414 var mockRenderer = mockControl.createLooseMock(1415 goog.ui.ac.Renderer, true);1416 mockRenderer.setAnchorElement(anchorElement);1417 var ignore = goog.testing.mockmatchers.ignoreArgument;1418 mockRenderer.renderRows(ignore, ignore, inputElement);1419 var mockInputHandler = mockControl.createLooseMock(1420 goog.ui.ac.InputHandler, true);1421 mockInputHandler.attachInputs(inputElement);1422 mockControl.$replayAll();1423 var autoComplete = new goog.ui.ac.AutoComplete(1424 null, mockRenderer, mockInputHandler);1425 autoComplete.attachInputWithAnchor(inputElement, anchorElement);1426 autoComplete.setTarget(inputElement);1427 autoComplete.renderRows(['abc', 'def']);1428 mockControl.$verifyAll();1429}1430function testDetachInputWithAnchor() {1431 var mockRenderer = mockControl.createLooseMock(1432 goog.ui.ac.Renderer, true);1433 var mockInputHandler = mockControl.createLooseMock(1434 goog.ui.ac.InputHandler, true);1435 var anchorElement = goog.dom.createDom('div', null, inputElement);1436 var inputElement2 = goog.dom.createDom('input', {type: 'text'});1437 var anchorElement2 = goog.dom.createDom('div', null, inputElement2);1438 mockControl.$replayAll();1439 var autoComplete = new goog.ui.ac.AutoComplete(1440 null, mockRenderer, mockInputHandler);1441 autoComplete.attachInputWithAnchor(inputElement, anchorElement);1442 autoComplete.attachInputWithAnchor(inputElement2, anchorElement2);1443 autoComplete.detachInputs(inputElement, inputElement2);1444 assertFalse(goog.getUid(inputElement) in autoComplete.inputToAnchorMap_);1445 assertFalse(goog.getUid(inputElement2) in autoComplete.inputToAnchorMap_);1446 mockControl.$verifyAll();...
cxhtml-test-data.ts
Source:cxhtml-test-data.ts
1export const CODE_MACRO = `<ac:structured-macro ac:name="code" ac:schema-version="1" ac:macro-id="1c61c2dd-3574-45f3-ac07-76d400504d84"><ac:parameter ac:name="language">js</ac:parameter><ac:parameter ac:name="theme">Confluence</ac:parameter><ac:parameter ac:name="title">Example</ac:parameter><ac:plain-text-body><![CDATA[if (true) {2 console.log('Hello World');3}]]></ac:plain-text-body></ac:structured-macro>`;4export const JIRA_ISSUE =5 '<p><ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="a1a887df-a2dd-492b-8b5c-415d8eab22cf"><ac:parameter ac:name="server">JIRA (product-fabric.atlassian.net)</ac:parameter><ac:parameter ac:name="serverId">70d83bc8-0aff-3fa5-8121-5ae90121f5fc</ac:parameter><ac:parameter ac:name="key">ED-1068</ac:parameter></ac:structured-macro></p>';6export const JIRA_ISSUES_LIST =7 '<p><ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="be852c2a-4d33-4ceb-8e21-b3b45791d92e"><ac:parameter ac:name="server">JIRA (product-fabric.atlassian.net)</ac:parameter><ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter><ac:parameter ac:name="maximumIssues">20</ac:parameter><ac:parameter ac:name="jqlQuery">project = ED AND component = codeblock</ac:parameter><ac:parameter ac:name="serverId">70d83bc8-0aff-3fa5-8121-5ae90121f5fc</ac:parameter></ac:structured-macro></p>';8export const PANEL_MACRO = `<ac:structured-macro ac:name="warning" ac:schema-version="1" ac:macro-id="f348e247-44a6-41e5-8034-e8aa469649b5"><ac:parameter ac:name="title">Hello</ac:parameter><ac:rich-text-body><p>Warning panel</p></ac:rich-text-body></ac:structured-macro>`;9export const INLINE_EXTENSION =10 '<p><ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="1511498935556"> <ac:parameter ac:name="color">Red</ac:parameter> <ac:parameter ac:name="title">Fail</ac:parameter> <ac:parameter ac:name="subtle">true</ac:parameter> <fab:display-type>INLINE</fab:display-type> </ac:structured-macro></p>';11export const EXTENSION =12 '<ac:structured-macro ac:name="gallery" ac:schema-version="1" ac:macro-id="1511499023528"> <ac:parameter ac:name="color">Red</ac:parameter> <fab:placeholder-url>//pug.jira-dev.com/wiki/plugins/servlet/confluence/placeholder/macro?definition=e2dhbGxlcnl9&locale=en_GB&version=2</fab:placeholder-url> <fab:display-type>BLOCK</fab:display-type></ac:structured-macro>';13export const BODIED_EXTENSION =14 '<ac:structured-macro ac:name="expand" ac:schema-version="1" ac:macro-id="1511499130537"> <fab:placeholder-url>//pug.jira-dev.com/wiki/plugins/servlet/confluence/placeholder/macro?definition=e2V4cGFuZH0&locale=en_GB&version=2</fab:placeholder-url> <fab:display-type>BLOCK</fab:display-type> <ac:rich-text-body> <h5>Heading</h5> <p> <u>Foo</u> </p> </ac:rich-text-body></ac:structured-macro>';15export const BODIED_NESTED_EXTENSION =16 '<ac:structured-macro ac:name="expand" ac:schema-version="1" ac:macro-id="1511499178897"> <fab:display-type>BLOCK</fab:display-type> <ac:rich-text-body> <h5>Heading</h5> <p> <u>Foo</u> <ac:structured-macro ac:name="status" ac:schema-version="1" ac:macro-id="1511499178897"> <ac:parameter ac:name="color">Green</ac:parameter> <ac:parameter ac:name="title">OK</ac:parameter> <ac:parameter ac:name="subtle">true</ac:parameter> <fab:placeholder-url>//pug.jira-dev.com/wiki/plugins/servlet/confluence/placeholder/macro?definition=e3N0YXR1czpzdWJ0bGU9dHJ1ZXxjb2xvdXI9R3JlZW58dGl0bGU9T0t9&locale=en_GB&version=2</fab:placeholder-url> <fab:display-type>INLINE</fab:display-type> </ac:structured-macro> </p> </ac:rich-text-body></ac:structured-macro>';...
Using AI Code Generation
1var wpt = require('webpagetest')2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 console.log('Test status:', data.statusText);5 console.log('Test ID:', data.data.testId);6 console.log('Test URL:', data.data.userUrl);7 console.log('Test from:', data.data.from);8 console.log('Test location:', data.data.location);9 console.log('Test date:', data.data.date);10 console.log('Test results:', data.data.summary);11});12var wpt = require('webpagetest')13var wpt = new WebPageTest('www.webpagetest.org');14 if (err) return console.error(err);15 console.log('Test status:', data.statusText);16 console.log('Test ID:', data.data.testId);17 console.log('Test URL:', data.data.userUrl);18 console.log('Test from:', data.data.from);19 console.log('Test location:', data.data.location);20 console.log('Test date:', data.data.date);21 console.log('Test results:', data.data.summary);22});23var wpt = require('webpagetest')24var wpt = new WebPageTest('www.webpagetest.org');25 if (err) return console.error(err);26 console.log('Test status:', data.statusText);27 console.log('Test ID:', data.data.testId);28 console.log('Test URL:', data.data.userUrl);29 console.log('Test from:', data.data.from);30 console.log('Test location:', data.data.location);31 console.log('Test date:', data.data.date);32 console.log('Test results:', data.data.summary);33});34var wpt = require('webpagetest')35var wpt = new WebPageTest('www.webpagetest.org');36 if (err) return console.error(err);37 console.log('Test
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {4 if (err) return console.error(err);5 console.log(data);6});7var wpt = require('webpagetest');8var api = new wpt('www.webpagetest.org');9api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {10 if (err) return console.error(err);11 console.log(data);12});13var wpt = require('webpagetest');14var api = new wpt('www.webpagetest.org');15api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {16 if (err) return console.error(err);17 console.log(data);18});19var wpt = require('webpagetest');20var api = new wpt('www.webpagetest.org');21api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {22 if (err) return console.error(err);23 console.log(data);24});25var wpt = require('webpagetest');26var api = new wpt('www.webpagetest.org');27api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {28 if (err) return console.error(err);29 console.log(data);30});31var wpt = require('webpagetest');32var api = new wpt('www.webpagetest.org');33api.runTest('www.example.com', {location: 'Dulles:Chrome'}, function(err, data) {34 if (err) return console.error(err);35 console.log(data);36});37var wpt = require('webpagetest');38var api = new wpt('www.webpagetest.org');
Using AI Code Generation
1var wpt = require('webpagetest');2var api = new wpt('www.webpagetest.org');3api.runTest('www.google.com', {4}, function(err, data) {5 if (err) {6 console.log('Error: ' + err);7 } else {8 console.log('Test status: ' + data.statusText);9 console.log('Test ID: ' + data.data.testId);10 console.log('Test URL: ' + data.data.userUrl);11 console.log('Test Results URL: ' + data.data.summary);12 }13});14var wpt = require('webpagetest');15var api = new wpt('www.webpagetest.org');16api.runTest('www.google.com', {17}, function(err, data) {18 if (err) {19 console.log('Error: ' + err);20 } else {21 console.log('Test status: ' + data.statusText);22 console.log('Test ID: ' + data.data.testId);23 console.log('Test URL: ' + data.data.userUrl);24 console.log('Test Results URL: ' + data.data.summary);25 }26});27var wpt = require('webpagetest');28var api = new wpt('www.webpagetest.org');29api.runTest('www.google.com', {
Using AI Code Generation
1var wptool = require('wptool');2var ac = wptool.ac;3var wptool = require('wptool');4var ac = wptool.ac;5var wptool = require('wptool');6var ac = wptool.ac;7var wptool = require('wptool');8var ac = wptool.ac;9var wptool = require('wptool');10var ac = wptool.ac;11var wptool = require('wptool');12var ac = wptool.ac;13var wptool = require('wptool');14var ac = wptool.ac;15var wptool = require('wptool');16var ac = wptool.ac;17var wptool = require('wptool');18var ac = wptool.ac;19var wptool = require('wptool');20var ac = wptool.ac;21var wptool = require('wptool');22var ac = wptool.ac;23var wptool = require('wptool');24var ac = wptool.ac;
Using AI Code Generation
1var wpt = require('wpt');2wpt.ac('test', function(err, data) {3 if (err) {4 console.log(err);5 } else {6 console.log("test.js: " + data);7 }8});9var wpt = require('wpt');10wpt.ac('test2', function(err, data) {11 if (err) {12 console.log(err);13 } else {14 console.log("test2.js: " + data);15 }16});17 at Object.fs.openSync (fs.js:439:18)18 at Object.fs.readFileSync (fs.js:290:15)19 at Object.readFileSync (C:\Users\myuser\Desktop\test\node_modules\wpt\wpt.js:2:15)20 at Object.ac (C:\Users\myuser\Desktop\test\node_modules\wpt\wpt.js:14:25)21 at Object.<anonymous> (C:\Users\myuser\Desktop\test\test2.js:4:4)22 at Module._compile (module.js:456:26)23 at Object.Module._extensions..js (module.js:474:10)24 at Module.load (module.js:356:32)25 at Function.Module._load (module.js:312:12)26 at Function.Module.runMain (module.js:497:10)27var path = require('path');28var wpt = require('wpt');29var data = fs.readFileSync(path.join(__dirname, 'wpt.js'));
Using AI Code Generation
1var wpt = require('wpt');2var fs = require('fs');3var wpt = new WebPageTest('www.webpagetest.org');4var testId;5var testResults;6var testResultsJson;7var testResultsJsonString;8var testResultsJsonStringPretty;9var testResultsJsonStringPretty1;10var testResultsJsonStringPretty2;11var testResultsJsonStringPretty3;12var testResultsJsonStringPretty4;13var testResultsJsonStringPretty5;14var testResultsJsonStringPretty6;15var testResultsJsonStringPretty7;16var testResultsJsonStringPretty8;17var testResultsJsonStringPretty9;18var testResultsJsonStringPretty10;19var testResultsJsonStringPretty11;20var testResultsJsonStringPretty12;21var testResultsJsonStringPretty13;22var testResultsJsonStringPretty14;23var testResultsJsonStringPretty15;24var testResultsJsonStringPretty16;25var testResultsJsonStringPretty17;26var testResultsJsonStringPretty18;27var testResultsJsonStringPretty19;28var testResultsJsonStringPretty20;29var testResultsJsonStringPretty21;30var testResultsJsonStringPretty22;31var testResultsJsonStringPretty23;32var testResultsJsonStringPretty24;33var testResultsJsonStringPretty25;34var testResultsJsonStringPretty26;35var testResultsJsonStringPretty27;36var testResultsJsonStringPretty28;37var testResultsJsonStringPretty29;38var testResultsJsonStringPretty30;39var testResultsJsonStringPretty31;40var testResultsJsonStringPretty32;41var testResultsJsonStringPretty33;42var testResultsJsonStringPretty34;43var testResultsJsonStringPretty35;44var testResultsJsonStringPretty36;45var testResultsJsonStringPretty37;46var testResultsJsonStringPretty38;47var testResultsJsonStringPretty39;48var testResultsJsonStringPretty40;49var testResultsJsonStringPretty41;50var testResultsJsonStringPretty42;51var testResultsJsonStringPretty43;52var testResultsJsonStringPretty44;53var testResultsJsonStringPretty45;54var testResultsJsonStringPretty46;55var testResultsJsonStringPretty47;56var testResultsJsonStringPretty48;57var testResultsJsonStringPretty49;58var testResultsJsonStringPretty50;59var testResultsJsonStringPretty51;60var testResultsJsonStringPretty52;61var testResultsJsonStringPretty53;
Using AI Code Generation
1var wptools = require('wptools');2var wiki = wptools.page('Barack Obama');3wiki.ac('birth_place', function(err, resp){4 console.log(resp);5});6var wiki = wptools.page('Barack Obama');7wiki.ac('birth_place', function(err, resp){8 console.log(resp);9});10var wiki = wptools.page('Barack Obama');11wiki.ac('birth_place', function(err, resp){12 console.log(resp);13});14var wiki = wptools.page('Barack Obama');15wiki.ac('birth_place', function(err, resp){16 console.log(resp);17});18var wiki = wptools.page('Barack Obama');19wiki.ac('birth_place', function(err, resp){20 console.log(resp);21});22var wiki = wptools.page('Barack Obama');23wiki.ac('birth_place', function(err, resp){24 console.log(resp);25});26var wiki = wptools.page('Barack Obama');27wiki.ac('birth_place', function(err, resp){28 console.log(resp);29});30var wiki = wptools.page('Barack Obama');31wiki.ac('birth_place', function(err, resp){32 console.log(resp);33});34var wiki = wptools.page('Barack Obama');35wiki.ac('birth_place', function(err, resp){36 console.log(resp);37});38var wiki = wptools.page('Barack Obama');39wiki.ac('birth_place', function(err, resp){40 console.log(resp);41});42var wiki = wptools.page('Barack Obama');43wiki.ac('birth_place', function(err, resp){44 console.log(resp);45});46var wiki = wptools.page('Barack Obama');47wiki.ac('birth_place', function(err, resp){48 console.log(resp);49});
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!