toolkit/components/satchel/test/unit/test_autocomplete.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 var testnum = 0;
michael@0 8 var fac;
michael@0 9 var prefs;
michael@0 10
michael@0 11 let numRecords, timeGroupingSize, now;
michael@0 12
michael@0 13 const DEFAULT_EXPIRE_DAYS = 180;
michael@0 14
michael@0 15 function padLeft(number, length) {
michael@0 16 var str = number + '';
michael@0 17 while (str.length < length)
michael@0 18 str = '0' + str;
michael@0 19 return str;
michael@0 20 }
michael@0 21
michael@0 22 function getFormExpiryDays() {
michael@0 23 if (prefs.prefHasUserValue("browser.formfill.expire_days"))
michael@0 24 return prefs.getIntPref("browser.formfill.expire_days");
michael@0 25 else
michael@0 26 return DEFAULT_EXPIRE_DAYS;
michael@0 27 }
michael@0 28
michael@0 29 function run_test() {
michael@0 30 // ===== test init =====
michael@0 31 var testfile = do_get_file("formhistory_autocomplete.sqlite");
michael@0 32 var profileDir = dirSvc.get("ProfD", Ci.nsIFile);
michael@0 33
michael@0 34 // Cleanup from any previous tests or failures.
michael@0 35 var destFile = profileDir.clone();
michael@0 36 destFile.append("formhistory.sqlite");
michael@0 37 if (destFile.exists())
michael@0 38 destFile.remove(false);
michael@0 39
michael@0 40 testfile.copyTo(profileDir, "formhistory.sqlite");
michael@0 41
michael@0 42 fac = Cc["@mozilla.org/satchel/form-autocomplete;1"].
michael@0 43 getService(Ci.nsIFormAutoComplete);
michael@0 44 prefs = Cc["@mozilla.org/preferences-service;1"].
michael@0 45 getService(Ci.nsIPrefBranch);
michael@0 46
michael@0 47 timeGroupingSize = prefs.getIntPref("browser.formfill.timeGroupingSize") * 1000 * 1000;
michael@0 48
michael@0 49 run_next_test();
michael@0 50 }
michael@0 51
michael@0 52 add_test(function test0() {
michael@0 53 var maxTimeGroupings = prefs.getIntPref("browser.formfill.maxTimeGroupings");
michael@0 54 var bucketSize = prefs.getIntPref("browser.formfill.bucketSize");
michael@0 55
michael@0 56 // ===== Tests with constant timesUsed and varying lastUsed date =====
michael@0 57 // insert 2 records per bucket to check alphabetical sort within
michael@0 58 now = 1000 * Date.now();
michael@0 59 numRecords = Math.ceil(maxTimeGroupings / bucketSize) * 2;
michael@0 60
michael@0 61 let changes = [ ];
michael@0 62 for (let i = 0; i < numRecords; i+=2) {
michael@0 63 let useDate = now - (i/2 * bucketSize * timeGroupingSize);
michael@0 64
michael@0 65 changes.push({ op : "add", fieldname: "field1", value: "value" + padLeft(numRecords - 1 - i, 2),
michael@0 66 timesUsed: 1, firstUsed: useDate, lastUsed: useDate });
michael@0 67 changes.push({ op : "add", fieldname: "field1", value: "value" + padLeft(numRecords - 2 - i, 2),
michael@0 68 timesUsed: 1, firstUsed: useDate, lastUsed: useDate });
michael@0 69 }
michael@0 70
michael@0 71 updateFormHistory(changes, run_next_test);
michael@0 72 });
michael@0 73
michael@0 74 add_test(function test1() {
michael@0 75 do_log_info("Check initial state is as expected");
michael@0 76
michael@0 77 countEntries(null, null, function (count) {
michael@0 78 countEntries("field1", null, function (count) {
michael@0 79 do_check_true(count > 0);
michael@0 80 run_next_test();
michael@0 81 });
michael@0 82 });
michael@0 83 });
michael@0 84
michael@0 85 add_test(function test2() {
michael@0 86 do_log_info("Check search contains all entries");
michael@0 87
michael@0 88 fac.autoCompleteSearchAsync("field1", "", null, null, {
michael@0 89 onSearchCompletion : function(aResults) {
michael@0 90 do_check_eq(numRecords, aResults.matchCount);
michael@0 91 run_next_test();
michael@0 92 }
michael@0 93 });
michael@0 94 });
michael@0 95
michael@0 96 add_test(function test3() {
michael@0 97 do_log_info("Check search result ordering with empty search term");
michael@0 98
michael@0 99 let lastFound = numRecords;
michael@0 100 fac.autoCompleteSearchAsync("field1", "", null, null, {
michael@0 101 onSearchCompletion : function(aResults) {
michael@0 102 for (let i = 0; i < numRecords; i+=2) {
michael@0 103 do_check_eq(parseInt(aResults.getValueAt(i + 1).substr(5), 10), --lastFound);
michael@0 104 do_check_eq(parseInt(aResults.getValueAt(i).substr(5), 10), --lastFound);
michael@0 105 }
michael@0 106 run_next_test();
michael@0 107 }
michael@0 108 });
michael@0 109 });
michael@0 110
michael@0 111 add_test(function test4() {
michael@0 112 do_log_info("Check search result ordering with \"v\"");
michael@0 113
michael@0 114 let lastFound = numRecords;
michael@0 115 fac.autoCompleteSearchAsync("field1", "v", null, null, {
michael@0 116 onSearchCompletion : function(aResults) {
michael@0 117 for (let i = 0; i < numRecords; i+=2) {
michael@0 118 do_check_eq(parseInt(aResults.getValueAt(i + 1).substr(5), 10), --lastFound);
michael@0 119 do_check_eq(parseInt(aResults.getValueAt(i).substr(5), 10), --lastFound);
michael@0 120 }
michael@0 121 run_next_test();
michael@0 122 }
michael@0 123 });
michael@0 124 });
michael@0 125
michael@0 126 const timesUsedSamples = 20;
michael@0 127
michael@0 128 add_test(function test5() {
michael@0 129 do_log_info("Begin tests with constant use dates and varying timesUsed");
michael@0 130
michael@0 131 let changes = [];
michael@0 132 for (let i = 0; i < timesUsedSamples; i++) {
michael@0 133 let timesUsed = (timesUsedSamples - i);
michael@0 134 let change = { op : "add", fieldname: "field2", value: "value" + (timesUsedSamples - 1 - i),
michael@0 135 timesUsed: timesUsed * timeGroupingSize, firstUsed: now, lastUsed: now };
michael@0 136 changes.push(change);
michael@0 137 }
michael@0 138 updateFormHistory(changes, run_next_test);
michael@0 139 });
michael@0 140
michael@0 141 add_test(function test6() {
michael@0 142 do_log_info("Check search result ordering with empty search term");
michael@0 143
michael@0 144 let lastFound = timesUsedSamples;
michael@0 145 fac.autoCompleteSearchAsync("field2", "", null, null, {
michael@0 146 onSearchCompletion : function(aResults) {
michael@0 147 for (let i = 0; i < timesUsedSamples; i++) {
michael@0 148 do_check_eq(parseInt(aResults.getValueAt(i).substr(5)), --lastFound);
michael@0 149 }
michael@0 150 run_next_test();
michael@0 151 }
michael@0 152 });
michael@0 153 });
michael@0 154
michael@0 155 add_test(function test7() {
michael@0 156 do_log_info("Check search result ordering with \"v\"");
michael@0 157
michael@0 158 let lastFound = timesUsedSamples;
michael@0 159 fac.autoCompleteSearchAsync("field2", "v", null, null, {
michael@0 160 onSearchCompletion : function(aResults) {
michael@0 161 for (let i = 0; i < timesUsedSamples; i++) {
michael@0 162 do_check_eq(parseInt(aResults.getValueAt(i).substr(5)), --lastFound);
michael@0 163 }
michael@0 164 run_next_test();
michael@0 165 }
michael@0 166 });
michael@0 167 });
michael@0 168
michael@0 169 add_test(function test8() {
michael@0 170 do_log_info("Check that \"senior citizen\" entries get a bonus (browser.formfill.agedBonus)");
michael@0 171
michael@0 172 let agedDate = 1000 * (Date.now() - getFormExpiryDays() * 24 * 60 * 60 * 1000);
michael@0 173
michael@0 174 let changes = [ ];
michael@0 175 changes.push({ op : "add", fieldname: "field3", value: "old but not senior",
michael@0 176 timesUsed: 100, firstUsed: (agedDate + 60 * 1000 * 1000), lastUsed: now });
michael@0 177 changes.push({ op : "add", fieldname: "field3", value: "senior citizen",
michael@0 178 timesUsed: 100, firstUsed: (agedDate - 60 * 1000 * 1000), lastUsed: now });
michael@0 179 updateFormHistory(changes, run_next_test);
michael@0 180 });
michael@0 181
michael@0 182 add_test(function test9() {
michael@0 183 fac.autoCompleteSearchAsync("field3", "", null, null, {
michael@0 184 onSearchCompletion : function(aResults) {
michael@0 185 do_check_eq(aResults.getValueAt(0), "senior citizen");
michael@0 186 do_check_eq(aResults.getValueAt(1), "old but not senior");
michael@0 187 run_next_test();
michael@0 188 }
michael@0 189 });
michael@0 190 });
michael@0 191
michael@0 192 add_test(function test10() {
michael@0 193 do_log_info("Check entries that are really old or in the future");
michael@0 194
michael@0 195 let changes = [ ];
michael@0 196 changes.push({ op : "add", fieldname: "field4", value: "date of 0",
michael@0 197 timesUsed: 1, firstUsed: 0, lastUsed: 0 });
michael@0 198 changes.push({ op : "add", fieldname: "field4", value: "in the future 1",
michael@0 199 timesUsed: 1, firstUsed: 0, lastUsed: now * 2 });
michael@0 200 changes.push({ op : "add", fieldname: "field4", value: "in the future 2",
michael@0 201 timesUsed: 1, firstUsed: now * 2, lastUsed: now * 2 });
michael@0 202 updateFormHistory(changes, run_next_test);
michael@0 203 });
michael@0 204
michael@0 205 add_test(function test11() {
michael@0 206 fac.autoCompleteSearchAsync("field4", "", null, null, {
michael@0 207 onSearchCompletion : function(aResults) {
michael@0 208 do_check_eq(aResults.matchCount, 3);
michael@0 209 run_next_test();
michael@0 210 }
michael@0 211 });
michael@0 212 });
michael@0 213
michael@0 214 let syncValues = ["sync1", "sync1a", "sync2", "sync3"]
michael@0 215
michael@0 216 add_test(function test12() {
michael@0 217 do_log_info("Check old synchronous api");
michael@0 218
michael@0 219 let changes = [ ];
michael@0 220 for (let value of syncValues) {
michael@0 221 changes.push({ op : "add", fieldname: "field5", value: value });
michael@0 222 }
michael@0 223 updateFormHistory(changes, run_next_test);
michael@0 224 });
michael@0 225
michael@0 226 add_test(function test13() {
michael@0 227 let autocompleteService = Cc["@mozilla.org/satchel/form-autocomplete;1"].getService(Ci.nsIFormAutoComplete);
michael@0 228 let results = autocompleteService.autoCompleteSearch("field5", "", null, null);
michael@0 229 do_check_eq(results.matchCount, syncValues.length, "synchronous matchCount");
michael@0 230 for (let i = 0; i < results.matchCount; i++) {
michael@0 231 do_check_eq(results.getValueAt(i), syncValues[i]);
michael@0 232 }
michael@0 233
michael@0 234 let results = autocompleteService.autoCompleteSearch("field5", "sync1", null, null);
michael@0 235 do_check_eq(results.matchCount, 2, "synchronous matchCount");
michael@0 236 do_check_eq(results.getValueAt(0), "sync1");
michael@0 237 do_check_eq(results.getValueAt(1), "sync1a");
michael@0 238 run_next_test();
michael@0 239 });
michael@0 240
michael@0 241 add_test(function test_token_limit_DB() {
michael@0 242 function test_token_limit_previousResult(previousResult) {
michael@0 243 do_log_info("Check that the number of tokens used in a search is not capped to " +
michael@0 244 "MAX_SEARCH_TOKENS when using a previousResult");
michael@0 245 // This provide more accuracy since performance is less of an issue.
michael@0 246 // Search for a string where the first 10 tokens match the previous value but the 11th does not
michael@0 247 // when re-using a previous result.
michael@0 248 fac.autoCompleteSearchAsync("field_token_cap",
michael@0 249 "a b c d e f g h i j .",
michael@0 250 null, previousResult, {
michael@0 251 onSearchCompletion : function(aResults) {
michael@0 252 do_check_eq(aResults.matchCount, 0,
michael@0 253 "All search tokens should be used with " +
michael@0 254 "previous results");
michael@0 255 run_next_test();
michael@0 256 }
michael@0 257 });
michael@0 258 }
michael@0 259
michael@0 260 do_log_info("Check that the number of tokens used in a search is capped to MAX_SEARCH_TOKENS " +
michael@0 261 "for performance when querying the DB");
michael@0 262 let changes = [ ];
michael@0 263 changes.push({ op : "add", fieldname: "field_token_cap",
michael@0 264 // value with 15 unique tokens
michael@0 265 value: "a b c d e f g h i j k l m n o",
michael@0 266 timesUsed: 1, firstUsed: 0, lastUsed: 0 });
michael@0 267 updateFormHistory(changes, () => {
michael@0 268 // Search for a string where the first 10 tokens match the value above but the 11th does not
michael@0 269 // (which would prevent the result from being returned if the 11th term was used).
michael@0 270 fac.autoCompleteSearchAsync("field_token_cap",
michael@0 271 "a b c d e f g h i j .",
michael@0 272 null, null, {
michael@0 273 onSearchCompletion : function(aResults) {
michael@0 274 do_check_eq(aResults.matchCount, 1,
michael@0 275 "Only the first MAX_SEARCH_TOKENS tokens " +
michael@0 276 "should be used for DB queries");
michael@0 277 test_token_limit_previousResult(aResults);
michael@0 278 }
michael@0 279 });
michael@0 280 });
michael@0 281 });

mercurial