|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function AutoCompleteResult(aValues, aFinalCompleteValues) { |
|
6 this._values = aValues; |
|
7 this._finalCompleteValues = aFinalCompleteValues; |
|
8 this.defaultIndex = 0; |
|
9 } |
|
10 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
11 |
|
12 function AutoCompleteInput(aSearches) { |
|
13 this.searches = aSearches; |
|
14 this.popup.selectedIndex = -1; |
|
15 this.completeDefaultIndex = true; |
|
16 } |
|
17 AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype); |
|
18 |
|
19 function run_test() { |
|
20 run_next_test(); |
|
21 } |
|
22 |
|
23 add_test(function test_keyNavigation() { |
|
24 doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { |
|
25 do_check_eq(aController.input.textValue, "mozilla.com"); |
|
26 aController.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_RIGHT); |
|
27 do_check_eq(aController.input.textValue, "mozilla.com"); |
|
28 }); |
|
29 }); |
|
30 |
|
31 add_test(function test_handleEnter() { |
|
32 doSearch("moz", "mozilla.com", "http://www.mozilla.com", function(aController) { |
|
33 do_check_eq(aController.input.textValue, "mozilla.com"); |
|
34 aController.handleEnter(false); |
|
35 do_check_eq(aController.input.textValue, "http://www.mozilla.com"); |
|
36 }); |
|
37 }); |
|
38 |
|
39 function doSearch(aSearchString, aResultValue, aFinalCompleteValue, aOnCompleteCallback) { |
|
40 let search = new AutoCompleteSearchBase( |
|
41 "search", |
|
42 new AutoCompleteResult([ aResultValue ], [ aFinalCompleteValue ]) |
|
43 ); |
|
44 registerAutoCompleteSearch(search); |
|
45 |
|
46 let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
47 getService(Ci.nsIAutoCompleteController); |
|
48 |
|
49 // Make an AutoCompleteInput that uses our searches and confirms results. |
|
50 let input = new AutoCompleteInput([ search.name ]); |
|
51 input.textValue = aSearchString; |
|
52 |
|
53 // Caret must be at the end for autofill to happen. |
|
54 let strLen = aSearchString.length; |
|
55 input.selectTextRange(strLen, strLen); |
|
56 controller.input = input; |
|
57 controller.startSearch(aSearchString); |
|
58 |
|
59 input.onSearchComplete = function onSearchComplete() { |
|
60 aOnCompleteCallback(controller); |
|
61 |
|
62 // Clean up. |
|
63 unregisterAutoCompleteSearch(search); |
|
64 run_next_test(); |
|
65 }; |
|
66 } |