|
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 AutoCompleteTypeAheadResult(aValues, aFinalCompleteValues) { |
|
6 this._values = aValues; |
|
7 this._finalCompleteValues = aFinalCompleteValues; |
|
8 this.defaultIndex = 0; |
|
9 this._typeAheadResult = true; |
|
10 } |
|
11 AutoCompleteTypeAheadResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
12 |
|
13 function AutoCompleteResult(aValues) { |
|
14 this._values = aValues; |
|
15 } |
|
16 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype); |
|
17 |
|
18 function AutoCompleteInput(aSearches) { |
|
19 this.searches = aSearches; |
|
20 this.popupOpen = true; |
|
21 this.completeDefaultIndex = true; |
|
22 this.completeSelectedIndex = true; |
|
23 } |
|
24 AutoCompleteInput.prototype = Object.create(AutoCompleteInputBase.prototype); |
|
25 |
|
26 function run_test() { |
|
27 run_next_test(); |
|
28 } |
|
29 |
|
30 add_test(function test_handleEnter() { |
|
31 doSearch("moz", function(aController) { |
|
32 do_check_eq(aController.input.textValue, "mozilla.com"); |
|
33 aController.handleEnter(true); |
|
34 do_check_eq(aController.input.textValue, "mozilla.org"); |
|
35 }); |
|
36 }); |
|
37 |
|
38 function doSearch(aSearchString, aOnCompleteCallback) { |
|
39 let typeAheadSearch = new AutoCompleteSearchBase( |
|
40 "typeAheadSearch", |
|
41 new AutoCompleteTypeAheadResult([ "mozilla.com" ], [ "http://www.mozilla.com" ]) |
|
42 ); |
|
43 registerAutoCompleteSearch(typeAheadSearch); |
|
44 |
|
45 let search = new AutoCompleteSearchBase( |
|
46 "search", |
|
47 new AutoCompleteResult([ "mozilla.org" ]) |
|
48 ); |
|
49 registerAutoCompleteSearch(search); |
|
50 |
|
51 let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
|
52 getService(Ci.nsIAutoCompleteController); |
|
53 |
|
54 // Make an AutoCompleteInput that uses our searches and confirms results. |
|
55 let input = new AutoCompleteInput([ typeAheadSearch.name, search.name ]); |
|
56 input.textValue = aSearchString; |
|
57 |
|
58 // Caret must be at the end for autofill to happen. |
|
59 let strLen = aSearchString.length; |
|
60 input.selectTextRange(strLen, strLen); |
|
61 controller.input = input; |
|
62 controller.startSearch(aSearchString); |
|
63 |
|
64 input.onSearchComplete = function onSearchComplete() { |
|
65 aOnCompleteCallback(controller); |
|
66 |
|
67 // Clean up. |
|
68 unregisterAutoCompleteSearch(search); |
|
69 run_next_test(); |
|
70 }; |
|
71 } |