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