1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_adaptive_bug527311.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,129 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim:set ts=2 sw=2 sts=2 et: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +const TEST_URL = "http://adapt.mozilla.org/"; 1.11 +const SEARCH_STRING = "adapt"; 1.12 + 1.13 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.14 + 1.15 +let hs = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.16 + getService(Ci.nsINavHistoryService); 1.17 +let bs = Cc["@mozilla.org/browser/nav-bookmarks-service;1"]. 1.18 + getService(Ci.nsINavBookmarksService); 1.19 +let os = Cc["@mozilla.org/observer-service;1"]. 1.20 + getService(Ci.nsIObserverService); 1.21 +let ps = Cc["@mozilla.org/preferences-service;1"]. 1.22 + getService(Ci.nsIPrefBranch); 1.23 + 1.24 +const PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC = 1.25 + "places-autocomplete-feedback-updated"; 1.26 + 1.27 +function AutoCompleteInput(aSearches) { 1.28 + this.searches = aSearches; 1.29 +} 1.30 +AutoCompleteInput.prototype = { 1.31 + constructor: AutoCompleteInput, 1.32 + searches: null, 1.33 + minResultsForPopup: 0, 1.34 + timeout: 10, 1.35 + searchParam: "", 1.36 + textValue: "", 1.37 + disableAutoComplete: false, 1.38 + completeDefaultIndex: false, 1.39 + 1.40 + get searchCount() { 1.41 + return this.searches.length; 1.42 + }, 1.43 + 1.44 + getSearchAt: function ACI_getSearchAt(aIndex) { 1.45 + return this.searches[aIndex]; 1.46 + }, 1.47 + 1.48 + onSearchComplete: function ACI_onSearchComplete() {}, 1.49 + 1.50 + popupOpen: false, 1.51 + 1.52 + popup: { 1.53 + setSelectedIndex: function() {}, 1.54 + invalidate: function() {}, 1.55 + 1.56 + QueryInterface: function(iid) { 1.57 + if (iid.equals(Ci.nsISupports) || 1.58 + iid.equals(Ci.nsIAutoCompletePopup)) 1.59 + return this; 1.60 + 1.61 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.62 + } 1.63 + }, 1.64 + 1.65 + onSearchBegin: function() {}, 1.66 + 1.67 + QueryInterface: function(iid) { 1.68 + if (iid.equals(Ci.nsISupports) || 1.69 + iid.equals(Ci.nsIAutoCompleteInput)) 1.70 + return this; 1.71 + 1.72 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.73 + } 1.74 +} 1.75 + 1.76 + 1.77 +function check_results() { 1.78 + let controller = Cc["@mozilla.org/autocomplete/controller;1"]. 1.79 + getService(Ci.nsIAutoCompleteController); 1.80 + let input = new AutoCompleteInput(["history"]); 1.81 + controller.input = input; 1.82 + 1.83 + input.onSearchComplete = function() { 1.84 + do_check_eq(controller.searchStatus, 1.85 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_NO_MATCH); 1.86 + do_check_eq(controller.matchCount, 0); 1.87 + 1.88 + remove_all_bookmarks(); 1.89 + do_test_finished(); 1.90 + }; 1.91 + 1.92 + controller.startSearch(SEARCH_STRING); 1.93 +} 1.94 + 1.95 + 1.96 +function addAdaptiveFeedback(aUrl, aSearch, aCallback) { 1.97 + let observer = { 1.98 + observe: function(aSubject, aTopic, aData) { 1.99 + os.removeObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC); 1.100 + do_timeout(0, aCallback); 1.101 + } 1.102 + }; 1.103 + os.addObserver(observer, PLACES_AUTOCOMPLETE_FEEDBACK_UPDATED_TOPIC, false); 1.104 + 1.105 + let thing = { 1.106 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteInput, 1.107 + Ci.nsIAutoCompletePopup, 1.108 + Ci.nsIAutoCompleteController]), 1.109 + get popup() { return thing; }, 1.110 + get controller() { return thing; }, 1.111 + popupOpen: true, 1.112 + selectedIndex: 0, 1.113 + getValueAt: function() aUrl, 1.114 + searchString: aSearch 1.115 + }; 1.116 + 1.117 + os.notifyObservers(thing, "autocomplete-will-enter-text", null); 1.118 +} 1.119 + 1.120 + 1.121 +function run_test() { 1.122 + do_test_pending(); 1.123 + 1.124 + // Add a bookmark to our url. 1.125 + bs.insertBookmark(bs.unfiledBookmarksFolder, uri(TEST_URL), 1.126 + bs.DEFAULT_INDEX, "test_book"); 1.127 + // We want to search only history. 1.128 + ps.setIntPref("browser.urlbar.default.behavior", 1.129 + Ci.mozIPlacesAutoComplete.BEHAVIOR_HISTORY); 1.130 + // Add an adaptive entry. 1.131 + addAdaptiveFeedback(TEST_URL, SEARCH_STRING, check_results); 1.132 +}