summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/unit/test_autocomplete.js
blob: 211753809911eebf8e246e57519d0b798817c75a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var testnum = 0;
var fac;
var prefs;

var numRecords, timeGroupingSize, now;

const DEFAULT_EXPIRE_DAYS = 180;

function padLeft(number, length) {
    var str = number + '';
    while (str.length < length)
        str = '0' + str;
    return str;
}

function getFormExpiryDays() {
    if (prefs.prefHasUserValue("browser.formfill.expire_days")) {
        return prefs.getIntPref("browser.formfill.expire_days");
    }
    return DEFAULT_EXPIRE_DAYS;
}

function run_test() {
    // ===== test init =====
    var testfile = do_get_file("formhistory_autocomplete.sqlite");
    var profileDir = dirSvc.get("ProfD", Ci.nsIFile);

    // Cleanup from any previous tests or failures.
    var destFile = profileDir.clone();
    destFile.append("formhistory.sqlite");
    if (destFile.exists())
      destFile.remove(false);

    testfile.copyTo(profileDir, "formhistory.sqlite");

    fac = Cc["@mozilla.org/satchel/form-autocomplete;1"].
          getService(Ci.nsIFormAutoComplete);
    prefs = Cc["@mozilla.org/preferences-service;1"].
            getService(Ci.nsIPrefBranch);

    timeGroupingSize = prefs.getIntPref("browser.formfill.timeGroupingSize") * 1000 * 1000;

    run_next_test();
}

add_test(function test0() {
    var maxTimeGroupings = prefs.getIntPref("browser.formfill.maxTimeGroupings");
    var bucketSize = prefs.getIntPref("browser.formfill.bucketSize");

    // ===== Tests with constant timesUsed and varying lastUsed date =====
    // insert 2 records per bucket to check alphabetical sort within
    now = 1000 * Date.now();
    numRecords = Math.ceil(maxTimeGroupings / bucketSize) * 2;

    let changes = [ ];
    for (let i = 0; i < numRecords; i+=2) {
        let useDate = now - (i/2 * bucketSize * timeGroupingSize);

        changes.push({ op : "add", fieldname: "field1", value: "value" + padLeft(numRecords - 1 - i, 2),
                       timesUsed: 1, firstUsed: useDate, lastUsed: useDate });
        changes.push({ op : "add", fieldname: "field1", value: "value" + padLeft(numRecords - 2 - i, 2),
                       timesUsed: 1, firstUsed: useDate, lastUsed: useDate });
    }

    updateFormHistory(changes, run_next_test);
});

add_test(function test1() {
    do_log_info("Check initial state is as expected");

    countEntries(null, null, function () {
      countEntries("field1", null, function (count) {
        do_check_true(count > 0);
        run_next_test();
      });
    });
});

add_test(function test2() {
    do_log_info("Check search contains all entries");

    fac.autoCompleteSearchAsync("field1", "", null, null, null, {
        onSearchCompletion : function(aResults) {
            do_check_eq(numRecords, aResults.matchCount);
            run_next_test();
        }
    });
});

add_test(function test3() {
    do_log_info("Check search result ordering with empty search term");

    let lastFound = numRecords;
    fac.autoCompleteSearchAsync("field1", "", null, null, null, {
        onSearchCompletion : function(aResults) {
            for (let i = 0; i < numRecords; i+=2) {
                do_check_eq(parseInt(aResults.getValueAt(i + 1).substr(5), 10), --lastFound);
                do_check_eq(parseInt(aResults.getValueAt(i).substr(5), 10), --lastFound);
            }
            run_next_test();
        }
    });
});

add_test(function test4() {
    do_log_info("Check search result ordering with \"v\"");

    let lastFound = numRecords;
    fac.autoCompleteSearchAsync("field1", "v", null, null, null, {
        onSearchCompletion : function(aResults) {
            for (let i = 0; i < numRecords; i+=2) {
                do_check_eq(parseInt(aResults.getValueAt(i + 1).substr(5), 10), --lastFound);
                do_check_eq(parseInt(aResults.getValueAt(i).substr(5), 10), --lastFound);
            }
            run_next_test();
        }
    });
});

const timesUsedSamples = 20;

add_test(function test5() {
    do_log_info("Begin tests with constant use dates and varying timesUsed");

    let changes =  [];
    for (let i = 0; i < timesUsedSamples; i++) {
        let timesUsed = (timesUsedSamples - i);
        let change = { op : "add", fieldname: "field2", value: "value" + (timesUsedSamples - 1 -  i),
                       timesUsed: timesUsed * timeGroupingSize, firstUsed: now, lastUsed: now };
        changes.push(change);
    }
    updateFormHistory(changes, run_next_test);
});

