Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | * http://creativecommons.org/publicdomain/zero/1.0/ |
michael@0 | 4 | */ |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * Purpose of the test is to check that a stopSearch call comes always before a |
michael@0 | 8 | * startSearch call. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | /** |
michael@0 | 15 | * Dummy nsIAutoCompleteInput source that returns |
michael@0 | 16 | * the given list of AutoCompleteSearch names. |
michael@0 | 17 | * |
michael@0 | 18 | * Implements only the methods needed for this test. |
michael@0 | 19 | */ |
michael@0 | 20 | function AutoCompleteInput(aSearches) |
michael@0 | 21 | { |
michael@0 | 22 | this.searches = aSearches; |
michael@0 | 23 | } |
michael@0 | 24 | AutoCompleteInput.prototype = { |
michael@0 | 25 | constructor: AutoCompleteInput, |
michael@0 | 26 | minResultsForPopup: 0, |
michael@0 | 27 | timeout: 10, |
michael@0 | 28 | searchParam: "", |
michael@0 | 29 | textValue: "hello", |
michael@0 | 30 | disableAutoComplete: false, |
michael@0 | 31 | completeDefaultIndex: false, |
michael@0 | 32 | set popupOpen(val) { return val; }, // ignore |
michael@0 | 33 | get popupOpen() { return false; }, |
michael@0 | 34 | get searchCount() { return this.searches.length; }, |
michael@0 | 35 | getSearchAt: function(aIndex) { return this.searches[aIndex]; }, |
michael@0 | 36 | onSearchBegin: function() {}, |
michael@0 | 37 | onSearchComplete: function() {}, |
michael@0 | 38 | onTextReverted: function () {}, |
michael@0 | 39 | onTextEntered: function () {}, |
michael@0 | 40 | popup: { |
michael@0 | 41 | selectBy: function() {}, |
michael@0 | 42 | invalidate: function() {}, |
michael@0 | 43 | set selectedIndex(val) { return val; }, // ignore |
michael@0 | 44 | get selectedIndex() { return -1 }, |
michael@0 | 45 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompletePopup]) |
michael@0 | 46 | }, |
michael@0 | 47 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput]) |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | |
michael@0 | 51 | /** |
michael@0 | 52 | * nsIAutoCompleteSearch implementation. |
michael@0 | 53 | */ |
michael@0 | 54 | function AutoCompleteSearch(aName) |
michael@0 | 55 | { |
michael@0 | 56 | this.name = aName; |
michael@0 | 57 | } |
michael@0 | 58 | AutoCompleteSearch.prototype = { |
michael@0 | 59 | constructor: AutoCompleteSearch, |
michael@0 | 60 | stopSearchInvoked: true, |
michael@0 | 61 | startSearch: function(aSearchString, aSearchParam, aPreviousResult, aListener) |
michael@0 | 62 | { |
michael@0 | 63 | print("Check stop search has been called"); |
michael@0 | 64 | do_check_true(this.stopSearchInvoked); |
michael@0 | 65 | this.stopSearchInvoked = false; |
michael@0 | 66 | }, |
michael@0 | 67 | stopSearch: function() |
michael@0 | 68 | { |
michael@0 | 69 | this.stopSearchInvoked = true; |
michael@0 | 70 | }, |
michael@0 | 71 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 72 | Ci.nsIFactory |
michael@0 | 73 | , Ci.nsIAutoCompleteSearch |
michael@0 | 74 | ]), |
michael@0 | 75 | createInstance: function(outer, iid) |
michael@0 | 76 | { |
michael@0 | 77 | return this.QueryInterface(iid); |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | /** |
michael@0 | 83 | * Helper to register an AutoCompleteSearch with the given name. |
michael@0 | 84 | * Allows the AutoCompleteController to find the search. |
michael@0 | 85 | */ |
michael@0 | 86 | function registerAutoCompleteSearch(aSearch) |
michael@0 | 87 | { |
michael@0 | 88 | let name = "@mozilla.org/autocomplete/search;1?name=" + aSearch.name; |
michael@0 | 89 | let uuidGenerator = Cc["@mozilla.org/uuid-generator;1"]. |
michael@0 | 90 | getService(Ci.nsIUUIDGenerator); |
michael@0 | 91 | let cid = uuidGenerator.generateUUID(); |
michael@0 | 92 | let desc = "Test AutoCompleteSearch"; |
michael@0 | 93 | let componentManager = Components.manager |
michael@0 | 94 | .QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 95 | componentManager.registerFactory(cid, desc, name, aSearch); |
michael@0 | 96 | // Keep the id on the object so we can unregister later |
michael@0 | 97 | aSearch.cid = cid; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | |
michael@0 | 101 | /** |
michael@0 | 102 | * Helper to unregister an AutoCompleteSearch. |
michael@0 | 103 | */ |
michael@0 | 104 | function unregisterAutoCompleteSearch(aSearch) { |
michael@0 | 105 | let componentManager = Components.manager |
michael@0 | 106 | .QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 107 | componentManager.unregisterFactory(aSearch.cid, aSearch); |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | let gTests = [ |
michael@0 | 112 | function(controller) { |
michael@0 | 113 | print("handleText"); |
michael@0 | 114 | controller.input.textValue = "hel"; |
michael@0 | 115 | controller.handleText(); |
michael@0 | 116 | }, |
michael@0 | 117 | function(controller) { |
michael@0 | 118 | print("handleStartComposition"); |
michael@0 | 119 | controller.handleStartComposition(); |
michael@0 | 120 | }, |
michael@0 | 121 | function(controller) { |
michael@0 | 122 | print("handleEndComposition"); |
michael@0 | 123 | controller.handleEndComposition(); |
michael@0 | 124 | // an input event always follows compositionend event. |
michael@0 | 125 | controller.handleText(); |
michael@0 | 126 | }, |
michael@0 | 127 | function(controller) { |
michael@0 | 128 | print("handleEscape"); |
michael@0 | 129 | controller.handleEscape(); |
michael@0 | 130 | }, |
michael@0 | 131 | function(controller) { |
michael@0 | 132 | print("handleEnter"); |
michael@0 | 133 | controller.handleEnter(false); |
michael@0 | 134 | }, |
michael@0 | 135 | function(controller) { |
michael@0 | 136 | print("handleTab"); |
michael@0 | 137 | controller.handleTab(); |
michael@0 | 138 | }, |
michael@0 | 139 | |
michael@0 | 140 | function(controller) { |
michael@0 | 141 | print("handleKeyNavigation"); |
michael@0 | 142 | controller.handleKeyNavigation(Ci.nsIDOMKeyEvent.DOM_VK_UP); |
michael@0 | 143 | }, |
michael@0 | 144 | ]; |
michael@0 | 145 | |
michael@0 | 146 | |
michael@0 | 147 | let gSearch; |
michael@0 | 148 | let gCurrentTest; |
michael@0 | 149 | function run_test() { |
michael@0 | 150 | // Make an AutoCompleteSearch that always returns nothing |
michael@0 | 151 | gSearch = new AutoCompleteSearch("test"); |
michael@0 | 152 | registerAutoCompleteSearch(gSearch); |
michael@0 | 153 | |
michael@0 | 154 | let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
michael@0 | 155 | getService(Ci.nsIAutoCompleteController); |
michael@0 | 156 | |
michael@0 | 157 | // Make an AutoCompleteInput that uses our search. |
michael@0 | 158 | let input = new AutoCompleteInput([gSearch.name]); |
michael@0 | 159 | controller.input = input; |
michael@0 | 160 | |
michael@0 | 161 | input.onSearchBegin = function() { |
michael@0 | 162 | do_execute_soon(function() { |
michael@0 | 163 | gCurrentTest(controller); |
michael@0 | 164 | }); |
michael@0 | 165 | }; |
michael@0 | 166 | input.onSearchComplete = function() { |
michael@0 | 167 | run_next_test(controller); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | // Search is asynchronous, so don't let the test finish immediately |
michael@0 | 171 | do_test_pending(); |
michael@0 | 172 | |
michael@0 | 173 | run_next_test(controller); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | function run_next_test(controller) { |
michael@0 | 177 | if (gTests.length == 0) { |
michael@0 | 178 | unregisterAutoCompleteSearch(gSearch); |
michael@0 | 179 | controller.stopSearch(); |
michael@0 | 180 | controller.input = null; |
michael@0 | 181 | do_test_finished(); |
michael@0 | 182 | return; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | gCurrentTest = gTests.shift(); |
michael@0 | 186 | controller.startSearch("hello"); |
michael@0 | 187 | } |