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 | /* 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 |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const Ci = Components.interfaces; |
michael@0 | 6 | const Cc = Components.classes; |
michael@0 | 7 | const Cr = Components.results; |
michael@0 | 8 | const Cu = Components.utils; |
michael@0 | 9 | |
michael@0 | 10 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | // Import common head. |
michael@0 | 13 | let (commonFile = do_get_file("../head_common.js", false)) { |
michael@0 | 14 | let uri = Services.io.newFileURI(commonFile); |
michael@0 | 15 | Services.scriptloader.loadSubScript(uri.spec, this); |
michael@0 | 16 | } |
michael@0 | 17 | |
michael@0 | 18 | // Put any other stuff relative to this test folder below. |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | /** |
michael@0 | 22 | * Header file for autocomplete testcases that create a set of pages with uris, |
michael@0 | 23 | * titles, tags and tests that a given search term matches certain pages. |
michael@0 | 24 | */ |
michael@0 | 25 | |
michael@0 | 26 | let current_test = 0; |
michael@0 | 27 | |
michael@0 | 28 | function AutoCompleteInput(aSearches) { |
michael@0 | 29 | this.searches = aSearches; |
michael@0 | 30 | } |
michael@0 | 31 | AutoCompleteInput.prototype = { |
michael@0 | 32 | timeout: 10, |
michael@0 | 33 | textValue: "", |
michael@0 | 34 | searches: null, |
michael@0 | 35 | searchParam: "", |
michael@0 | 36 | popupOpen: false, |
michael@0 | 37 | minResultsForPopup: 0, |
michael@0 | 38 | invalidate: function() {}, |
michael@0 | 39 | disableAutoComplete: false, |
michael@0 | 40 | completeDefaultIndex: false, |
michael@0 | 41 | get popup() { return this; }, |
michael@0 | 42 | onSearchBegin: function() {}, |
michael@0 | 43 | onSearchComplete: function() {}, |
michael@0 | 44 | setSelectedIndex: function() {}, |
michael@0 | 45 | get searchCount() { return this.searches.length; }, |
michael@0 | 46 | getSearchAt: function(aIndex) this.searches[aIndex], |
michael@0 | 47 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 48 | Ci.nsIAutoCompleteInput, |
michael@0 | 49 | Ci.nsIAutoCompletePopup, |
michael@0 | 50 | ]) |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | function toURI(aSpec) { |
michael@0 | 54 | return uri(aSpec); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | let appendTags = true; |
michael@0 | 58 | // Helper to turn off tag matching in results |
michael@0 | 59 | function ignoreTags() |
michael@0 | 60 | { |
michael@0 | 61 | print("Ignoring tags from results"); |
michael@0 | 62 | appendTags = false; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | function ensure_results(aSearch, aExpected) |
michael@0 | 66 | { |
michael@0 | 67 | let controller = Cc["@mozilla.org/autocomplete/controller;1"]. |
michael@0 | 68 | getService(Ci.nsIAutoCompleteController); |
michael@0 | 69 | |
michael@0 | 70 | // Make an AutoCompleteInput that uses our searches |
michael@0 | 71 | // and confirms results on search complete |
michael@0 | 72 | let input = new AutoCompleteInput(["history"]); |
michael@0 | 73 | |
michael@0 | 74 | controller.input = input; |
michael@0 | 75 | |
michael@0 | 76 | if (typeof kSearchParam == "string") |
michael@0 | 77 | input.searchParam = kSearchParam; |
michael@0 | 78 | |
michael@0 | 79 | let numSearchesStarted = 0; |
michael@0 | 80 | input.onSearchBegin = function() { |
michael@0 | 81 | numSearchesStarted++; |
michael@0 | 82 | do_check_eq(numSearchesStarted, 1); |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | input.onSearchComplete = function() { |
michael@0 | 86 | do_check_eq(numSearchesStarted, 1); |
michael@0 | 87 | aExpected = aExpected.slice(); |
michael@0 | 88 | |
michael@0 | 89 | // Check to see the expected uris and titles match up (in any order) |
michael@0 | 90 | for (let i = 0; i < controller.matchCount; i++) { |
michael@0 | 91 | let value = controller.getValueAt(i); |
michael@0 | 92 | let comment = controller.getCommentAt(i); |
michael@0 | 93 | |
michael@0 | 94 | print("Looking for '" + value + "', '" + comment + "' in expected results..."); |
michael@0 | 95 | let j; |
michael@0 | 96 | for (j = 0; j < aExpected.length; j++) { |
michael@0 | 97 | // Skip processed expected results |
michael@0 | 98 | if (aExpected[j] == undefined) |
michael@0 | 99 | continue; |
michael@0 | 100 | |
michael@0 | 101 | let [uri, title, tags] = gPages[aExpected[j]]; |
michael@0 | 102 | |
michael@0 | 103 | // Load the real uri and titles and tags if necessary |
michael@0 | 104 | uri = toURI(kURIs[uri]).spec; |
michael@0 | 105 | title = kTitles[title]; |
michael@0 | 106 | if (tags && appendTags) |
michael@0 | 107 | title += " \u2013 " + tags.map(function(aTag) kTitles[aTag]); |
michael@0 | 108 | print("Checking against expected '" + uri + "', '" + title + "'..."); |
michael@0 | 109 | |
michael@0 | 110 | // Got a match on both uri and title? |
michael@0 | 111 | if (uri == value && title == comment) { |
michael@0 | 112 | print("Got it at index " + j + "!!"); |
michael@0 | 113 | // Make it undefined so we don't process it again |
michael@0 | 114 | aExpected[j] = undefined; |
michael@0 | 115 | break; |
michael@0 | 116 | } |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | // We didn't hit the break, so we must have not found it |
michael@0 | 120 | if (j == aExpected.length) |
michael@0 | 121 | do_throw("Didn't find the current result ('" + value + "', '" + comment + "') in expected: " + aExpected); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | // Make sure we have the right number of results |
michael@0 | 125 | print("Expecting " + aExpected.length + " results; got " + |
michael@0 | 126 | controller.matchCount + " results"); |
michael@0 | 127 | do_check_eq(controller.matchCount, aExpected.length); |
michael@0 | 128 | |
michael@0 | 129 | // If we expect results, make sure we got matches |
michael@0 | 130 | do_check_eq(controller.searchStatus, aExpected.length ? |
michael@0 | 131 | Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH : |
michael@0 | 132 | Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); |
michael@0 | 133 | |
michael@0 | 134 | // Fetch the next test if we have more |
michael@0 | 135 | if (++current_test < gTests.length) |
michael@0 | 136 | run_test(); |
michael@0 | 137 | |
michael@0 | 138 | do_test_finished(); |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | print("Searching for.. '" + aSearch + "'"); |
michael@0 | 142 | controller.startSearch(aSearch); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | // Get history services |
michael@0 | 146 | var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. |
michael@0 | 147 | getService(Ci.nsINavHistoryService); |
michael@0 | 148 | var bhist = histsvc.QueryInterface(Ci.nsIBrowserHistory); |
michael@0 | 149 | var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. |
michael@0 | 150 | getService(Ci.nsINavBookmarksService); |
michael@0 | 151 | var tagsvc = Cc["@mozilla.org/browser/tagging-service;1"]. |
michael@0 | 152 | getService(Ci.nsITaggingService); |
michael@0 | 153 | var iosvc = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 154 | getService(Ci.nsIIOService); |
michael@0 | 155 | var prefs = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 156 | getService(Ci.nsIPrefBranch); |
michael@0 | 157 | |
michael@0 | 158 | // Some date not too long ago |
michael@0 | 159 | let gDate = new Date(Date.now() - 1000 * 60 * 60) * 1000; |
michael@0 | 160 | // Store the page info for each uri |
michael@0 | 161 | let gPages = []; |
michael@0 | 162 | |
michael@0 | 163 | // Initialization tasks to be run before the next test |
michael@0 | 164 | let gNextTestSetupTasks = []; |
michael@0 | 165 | |
michael@0 | 166 | /** |
michael@0 | 167 | * Adds a page, and creates various properties for it depending on the |
michael@0 | 168 | * parameters passed in. This function will also add one visit, unless |
michael@0 | 169 | * aNoVisit is true. |
michael@0 | 170 | * |
michael@0 | 171 | * @param aURI |
michael@0 | 172 | * An index into kURIs that holds the string for the URI we are to add a |
michael@0 | 173 | * page for. |
michael@0 | 174 | * @param aTitle |
michael@0 | 175 | * An index into kTitles that holds the string for the title we are to |
michael@0 | 176 | * associate with the specified URI. |
michael@0 | 177 | * @param aBook [optional] |
michael@0 | 178 | * An index into kTitles that holds the string for the title we are to |
michael@0 | 179 | * associate with the bookmark. If this is undefined, no bookmark is |
michael@0 | 180 | * created. |
michael@0 | 181 | * @param aTags [optional] |
michael@0 | 182 | * An array of indexes into kTitles that hold the strings for the tags we |
michael@0 | 183 | * are to associate with the URI. If this is undefined (or aBook is), no |
michael@0 | 184 | * tags are added. |
michael@0 | 185 | * @param aKey [optional] |
michael@0 | 186 | * A string to associate as the keyword for this bookmark. aBook must be |
michael@0 | 187 | * a valid index into kTitles for this to be checked and used. |
michael@0 | 188 | * @param aTransitionType [optional] |
michael@0 | 189 | * The transition type to use when adding the visit. The default is |
michael@0 | 190 | * nsINavHistoryService::TRANSITION_LINK. |
michael@0 | 191 | * @param aNoVisit [optional] |
michael@0 | 192 | * If true, no visit is added for the URI. If false or undefined, a |
michael@0 | 193 | * visit is added. |
michael@0 | 194 | */ |
michael@0 | 195 | function addPageBook(aURI, aTitle, aBook, aTags, aKey, aTransitionType, aNoVisit) |
michael@0 | 196 | { |
michael@0 | 197 | gNextTestSetupTasks.push([task_addPageBook, arguments]); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | function task_addPageBook(aURI, aTitle, aBook, aTags, aKey, aTransitionType, aNoVisit) |
michael@0 | 201 | { |
michael@0 | 202 | // Add a page entry for the current uri |
michael@0 | 203 | gPages[aURI] = [aURI, aBook != undefined ? aBook : aTitle, aTags]; |
michael@0 | 204 | |
michael@0 | 205 | let uri = toURI(kURIs[aURI]); |
michael@0 | 206 | let title = kTitles[aTitle]; |
michael@0 | 207 | |
michael@0 | 208 | let out = [aURI, aTitle, aBook, aTags, aKey]; |
michael@0 | 209 | out.push("\nuri=" + kURIs[aURI]); |
michael@0 | 210 | out.push("\ntitle=" + title); |
michael@0 | 211 | |
michael@0 | 212 | // Add the page and a visit if we need to |
michael@0 | 213 | if (!aNoVisit) { |
michael@0 | 214 | yield promiseAddVisits({ |
michael@0 | 215 | uri: uri, |
michael@0 | 216 | transition: aTransitionType || TRANSITION_LINK, |
michael@0 | 217 | visitDate: gDate, |
michael@0 | 218 | title: title |
michael@0 | 219 | }); |
michael@0 | 220 | out.push("\nwith visit"); |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | // Add a bookmark if we need to |
michael@0 | 224 | if (aBook != undefined) { |
michael@0 | 225 | let book = kTitles[aBook]; |
michael@0 | 226 | let bmid = bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, uri, |
michael@0 | 227 | bmsvc.DEFAULT_INDEX, book); |
michael@0 | 228 | out.push("\nbook=" + book); |
michael@0 | 229 | |
michael@0 | 230 | // Add a keyword to the bookmark if we need to |
michael@0 | 231 | if (aKey != undefined) |
michael@0 | 232 | bmsvc.setKeywordForBookmark(bmid, aKey); |
michael@0 | 233 | |
michael@0 | 234 | // Add tags if we need to |
michael@0 | 235 | if (aTags != undefined && aTags.length > 0) { |
michael@0 | 236 | // Convert each tag index into the title |
michael@0 | 237 | let tags = aTags.map(function(aTag) kTitles[aTag]); |
michael@0 | 238 | tagsvc.tagURI(uri, tags); |
michael@0 | 239 | out.push("\ntags=" + tags); |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | print("\nAdding page/book/tag: " + out.join(", ")); |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | function run_test() { |
michael@0 | 247 | print("\n"); |
michael@0 | 248 | // always search in history + bookmarks, no matter what the default is |
michael@0 | 249 | prefs.setIntPref("browser.urlbar.search.sources", 3); |
michael@0 | 250 | prefs.setIntPref("browser.urlbar.default.behavior", 0); |
michael@0 | 251 | |
michael@0 | 252 | // Search is asynchronous, so don't let the test finish immediately |
michael@0 | 253 | do_test_pending(); |
michael@0 | 254 | |
michael@0 | 255 | // Load the test and print a description then run the test |
michael@0 | 256 | let [description, search, expected, func] = gTests[current_test]; |
michael@0 | 257 | print(description); |
michael@0 | 258 | |
michael@0 | 259 | // By default assume we want to match tags |
michael@0 | 260 | appendTags = true; |
michael@0 | 261 | |
michael@0 | 262 | // Do an extra function if necessary |
michael@0 | 263 | if (func) |
michael@0 | 264 | func(); |
michael@0 | 265 | |
michael@0 | 266 | Task.spawn(function () { |
michael@0 | 267 | // Iterate over all tasks and execute them |
michael@0 | 268 | for (let [, [fn, args]] in Iterator(gNextTestSetupTasks)) { |
michael@0 | 269 | yield fn.apply(this, args); |
michael@0 | 270 | }; |
michael@0 | 271 | |
michael@0 | 272 | // Clean up to allow tests to register more functions. |
michael@0 | 273 | gNextTestSetupTasks = []; |
michael@0 | 274 | |
michael@0 | 275 | // At this point frecency could still be updating due to latest pages |
michael@0 | 276 | // updates. This is not a problem in real life, but autocomplete tests |
michael@0 | 277 | // should return reliable resultsets, thus we have to wait. |
michael@0 | 278 | yield promiseAsyncUpdates(); |
michael@0 | 279 | |
michael@0 | 280 | }).then(function () ensure_results(search, expected), |
michael@0 | 281 | do_report_unexpected_exception); |
michael@0 | 282 | } |
michael@0 | 283 | |
michael@0 | 284 | // Utility function to remove history pages |
michael@0 | 285 | function removePages(aURIs) |
michael@0 | 286 | { |
michael@0 | 287 | gNextTestSetupTasks.push([do_removePages, arguments]); |
michael@0 | 288 | } |
michael@0 | 289 | |
michael@0 | 290 | function do_removePages(aURIs) |
michael@0 | 291 | { |
michael@0 | 292 | for each (let uri in aURIs) |
michael@0 | 293 | histsvc.removePage(toURI(kURIs[uri])); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | // Utility function to mark pages as typed |
michael@0 | 297 | function markTyped(aURIs, aTitle) |
michael@0 | 298 | { |
michael@0 | 299 | gNextTestSetupTasks.push([task_markTyped, arguments]); |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | function task_markTyped(aURIs, aTitle) |
michael@0 | 303 | { |
michael@0 | 304 | for (let uri of aURIs) { |
michael@0 | 305 | yield promiseAddVisits({ |
michael@0 | 306 | uri: toURI(kURIs[uri]), |
michael@0 | 307 | transition: TRANSITION_TYPED, |
michael@0 | 308 | title: kTitles[aTitle] |
michael@0 | 309 | }); |
michael@0 | 310 | } |
michael@0 | 311 | } |