merging
[mozilla-central.git] / toolkit / components / places / tests / unit / test_408221.js
blob33b60ce92e8cc07216b859f5b75435a75ddfe8c1
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
5  *
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/
10  *
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
14  * License.
15  *
16  * The Original Code is Bug 378079 unit test code.
17  *
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.
21  *
22  * Contributor(s):
23  *   Matt Crocker <matt@songbirdnest.com>
24  *   Seth Spitzer <sspitzer@mozilla.org>
25  *   Edward Lee <edward.lee@engineering.uiuc.edu>
26  *
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.
38  *
39  * ***** END LICENSE BLOCK ***** */
41 var current_test = 0;
43 function AutoCompleteInput(aSearches) {
44   this.searches = aSearches;
46 AutoCompleteInput.prototype = {
47   constructor: AutoCompleteInput, 
49   searches: null,
50   
51   minResultsForPopup: 0,
52   timeout: 10,
53   searchParam: "",
54   textValue: "",
55   disableAutoComplete: false,  
56   completeDefaultIndex: false,
57   
58   get searchCount() {
59     return this.searches.length;
60   },
61   
62   getSearchAt: function(aIndex) {
63     return this.searches[aIndex];
64   },
65   
66   onSearchBegin: function() {},
67   onSearchComplete: function() {},
68   
69   popupOpen: false,  
70   
71   popup: { 
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))
79         return this;
81       throw Components.results.NS_ERROR_NO_INTERFACE;
82     }    
83   },
84     
85   // nsISupports implementation
86   QueryInterface: function(iid) {
87     if (iid.equals(Ci.nsISupports) ||
88         iid.equals(Ci.nsIAutoCompleteInput))
89       return this;
91     throw Components.results.NS_ERROR_NO_INTERFACE;
92   }
95 // Get tagging service
96 try {
97   var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
98                 getService(Ci.nsITaggingService);
99 } catch(ex) {
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);  
107   
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
115   do_test_pending();
117   var numSearchesStarted = 0;
118   input.onSearchBegin = function() {
119     numSearchesStarted++;
120     do_check_eq(numSearchesStarted, 1);
121   };
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);
128     let vals = [];
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");
133     }
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))
136    
137     if (current_test < (tests.length - 1)) {
138       current_test++;
139       tests[current_test]();
140     }
142     do_test_finished();
143   };
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");
154   
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"); }];
162 /** 
163  * Test bug #408221
164  */
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"]);
182   tests[0]();