michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var testnum = 0; michael@0: var fh; michael@0: var fac; michael@0: var prefs; michael@0: michael@0: function countAllEntries() { michael@0: let stmt = fh.DBConnection.createStatement("SELECT COUNT(*) as numEntries FROM moz_formhistory"); michael@0: do_check_true(stmt.executeStep()); michael@0: let numEntries = stmt.row.numEntries; michael@0: stmt.finalize(); michael@0: return numEntries; michael@0: } michael@0: michael@0: function do_AC_search(searchTerm, previousResult) { michael@0: var duration = 0; michael@0: var searchCount = 5; michael@0: var tempPrevious = null; michael@0: var startTime; michael@0: for (var i = 0; i < searchCount; i++) { michael@0: if (previousResult !== null) michael@0: tempPrevious = fac.autoCompleteSearch("searchbar-history", previousResult, null, null); michael@0: startTime = Date.now(); michael@0: results = fac.autoCompleteSearch("searchbar-history", searchTerm, null, tempPrevious); michael@0: duration += Date.now() - startTime; michael@0: } michael@0: dump("[autoCompleteSearch][test " + testnum + "] for '" + searchTerm + "' "); michael@0: if (previousResult !== null) michael@0: dump("with '" + previousResult + "' previous result "); michael@0: else michael@0: dump("w/o previous result "); michael@0: dump("took " + duration + " ms with " + results.matchCount + " matches. "); michael@0: dump("Average of " + Math.round(duration / searchCount) + " ms per search\n"); michael@0: return results; michael@0: } michael@0: michael@0: function run_test() { michael@0: try { michael@0: michael@0: // ===== test init ===== michael@0: var testfile = do_get_file("formhistory_1000.sqlite"); michael@0: var profileDir = dirSvc.get("ProfD", Ci.nsIFile); michael@0: var results; michael@0: michael@0: // Cleanup from any previous tests or failures. michael@0: var destFile = profileDir.clone(); michael@0: destFile.append("formhistory.sqlite"); michael@0: if (destFile.exists()) michael@0: destFile.remove(false); michael@0: michael@0: testfile.copyTo(profileDir, "formhistory.sqlite"); michael@0: michael@0: fh = Cc["@mozilla.org/satchel/form-history;1"]. michael@0: getService(Ci.nsIFormHistory2); michael@0: fac = Cc["@mozilla.org/satchel/form-autocomplete;1"]. michael@0: getService(Ci.nsIFormAutoComplete); michael@0: prefs = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: michael@0: timeGroupingSize = prefs.getIntPref("browser.formfill.timeGroupingSize") * 1000 * 1000; michael@0: maxTimeGroupings = prefs.getIntPref("browser.formfill.maxTimeGroupings"); michael@0: bucketSize = prefs.getIntPref("browser.formfill.bucketSize"); michael@0: michael@0: // ===== 1 ===== michael@0: // Check initial state is as expected michael@0: testnum++; michael@0: do_check_true(fh.hasEntries); michael@0: do_check_eq(1000, countAllEntries()); michael@0: fac.autoCompleteSearch("searchbar-history", "zzzzzzzzzz", null, null); // warm-up search michael@0: do_check_true(fh.nameExists("searchbar-history")); michael@0: michael@0: // ===== 2 ===== michael@0: // Search for '' with no previous result michael@0: testnum++; michael@0: results = do_AC_search("", null); michael@0: do_check_true(results.matchCount > 0); michael@0: michael@0: // ===== 3 ===== michael@0: // Search for 'r' with no previous result michael@0: testnum++; michael@0: results = do_AC_search("r", null); michael@0: do_check_true(results.matchCount > 0); michael@0: michael@0: // ===== 4 ===== michael@0: // Search for 'r' with '' previous result michael@0: testnum++; michael@0: results = do_AC_search("r", ""); michael@0: do_check_true(results.matchCount > 0); michael@0: michael@0: // ===== 5 ===== michael@0: // Search for 're' with no previous result michael@0: testnum++; michael@0: results = do_AC_search("re", null); michael@0: do_check_true(results.matchCount > 0); michael@0: michael@0: // ===== 6 ===== michael@0: // Search for 're' with 'r' previous result michael@0: testnum++; michael@0: results = do_AC_search("re", "r"); michael@0: do_check_true(results.matchCount > 0); michael@0: michael@0: // ===== 7 ===== michael@0: // Search for 'rea' without previous result michael@0: testnum++; michael@0: results = do_AC_search("rea", null); michael@0: let countREA = results.matchCount; michael@0: michael@0: // ===== 8 ===== michael@0: // Search for 'rea' with 're' previous result michael@0: testnum++; michael@0: results = do_AC_search("rea", "re"); michael@0: do_check_eq(countREA, results.matchCount); michael@0: michael@0: // ===== 9 ===== michael@0: // Search for 'real' with 'rea' previous result michael@0: testnum++; michael@0: results = do_AC_search("real", "rea"); michael@0: let countREAL = results.matchCount; michael@0: do_check_true(results.matchCount <= countREA); michael@0: michael@0: // ===== 10 ===== michael@0: // Search for 'real' with 're' previous result michael@0: testnum++; michael@0: results = do_AC_search("real", "re"); michael@0: do_check_eq(countREAL, results.matchCount); michael@0: michael@0: // ===== 11 ===== michael@0: // Search for 'real' with no previous result michael@0: testnum++; michael@0: results = do_AC_search("real", null); michael@0: do_check_eq(countREAL, results.matchCount); michael@0: michael@0: michael@0: } catch (e) { michael@0: throw "FAILED in test #" + testnum + " -- " + e; michael@0: } michael@0: }