toolkit/components/autocomplete/tests/unit/test_immediate_search.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5
michael@0 6 function AutoCompleteImmediateSearch(aName, aResult) {
michael@0 7 this.name = aName;
michael@0 8 this._result = aResult;
michael@0 9 }
michael@0 10 AutoCompleteImmediateSearch.prototype = Object.create(AutoCompleteSearchBase.prototype);
michael@0 11 AutoCompleteImmediateSearch.prototype.searchType =
michael@0 12 Ci.nsIAutoCompleteSearchDescriptor.SEARCH_TYPE_IMMEDIATE;
michael@0 13 AutoCompleteImmediateSearch.prototype.QueryInterface =
michael@0 14 XPCOMUtils.generateQI([Ci.nsIFactory,
michael@0 15 Ci.nsIAutoCompleteSearch,
michael@0 16 Ci.nsIAutoCompleteSearchDescriptor]);
michael@0 17
michael@0 18 function AutoCompleteDelayedSearch(aName, aResult) {
michael@0 19 this.name = aName;
michael@0 20 this._result = aResult;
michael@0 21 }
michael@0 22 AutoCompleteDelayedSearch.prototype = Object.create(AutoCompleteSearchBase.prototype);
michael@0 23
michael@0 24 function AutoCompleteResult(aValues, aDefaultIndex) {
michael@0 25 this._values = aValues;
michael@0 26 this.defaultIndex = aDefaultIndex;
michael@0 27 }
michael@0 28 AutoCompleteResult.prototype = Object.create(AutoCompleteResultBase.prototype);
michael@0 29
michael@0 30 function run_test() {
michael@0 31 run_next_test();
michael@0 32 }
michael@0 33
michael@0 34 /**
michael@0 35 * An immediate search should be executed synchronously.
michael@0 36 */
michael@0 37 add_test(function test_immediate_search() {
michael@0 38 let immediateResults = ["mozillaTest"];
michael@0 39 let inputStr = "moz";
michael@0 40
michael@0 41 let immediateSearch = new AutoCompleteImmediateSearch(
michael@0 42 "immediate", new AutoCompleteResult(["moz-immediate"], 0));
michael@0 43 registerAutoCompleteSearch(immediateSearch);
michael@0 44 let delayedSearch = new AutoCompleteDelayedSearch(
michael@0 45 "delayed", new AutoCompleteResult(["moz-delayed"], 0));
michael@0 46 registerAutoCompleteSearch(delayedSearch);
michael@0 47
michael@0 48 let controller = Cc["@mozilla.org/autocomplete/controller;1"].
michael@0 49 getService(Ci.nsIAutoCompleteController);
michael@0 50
michael@0 51 let input = new AutoCompleteInputBase([delayedSearch.name,
michael@0 52 immediateSearch.name]);
michael@0 53 input.completeDefaultIndex = true;
michael@0 54 input.textValue = inputStr;
michael@0 55
michael@0 56 // Caret must be at the end. Autofill doesn't happen unless you're typing
michael@0 57 // characters at the end.
michael@0 58 let strLen = inputStr.length;
michael@0 59 input.selectTextRange(strLen, strLen);
michael@0 60
michael@0 61 controller.input = input;
michael@0 62 controller.startSearch(inputStr);
michael@0 63
michael@0 64 // Immediately check the result, the immediate search should have finished.
michael@0 65 do_check_eq(input.textValue, "moz-immediate");
michael@0 66
michael@0 67 // Wait for both queries to finish.
michael@0 68 input.onSearchComplete = function() {
michael@0 69 // Sanity check.
michael@0 70 do_check_eq(input.textValue, "moz-immediate");
michael@0 71
michael@0 72 unregisterAutoCompleteSearch(immediateSearch);
michael@0 73 unregisterAutoCompleteSearch(delayedSearch);
michael@0 74 run_next_test();
michael@0 75 };
michael@0 76 });
michael@0 77
michael@0 78 /**
michael@0 79 * An immediate search should be executed before any delayed search.
michael@0 80 */
michael@0 81 add_test(function test_immediate_search_notimeout() {
michael@0 82 let immediateResults = ["mozillaTest"];
michael@0 83 let inputStr = "moz";
michael@0 84
michael@0 85 let immediateSearch = new AutoCompleteImmediateSearch(
michael@0 86 "immediate", new AutoCompleteResult(["moz-immediate"], 0));
michael@0 87 registerAutoCompleteSearch(immediateSearch);
michael@0 88
michael@0 89 let delayedSearch = new AutoCompleteDelayedSearch(
michael@0 90 "delayed", new AutoCompleteResult(["moz-delayed"], 0));
michael@0 91 registerAutoCompleteSearch(delayedSearch);
michael@0 92
michael@0 93 let controller = Cc["@mozilla.org/autocomplete/controller;1"].
michael@0 94 getService(Ci.nsIAutoCompleteController);
michael@0 95
michael@0 96 let input = new AutoCompleteInputBase([delayedSearch.name,
michael@0 97 immediateSearch.name]);
michael@0 98 input.completeDefaultIndex = true;
michael@0 99 input.textValue = inputStr;
michael@0 100 input.timeout = 0;
michael@0 101
michael@0 102 // Caret must be at the end. Autofill doesn't happen unless you're typing
michael@0 103 // characters at the end.
michael@0 104 let strLen = inputStr.length;
michael@0 105 input.selectTextRange(strLen, strLen);
michael@0 106
michael@0 107 controller.input = input;
michael@0 108 let complete = false;
michael@0 109 input.onSearchComplete = function() {
michael@0 110 complete = true;
michael@0 111 };
michael@0 112 controller.startSearch(inputStr);
michael@0 113 do_check_true(complete);
michael@0 114
michael@0 115 // Immediately check the result, the immediate search should have finished.
michael@0 116 do_check_eq(input.textValue, "moz-immediate");
michael@0 117
michael@0 118 unregisterAutoCompleteSearch(immediateSearch);
michael@0 119 unregisterAutoCompleteSearch(delayedSearch);
michael@0 120 run_next_test();
michael@0 121 });
michael@0 122
michael@0 123 /**
michael@0 124 * A delayed search should be executed synchronously with a zero timeout.
michael@0 125 */
michael@0 126 add_test(function test_delayed_search_notimeout() {
michael@0 127 let immediateResults = ["mozillaTest"];
michael@0 128 let inputStr = "moz";
michael@0 129
michael@0 130 let delayedSearch = new AutoCompleteDelayedSearch(
michael@0 131 "delayed", new AutoCompleteResult(["moz-delayed"], 0));
michael@0 132 registerAutoCompleteSearch(delayedSearch);
michael@0 133
michael@0 134 let controller = Cc["@mozilla.org/autocomplete/controller;1"].
michael@0 135 getService(Ci.nsIAutoCompleteController);
michael@0 136
michael@0 137 let input = new AutoCompleteInputBase([delayedSearch.name]);
michael@0 138 input.completeDefaultIndex = true;
michael@0 139 input.textValue = inputStr;
michael@0 140 input.timeout = 0;
michael@0 141
michael@0 142 // Caret must be at the end. Autofill doesn't happen unless you're typing
michael@0 143 // characters at the end.
michael@0 144 let strLen = inputStr.length;
michael@0 145 input.selectTextRange(strLen, strLen);
michael@0 146
michael@0 147 controller.input = input;
michael@0 148 let complete = false;
michael@0 149 input.onSearchComplete = function() {
michael@0 150 complete = true;
michael@0 151 };
michael@0 152 controller.startSearch(inputStr);
michael@0 153 do_check_true(complete);
michael@0 154
michael@0 155 // Immediately check the result, the delayed search should have finished.
michael@0 156 do_check_eq(input.textValue, "moz-delayed");
michael@0 157
michael@0 158 unregisterAutoCompleteSearch(delayedSearch);
michael@0 159 run_next_test();
michael@0 160 });

mercurial