summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/unit/test_history_api.js
blob: 91da24696c38a7eb0dcdc1036fec7329ae12d051 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* 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/. */

var testnum = 0;
var dbConnection; // used for deleted table tests

async function countDeletedEntries(expected) {
  let stmt = "SELECT COUNT(*) AS numEntries FROM moz_deleted_formhistory";
  try {
    let requiredRow = await dbConnection.executeCached(stmt);
    Assert.equal(expected, requiredRow[0].getResultByName("numEntries"));
  } catch (error) {
    do_throw("Error occurred counting deleted entries: " + error);
  }
}

async function checkTimeDeleted(guid, checkFunction) {
  let stmt =
    "SELECT timeDeleted FROM moz_deleted_formhistory WHERE guid = :guid";
  let params = { guid };

  try {
    let requiredRow = await dbConnection.executeCached(stmt, params);
    checkFunction(requiredRow[0].getResultByName("timeDeleted"));
  } catch (error) {
    do_throw("Error occurred getting deleted entries: " + error);
  }
}

function promiseUpdateEntry(op, name, value) {
  let change = { op };
  if (name !== null) {
    change.fieldname = name;
  }
  if (value !== null) {
    change.value = value;
  }
  return promiseUpdate(change);
}

add_task(async function () {
  let oldSupportsDeletedTable = FormHistory._supportsDeletedTable;
  FormHistory._supportsDeletedTable = true;

  try {
    // ===== test init =====
    let testfile = do_get_file("formhistory_apitest.sqlite");
    let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);

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

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

    function checkExists(num) {
      Assert.ok(num > 0);
    }
    function checkNotExists(num) {
      Assert.ok(num == 0);
    }

    // ===== 1 =====
    // Check initial state is as expected
    testnum++;
    await promiseCountEntries("name-A", null, checkExists);
    await promiseCountEntries("name-B", null, checkExists);
    await promiseCountEntries("name-C", null, checkExists);
    await promiseCountEntries("name-D", null, checkExists);
    await promiseCountEntries("name-A", "value-A", checkExists);
    await promiseCountEntries("name-B", "value-B1", checkExists);
    await promiseCountEntries("name-B", "value-B2", checkExists);
    await promiseCountEntries("name-C", "value-C", checkExists);
    await promiseCountEntries("name-D", "value-D", checkExists);
    // time-A/B/C/D checked below.

    // Delete anything from the deleted table
    let dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile).clone();
    dbFile.append("formhistory.sqlite");

    dbConnection = await Sqlite.openConnection({
      path: dbFile.path,
      sharedMemoryCache: false,
    });

    let stmt = "DELETE FROM moz_deleted_formhistory";
    try {
      await dbConnection.executeCached(stmt);
    } catch (error) {
      do_throw("Error occurred counting deleted all entries: " + error);
    }

    // ===== 2 =====
    // Test looking for nonexistent / bogus data.
    testnum++;
    await promiseCountEntries("blah", null, checkNotExists);
    await promiseCountEntries("", null, checkNotExists);
    await promiseCountEntries("name-A", "blah", checkNotExists);
    await promiseCountEntries("name-A", "", checkNotExists);
    await promiseCountEntries("name-A", null, checkExists);
    await promiseCountEntries("blah", "value-A", checkNotExists);
    await promiseCountEntries("", "value-A", checkNotExists);
    await promiseCountEntries(null, "value-A", checkExists);

    // Cannot use promiseCountEntries when name and value are null
    // because it treats null values as not set
    // and here a search should be done explicity for null.
    let count = await FormHistory.count({ fieldname: null, value: null });
    checkNotExists(count);

    // ===== 3 =====
    // Test removeEntriesForName with a single matching value
    testnum++;
    await promiseUpdateEntry("remove", "name-A", null);

    await promiseCountEntries("name-A", "value-A", checkNotExists);
    await promiseCountEntries("name-B", "value-B1", checkExists);
    await promiseCountEntries("name-B", "value-B2", checkExists);
    await promiseCountEntries("name-C", "value-C", checkExists);
    await promiseCountEntries("name-D", "value-D", checkExists);
    await countDeletedEntries(1);

    // ===== 4 =====
    // Test removeEntriesForName with multiple matching values
    testnum++;
    await promiseUpdateEntry("remove", "name-B", null);

    await promiseCountEntries("name-A", "value-A", checkNotExists);
    await promiseCountEntries("name-B", "value-B1", checkNotExists);
    await promiseCountEntries("name-B", "value-B2", checkNotExists);
    await promiseCountEntries("name-C", "value-C", checkExists);
    await promiseCountEntries("name-D", "value-D", checkExists);
    await countDeletedEntries(3);

    // ===== 5 =====
    // Test removing by time range (single entry, not surrounding entries)
    testnum++;
    await promiseCountEntries("time-A", null, checkExists); // firstUsed=1000, lastUsed=1000
    await promiseCountEntries("time-B", null, checkExists); // firstUsed=1000, lastUsed=1099
    await promiseCountEntries("time-C", null, checkExists); // firstUsed=1099, lastUsed=1099
    await promiseCountEntries("time-D", null, checkExists); // firstUsed=2001, lastUsed=2001
    await promiseUpdate({
      op: "remove",
      firstUsedStart: 1050,
      firstUsedEnd: 2000,
    });

    await promiseCountEntries("time-A", null, checkExists);
    await promiseCountEntries("time-B", null, checkExists);
    await promiseCountEntries("time-C", null, checkNotExists);
    await promiseCountEntries("time-D", null, checkExists);
    await countDeletedEntries(4);

    // ===== 6 =====
    // Test removing by time range (multiple entries)
    testnum++;
    await promiseUpdate({
      op: "remove",
      firstUsedStart: 1000,
      firstUsedEnd: 2000,
    });

    await promiseCountEntries("time-A", null, checkNotExists);
    await promiseCountEntries("time-B", null, checkNotExists);
    await promiseCountEntries("time-C", null, checkNotExists);
    await promiseCountEntries("time-D", null, checkExists);
    await countDeletedEntries(6);

    // ===== 7 =====
    // test removeAllEntries
    testnum++;
    await promiseUpdateEntry("remove", null, null);

    await promiseCountEntries("name-C", null, checkNotExists);
    await promiseCountEntries("name-D", null, checkNotExists);
    await promiseCountEntries("name-C", "value-C", checkNotExists);
    await promiseCountEntries("name-D", "value-D", checkNotExists);

    await promiseCountEntries(null, null, checkNotExists);
    await countDeletedEntries(6);

    // ===== 8 =====
    // Add a single entry back
    testnum++;
    await promiseUpdateEntry("add", "newname-A", "newvalue-A");
    await promiseCountEntries("newname-A", "newvalue-A", checkExists);

    // ===== 9 =====
    // Remove the single entry
    testnum++;
    await promiseUpdateEntry("remove", "newname-A", "newvalue-A");
    await promiseCountEntries("newname-A", "newvalue-A", checkNotExists);

    // ===== 10 =====
    // Add a single entry
    testnum++;
    await promiseUpdateEntry("add", "field1", "value1");
    await promiseCountEntries("field1", "value1", checkExists);

    let processFirstResult = function processResults(results) {
      // Only handle the first result
      if (results.length) {
        let result = results[0];
        return [
          result.timesUsed,
          result.firstUsed,
          result.lastUsed,
          result.guid,
        ];
      }
      return undefined;
    };

    let results = await FormHistory.search(
      ["timesUsed", "firstUsed", "lastUsed"],
      { fieldname: "field1", value: "value1" }
    );
    let [timesUsed, firstUsed, lastUsed] = processFirstResult(results);
    Assert.equal(1, timesUsed);
    Assert.ok(firstUsed > 0);
    Assert.ok(lastUsed > 0);
    await promiseCountEntries(null, null, num => Assert.equal(num, 1));

    // ===== 11 =====
    // Add another single entry
    testnum++;
    await promiseUpdateEntry("add", "field1", "value1b");
    await promiseCountEntries("field1", "value1", checkExists);
    await promiseCountEntries("field1", "value1b", checkExists);
    await promiseCountEntries(null, null, num => Assert.equal(num, 2));

    // ===== 12 =====
    // Update a single entry
    testnum++;

    results = await FormHistory.search(["guid"], {
      fieldname: "field1",
      value: "value1",
    });
    let guid = processFirstResult(results)[3];

    await promiseUpdate({ op: "update", guid, value: "modifiedValue" });
    await promiseCountEntries("field1", "modifiedValue", checkExists);
    await promiseCountEntries("field1", "value1", checkNotExists);
    await promiseCountEntries("field1", "value1b", checkExists);
    await promiseCountEntries(null, null, num => Assert.equal(num, 2));

    // ===== 13 =====
    // Add a single entry with times
    testnum++;
    await promiseUpdate({
      op: "add",
      fieldname: "field2",
      value: "value2",
      timesUsed: 20,
      firstUsed: 100,
      lastUsed: 500,
    });

    results = await FormHistory.search(["timesUsed", "firstUsed", "lastUsed"], {
      fieldname: "field2",
      value: "value2",
    });
    [timesUsed, firstUsed, lastUsed] = processFirstResult(results);

    Assert.equal(20, timesUsed);
    Assert.equal(100, firstUsed);
    Assert.equal(500, lastUsed);
    await promiseCountEntries(null, null, num => Assert.equal(num, 3));

    // ===== 14 =====
    // Bump an entry, which updates its lastUsed field
    testnum++;
    await promiseUpdate({
      op: "bump",
      fieldname: "field2",
      value: "value2",
      timesUsed: 20,
      firstUsed: 100,
      lastUsed: 500,
    });
    results = await FormHistory.search(["timesUsed", "firstUsed", "lastUsed"], {
      fieldname: "field2",
      value: "value2",
    });
    [timesUsed, firstUsed, lastUsed] = processFirstResult(results);
    Assert.equal(21, timesUsed);
    Assert.equal(100, firstUsed);
    Assert.ok(lastUsed > 500);
    await promiseCountEntries(null, null, num => Assert.equal(num, 3));

    // ===== 15 =====
    // Bump an entry that does not exist
    testnum++;
    await promiseUpdate({
      op: "bump",
      fieldname: "field3",
      value: "value3",
      timesUsed: 10,
      firstUsed: 50,
      lastUsed: 400,
    });
    results = await FormHistory.search(["timesUsed", "firstUsed", "lastUsed"], {
      fieldname: "field3",
      value: "value3",
    });
    [timesUsed, firstUsed, lastUsed] = processFirstResult(results);
    Assert.equal(10, timesUsed);
    Assert.equal(50, firstUsed);
    Assert.equal(400, lastUsed);
    await promiseCountEntries(null, null, num => Assert.equal(num, 4));

    // ===== 16 =====
    // Bump an entry with a guid
    testnum++;
    results = await FormHistory.search(["guid"], {
      fieldname: "field3",
      value: "value3",
    });
    guid = processFirstResult(results)[3];
    await promiseUpdate({
      op: "bump",
      guid,
      timesUsed: 20,
      firstUsed: 55,
      lastUsed: 400,
    });
    results = await FormHistory.search(["timesUsed", "firstUsed", "lastUsed"], {
      fieldname: "field3",
      value: "value3",
    });
    [timesUsed, firstUsed, lastUsed] = processFirstResult(results);
    Assert.equal(11, timesUsed);
    Assert.equal(50, firstUsed);
    Assert.ok(lastUsed > 400);
    await promiseCountEntries(null, null, num => Assert.equal(num, 4));

    // ===== 17 =====
    // Remove an entry
    testnum++;
    await countDeletedEntries(7);

    results = await FormHistory.search(["guid"], {
      fieldname: "field1",
      value: "value1b",
    });
    guid = processFirstResult(results)[3];

    await promiseUpdate({ op: "remove", guid });
    await promiseCountEntries("field1", "modifiedValue", checkExists);
    await promiseCountEntries("field1", "value1b", checkNotExists);
    await promiseCountEntries(null, null, num => Assert.equal(num, 3));

    await countDeletedEntries(8);
    await checkTimeDeleted(guid, timeDeleted => Assert.ok(timeDeleted > 10000));

    // ===== 18 =====
    // Add yet another single entry
    testnum++;
    await promiseUpdate({
      op: "add",
      fieldname: "field4",
      value: "value4",
      timesUsed: 5,
      firstUsed: 230,
      lastUsed: 600,
    });
    await promiseCountEntries(null, null, num => Assert.equal(num, 4));

    // ===== 19 =====
    // Remove an entry by time
    testnum++;
    await promiseUpdate({
      op: "remove",
      firstUsedStart: 60,
      firstUsedEnd: 250,
    });
    await promiseCountEntries("field1", "modifiedValue", checkExists);
    await promiseCountEntries("field2", "value2", checkNotExists);
    await promiseCountEntries("field3", "value3", checkExists);
    await promiseCountEntries("field4", "value4", checkNotExists);
    await promiseCountEntries(null, null, num => Assert.equal(num, 2));
    await countDeletedEntries(10);

    // ===== 20 =====
    // Bump multiple existing entries at once
    testnum++;

    await promiseUpdate([
      {
        op: "add",
        fieldname: "field5",
        value: "value5",
        timesUsed: 5,
        firstUsed: 230,
        lastUsed: 600,
      },
      {
        op: "add",
        fieldname: "field6",
        value: "value6",
        timesUsed: 12,
        firstUsed: 430,
        lastUsed: 700,
      },
    ]);
    await promiseCountEntries(null, null, num => Assert.equal(num, 4));

    await promiseUpdate([
      { op: "bump", fieldname: "field5", value: "value5" },
      { op: "bump", fieldname: "field6", value: "value6" },
    ]);
    results = await FormHistory.search(
      ["fieldname", "timesUsed", "firstUsed", "lastUsed"],
      {}
    );

    Assert.equal(6, results[2].timesUsed);
    Assert.equal(13, results[3].timesUsed);
    Assert.equal(230, results[2].firstUsed);
    Assert.equal(430, results[3].firstUsed);
    Assert.ok(results[2].lastUsed > 600);
    Assert.ok(results[3].lastUsed > 700);

    await promiseCountEntries(null, null, num => Assert.equal(num, 4));

    // ===== 21 =====
    // Check update fails if form history is disabled and the operation is not a
    // pure removal.
    testnum++;
    Services.prefs.setBoolPref("browser.formfill.enable", false);

    // Cannot use arrow functions, see bug 1237961.
    await Assert.rejects(
      promiseUpdate({ op: "bump", fieldname: "field5", value: "value5" }),
      /Form history is disabled, only remove operations are allowed/,
      "bumping when form history is disabled should fail"
    );
    await Assert.rejects(
      promiseUpdate({ op: "add", fieldname: "field5", value: "value5" }),
      /Form history is disabled, only remove operations are allowed/,
      "Adding when form history is disabled should fail"
    );
    await Assert.rejects(
      promiseUpdate([
        { op: "update", fieldname: "field5", value: "value5" },
        { op: "remove", fieldname: "field5", value: "value5" },
      ]),
      /Form history is disabled, only remove operations are allowed/,
      "mixed operations when form history is disabled should fail"
    );
    await Assert.rejects(
      promiseUpdate([
        null,
        undefined,
        "",
        1,
        {},
        { op: "remove", fieldname: "field5", value: "value5" },
      ]),
      /Form history is disabled, only remove operations are allowed/,
      "Invalid entries when form history is disabled should fail"
    );

    // Remove should work though.
    await promiseUpdate([
      { op: "remove", fieldname: "field5", value: null },
      { op: "remove", fieldname: null, value: null },
    ]);
    Services.prefs.clearUserPref("browser.formfill.enable");
  } catch (e) {
    throw new Error(`FAILED in test #${testnum} -- ${e}`);
  } finally {
    FormHistory._supportsDeletedTable = oldSupportsDeletedTable;
    await dbConnection.close(do_test_finished);
  }
});

function run_test() {
  return run_next_test();
}