summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browsingData_formData.js
blob: 06d928b9b1cf7381a90e99bdf5cbfb1bf11d5cfb (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  FormHistory: "resource://gre/modules/FormHistory.sys.mjs",
  PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs",
});

const REFERENCE_DATE = Date.now();

async function countEntries(fieldname, message, expected) {
  let count = await FormHistory.count({ fieldname });
  is(count, expected, message);
}

async function setupFormHistory() {
  async function searchFirstEntry(terms, params) {
    return (await FormHistory.search(terms, params))[0];
  }

  // Make sure we've got a clean DB to start with, then add the entries we'll be testing.
  await FormHistory.update([
    { op: "remove" },
    {
      op: "add",
      fieldname: "reference",
      value: "reference",
    },
    {
      op: "add",
      fieldname: "10secondsAgo",
      value: "10s",
    },
    {
      op: "add",
      fieldname: "10minutesAgo",
      value: "10m",
    },
  ]);

  // Age the entries to the proper vintage.
  let timestamp = PlacesUtils.toPRTime(REFERENCE_DATE);
  let result = await searchFirstEntry(["guid"], { fieldname: "reference" });
  await FormHistory.update({
    op: "update",
    firstUsed: timestamp,
    guid: result.guid,
  });

  timestamp = PlacesUtils.toPRTime(REFERENCE_DATE - 10000);
  result = await searchFirstEntry(["guid"], { fieldname: "10secondsAgo" });
  await FormHistory.update({
    op: "update",
    firstUsed: timestamp,
    guid: result.guid,
  });

  timestamp = PlacesUtils.toPRTime(REFERENCE_DATE - 10000 * 60);
  result = await searchFirstEntry(["guid"], { fieldname: "10minutesAgo" });
  await FormHistory.update({
    op: "update",
    firstUsed: timestamp,
    guid: result.guid,
  });

  // Sanity check.
  await countEntries(
    "reference",
    "Checking for 10minutes form history entry creation",
    1
  );
  await countEntries(
    "10secondsAgo",
    "Checking for 1hour form history entry creation",
    1
  );
  await countEntries(
    "10minutesAgo",
    "Checking for 1hour10minutes form history entry creation",
    1
  );
}

add_task(async function testFormData() {
  function background() {
    browser.test.onMessage.addListener(async (msg, options) => {
      if (msg == "removeFormData") {
        await browser.browsingData.removeFormData(options);
      } else {
        await browser.browsingData.remove(options, { formData: true });
      }
      browser.test.sendMessage("formDataRemoved");
    });
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["browsingData"],
    },
  });

  async function testRemovalMethod(method) {
    // Clear form data with no since value.
    await setupFormHistory();
    extension.sendMessage(method, {});
    await extension.awaitMessage("formDataRemoved");

    await countEntries(
      "reference",
      "reference form entry should be deleted.",
      0
    );
    await countEntries(
      "10secondsAgo",
      "10secondsAgo form entry should be deleted.",
      0
    );
    await countEntries(
      "10minutesAgo",
      "10minutesAgo form entry should be deleted.",
      0
    );

    // Clear form data with recent since value.
    await setupFormHistory();
    extension.sendMessage(method, { since: REFERENCE_DATE });
    await extension.awaitMessage("formDataRemoved");

    await countEntries(
      "reference",
      "reference form entry should be deleted.",
      0
    );
    await countEntries(
      "10secondsAgo",
      "10secondsAgo form entry should still exist.",
      1
    );
    await countEntries(
      "10minutesAgo",
      "10minutesAgo form entry should still exist.",
      1
    );

    // Clear form data with old since value.
    await setupFormHistory();
    extension.sendMessage(method, { since: REFERENCE_DATE - 1000000 });
    await extension.awaitMessage("formDataRemoved");

    await countEntries(
      "reference",
      "reference form entry should be deleted.",
      0
    );
    await countEntries(
      "10secondsAgo",
      "10secondsAgo form entry should be deleted.",
      0
    );
    await countEntries(
      "10minutesAgo",
      "10minutesAgo form entry should be deleted.",
      0
    );
  }

  await extension.startup();

  await testRemovalMethod("removeFormData");
  await testRemovalMethod("remove");

  await extension.unload();
});