add_test(function test6() {
    do_log_info("Check search result ordering with empty search term");

    let lastFound = timesUsedSamples;
    fac.autoCompleteSearchAsync("field2", "", null, null, null, {
        onSearchCompletion : function(aResults) {
            for (let i = 0; i < timesUsedSamples; i++) {
                do_check_eq(parseInt(aResults.getValueAt(i).substr(5)), --lastFound);
            }
            run_next_test();
        }
    });
});

add_test(function test7() {
    do_log_info("Check search result ordering with \"v\"");

    let lastFound = timesUsedSamples;
    fac.autoCompleteSearchAsync("field2", "v", null, null, null, {
        onSearchCompletion : function(aResults) {
            for (let i = 0; i < timesUsedSamples; i++) {
                do_check_eq(parseInt(aResults.getValueAt(i).substr(5)), --lastFound);
            }
            run_next_test();
        }
    });
});

add_test(function test8() {
    do_log_info("Check that \"senior citizen\" entries get a bonus (browser.formfill.agedBonus)");

    let agedDate = 1000 * (Date.now() - getFormExpiryDays() * 24 * 60 * 60 * 1000);

    let changes = [ ];
    changes.push({ op : "add", fieldname: "field3", value: "old but not senior",
                   timesUsed: 100, firstUsed: (agedDate + 60 * 1000 * 1000), lastUsed: now });
    changes.push({ op : "add", fieldname: "field3", value: "senior citizen",
                   timesUsed: 100, firstUsed: (agedDate - 60 * 1000 * 1000), lastUsed: now });
    updateFormHistory(changes, run_next_test);
});

add_test(function test9() {
    fac.autoCompleteSearchAsync("field3", "", null, null, null, {
        onSearchCompletion : function(aResults) {
            do_check_eq(aResults.getValueAt(0), "senior citizen");
            do_check_eq(aResults.getValueAt(1), "old but not senior");
            run_next_test();
        }
    });
});

add_test(function test10() {
    do_log_info("Check entries that are really old or in the future");

    let changes = [ ];
    changes.push({ op : "add", fieldname: "field4", value: "date of 0",
                   timesUsed: 1, firstUsed: 0, lastUsed: 0 });
    changes.push({ op : "add", fieldname: "field4", value: "in the future 1",
                   timesUsed: 1, firstUsed: 0, lastUsed: now * 2 });
    changes.push({ op : "add", fieldname: "field4", value: "in the future 2",
                   timesUsed: 1, firstUsed: now * 2, lastUsed: now * 2 });
    updateFormHistory(changes, run_next_test);
});

add_test(function test11() {
    fac.autoCompleteSearchAsync("field4", "", null, null, null, {
        onSearchCompletion : function(aResults) {
            do_check_eq(aResults.matchCount, 3);
            run_next_test();
        }
    });
});

var syncValues = ["sync1", "sync1a", "sync2", "sync3"]

add_test(function test12() {
    do_log_info("Check old synchronous api");

    let changes = [ ];
    for (let value of syncValues) {
      changes.push({ op : "add", fieldname: "field5", value: value });
    }
    updateFormHistory(changes, run_next_test);
});

add_test(function test_token_limit_DB() {
    function test_token_limit_previousResult(previousResult) {
        do_log_info("Check that the number of tokens used in a search is not capped to " +
                    "MAX_SEARCH_TOKENS when using a previousResult");
        // This provide more accuracy since performance is less of an issue.
        // Search for a string where the first 10 tokens match the previous value but the 11th does not
        // when re-using a previous result.
        fac.autoCompleteSearchAsync("field_token_cap",
                                    "a b c d e f g h i j .",
                                    null, previousResult, null, {
                                        onSearchCompletion : function(aResults) {
                                            do_check_eq(aResults.matchCount, 0,
                                                        "All search tokens should be used with " +
                                                        "previous results");
                                            run_next_test();
                                        }
                                    });
    }

    do_log_info("Check that the number of tokens used in a search is capped to MAX_SEARCH_TOKENS " +
                "for performance when querying the DB");
    let changes = [ ];
    changes.push({ op : "add", fieldname: "field_token_cap",
                   // value with 15 unique tokens
                   value: "a b c d e f g h i j k l m n o",
                   timesUsed: 1, firstUsed: 0, lastUsed: 0 });
    updateFormHistory(changes, () => {
        // Search for a string where the first 10 tokens match the value above but the 11th does not
        // (which would prevent the result from being returned if the 11th term was used).
        fac.autoCompleteSearchAsync("field_token_cap",
                                    "a b c d e f g h i j .",
                                    null, null, null, {
                                        onSearchCompletion : function(aResults) {
                                            do_check_eq(aResults.matchCount, 1,
                                                        "Only the first MAX_SEARCH_TOKENS tokens " +
                                                        "should be used for DB queries");
                                            test_token_limit_previousResult(aResults);
                                        }
        });
    });
});