1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/autocomplete/head_autocomplete.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,311 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Ci = Components.interfaces; 1.9 +const Cc = Components.classes; 1.10 +const Cr = Components.results; 1.11 +const Cu = Components.utils; 1.12 + 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +// Import common head. 1.16 +let (commonFile = do_get_file("../head_common.js", false)) { 1.17 + let uri = Services.io.newFileURI(commonFile); 1.18 + Services.scriptloader.loadSubScript(uri.spec, this); 1.19 +} 1.20 + 1.21 +// Put any other stuff relative to this test folder below. 1.22 + 1.23 + 1.24 +/** 1.25 + * Header file for autocomplete testcases that create a set of pages with uris, 1.26 + * titles, tags and tests that a given search term matches certain pages. 1.27 + */ 1.28 + 1.29 +let current_test = 0; 1.30 + 1.31 +function AutoCompleteInput(aSearches) { 1.32 + this.searches = aSearches; 1.33 +} 1.34 +AutoCompleteInput.prototype = { 1.35 + timeout: 10, 1.36 + textValue: "", 1.37 + searches: null, 1.38 + searchParam: "", 1.39 + popupOpen: false, 1.40 + minResultsForPopup: 0, 1.41 + invalidate: function() {}, 1.42 + disableAutoComplete: false, 1.43 + completeDefaultIndex: false, 1.44 + get popup() { return this; }, 1.45 + onSearchBegin: function() {}, 1.46 + onSearchComplete: function() {}, 1.47 + setSelectedIndex: function() {}, 1.48 + get searchCount() { return this.searches.length; }, 1.49 + getSearchAt: function(aIndex) this.searches[aIndex], 1.50 + QueryInterface: XPCOMUtils.generateQI([ 1.51 + Ci.nsIAutoCompleteInput, 1.52 + Ci.nsIAutoCompletePopup, 1.53 + ]) 1.54 +}; 1.55 + 1.56 +function toURI(aSpec) { 1.57 + return uri(aSpec); 1.58 +} 1.59 + 1.60 +let appendTags = true; 1.61 +// Helper to turn off tag matching in results 1.62 +function ignoreTags() 1.63 +{ 1.64 + print("Ignoring tags from results"); 1.65 + appendTags = false; 1.66 +} 1.67 + 1.68 +function ensure_results(aSearch, aExpected) 1.69 +{ 1.70 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.71 + getService(Ci.nsIAutoCompleteController); 1.72 + 1.73 + // Make an AutoCompleteInput that uses our searches 1.74 + // and confirms results on search complete 1.75 + let input = new AutoCompleteInput(["history"]); 1.76 + 1.77 + controller.input = input; 1.78 + 1.79 + if (typeof kSearchParam == "string") 1.80 + input.searchParam = kSearchParam; 1.81 + 1.82 + let numSearchesStarted = 0; 1.83 + input.onSearchBegin = function() { 1.84 + numSearchesStarted++; 1.85 + do_check_eq(numSearchesStarted, 1); 1.86 + }; 1.87 + 1.88 + input.onSearchComplete = function() { 1.89 + do_check_eq(numSearchesStarted, 1); 1.90 + aExpected = aExpected.slice(); 1.91 + 1.92 + // Check to see the expected uris and titles match up (in any order) 1.93 + for (let i = 0; i < controller.matchCount; i++) { 1.94 + let value = controller.getValueAt(i); 1.95 + let comment = controller.getCommentAt(i); 1.96 + 1.97 + print("Looking for '" + value + "', '" + comment + "' in expected results..."); 1.98 + let j; 1.99 + for (j = 0; j < aExpected.length; j++) { 1.100 + // Skip processed expected results 1.101 + if (aExpected[j] == undefined) 1.102 + continue; 1.103 + 1.104 + let [uri, title, tags] = gPages[aExpected[j]]; 1.105 + 1.106 + // Load the real uri and titles and tags if necessary 1.107 + uri = toURI(kURIs[uri]).spec; 1.108 + title = kTitles[title]; 1.109 + if (tags && appendTags) 1.110 + title += " \u2013 " + tags.map(function(aTag) kTitles[aTag]); 1.111 + print("Checking against expected '" + uri + "', '" + title + "'..."); 1.112 + 1.113 + // Got a match on both uri and title? 1.114 + if (uri == value && title == comment) { 1.115 + print("Got it at index " + j + "!!"); 1.116 + // Make it undefined so we don't process it again 1.117 + aExpected[j] = undefined; 1.118 + break; 1.119 + } 1.120 + } 1.121 + 1.122 + // We didn't hit the break, so we must have not found it 1.123 + if (j == aExpected.length) 1.124 + do_throw("Didn't find the current result ('" + value + "', '" + comment + "') in expected: " + aExpected); 1.125 + } 1.126 + 1.127 + // Make sure we have the right number of results 1.128 + print("Expecting " + aExpected.length + " results; got " + 1.129 + controller.matchCount + " results"); 1.130 + do_check_eq(controller.matchCount, aExpected.length); 1.131 + 1.132 + // If we expect results, make sure we got matches 1.133 + do_check_eq(controller.searchStatus, aExpected.length ? 1.134 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH : 1.135 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); 1.136 + 1.137 + // Fetch the next test if we have more 1.138 + if (++current_test < gTests.length) 1.139 + run_test(); 1.140 + 1.141 + do_test_finished(); 1.142 + }; 1.143 + 1.144 + print("Searching for.. '" + aSearch + "'"); 1.145 + controller.startSearch(aSearch); 1.146 +} 1.147 + 1.148 +// Get history services 1.149 +var histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.150 + getService(Ci.nsINavHistoryService); 1.151 +var bhist = histsvc.QueryInterface(Ci.nsIBrowserHistory); 1.152 +var bmsvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. 1.153 + getService(Ci.nsINavBookmarksService); 1.154 +var tagsvc = Cc["@mozilla.org/browser/tagging-service;1"]. 1.155 + getService(Ci.nsITaggingService); 1.156 +var iosvc = Cc["@mozilla.org/network/io-service;1"]. 1.157 + getService(Ci.nsIIOService); 1.158 +var prefs = Cc["@mozilla.org/preferences-service;1"]. 1.159 + getService(Ci.nsIPrefBranch); 1.160 + 1.161 +// Some date not too long ago 1.162 +let gDate = new Date(Date.now() - 1000 * 60 * 60) * 1000; 1.163 +// Store the page info for each uri 1.164 +let gPages = []; 1.165 + 1.166 +// Initialization tasks to be run before the next test 1.167 +let gNextTestSetupTasks = []; 1.168 + 1.169 +/** 1.170 + * Adds a page, and creates various properties for it depending on the 1.171 + * parameters passed in. This function will also add one visit, unless 1.172 + * aNoVisit is true. 1.173 + * 1.174 + * @param aURI 1.175 + * An index into kURIs that holds the string for the URI we are to add a 1.176 + * page for. 1.177 + * @param aTitle 1.178 + * An index into kTitles that holds the string for the title we are to 1.179 + * associate with the specified URI. 1.180 + * @param aBook [optional] 1.181 + * An index into kTitles that holds the string for the title we are to 1.182 + * associate with the bookmark. If this is undefined, no bookmark is 1.183 + * created. 1.184 + * @param aTags [optional] 1.185 + * An array of indexes into kTitles that hold the strings for the tags we 1.186 + * are to associate with the URI. If this is undefined (or aBook is), no 1.187 + * tags are added. 1.188 + * @param aKey [optional] 1.189 + * A string to associate as the keyword for this bookmark. aBook must be 1.190 + * a valid index into kTitles for this to be checked and used. 1.191 + * @param aTransitionType [optional] 1.192 + * The transition type to use when adding the visit. The default is 1.193 + * nsINavHistoryService::TRANSITION_LINK. 1.194 + * @param aNoVisit [optional] 1.195 + * If true, no visit is added for the URI. If false or undefined, a 1.196 + * visit is added. 1.197 + */ 1.198 +function addPageBook(aURI, aTitle, aBook, aTags, aKey, aTransitionType, aNoVisit) 1.199 +{ 1.200 + gNextTestSetupTasks.push([task_addPageBook, arguments]); 1.201 +} 1.202 + 1.203 +function task_addPageBook(aURI, aTitle, aBook, aTags, aKey, aTransitionType, aNoVisit) 1.204 +{ 1.205 + // Add a page entry for the current uri 1.206 + gPages[aURI] = [aURI, aBook != undefined ? aBook : aTitle, aTags]; 1.207 + 1.208 + let uri = toURI(kURIs[aURI]); 1.209 + let title = kTitles[aTitle]; 1.210 + 1.211 + let out = [aURI, aTitle, aBook, aTags, aKey]; 1.212 + out.push("\nuri=" + kURIs[aURI]); 1.213 + out.push("\ntitle=" + title); 1.214 + 1.215 + // Add the page and a visit if we need to 1.216 + if (!aNoVisit) { 1.217 + yield promiseAddVisits({ 1.218 + uri: uri, 1.219 + transition: aTransitionType || TRANSITION_LINK, 1.220 + visitDate: gDate, 1.221 + title: title 1.222 + }); 1.223 + out.push("\nwith visit"); 1.224 + } 1.225 + 1.226 + // Add a bookmark if we need to 1.227 + if (aBook != undefined) { 1.228 + let book = kTitles[aBook]; 1.229 + let bmid = bmsvc.insertBookmark(bmsvc.unfiledBookmarksFolder, uri, 1.230 + bmsvc.DEFAULT_INDEX, book); 1.231 + out.push("\nbook=" + book); 1.232 + 1.233 + // Add a keyword to the bookmark if we need to 1.234 + if (aKey != undefined) 1.235 + bmsvc.setKeywordForBookmark(bmid, aKey); 1.236 + 1.237 + // Add tags if we need to 1.238 + if (aTags != undefined && aTags.length > 0) { 1.239 + // Convert each tag index into the title 1.240 + let tags = aTags.map(function(aTag) kTitles[aTag]); 1.241 + tagsvc.tagURI(uri, tags); 1.242 + out.push("\ntags=" + tags); 1.243 + } 1.244 + } 1.245 + 1.246 + print("\nAdding page/book/tag: " + out.join(", ")); 1.247 +} 1.248 + 1.249 +function run_test() { 1.250 + print("\n"); 1.251 + // always search in history + bookmarks, no matter what the default is 1.252 + prefs.setIntPref("browser.urlbar.search.sources", 3); 1.253 + prefs.setIntPref("browser.urlbar.default.behavior", 0); 1.254 + 1.255 + // Search is asynchronous, so don't let the test finish immediately 1.256 + do_test_pending(); 1.257 + 1.258 + // Load the test and print a description then run the test 1.259 + let [description, search, expected, func] = gTests[current_test]; 1.260 + print(description); 1.261 + 1.262 + // By default assume we want to match tags 1.263 + appendTags = true; 1.264 + 1.265 + // Do an extra function if necessary 1.266 + if (func) 1.267 + func(); 1.268 + 1.269 + Task.spawn(function () { 1.270 + // Iterate over all tasks and execute them 1.271 + for (let [, [fn, args]] in Iterator(gNextTestSetupTasks)) { 1.272 + yield fn.apply(this, args); 1.273 + }; 1.274 + 1.275 + // Clean up to allow tests to register more functions. 1.276 + gNextTestSetupTasks = []; 1.277 + 1.278 + // At this point frecency could still be updating due to latest pages 1.279 + // updates. This is not a problem in real life, but autocomplete tests 1.280 + // should return reliable resultsets, thus we have to wait. 1.281 + yield promiseAsyncUpdates(); 1.282 + 1.283 + }).then(function () ensure_results(search, expected), 1.284 + do_report_unexpected_exception); 1.285 +} 1.286 + 1.287 +// Utility function to remove history pages 1.288 +function removePages(aURIs) 1.289 +{ 1.290 + gNextTestSetupTasks.push([do_removePages, arguments]); 1.291 +} 1.292 + 1.293 +function do_removePages(aURIs) 1.294 +{ 1.295 + for each (let uri in aURIs) 1.296 + histsvc.removePage(toURI(kURIs[uri])); 1.297 +} 1.298 + 1.299 +// Utility function to mark pages as typed 1.300 +function markTyped(aURIs, aTitle) 1.301 +{ 1.302 + gNextTestSetupTasks.push([task_markTyped, arguments]); 1.303 +} 1.304 + 1.305 +function task_markTyped(aURIs, aTitle) 1.306 +{ 1.307 + for (let uri of aURIs) { 1.308 + yield promiseAddVisits({ 1.309 + uri: toURI(kURIs[uri]), 1.310 + transition: TRANSITION_TYPED, 1.311 + title: kTitles[aTitle] 1.312 + }); 1.313 + } 1.314 +}