1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Bug 378079 unit test code.
18 * The Initial Developer of the Original Code is POTI Inc.
19 * Portions created by the Initial Developer are Copyright (C) 2007
20 * the Initial Developer. All Rights Reserved.
23 * Matt Crocker <matt@songbirdnest.com>
24 * Seth Spitzer <sspitzer@mozilla.org>
25 * Edward Lee <edward.lee@engineering.uiuc.edu>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
43 function AutoCompleteInput(aSearches) {
44 this.searches = aSearches;
46 AutoCompleteInput.prototype = {
47 constructor: AutoCompleteInput,
51 minResultsForPopup: 0,
55 disableAutoComplete: false,
56 completeDefaultIndex: false,
59 return this.searches.length;
62 getSearchAt: function(aIndex) {
63 return this.searches[aIndex];
66 onSearchBegin: function() {},
67 onSearchComplete: function() {},
72 setSelectedIndex: function(aIndex) {},
73 invalidate: function() {},
75 // nsISupports implementation
76 QueryInterface: function(iid) {
77 if (iid.equals(Ci.nsISupports) ||
78 iid.equals(Ci.nsIAutoCompletePopup))
81 throw Components.results.NS_ERROR_NO_INTERFACE;
85 // nsISupports implementation
86 QueryInterface: function(iid) {
87 if (iid.equals(Ci.nsISupports) ||
88 iid.equals(Ci.nsIAutoCompleteInput))
91 throw Components.results.NS_ERROR_NO_INTERFACE;
95 // Get tagging service
97 var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
98 getService(Ci.nsITaggingService);
100 do_throw("Could not get tagging service\n");
103 function ensure_tag_results(uris, searchTerm)
105 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
106 getService(Components.interfaces.nsIAutoCompleteController);
108 // Make an AutoCompleteInput that uses our searches
109 // and confirms results on search complete
110 var input = new AutoCompleteInput(["history"]);
112 controller.input = input;
114 // Search is asynchronous, so don't let the test finish immediately
117 var numSearchesStarted = 0;
118 input.onSearchBegin = function() {
119 numSearchesStarted++;
120 do_check_eq(numSearchesStarted, 1);
123 input.onSearchComplete = function() {
124 do_check_eq(numSearchesStarted, 1);
125 do_check_eq(controller.searchStatus,
126 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
127 do_check_eq(controller.matchCount, uris.length);
129 for (var i=0; i<controller.matchCount; i++) {
130 // Keep the URL for later because order of tag results is undefined
131 vals.push(controller.getValueAt(i));
132 do_check_eq(controller.getStyleAt(i), "tag");
134 // Sort the results then check if we have the right items
135 vals.sort().forEach(function(val, i) do_check_eq(val, uris[i].spec))
137 if (current_test < (tests.length - 1)) {
139 tests[current_test]();
145 controller.startSearch(searchTerm);
148 var uri1 = uri("http://site.tld/1");
149 var uri2 = uri("http://site.tld/2");
150 var uri3 = uri("http://site.tld/3");
151 var uri4 = uri("http://site.tld/4");
152 var uri5 = uri("http://site.tld/5");
153 var uri6 = uri("http://site.tld/6");
155 var tests = [function() { ensure_tag_results([uri1, uri2, uri3], "foo"); },
156 function() { ensure_tag_results([uri1, uri2, uri3], "Foo"); },
157 function() { ensure_tag_results([uri1, uri2, uri3], "foO"); },
158 function() { ensure_tag_results([uri4, uri5, uri6], "bar mud"); },
159 function() { ensure_tag_results([uri4, uri5, uri6], "BAR MUD"); },
160 function() { ensure_tag_results([uri4, uri5, uri6], "Bar Mud"); }];
165 function run_test() {
166 // always search in history + bookmarks, no matter what the default is
167 var prefs = Cc["@mozilla.org/preferences-service;1"].
168 getService(Ci.nsIPrefBranch);
169 prefs.setIntPref("browser.urlbar.search.sources", 3);
170 prefs.setIntPref("browser.urlbar.default.behavior", 0);
172 tagssvc.tagURI(uri1, ["Foo"]);
173 tagssvc.tagURI(uri2, ["FOO"]);
174 tagssvc.tagURI(uri3, ["foO"]);
175 tagssvc.tagURI(uri4, ["BAR"]);
176 tagssvc.tagURI(uri4, ["MUD"]);
177 tagssvc.tagURI(uri5, ["bar"]);
178 tagssvc.tagURI(uri5, ["mud"]);
179 tagssvc.tagURI(uri6, ["baR"]);
180 tagssvc.tagURI(uri6, ["muD"]);