Debian Codegraph @

closure-library/closure/goog/ui/ac/autocomplete_test.js

raw
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 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.AutoCompleteTest');
16 goog.setTestOnly('goog.ui.ac.AutoCompleteTest');
17
18 goog.require('goog.a11y.aria');
19 goog.require('goog.a11y.aria.Role');
20 goog.require('goog.dom');
21 goog.require('goog.dom.InputType');
22 goog.require('goog.dom.TagName');
23 goog.require('goog.events.EventHandler');
24 goog.require('goog.events.EventTarget');
25 goog.require('goog.string');
26 goog.require('goog.testing.MockControl');
27 goog.require('goog.testing.events');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.testing.mockmatchers');
30 goog.require('goog.ui.ac.AutoComplete');
31 goog.require('goog.ui.ac.InputHandler');
32 goog.require('goog.ui.ac.RenderOptions');
33 goog.require('goog.ui.ac.Renderer');
34
35
36
37 /**
38 * Mock DataStore
39 * @constructor
40 */
41 function MockDS(opt_autoHilite) {
42 this.autoHilite_ = opt_autoHilite;
43 var disabledRow = {
44 match: function(str) { return this.text.match(str); },
45 rowDisabled: true,
46 text: '[email protected]'
47 };
48 this.rows_ = [
49 '"Slartibartfast Theadore" <[email protected]>',
50 '"Zaphod Beeblebrox" <[email protected]>',
51 '"Ford Prefect" <[email protected]>',
52 '"Arthur Dent" <[email protected]>',
53 '"Marvin The Paranoid Android" <[email protected]>',
54 '[email protected]', '[email protected]', '[email protected]',
55 disabledRow, '[email protected]', '[email protected]'
56 ];
57 this.isRowDisabled = function(row) { return !!row.rowDisabled; };
58 }
59
60 MockDS.prototype.requestMatchingRows = function(
61 token, maxMatches, matchHandler) {
62 var escapedToken = goog.string.regExpEscape(token);
63 var matcher = new RegExp('(^|\\W+)' + escapedToken);
64 var matches = [];
65 for (var i = 0; i < this.rows_.length && matches.length < maxMatches; ++i) {
66 var row = this.rows_[i];
67 if (row.match(matcher)) {
68 matches.push(row);
69 }
70 }
71 if (this.autoHilite_ === undefined) {
72 matchHandler(token, matches);
73 } else {
74 var options = new goog.ui.ac.RenderOptions();
75 options.setAutoHilite(this.autoHilite_);
76 matchHandler(token, matches, options);
77 }
78 };
79
80
81 /**
82 * Mock Selection Handler
83 */
84
85 function MockSelect() {}
86 goog.inherits(MockSelect, goog.events.EventTarget);
87
88 MockSelect.prototype.selectRow = function(row) {
89 this.selectedRow = row;
90 };
91
92
93
94 /**
95 * Renderer subclass that exposes additional private members for testing.
96 * @constructor
97 */
98 function TestRend() {
99 goog.ui.ac.Renderer.call(this, goog.dom.getElement('test-area'));
100 }
101 goog.inherits(TestRend, goog.ui.ac.Renderer);
102
103 TestRend.prototype.getRenderedRows = function() {
104 return this.rows_;
105 };
106
107 TestRend.prototype.getHilitedRowIndex = function() {
108 return this.hilitedRow_;
109 };
110
111 TestRend.prototype.getHilitedRowDiv = function() {
112 return this.rowDivs_[this.hilitedRow_];
113 };
114
115 TestRend.prototype.getRowDiv = function(index) {
116 return this.rowDivs_[index];
117 };
118
119 var handler;
120 var inputElement;
121 var mockControl;
122
123 function setUp() {
124 inputElement = goog.dom.createDom(
125 goog.dom.TagName.INPUT, {type: goog.dom.InputType.TEXT});
126 handler = new goog.events.EventHandler();
127 mockControl = new goog.testing.MockControl();
128 }
129
130 function tearDown() {
131 handler.dispose();
132 mockControl.$tearDown();
133 goog.dom.removeChildren(goog.dom.getElement('test-area'));
134 }
135
136
137 /**
138 * Make sure results are truncated (or not) by setMaxMatches.
139 */
140 function testMaxMatches() {
141 var ds = new MockDS();
142 var rend = new TestRend();
143 var select = new MockSelect();
144 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
145
146 ac.setMaxMatches(2);
147 ac.setToken('the');
148 assertEquals(2, rend.getRenderedRows().length);
149 ac.setToken('');
150
151 ac.setMaxMatches(3);
152 ac.setToken('the');
153 assertEquals(3, rend.getRenderedRows().length);
154 ac.setToken('');
155
156 ac.setMaxMatches(1000);
157 ac.setToken('the');
158 assertEquals(4, rend.getRenderedRows().length);
159 ac.setToken('');
160 }
161
162 function testHiliteViaMouse() {
163 var ds = new MockDS();
164 var rend = new TestRend();
165 var select = new MockSelect();
166 var updates = 0;
167 var row = null;
168 var rowNode = null;
169 handler.listen(
170 rend, goog.ui.ac.AutoComplete.EventType.ROW_HILITE, function(evt) {
171 updates++;
172 rowNode = evt.rowNode;
173 });
174 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
175 ac.setMaxMatches(4);
176 ac.setToken('the');
177 // Need to set the startRenderingRows_ time to something long ago, otherwise
178 // the mouse event will not be fired. (The autocomplete logic waits for some
179 // time to pass after rendering before firing mouseover events.)
180 rend.startRenderingRows_ = -1;
181 var hilitedRowDiv = rend.getRowDiv(3);
182 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);
183 assertEquals(2, updates);
184 assertTrue(goog.string.contains(rowNode.innerHTML, '[email protected]'));
185 }
186
187 function testMouseClickBeforeHilite() {
188 var ds = new MockDS();
189 var rend = new TestRend();
190 var select = new MockSelect();
191 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
192 ac.setMaxMatches(4);
193 ac.setToken('the');
194 // Need to set the startRenderingRows_ time to something long ago, otherwise
195 // the mouse event will not be fired. (The autocomplete logic waits for some
196 // time to pass after rendering before firing mouseover events.)
197 rend.startRenderingRows_ = -1;
198
199 // hilite row 3...
200 var hilitedRowDiv = rend.getRowDiv(3);
201 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);
202
203 // but click row 2, to simulate mouse getting ahead of focus.
204 var targetRowDiv = rend.getRowDiv(2);
205 goog.testing.events.fireClickEvent(targetRowDiv);
206
207 assertEquals('[email protected]', select.selectedRow);
208 }
209
210 function testMouseClickOnFirstRowBeforeHilite() {
211 var ds = new MockDS();
212 var rend = new TestRend();
213 var select = new MockSelect();
214 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
215 ac.setAutoHilite(false);
216 ac.setMaxMatches(4);
217 ac.setToken('the');
218
219 // Click the first row before highlighting it, to simulate mouse getting ahead
220 // of focus.
221 var targetRowDiv = rend.getRowDiv(0);
222 goog.testing.events.fireClickEvent(targetRowDiv);
223
224 assertEquals(
225 '"Zaphod Beeblebrox" <[email protected]>', select.selectedRow);
226 }
227
228 function testMouseClickOnRowAfterBlur() {
229 var ds = new MockDS();
230 var rend = new TestRend();
231 var ih = new goog.ui.ac.InputHandler();
232 ih.attachInput(inputElement);
233
234 var ac = new goog.ui.ac.AutoComplete(ds, rend, ih);
235 goog.testing.events.fireFocusEvent(inputElement);
236 ac.setToken('the');
237 var targetRowDiv = rend.getRowDiv(0);
238
239 // Simulate the user clicking on an autocomplete row in the short time between
240 // blur and autocomplete dismissal.
241 goog.testing.events.fireBlurEvent(inputElement);
242 assertNotThrows(function() {
243 goog.testing.events.fireClickEvent(targetRowDiv);
244 });
245 }
246
247
248 /*
249 * Send AutoComplete a SELECT event with empty string for the row. We can't
250 * simulate with a simple mouse click, so we dispatch the event directly.
251 */
252 function testSelectEventEmptyRow() {
253 var ds = new MockDS();
254 var rend = new TestRend();
255 var select = new MockSelect();
256 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
257 ac.setMaxMatches(4);
258 ac.setToken('the');
259 rend.startRenderingRows_ = -1;
260
261 // hilight row 2 ('the.mice@...')
262 var hilitedRowDiv = rend.getRowDiv(2);
263 goog.testing.events.fireMouseOverEvent(hilitedRowDiv);
264 assertUndefined(select.selectedRow);
265
266 // Dispatch an event that does not specify a row.
267 rend.dispatchEvent({type: goog.ui.ac.AutoComplete.EventType.SELECT, row: ''});
268
269 assertEquals('[email protected]', select.selectedRow);
270 }
271
272 function testSuggestionsUpdateEvent() {
273 var ds = new MockDS();
274 var rend = new TestRend();
275 var select = new MockSelect();
276 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
277 var updates = 0;
278 handler.listen(
279 ac, goog.ui.ac.AutoComplete.EventType.SUGGESTIONS_UPDATE,
280 function() { updates++; });
281
282 ac.setToken('the');
283 assertEquals(1, updates);
284
285 ac.setToken('beeb');
286 assertEquals(2, updates);
287
288 ac.setToken('ford');
289 assertEquals(3, updates);
290
291 ac.dismiss();
292 assertEquals(4, updates);
293
294 ac.setToken('dent');
295 assertEquals(5, updates);
296 }
297
298 function checkHilitedIndex(renderer, index) {
299 assertEquals(index, renderer.getHilitedRowIndex());
300 }
301
302 function testGetRowCount() {
303 var ds = new MockDS();
304 var rend = new TestRend();
305 var select = new MockSelect();
306 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
307 assertEquals(0, ac.getRowCount());
308
309 ac.setToken('Zaphod');
310 assertEquals(1, ac.getRowCount());
311
312 ac.setMaxMatches(2);
313 ac.setToken('the');
314 assertEquals(2, ac.getRowCount());
315 }
316
317
318 /**
319 * Try using next and prev to navigate past the ends with default behavior of
320 * allowFreeSelect_ and wrap_.
321 */
322 function testHiliteNextPrev_default() {
323 var ds = new MockDS();
324 var rend = new TestRend();
325 var select = new MockSelect();
326 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
327
328 var updates = 0;
329 handler.listen(
330 rend, goog.ui.ac.AutoComplete.EventType.ROW_HILITE,
331 function() { updates++; });
332
333 // make sure 'next' and 'prev' don't explode before any token is set
334 ac.hiliteNext();
335 ac.hilitePrev();
336 ac.setMaxMatches(4);
337 assertEquals(0, rend.getRenderedRows().length);
338
339 // check a few times
340 for (var i = 0; i < 3; ++i) {
341 ac.setToken('');
342 ac.setToken('the');
343 assertEquals(4, rend.getRenderedRows().length);
344 // check to see if we can select the last of the 4 items
345 checkHilitedIndex(rend, 0);
346 ac.hiliteNext();
347 checkHilitedIndex(rend, 1);
348 ac.hiliteNext();
349 checkHilitedIndex(rend, 2);
350 ac.hiliteNext();
351 checkHilitedIndex(rend, 3);
352 // try going over the edge
353 ac.hiliteNext();
354 checkHilitedIndex(rend, 3);
355
356 // go back down
357 ac.hilitePrev();
358 checkHilitedIndex(rend, 2);
359 ac.hilitePrev();
360 checkHilitedIndex(rend, 1);
361 ac.hilitePrev();
362 checkHilitedIndex(rend, 0);
363 ac.hilitePrev();
364 checkHilitedIndex(rend, 0);
365 }
366 // 21 changes in the loop above (3 * 7)
367 assertEquals(21, updates);
368 }
369
370
371 /**
372 * Try using next and prev to navigate past the ends with default behavior of
373 * allowFreeSelect_ and wrap_ and with a disabled first row.
374 */
375 function testHiliteNextPrevWithDisabledFirstRow_default() {
376 var ds = new MockDS();
377 var rend = new TestRend();
378 var select = new MockSelect();
379 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
380
381 var updates = 0;
382 handler.listen(
383 rend, goog.ui.ac.AutoComplete.EventType.ROW_HILITE,
384 function() { updates++; });
385
386 // make sure 'next' and 'prev' don't explode before any token is set
387 ac.hiliteNext();
388 ac.hilitePrev();
389 ac.setMaxMatches(3);
390 assertEquals(0, rend.getRenderedRows().length);
391
392 // check a few times with disabled first row
393 for (var i = 0; i < 3; ++i) {
394 ac.setToken('');
395 ac.setToken('edu');
396 assertEquals(3, rend.getRenderedRows().length);
397 // The first row is disabled, second should be highlighted.
398 checkHilitedIndex(rend, 1);
399 ac.hiliteNext();
400 checkHilitedIndex(rend, 2);
401 // try going over the edge
402 ac.hiliteNext();
403 checkHilitedIndex(rend, 2);
404
405 // go back down
406 ac.hilitePrev();
407 checkHilitedIndex(rend, 1);
408 // First row is disabled, make sure we don't highlight it.
409 ac.hilitePrev();
410 checkHilitedIndex(rend, 1);
411 }
412 // 9 changes in the loop above (3 * 3)
413 assertEquals(9, updates);
414 }
415
416
417 /**
418 * Try using next and prev to navigate past the ends with default behavior of
419 * allowFreeSelect_ and wrap_ and with a disabled middle row.
420 */
421 function testHiliteNextPrevWithDisabledMiddleRow_default() {
422 var ds = new MockDS();
423 var rend = new TestRend();
424 var select = new MockSelect();
425 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
426
427 var updates = 0;
428 handler.listen(
429 rend, goog.ui.ac.AutoComplete.EventType.ROW_HILITE,
430 function() { updates++; });
431
432 // make sure 'next' and 'prev' don't explode before any token is set
433 ac.hiliteNext();
434 ac.hilitePrev();
435 ac.setMaxMatches(3);
436 assertEquals(0, rend.getRenderedRows().length);
437
438 // check a few times with disabled middle row
439 for (var i = 0; i < 3; ++i) {
440 ac.setToken('');
441 ac.setToken('u');
442 assertEquals(3, rend.getRenderedRows().length);
443 checkHilitedIndex(rend, 0);
444 ac.hiliteNext();
445 // Second row is disabled and should be skipped.
446 checkHilitedIndex(rend, 2);
447 // try going over the edge
448 ac.hiliteNext();
449 checkHilitedIndex(rend, 2);
450
451 // go back down
452 ac.hilitePrev();
453 // Second row is disabled, make sure we don't highlight it.
454 checkHilitedIndex(rend, 0);
455 ac.hilitePrev();
456 checkHilitedIndex(rend, 0);
457 }
458 // 9 changes in the loop above (3 * 3)
459 assertEquals(9, updates);
460 }
461
462
463 /**
464 * Try using next and prev to navigate past the ends with default behavior of
465 * allowFreeSelect_ and wrap_ and with a disabled last row.
466 */
467 function testHiliteNextPrevWithDisabledLastRow_default() {
468 var ds = new MockDS();
469 var rend = new TestRend();
470 var select = new MockSelect();
471 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
472
473 var updates = 0;
474 handler.listen(
475 rend, goog.ui.ac.AutoComplete.EventType.ROW_HILITE,
476 function() { updates++; });
477
478 // make sure 'next' and 'prev' don't explode before any token is set
479 ac.hiliteNext();
480 ac.hilitePrev();
481 ac.setMaxMatches(3);
482 assertEquals(0, rend.getRenderedRows().length);
483
484 // check a few times with disabled last row
485 for (var i = 0; i < 3; ++i) {
486 ac.setToken('');
487 ac.setToken('h');
488 assertEquals(3, rend.getRenderedRows().length);
489 checkHilitedIndex(rend, 0);
490 ac.hiliteNext();
491 checkHilitedIndex(rend, 1);
492 // try going over the edge since last row is disabled
493 ac.hiliteNext();
494 checkHilitedIndex(rend, 1);
495
496 // go back down
497 ac.hilitePrev();
498 checkHilitedIndex(rend, 0);
499 ac.hilitePrev();
500 checkHilitedIndex(rend, 0);
501 }
502 // 9 changes in the loop above (3 * 3)
503 assertEquals(9, updates);
504 }
505
506
507 /**
508 * Try using next and prev to navigate past the ends with wrap_ off and
509 * allowFreeSelect_ on.
510 */
511 function testHiliteNextPrev_allowFreeSelect() {
512 var ds = new MockDS();
513 var rend = new TestRend();
514 var select = new MockSelect();
515 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
516 ac.setAllowFreeSelect(true);
517
518 // make sure 'next' and 'prev' don't explode before any token is set
519 ac.hiliteNext();
520 ac.hilitePrev();
521 ac.setMaxMatches(4);
522 assertEquals(0, rend.getRenderedRows().length);
523
524 // check a few times
525 for (var i = 0; i < 3; ++i) {
526 ac.setToken('');
527 ac.setToken('the');
528 assertEquals(4, rend.getRenderedRows().length);
529 // check to see if we can select the last of the 4 items
530 checkHilitedIndex(rend, 0);
531 ac.hiliteNext();
532 checkHilitedIndex(rend, 1);
533 ac.hiliteNext();
534 checkHilitedIndex(rend, 2);
535 ac.hiliteNext();
536 checkHilitedIndex(rend, 3);
537 // try going over the edge. Since allowFreeSelect is on, this will
538 // deselect the last row.
539 ac.hiliteNext();
540 checkHilitedIndex(rend, -1);
541
542 // go back down the list
543 ac.hiliteNext();
544 checkHilitedIndex(rend, 0);
545 ac.hiliteNext();
546 checkHilitedIndex(rend, 1);
547
548 // go back up the list.
549 ac.hilitePrev();
550 checkHilitedIndex(rend, 0);
551 // go back above the first, deselects first.
552 ac.hilitePrev();
553 checkHilitedIndex(rend, -1);
554 }
555 }
556
557
558 /**
559 * Try using next and prev to navigate past the ends with wrap_ off and
560 * allowFreeSelect_ on, and a disabled first row.
561 */
562 function testHiliteNextPrevWithDisabledFirstRow_allowFreeSelect() {
563 var ds = new MockDS();
564 var rend = new TestRend();
565 var select = new MockSelect();
566 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
567 ac.setAllowFreeSelect(true);
568
569 // make sure 'next' and 'prev' don't explode before any token is set
570 ac.hiliteNext();
571 ac.hilitePrev();
572 ac.setMaxMatches(4);
573 assertEquals(0, rend.getRenderedRows().length);
574
575 // check a few times with disabled first row
576 for (var i = 0; i < 3; ++i) {
577 ac.setToken('');
578 ac.setToken('edu');
579 assertEquals(3, rend.getRenderedRows().length);
580 // The first row is disabled, second should be highlighted.
581 checkHilitedIndex(rend, 1);
582 ac.hiliteNext();
583 checkHilitedIndex(rend, 2);
584 // Try going over the edge. Since allowFreeSelect is on, this will
585 // deselect the last row.
586 ac.hiliteNext();
587 checkHilitedIndex(rend, -1);
588
589 // go back down the list, first row is disabled
590 ac.hiliteNext();
591 checkHilitedIndex(rend, 1);
592 ac.hiliteNext();
593 checkHilitedIndex(rend, 2);
594
595 // go back up the list.
596 ac.hilitePrev();
597 checkHilitedIndex(rend, 1);
598 // first is disabled, so deselect the second.
599 ac.hilitePrev();
600 checkHilitedIndex(rend, -1);
601 }
602 }
603
604
605 /**
606 * Try using next and prev to navigate past the ends with wrap_ off and
607 * allowFreeSelect_ on, and a disabled middle row.
608 */
609 function testHiliteNextPrevWithDisabledMiddleRow_allowFreeSelect() {
610 var ds = new MockDS();
611 var rend = new TestRend();
612 var select = new MockSelect();
613 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
614 ac.setAllowFreeSelect(true);
615
616 // make sure 'next' and 'prev' don't explode before any token is set
617 ac.hiliteNext();
618 ac.hilitePrev();
619 ac.setMaxMatches(4);
620 assertEquals(0, rend.getRenderedRows().length);
621
622 // check a few times with disabled middle row
623 for (var i = 0; i < 3; ++i) {
624 ac.setToken('');
625 ac.setToken('u');
626 assertEquals(3, rend.getRenderedRows().length);
627 checkHilitedIndex(rend, 0);
628 ac.hiliteNext();
629 // Second row is disabled and should be skipped.
630 checkHilitedIndex(rend, 2);
631 // try going over the edge. Since allowFreeSelect is on, this will
632 // deselect the last row.
633 ac.hiliteNext();
634 checkHilitedIndex(rend, -1);
635
636 // go back down the list
637 ac.hiliteNext();
638 checkHilitedIndex(rend, 0);
639 ac.hiliteNext();
640 checkHilitedIndex(rend, 2);
641
642 // go back up the list.
643 ac.hilitePrev();
644 checkHilitedIndex(rend, 0);
645 // go back above the first, deselects first.
646 ac.hilitePrev();
647 checkHilitedIndex(rend, -1);
648 }
649 }
650
651
652 /**
653 * Try using next and prev to navigate past the ends with wrap_ off and
654 * allowFreeSelect_ on, and a disabled last row.
655 */
656 function testHiliteNextPrevWithDisabledLastRow_allowFreeSelect() {
657 var ds = new MockDS();
658 var rend = new TestRend();
659 var select = new MockSelect();
660 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
661 ac.setAllowFreeSelect(true);
662
663 // make sure 'next' and 'prev' don't explode before any token is set
664 ac.hiliteNext();
665 ac.hilitePrev();
666 ac.setMaxMatches(4);
667 assertEquals(0, rend.getRenderedRows().length);
668
669 // check a few times with disabled last row
670 for (var i = 0; i < 3; ++i) {
671 ac.setToken('');
672 ac.setToken('h');
673 assertEquals(3, rend.getRenderedRows().length);
674 checkHilitedIndex(rend, 0);
675 ac.hiliteNext();
676 checkHilitedIndex(rend, 1);
677 // try going over the edge since last row is disabled. Since allowFreeSelect
678 // is on, this will deselect the last row.
679 ac.hiliteNext();
680 checkHilitedIndex(rend, -1);
681
682 // go back down the list
683 ac.hiliteNext();
684 checkHilitedIndex(rend, 0);
685 ac.hiliteNext();
686 checkHilitedIndex(rend, 1);
687
688 // go back up the list.
689 ac.hilitePrev();
690 checkHilitedIndex(rend, 0);
691 // go back above the first, deselects first.
692 ac.hilitePrev();
693 checkHilitedIndex(rend, -1);
694 }
695 }
696
697
698 /**
699 * Try using next and prev to navigate past the ends with wrap_ on
700 * allowFreeSelect_ off.
701 */
702 function testHiliteNextPrev_wrap() {
703 var ds = new MockDS();
704 var rend = new TestRend();
705 var select = new MockSelect();
706 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
707 ac.setWrap(true);
708
709 // make sure 'next' and 'prev' don't explode before any token is set
710 ac.hiliteNext();
711 ac.hilitePrev();
712 ac.setMaxMatches(4);
713 assertEquals(0, rend.getRenderedRows().length);
714
715 // check a few times
716 for (var i = 0; i < 3; ++i) {
717 ac.setToken('');
718 ac.setToken('the');
719 assertEquals(4, rend.getRenderedRows().length);
720 // check to see if we can select the last of the 4 items
721 checkHilitedIndex(rend, 0);
722 ac.hiliteNext();
723 checkHilitedIndex(rend, 1);
724 ac.hiliteNext();
725 checkHilitedIndex(rend, 2);
726 ac.hiliteNext();
727 checkHilitedIndex(rend, 3);
728 // try going over the edge. Since wrap is on, this will go back to 0.
729 ac.hiliteNext();
730 checkHilitedIndex(rend, 0);
731
732 // go back down the list
733 ac.hiliteNext();
734 checkHilitedIndex(rend, 1);
735
736 // go back up the list.
737 ac.hilitePrev();
738 checkHilitedIndex(rend, 0);
739 // go back above the first, selects last.
740 ac.hilitePrev();
741 checkHilitedIndex(rend, 3);
742 }
743 }
744
745
746 /**
747 * Try using next and prev to navigate past the ends with wrap_ on
748 * allowFreeSelect_ off and a disabled first row.
749 */
750 function testHiliteNextPrevWithDisabledFirstRow_wrap() {
751 var ds = new MockDS();
752 var rend = new TestRend();
753 var select = new MockSelect();
754 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
755 ac.setWrap(true);
756
757 // make sure 'next' and 'prev' don't explode before any token is set
758 ac.hiliteNext();
759 ac.hilitePrev();
760 ac.setMaxMatches(4);
761 assertEquals(0, rend.getRenderedRows().length);
762
763 // check a few times with disabled first row
764 for (var i = 0; i < 3; ++i) {
765 ac.setToken('');
766 ac.setToken('edu');
767 assertEquals(3, rend.getRenderedRows().length);
768 // The first row is disabled, second should be highlighted.
769 checkHilitedIndex(rend, 1);
770 ac.hiliteNext();
771 checkHilitedIndex(rend, 2);
772 // try going over the edge. Since wrap is on and first row is disabled,
773 // this will go back to 1.
774 ac.hiliteNext();
775 checkHilitedIndex(rend, 1);
776
777 // go back down the list
778 ac.hiliteNext();
779 checkHilitedIndex(rend, 2);
780
781 // go back up the list.
782 ac.hilitePrev();
783 checkHilitedIndex(rend, 1);
784 // first is disabled, so wrap and select the last.
785 ac.hilitePrev();
786 checkHilitedIndex(rend, 2);
787 }
788 }
789
790
791 /**
792 * Try using next and prev to navigate past the ends with wrap_ on
793 * allowFreeSelect_ off and a disabled middle row.
794 */
795 function testHiliteNextPrevWithDisabledMiddleRow_wrap() {
796 var ds = new MockDS();
797 var rend = new TestRend();
798 var select = new MockSelect();
799 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
800 ac.setWrap(true);
801
802 // make sure 'next' and 'prev' don't explode before any token is set
803 ac.hiliteNext();
804 ac.hilitePrev();
805 ac.setMaxMatches(4);
806 assertEquals(0, rend.getRenderedRows().length);
807
808 // check a few times with disabled middle row
809 for (var i = 0; i < 3; ++i) {
810 ac.setToken('');
811 ac.setToken('u');
812 assertEquals(3, rend.getRenderedRows().length);
813 checkHilitedIndex(rend, 0);
814 ac.hiliteNext();
815 // Second row is disabled and should be skipped.
816 checkHilitedIndex(rend, 2);
817 // try going over the edge. Since wrap is on, this will go back to 0.
818 ac.hiliteNext();
819 checkHilitedIndex(rend, 0);
820
821 // go back down the list
822 ac.hiliteNext();
823 checkHilitedIndex(rend, 2);
824
825 // go back up the list.
826 ac.hilitePrev();
827 checkHilitedIndex(rend, 0);
828 // go back above the first, selects last.
829 ac.hilitePrev();
830 checkHilitedIndex(rend, 2);
831 }
832 }
833
834
835 /**
836 * Try using next and prev to navigate past the ends with wrap_ on
837 * allowFreeSelect_ off and a disabled last row.
838 */
839 function testHiliteNextPrevWithDisabledLastRow_wrap() {
840 var ds = new MockDS();
841 var rend = new TestRend();
842 var select = new MockSelect();
843 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
844 ac.setWrap(true);
845
846 // make sure 'next' and 'prev' don't explode before any token is set
847 ac.hiliteNext();
848 ac.hilitePrev();
849 ac.setMaxMatches(4);
850 assertEquals(0, rend.getRenderedRows().length);
851
852 // check a few times with disabled last row
853 for (var i = 0; i < 3; ++i) {
854 ac.setToken('');
855 ac.setToken('h');
856 assertEquals(3, rend.getRenderedRows().length);
857 checkHilitedIndex(rend, 0);
858 ac.hiliteNext();
859 checkHilitedIndex(rend, 1);
860 // try going over the edge since last row is disabled. Since wrap is on,
861 // this will go back to 0.
862 ac.hiliteNext();
863 checkHilitedIndex(rend, 0);
864
865 // go back down the list
866 ac.hiliteNext();
867 checkHilitedIndex(rend, 1);
868
869 // go back up the list.
870 ac.hilitePrev();
871 checkHilitedIndex(rend, 0);
872 // go back above the first, since wrap is on and last row is disabled, this
873 // will select the second last.
874 ac.hilitePrev();
875 checkHilitedIndex(rend, 1);
876 }
877 }
878
879
880 /**
881 * Try using next and prev to navigate past the ends with wrap_ on
882 * allowFreeSelect_ on.
883 */
884 function testHiliteNextPrev_wrapAndAllowFreeSelect() {
885 var ds = new MockDS();
886 var rend = new TestRend();
887 var select = new MockSelect();
888 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
889 ac.setWrap(true);
890 ac.setAllowFreeSelect(true);
891
892 // make sure 'next' and 'prev' don't explode before any token is set
893 ac.hiliteNext();
894 ac.hilitePrev();
895 ac.setMaxMatches(4);
896 assertEquals(0, rend.getRenderedRows().length);
897
898 // check a few times
899 for (var i = 0; i < 3; ++i) {
900 ac.setToken('');
901 ac.setToken('the');
902 assertEquals(4, rend.getRenderedRows().length);
903 // check to see if we can select the last of the 4 items
904 checkHilitedIndex(rend, 0);
905 ac.hiliteNext();
906 checkHilitedIndex(rend, 1);
907 ac.hiliteNext();
908 checkHilitedIndex(rend, 2);
909 ac.hiliteNext();
910 checkHilitedIndex(rend, 3);
911 // try going over the edge. Since free select is on, this should go
912 // to -1.
913 ac.hiliteNext();
914 checkHilitedIndex(rend, -1);
915
916 // go back down the list
917 ac.hiliteNext();
918 checkHilitedIndex(rend, 0);
919 ac.hiliteNext();
920 checkHilitedIndex(rend, 1);
921
922 // go back up the list.
923 ac.hilitePrev();
924 checkHilitedIndex(rend, 0);
925 // go back above the first, free select.
926 ac.hilitePrev();
927 checkHilitedIndex(rend, -1);
928 // wrap to last
929 ac.hilitePrev();
930 checkHilitedIndex(rend, 3);
931 }
932 }
933
934
935 /**
936 * Try using next and prev to navigate past the ends with wrap_ on
937 * allowFreeSelect_ on and a disabled first row.
938 */
939 function testHiliteNextPrevWithDisabledFirstRow_wrapAndAllowFreeSelect() {
940 var ds = new MockDS();
941 var rend = new TestRend();
942 var select = new MockSelect();
943 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
944 ac.setWrap(true);
945 ac.setAllowFreeSelect(true);
946
947 // make sure 'next' and 'prev' don't explode before any token is set
948 ac.hiliteNext();
949 ac.hilitePrev();
950 ac.setMaxMatches(4);
951 assertEquals(0, rend.getRenderedRows().length);
952
953 // check a few times with disabled first row
954 for (var i = 0; i < 3; ++i) {
955 ac.setToken('');
956 ac.setToken('edu');
957 assertEquals(3, rend.getRenderedRows().length);
958 // The first row is disabled, second should be highlighted.
959 checkHilitedIndex(rend, 1);
960 ac.hiliteNext();
961 checkHilitedIndex(rend, 2);
962 // try going over the edge. Since free select is on, this should go to -1.
963 ac.hiliteNext();
964 checkHilitedIndex(rend, -1);
965
966 // go back down the list, fist row is disabled
967 ac.hiliteNext();
968 checkHilitedIndex(rend, 1);
969 ac.hiliteNext();
970 checkHilitedIndex(rend, 2);
971
972 // go back up the list.
973 ac.hilitePrev();
974 checkHilitedIndex(rend, 1);
975 // go back above the first, free select.
976 ac.hilitePrev();
977 checkHilitedIndex(rend, -1);
978 // wrap to last
979 ac.hilitePrev();
980 checkHilitedIndex(rend, 2);
981 }
982 }
983
984
985 /**
986 * Try using next and prev to navigate past the ends with wrap_ on
987 * allowFreeSelect_ on and a disabled middle row.
988 */
989 function testHiliteNextPrevWithDisabledMiddleRow_wrapAndAllowFreeSelect() {
990 var ds = new MockDS();
991 var rend = new TestRend();
992 var select = new MockSelect();
993 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
994 ac.setWrap(true);
995 ac.setAllowFreeSelect(true);
996
997 // make sure 'next' and 'prev' don't explode before any token is set
998 ac.hiliteNext();
999 ac.hilitePrev();
1000 ac.setMaxMatches(4);
1001 assertEquals(0, rend.getRenderedRows().length);
1002
1003 // check a few times with disabled middle row
1004 for (var i = 0; i < 3; ++i) {
1005 ac.setToken('');
1006 ac.setToken('u');
1007 assertEquals(3, rend.getRenderedRows().length);
1008 checkHilitedIndex(rend, 0);
1009 ac.hiliteNext();
1010 // Second row is disabled and should be skipped.
1011 checkHilitedIndex(rend, 2);
1012 // try going over the edge. Since free select is on, this should go to -1
1013 ac.hiliteNext();
1014 checkHilitedIndex(rend, -1);
1015
1016 // go back down the list
1017 ac.hiliteNext();
1018 checkHilitedIndex(rend, 0);
1019 ac.hiliteNext();
1020 checkHilitedIndex(rend, 2);
1021
1022 // go back up the list.
1023 ac.hilitePrev();
1024 checkHilitedIndex(rend, 0);
1025 // go back above the first, free select.
1026 ac.hilitePrev();
1027 checkHilitedIndex(rend, -1);
1028 // wrap to last
1029 ac.hilitePrev();
1030 checkHilitedIndex(rend, 2);
1031 }
1032 }
1033
1034
1035 /**
1036 * Try using next and prev to navigate past the ends with wrap_ on
1037 * allowFreeSelect_ on and a disabled last row.
1038 */
1039 function testHiliteNextPrevWithDisabledLastRow_wrapAndAllowFreeSelect() {
1040 var ds = new MockDS();
1041 var rend = new TestRend();
1042 var select = new MockSelect();
1043 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1044 ac.setWrap(true);
1045 ac.setAllowFreeSelect(true);
1046
1047 // make sure 'next' and 'prev' don't explode before any token is set
1048 ac.hiliteNext();
1049 ac.hilitePrev();
1050 ac.setMaxMatches(4);
1051 assertEquals(0, rend.getRenderedRows().length);
1052
1053 // check a few times with disabled last row
1054 for (var i = 0; i < 3; ++i) {
1055 ac.setToken('');
1056 ac.setToken('h');
1057 assertEquals(3, rend.getRenderedRows().length);
1058 checkHilitedIndex(rend, 0);
1059 ac.hiliteNext();
1060 checkHilitedIndex(rend, 1);
1061 // try going over the edge since last row is disabled. Since free select is
1062 // on, this should go to -1
1063 ac.hiliteNext();
1064 checkHilitedIndex(rend, -1);
1065
1066 // go back down the list
1067 ac.hiliteNext();
1068 checkHilitedIndex(rend, 0);
1069 ac.hiliteNext();
1070 checkHilitedIndex(rend, 1);
1071
1072 // go back up the list.
1073 ac.hilitePrev();
1074 checkHilitedIndex(rend, 0);
1075 // go back above the first, free select.
1076 ac.hilitePrev();
1077 checkHilitedIndex(rend, -1);
1078 // wrap to the second last, since last is disabled.
1079 ac.hilitePrev();
1080 checkHilitedIndex(rend, 1);
1081 }
1082 }
1083
1084
1085 /**
1086 * Try using next and prev to navigate past the ends with wrap_ on
1087 * allowFreeSelect_ on AND turn autoHilite_ off.
1088 */
1089 function testHiliteNextPrev_wrapAndAllowFreeSelectNoAutoHilite() {
1090 var ds = new MockDS();
1091 var rend = new TestRend();
1092 var select = new MockSelect();
1093 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1094 ac.setWrap(true);
1095 ac.setAllowFreeSelect(true);
1096 ac.setAutoHilite(false);
1097
1098 // make sure 'next' and 'prev' don't explode before any token is set
1099 ac.hiliteNext();
1100 ac.hilitePrev();
1101 ac.setMaxMatches(4);
1102 assertEquals(0, rend.getRenderedRows().length);
1103
1104 // check a few times
1105 for (var i = 0; i < 3; ++i) {
1106 ac.setToken('');
1107 ac.setToken('the');
1108 assertEquals(4, rend.getRenderedRows().length);
1109 // check to see if we can select the last of the 4 items.
1110 // Initially nothing should be selected since autoHilite_ is off.
1111 checkHilitedIndex(rend, -1);
1112 ac.hilitePrev();
1113 checkHilitedIndex(rend, 3);
1114 ac.hiliteNext();
1115 checkHilitedIndex(rend, -1);
1116 ac.hiliteNext();
1117 checkHilitedIndex(rend, 0);
1118 ac.hiliteNext();
1119 checkHilitedIndex(rend, 1);
1120 ac.hiliteNext();
1121 checkHilitedIndex(rend, 2);
1122 ac.hiliteNext();
1123 checkHilitedIndex(rend, 3);
1124 // try going over the edge. Since free select is on, this should go
1125 // to -1.
1126 ac.hiliteNext();
1127 checkHilitedIndex(rend, -1);
1128
1129 // go back down the list
1130 ac.hiliteNext();
1131 checkHilitedIndex(rend, 0);
1132 ac.hiliteNext();
1133 checkHilitedIndex(rend, 1);
1134
1135 // go back up the list.
1136 ac.hilitePrev();
1137 checkHilitedIndex(rend, 0);
1138 // go back above the first, free select.
1139 ac.hilitePrev();
1140 checkHilitedIndex(rend, -1);
1141 // wrap to last
1142 ac.hilitePrev();
1143 checkHilitedIndex(rend, 3);
1144 }
1145 }
1146
1147
1148 /**
1149 * Try using next and prev to navigate past the ends with wrap_ on
1150 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled first row.
1151 */
1152 function
1153 testHiliteNextPrevWithDisabledFirstRow_wrapAndAllowFreeSelectNoAutoHilite() {
1154 var ds = new MockDS();
1155 var rend = new TestRend();
1156 var select = new MockSelect();
1157 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1158 ac.setWrap(true);
1159 ac.setAllowFreeSelect(true);
1160 ac.setAutoHilite(false);
1161
1162 // make sure 'next' and 'prev' don't explode before any token is set
1163 ac.hiliteNext();
1164 ac.hilitePrev();
1165 ac.setMaxMatches(4);
1166 assertEquals(0, rend.getRenderedRows().length);
1167
1168 // check a few times with disabled first row
1169 for (var i = 0; i < 3; ++i) {
1170 ac.setToken('');
1171 ac.setToken('edu');
1172 assertEquals(3, rend.getRenderedRows().length);
1173 // Initially nothing should be selected since autoHilite_ is off.
1174 checkHilitedIndex(rend, -1);
1175 ac.hilitePrev();
1176 checkHilitedIndex(rend, 2);
1177 ac.hiliteNext();
1178 checkHilitedIndex(rend, -1);
1179 ac.hiliteNext();
1180 // First row is disabled.
1181 checkHilitedIndex(rend, 1);
1182 ac.hiliteNext();
1183 checkHilitedIndex(rend, 2);
1184 // try going over the edge. Since free select is on, this should go to -1
1185 ac.hiliteNext();
1186 checkHilitedIndex(rend, -1);
1187
1188 // go back down the list, first row is disabled
1189 ac.hiliteNext();
1190 checkHilitedIndex(rend, 1);
1191 ac.hiliteNext();
1192 checkHilitedIndex(rend, 2);
1193
1194 // go back up the list.
1195 ac.hilitePrev();
1196 checkHilitedIndex(rend, 1);
1197 // go back above the first, free select.
1198 ac.hilitePrev();
1199 checkHilitedIndex(rend, -1);
1200 // wrap to last
1201 ac.hilitePrev();
1202 checkHilitedIndex(rend, 2);
1203 }
1204 }
1205
1206
1207 /**
1208 * Try using next and prev to navigate past the ends with wrap_ on
1209 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled middle row.
1210 */
1211 function
1212 testHiliteNextPrevWithDisabledMiddleRow_wrapAndAllowFreeSelectNoAutoHilite() {
1213 var ds = new MockDS();
1214 var rend = new TestRend();
1215 var select = new MockSelect();
1216 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1217 ac.setWrap(true);
1218 ac.setAllowFreeSelect(true);
1219 ac.setAutoHilite(false);
1220
1221 // make sure 'next' and 'prev' don't explode before any token is set
1222 ac.hiliteNext();
1223 ac.hilitePrev();
1224 ac.setMaxMatches(4);
1225 assertEquals(0, rend.getRenderedRows().length);
1226
1227 // check a few times with disabled middle row
1228 for (var i = 0; i < 3; ++i) {
1229 ac.setToken('');
1230 ac.setToken('u');
1231 assertEquals(3, rend.getRenderedRows().length);
1232 // Initially nothing should be selected since autoHilite_ is off.
1233 checkHilitedIndex(rend, -1);
1234 ac.hilitePrev();
1235 checkHilitedIndex(rend, 2);
1236 ac.hiliteNext();
1237 checkHilitedIndex(rend, -1);
1238 ac.hiliteNext();
1239 checkHilitedIndex(rend, 0);
1240 ac.hiliteNext();
1241 // Second row is disabled
1242 checkHilitedIndex(rend, 2);
1243 // try going over the edge. Since free select is on, this should go to -1.
1244 ac.hiliteNext();
1245 checkHilitedIndex(rend, -1);
1246
1247 // go back down the list
1248 ac.hiliteNext();
1249 checkHilitedIndex(rend, 0);
1250 ac.hiliteNext();
1251 // Second row is disabled.
1252 checkHilitedIndex(rend, 2);
1253
1254 // go back up the list.
1255 ac.hilitePrev();
1256 checkHilitedIndex(rend, 0);
1257 // go back above the first, free select.
1258 ac.hilitePrev();
1259 checkHilitedIndex(rend, -1);
1260 // wrap to last
1261 ac.hilitePrev();
1262 checkHilitedIndex(rend, 2);
1263 }
1264 }
1265
1266
1267 /**
1268 * Try using next and prev to navigate past the ends with wrap_ on
1269 * allowFreeSelect_ on AND turn autoHilite_ off, and a disabled last row.
1270 */
1271 function
1272 testHiliteNextPrevWithDisabledLastRow_wrapAndAllowFreeSelectNoAutoHilite() {
1273 var ds = new MockDS();
1274 var rend = new TestRend();
1275 var select = new MockSelect();
1276 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1277 ac.setWrap(true);
1278 ac.setAllowFreeSelect(true);
1279 ac.setAutoHilite(false);
1280
1281 // make sure 'next' and 'prev' don't explode before any token is set
1282 ac.hiliteNext();
1283 ac.hilitePrev();
1284 ac.setMaxMatches(4);
1285 assertEquals(0, rend.getRenderedRows().length);
1286
1287 // check a few times with disabled last row
1288 for (var i = 0; i < 3; ++i) {
1289 ac.setToken('');
1290 ac.setToken('h');
1291 assertEquals(3, rend.getRenderedRows().length);
1292 // Initially nothing should be selected since autoHilite_ is off.
1293 checkHilitedIndex(rend, -1);
1294 ac.hilitePrev();
1295 // Last row is disabled
1296 checkHilitedIndex(rend, 1);
1297 ac.hiliteNext();
1298 checkHilitedIndex(rend, -1);
1299 ac.hiliteNext();
1300 checkHilitedIndex(rend, 0);
1301 ac.hiliteNext();
1302 checkHilitedIndex(rend, 1);
1303 // try going over the edge. Since free select is on, this should go to -1.
1304 ac.hiliteNext();
1305 checkHilitedIndex(rend, -1);
1306
1307 // go back down the list
1308 ac.hiliteNext();
1309 checkHilitedIndex(rend, 0);
1310 ac.hiliteNext();
1311 checkHilitedIndex(rend, 1);
1312
1313 // go back up the list.
1314 ac.hilitePrev();
1315 checkHilitedIndex(rend, 0);
1316 // go back above the first, free select.
1317 ac.hilitePrev();
1318 checkHilitedIndex(rend, -1);
1319 // wrap to last
1320 ac.hilitePrev();
1321 checkHilitedIndex(rend, 1);
1322 }
1323 }
1324
1325 function testHiliteWithChangingNumberOfRows() {
1326 var ds = new MockDS();
1327 var rend = new TestRend();
1328 var select = new MockSelect();
1329 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1330 ac.setAutoHilite(true);
1331 ac.setMaxMatches(4);
1332
1333 ac.setToken('m');
1334 assertEquals(4, rend.getRenderedRows().length);
1335 checkHilitedIndex(rend, 0);
1336
1337 ac.setToken('ma');
1338 assertEquals(3, rend.getRenderedRows().length);
1339 checkHilitedIndex(rend, 0);
1340
1341 // Hilite the second element
1342 var id = rend.getRenderedRows()[1].id;
1343 ac.hiliteId(id);
1344
1345 ac.setToken('mar');
1346 assertEquals(1, rend.getRenderedRows().length);
1347 checkHilitedIndex(rend, 0);
1348
1349 ac.setToken('ma');
1350 assertEquals(3, rend.getRenderedRows().length);
1351 checkHilitedIndex(rend, 0);
1352
1353 // Hilite the second element
1354 var id = rend.getRenderedRows()[1].id;
1355 ac.hiliteId(id);
1356
1357 ac.setToken('m');
1358 assertEquals(4, rend.getRenderedRows().length);
1359 checkHilitedIndex(rend, 0);
1360 }
1361
1362
1363 /**
1364 * Checks that autohilite is disabled when there is no token; this allows the
1365 * user to tab out of an empty autocomplete.
1366 */
1367 function testNoAutoHiliteWhenTokenIsEmpty() {
1368 var ds = new MockDS();
1369 var rend = new TestRend();
1370 var select = new MockSelect();
1371 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1372 ac.setWrap(true);
1373 ac.setAllowFreeSelect(true);
1374 ac.setAutoHilite(true);
1375 ac.setMaxMatches(4);
1376
1377 ac.setToken('');
1378 assertEquals(4, rend.getRenderedRows().length);
1379 // No token; nothing should be hilited.
1380 checkHilitedIndex(rend, -1);
1381
1382 ac.setToken('the');
1383 assertEquals(4, rend.getRenderedRows().length);
1384 // Now there is a token, so the first row should be highlighted.
1385 checkHilitedIndex(rend, 0);
1386 }
1387
1388
1389 /**
1390 * Checks that opt_preserveHilited works.
1391 */
1392 function testPreserveHilitedWithoutAutoHilite() {
1393 var ds = new MockDS();
1394 var rend = new TestRend();
1395 var select = new MockSelect();
1396 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1397 ac.setWrap(true);
1398 ac.setAllowFreeSelect(true);
1399 ac.setMaxMatches(4);
1400 ac.setAutoHilite(false);
1401
1402 ac.setToken('m');
1403 assertEquals(4, rend.getRenderedRows().length);
1404 // No token; nothing should be hilited.
1405 checkHilitedIndex(rend, -1);
1406
1407 // Hilite the second element
1408 var id = rend.getRenderedRows()[1].id;
1409 ac.hiliteId(id);
1410
1411 checkHilitedIndex(rend, 1);
1412
1413 // Re-render and check if the second element is still hilited
1414 ac.renderRows(rend.getRenderedRows(), true /* preserve hilite */);
1415
1416 checkHilitedIndex(rend, 1);
1417
1418 // Re-render without preservation
1419 ac.renderRows(rend.getRenderedRows());
1420
1421 checkHilitedIndex(rend, -1);
1422 }
1423
1424
1425 /**
1426 * Checks that the autohilite argument "true" of the matcher is used.
1427 */
1428 function testAutoHiliteFromMatcherTrue() {
1429 var ds = new MockDS(true);
1430 var rend = new TestRend();
1431 var select = new MockSelect();
1432 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1433 ac.setWrap(true);
1434 ac.setAllowFreeSelect(true);
1435 ac.setAutoHilite(false); // Will be overruled.
1436 ac.setMaxMatches(4);
1437
1438 ac.setToken('the');
1439 assertEquals(4, rend.getRenderedRows().length);
1440 // The first row should be highlighted.
1441 checkHilitedIndex(rend, 0);
1442 }
1443
1444
1445 /**
1446 * Checks that the autohilite argument "false" of the matcher is used.
1447 */
1448 function testAutoHiliteFromMatcherFalse() {
1449 var ds = new MockDS(false);
1450 var rend = new TestRend();
1451 var select = new MockSelect();
1452 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1453 ac.setWrap(true);
1454 ac.setAllowFreeSelect(true);
1455 ac.setAutoHilite(true); // Will be overruled.
1456 ac.setMaxMatches(4);
1457
1458 ac.setToken('the');
1459 assertEquals(4, rend.getRenderedRows().length);
1460 // The first row should not be highlighted.
1461 checkHilitedIndex(rend, -1);
1462 }
1463
1464
1465 /**
1466 * Hilite using ids, the way mouse-based hiliting would work.
1467 */
1468 function testHiliteId() {
1469 var ds = new MockDS();
1470 var rend = new TestRend();
1471 var select = new MockSelect();
1472 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1473
1474 // check a few times
1475 for (var i = 0; i < 3; ++i) {
1476 ac.setToken('m');
1477 assertEquals(4, rend.getRenderedRows().length);
1478 // try hiliting all 3
1479 for (var x = 0; x < 4; ++x) {
1480 var id = rend.getRenderedRows()[x].id;
1481 ac.hiliteId(id);
1482 assertEquals(ac.getIdOfIndex_(x), id);
1483 }
1484 }
1485 }
1486
1487
1488 /**
1489 * Test selecting the hilited row
1490 */
1491 function testSelection() {
1492 var ds = new MockDS();
1493 var rend = new TestRend();
1494 var select = new MockSelect();
1495 var ac;
1496
1497 // try with default selection
1498 ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1499 ac.setToken('m');
1500 ac.selectHilited();
1501 assertEquals(
1502 '"Slartibartfast Theadore" <[email protected]>',
1503 select.selectedRow);
1504
1505 // try second item
1506 ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1507 ac.setToken('the');
1508 ac.hiliteNext();
1509 ac.selectHilited();
1510 assertEquals('"Ford Prefect" <[email protected]>', select.selectedRow);
1511 }
1512
1513
1514 /**
1515 * Dismiss when empty and non-empty
1516 */
1517 function testDismiss() {
1518 var ds = new MockDS();
1519 var rend = new TestRend();
1520 var select = new MockSelect();
1521
1522 // dismiss empty
1523 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1524 var dismissed = 0;
1525 handler.listen(ac, goog.ui.ac.AutoComplete.EventType.DISMISS, function() {
1526 dismissed++;
1527 });
1528 ac.dismiss();
1529 assertEquals(1, dismissed);
1530
1531 ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1532 ac.setToken('sir not seen in this picture');
1533 ac.dismiss();
1534
1535 // dismiss with contents
1536 ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1537 ac.setToken('t');
1538 ac.dismiss();
1539 }
1540
1541 function testTriggerSuggestionsOnUpdate() {
1542 var ds = new MockDS();
1543 var rend = new TestRend();
1544 var select = new MockSelect();
1545 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1546
1547 var dismissCalled = 0;
1548 rend.dismiss = function() { dismissCalled++; };
1549
1550 var updateCalled = 0;
1551 select.update = function(opt_force) { updateCalled++; };
1552
1553 // Normally, menu is dismissed after selecting row (without updating).
1554 ac.setToken('the');
1555 ac.selectHilited();
1556 assertEquals(1, dismissCalled);
1557 assertEquals(0, updateCalled);
1558
1559 // But not if we re-trigger on update.
1560 ac.setTriggerSuggestionsOnUpdate(true);
1561 ac.setToken('the');
1562 ac.selectHilited();
1563 assertEquals(1, dismissCalled);
1564 assertEquals(1, updateCalled);
1565 }
1566
1567 function testDispose() {
1568 var ds = new MockDS();
1569 var rend = new TestRend();
1570 var select = new MockSelect();
1571 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1572 ac.setToken('the');
1573 ac.dispose();
1574 }
1575
1576
1577 /**
1578 * Ensure that activedescendant is updated properly.
1579 */
1580 function testRolesAndStates() {
1581 function checkActiveDescendant(activeDescendant) {
1582 assertNotNull(inputElement);
1583 assertEquals(
1584 goog.a11y.aria.getActiveDescendant(inputElement), activeDescendant);
1585 }
1586 function checkRole(el, role) {
1587 assertNotNull(el);
1588 assertEquals(goog.a11y.aria.getRole(el), role);
1589 }
1590 var ds = new MockDS();
1591 var rend = new TestRend();
1592 var select = new MockSelect();
1593 var ac = new goog.ui.ac.AutoComplete(ds, rend, select);
1594 ac.setTarget(inputElement);
1595
1596 // initially activedescendant is not set
1597 checkActiveDescendant(null);
1598
1599 // highlight the matching row and check that activedescendant updates
1600 ac.setToken('');
1601 ac.setToken('the');
1602 ac.hiliteNext();
1603 checkActiveDescendant(rend.getHilitedRowDiv());
1604
1605 // highligted row should have a role of 'option'
1606 checkRole(rend.getHilitedRowDiv(), goog.a11y.aria.Role.OPTION);
1607
1608 // closing the autocomplete should clear activedescendant
1609 ac.dismiss();
1610 checkActiveDescendant(null);
1611 }
1612
1613 function testAttachInputWithAnchor() {
1614 var anchorElement =
1615 goog.dom.createDom(goog.dom.TagName.DIV, null, inputElement);
1616
1617 var mockRenderer = mockControl.createLooseMock(goog.ui.ac.Renderer, true);
1618 mockRenderer.setAnchorElement(anchorElement);
1619 var ignore = goog.testing.mockmatchers.ignoreArgument;
1620 mockRenderer.renderRows(ignore, ignore, inputElement);
1621
1622 var mockInputHandler =
1623 mockControl.createLooseMock(goog.ui.ac.InputHandler, true);
1624 mockInputHandler.attachInputs(inputElement);
1625
1626 mockControl.$replayAll();
1627 var autoComplete =
1628 new goog.ui.ac.AutoComplete(null, mockRenderer, mockInputHandler);
1629 autoComplete.attachInputWithAnchor(inputElement, anchorElement);
1630 autoComplete.setTarget(inputElement);
1631
1632 autoComplete.renderRows(['abc', 'def']);
1633 mockControl.$verifyAll();
1634 }
1635
1636 function testDetachInputWithAnchor() {
1637 var mockRenderer = mockControl.createLooseMock(goog.ui.ac.Renderer, true);
1638 var mockInputHandler =
1639 mockControl.createLooseMock(goog.ui.ac.InputHandler, true);
1640 var anchorElement =
1641 goog.dom.createDom(goog.dom.TagName.DIV, null, inputElement);
1642 var inputElement2 = goog.dom.createDom(
1643 goog.dom.TagName.INPUT, {type: goog.dom.InputType.TEXT});
1644 var anchorElement2 =
1645 goog.dom.createDom(goog.dom.TagName.DIV, null, inputElement2);
1646
1647 mockControl.$replayAll();
1648 var autoComplete =
1649 new goog.ui.ac.AutoComplete(null, mockRenderer, mockInputHandler);
1650
1651 autoComplete.attachInputWithAnchor(inputElement, anchorElement);
1652 autoComplete.attachInputWithAnchor(inputElement2, anchorElement2);
1653 autoComplete.detachInputs(inputElement, inputElement2);
1654
1655 assertFalse(goog.getUid(inputElement) in autoComplete.inputToAnchorMap_);
1656 assertFalse(goog.getUid(inputElement2) in autoComplete.inputToAnchorMap_);
1657 mockControl.$verifyAll();
1658 }