Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>BrowsingData FormData test</title>
7
<script type="text/javascript" src="head.js"></script>
8
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
9
</head>
10
<body>
11
12
<script type="text/javascript">
13
"use strict";
14
15
var {FormHistory} = ChromeUtils.import("resource://gre/modules/FormHistory.jsm");
16
17
const REFERENCE_DATE = Date.now();
18
19
function countEntries(fieldname, message, expected) {
20
return new Promise((resolve, reject) => {
21
let callback = {
22
handleResult: result => {
23
is(result, expected, message);
24
resolve();
25
},
26
handleError: reject,
27
};
28
29
FormHistory.count({fieldname}, callback);
30
});
31
}
32
33
async function setupFormHistory() {
34
function searchEntries(terms, params) {
35
return new Promise((resolve, reject) => {
36
let callback = {
37
handleResult: resolve,
38
handleError: reject,
39
};
40
41
FormHistory.search(terms, params, callback);
42
});
43
}
44
45
function update(changes) {
46
return new Promise((resolve, reject) => {
47
let callback = {
48
handleError: reject,
49
handleCompletion: resolve,
50
};
51
FormHistory.update(changes, callback);
52
});
53
}
54
55
// Make sure we've got a clean DB to start with, then add the entries we'll be testing.
56
await update([
57
{op: "remove"},
58
{
59
op: "add",
60
fieldname: "reference",
61
value: "reference",
62
}, {
63
op: "add",
64
fieldname: "10secondsAgo",
65
value: "10s",
66
}, {
67
op: "add",
68
fieldname: "10minutesAgo",
69
value: "10m",
70
}]);
71
72
// Age the entries to the proper vintage.
73
let timestamp = REFERENCE_DATE * 1000;
74
let result = await searchEntries(["guid"], {fieldname: "reference"});
75
await update({op: "update", firstUsed: timestamp, guid: result.guid});
76
77
timestamp = (REFERENCE_DATE - 10000) * 1000;
78
result = await searchEntries(["guid"], {fieldname: "10secondsAgo"});
79
await update({op: "update", firstUsed: timestamp, guid: result.guid});
80
81
timestamp = (REFERENCE_DATE - 10000 * 60) * 1000;
82
result = await searchEntries(["guid"], {fieldname: "10minutesAgo"});
83
await update({op: "update", firstUsed: timestamp, guid: result.guid});
84
85
// Sanity check.
86
await countEntries("reference", "Checking for 10minutes form history entry creation", 1);
87
await countEntries("10secondsAgo", "Checking for 1hour form history entry creation", 1);
88
await countEntries("10minutesAgo", "Checking for 1hour10minutes form history entry creation", 1);
89
}
90
91
add_task(async function testFormData() {
92
function background() {
93
browser.test.onMessage.addListener(async (msg, options) => {
94
if (msg == "removeFormData") {
95
await browser.browsingData.removeFormData(options);
96
} else {
97
await browser.browsingData.remove(options, {formData: true});
98
}
99
browser.test.sendMessage("formDataRemoved");
100
});
101
}
102
103
let extension = ExtensionTestUtils.loadExtension({
104
background,
105
manifest: {
106
permissions: ["browsingData"],
107
},
108
});
109
110
async function testRemovalMethod(method) {
111
// Clear form data with no since value.
112
await setupFormHistory();
113
extension.sendMessage(method, {});
114
await extension.awaitMessage("formDataRemoved");
115
116
await countEntries("reference", "reference form entry should be deleted.", 0);
117
await countEntries("10secondsAgo", "10secondsAgo form entry should be deleted.", 0);
118
await countEntries("10minutesAgo", "10minutesAgo form entry should be deleted.", 0);
119
120
// Clear form data with recent since value.
121
await setupFormHistory();
122
extension.sendMessage(method, {since: REFERENCE_DATE});
123
await extension.awaitMessage("formDataRemoved");
124
125
await countEntries("reference", "reference form entry should be deleted.", 0);
126
await countEntries("10secondsAgo", "10secondsAgo form entry should still exist.", 1);
127
await countEntries("10minutesAgo", "10minutesAgo form entry should still exist.", 1);
128
129
// Clear form data with old since value.
130
await setupFormHistory();
131
extension.sendMessage(method, {since: REFERENCE_DATE - 1000000});
132
await extension.awaitMessage("formDataRemoved");
133
134
await countEntries("reference", "reference form entry should be deleted.", 0);
135
await countEntries("10secondsAgo", "10secondsAgo form entry should be deleted.", 0);
136
await countEntries("10minutesAgo", "10minutesAgo form entry should be deleted.", 0);
137
}
138
139
await extension.startup();
140
141
await testRemovalMethod("removeFormData");
142
await testRemovalMethod("remove");
143
144
await extension.unload();
145
});
146
147
</script>
148
</body>
149
</html>