1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_408221.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,165 @@ 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 +var current_test = 0; 1.11 + 1.12 +function AutoCompleteInput(aSearches) { 1.13 + this.searches = aSearches; 1.14 +} 1.15 +AutoCompleteInput.prototype = { 1.16 + constructor: AutoCompleteInput, 1.17 + 1.18 + searches: null, 1.19 + 1.20 + minResultsForPopup: 0, 1.21 + timeout: 10, 1.22 + searchParam: "", 1.23 + textValue: "", 1.24 + disableAutoComplete: false, 1.25 + completeDefaultIndex: false, 1.26 + 1.27 + get searchCount() { 1.28 + return this.searches.length; 1.29 + }, 1.30 + 1.31 + getSearchAt: function(aIndex) { 1.32 + return this.searches[aIndex]; 1.33 + }, 1.34 + 1.35 + onSearchBegin: function() {}, 1.36 + onSearchComplete: function() {}, 1.37 + 1.38 + popupOpen: false, 1.39 + 1.40 + popup: { 1.41 + setSelectedIndex: function(aIndex) {}, 1.42 + invalidate: function() {}, 1.43 + 1.44 + // nsISupports implementation 1.45 + QueryInterface: function(iid) { 1.46 + if (iid.equals(Ci.nsISupports) || 1.47 + iid.equals(Ci.nsIAutoCompletePopup)) 1.48 + return this; 1.49 + 1.50 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.51 + } 1.52 + }, 1.53 + 1.54 + // nsISupports implementation 1.55 + QueryInterface: function(iid) { 1.56 + if (iid.equals(Ci.nsISupports) || 1.57 + iid.equals(Ci.nsIAutoCompleteInput)) 1.58 + return this; 1.59 + 1.60 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.61 + } 1.62 +} 1.63 + 1.64 +// Get tagging service 1.65 +try { 1.66 + var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"]. 1.67 + getService(Ci.nsITaggingService); 1.68 +} catch(ex) { 1.69 + do_throw("Could not get tagging service\n"); 1.70 +} 1.71 + 1.72 +function ensure_tag_results(uris, searchTerm) 1.73 +{ 1.74 + var controller = Components.classes["@mozilla.org/autocomplete/controller;1"]. 1.75 + getService(Components.interfaces.nsIAutoCompleteController); 1.76 + 1.77 + // Make an AutoCompleteInput that uses our searches 1.78 + // and confirms results on search complete 1.79 + var input = new AutoCompleteInput(["history"]); 1.80 + 1.81 + controller.input = input; 1.82 + 1.83 + // Search is asynchronous, so don't let the test finish immediately 1.84 + do_test_pending(); 1.85 + 1.86 + var numSearchesStarted = 0; 1.87 + input.onSearchBegin = function() { 1.88 + numSearchesStarted++; 1.89 + do_check_eq(numSearchesStarted, 1); 1.90 + }; 1.91 + 1.92 + input.onSearchComplete = function() { 1.93 + do_check_eq(numSearchesStarted, 1); 1.94 + do_check_eq(controller.searchStatus, 1.95 + Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH); 1.96 + do_check_eq(controller.matchCount, uris.length); 1.97 + let vals = []; 1.98 + for (var i=0; i<controller.matchCount; i++) { 1.99 + // Keep the URL for later because order of tag results is undefined 1.100 + vals.push(controller.getValueAt(i)); 1.101 + do_check_eq(controller.getStyleAt(i), "tag"); 1.102 + } 1.103 + // Sort the results then check if we have the right items 1.104 + vals.sort().forEach(function(val, i) do_check_eq(val, uris[i].spec)) 1.105 + 1.106 + if (current_test < (tests.length - 1)) { 1.107 + current_test++; 1.108 + tests[current_test](); 1.109 + } 1.110 + 1.111 + do_test_finished(); 1.112 + }; 1.113 + 1.114 + controller.startSearch(searchTerm); 1.115 +} 1.116 + 1.117 +var uri1 = uri("http://site.tld/1"); 1.118 +var uri2 = uri("http://site.tld/2"); 1.119 +var uri3 = uri("http://site.tld/3"); 1.120 +var uri4 = uri("http://site.tld/4"); 1.121 +var uri5 = uri("http://site.tld/5"); 1.122 +var uri6 = uri("http://site.tld/6"); 1.123 + 1.124 +var tests = [function() { ensure_tag_results([uri1, uri2, uri3], "foo"); }, 1.125 + function() { ensure_tag_results([uri1, uri2, uri3], "Foo"); }, 1.126 + function() { ensure_tag_results([uri1, uri2, uri3], "foO"); }, 1.127 + function() { ensure_tag_results([uri4, uri5, uri6], "bar mud"); }, 1.128 + function() { ensure_tag_results([uri4, uri5, uri6], "BAR MUD"); }, 1.129 + function() { ensure_tag_results([uri4, uri5, uri6], "Bar Mud"); }]; 1.130 + 1.131 +/** 1.132 + * Properly tags a uri adding it to bookmarks. 1.133 + * 1.134 + * @param aURI 1.135 + * The nsIURI to tag. 1.136 + * @param aTags 1.137 + * The tags to add. 1.138 + */ 1.139 +function tagURI(aURI, aTags) { 1.140 + PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, 1.141 + aURI, 1.142 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.143 + "A title"); 1.144 + tagssvc.tagURI(aURI, aTags); 1.145 +} 1.146 + 1.147 +/** 1.148 + * Test bug #408221 1.149 + */ 1.150 +function run_test() { 1.151 + // always search in history + bookmarks, no matter what the default is 1.152 + var prefs = Cc["@mozilla.org/preferences-service;1"]. 1.153 + getService(Ci.nsIPrefBranch); 1.154 + prefs.setIntPref("browser.urlbar.search.sources", 3); 1.155 + prefs.setIntPref("browser.urlbar.default.behavior", 0); 1.156 + 1.157 + tagURI(uri1, ["Foo"]); 1.158 + tagURI(uri2, ["FOO"]); 1.159 + tagURI(uri3, ["foO"]); 1.160 + tagURI(uri4, ["BAR"]); 1.161 + tagURI(uri4, ["MUD"]); 1.162 + tagURI(uri5, ["bar"]); 1.163 + tagURI(uri5, ["mud"]); 1.164 + tagURI(uri6, ["baR"]); 1.165 + tagURI(uri6, ["muD"]); 1.166 + 1.167 + tests[0](); 1.168 +}