toolkit/components/places/tests/unit/test_408221.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 var current_test = 0;
michael@0 8
michael@0 9 function AutoCompleteInput(aSearches) {
michael@0 10 this.searches = aSearches;
michael@0 11 }
michael@0 12 AutoCompleteInput.prototype = {
michael@0 13 constructor: AutoCompleteInput,
michael@0 14
michael@0 15 searches: null,
michael@0 16
michael@0 17 minResultsForPopup: 0,
michael@0 18 timeout: 10,
michael@0 19 searchParam: "",
michael@0 20 textValue: "",
michael@0 21 disableAutoComplete: false,
michael@0 22 completeDefaultIndex: false,
michael@0 23
michael@0 24 get searchCount() {
michael@0 25 return this.searches.length;
michael@0 26 },
michael@0 27
michael@0 28 getSearchAt: function(aIndex) {
michael@0 29 return this.searches[aIndex];
michael@0 30 },
michael@0 31
michael@0 32 onSearchBegin: function() {},
michael@0 33 onSearchComplete: function() {},
michael@0 34
michael@0 35 popupOpen: false,
michael@0 36
michael@0 37 popup: {
michael@0 38 setSelectedIndex: function(aIndex) {},
michael@0 39 invalidate: function() {},
michael@0 40
michael@0 41 // nsISupports implementation
michael@0 42 QueryInterface: function(iid) {
michael@0 43 if (iid.equals(Ci.nsISupports) ||
michael@0 44 iid.equals(Ci.nsIAutoCompletePopup))
michael@0 45 return this;
michael@0 46
michael@0 47 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 48 }
michael@0 49 },
michael@0 50
michael@0 51 // nsISupports implementation
michael@0 52 QueryInterface: function(iid) {
michael@0 53 if (iid.equals(Ci.nsISupports) ||
michael@0 54 iid.equals(Ci.nsIAutoCompleteInput))
michael@0 55 return this;
michael@0 56
michael@0 57 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 58 }
michael@0 59 }
michael@0 60
michael@0 61 // Get tagging service
michael@0 62 try {
michael@0 63 var tagssvc = Cc["@mozilla.org/browser/tagging-service;1"].
michael@0 64 getService(Ci.nsITaggingService);
michael@0 65 } catch(ex) {
michael@0 66 do_throw("Could not get tagging service\n");
michael@0 67 }
michael@0 68
michael@0 69 function ensure_tag_results(uris, searchTerm)
michael@0 70 {
michael@0 71 var controller = Components.classes["@mozilla.org/autocomplete/controller;1"].
michael@0 72 getService(Components.interfaces.nsIAutoCompleteController);
michael@0 73
michael@0 74 // Make an AutoCompleteInput that uses our searches
michael@0 75 // and confirms results on search complete
michael@0 76 var input = new AutoCompleteInput(["history"]);
michael@0 77
michael@0 78 controller.input = input;
michael@0 79
michael@0 80 // Search is asynchronous, so don't let the test finish immediately
michael@0 81 do_test_pending();
michael@0 82
michael@0 83 var numSearchesStarted = 0;
michael@0 84 input.onSearchBegin = function() {
michael@0 85 numSearchesStarted++;
michael@0 86 do_check_eq(numSearchesStarted, 1);
michael@0 87 };
michael@0 88
michael@0 89 input.onSearchComplete = function() {
michael@0 90 do_check_eq(numSearchesStarted, 1);
michael@0 91 do_check_eq(controller.searchStatus,
michael@0 92 Ci.nsIAutoCompleteController.STATUS_COMPLETE_MATCH);
michael@0 93 do_check_eq(controller.matchCount, uris.length);
michael@0 94 let vals = [];
michael@0 95 for (var i=0; i<controller.matchCount; i++) {
michael@0 96 // Keep the URL for later because order of tag results is undefined
michael@0 97 vals.push(controller.getValueAt(i));
michael@0 98 do_check_eq(controller.getStyleAt(i), "tag");
michael@0 99 }
michael@0 100 // Sort the results then check if we have the right items
michael@0 101 vals.sort().forEach(function(val, i) do_check_eq(val, uris[i].spec))
michael@0 102
michael@0 103 if (current_test < (tests.length - 1)) {
michael@0 104 current_test++;
michael@0 105 tests[current_test]();
michael@0 106 }
michael@0 107
michael@0 108 do_test_finished();
michael@0 109 };
michael@0 110
michael@0 111 controller.startSearch(searchTerm);
michael@0 112 }
michael@0 113
michael@0 114 var uri1 = uri("http://site.tld/1");
michael@0 115 var uri2 = uri("http://site.tld/2");
michael@0 116 var uri3 = uri("http://site.tld/3");
michael@0 117 var uri4 = uri("http://site.tld/4");
michael@0 118 var uri5 = uri("http://site.tld/5");
michael@0 119 var uri6 = uri("http://site.tld/6");
michael@0 120
michael@0 121 var tests = [function() { ensure_tag_results([uri1, uri2, uri3], "foo"); },
michael@0 122 function() { ensure_tag_results([uri1, uri2, uri3], "Foo"); },
michael@0 123 function() { ensure_tag_results([uri1, uri2, uri3], "foO"); },
michael@0 124 function() { ensure_tag_results([uri4, uri5, uri6], "bar mud"); },
michael@0 125 function() { ensure_tag_results([uri4, uri5, uri6], "BAR MUD"); },
michael@0 126 function() { ensure_tag_results([uri4, uri5, uri6], "Bar Mud"); }];
michael@0 127
michael@0 128 /**
michael@0 129 * Properly tags a uri adding it to bookmarks.
michael@0 130 *
michael@0 131 * @param aURI
michael@0 132 * The nsIURI to tag.
michael@0 133 * @param aTags
michael@0 134 * The tags to add.
michael@0 135 */
michael@0 136 function tagURI(aURI, aTags) {
michael@0 137 PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
michael@0 138 aURI,
michael@0 139 PlacesUtils.bookmarks.DEFAULT_INDEX,
michael@0 140 "A title");
michael@0 141 tagssvc.tagURI(aURI, aTags);
michael@0 142 }
michael@0 143
michael@0 144 /**
michael@0 145 * Test bug #408221
michael@0 146 */
michael@0 147 function run_test() {
michael@0 148 // always search in history + bookmarks, no matter what the default is
michael@0 149 var prefs = Cc["@mozilla.org/preferences-service;1"].
michael@0 150 getService(Ci.nsIPrefBranch);
michael@0 151 prefs.setIntPref("browser.urlbar.search.sources", 3);
michael@0 152 prefs.setIntPref("browser.urlbar.default.behavior", 0);
michael@0 153
michael@0 154 tagURI(uri1, ["Foo"]);
michael@0 155 tagURI(uri2, ["FOO"]);
michael@0 156 tagURI(uri3, ["foO"]);
michael@0 157 tagURI(uri4, ["BAR"]);
michael@0 158 tagURI(uri4, ["MUD"]);
michael@0 159 tagURI(uri5, ["bar"]);
michael@0 160 tagURI(uri5, ["mud"]);
michael@0 161 tagURI(uri6, ["baR"]);
michael@0 162 tagURI(uri6, ["muD"]);
michael@0 163
michael@0 164 tests[0]();
michael@0 165 }

mercurial