-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_removeByFilter.js
324 lines (311 loc) · 12.8 KB
/
test_removeByFilter.js
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
"use strict";
/*
This test will ideally test the following cases
(each with and without a callback associated with it)
Case A: Tests which should remove pages (Positives)
Case A 1: Page has multiple visits both in/out of timeframe, all get deleted
Case A 2: Page has single uri, removed by host
Case A 3: Page has random subhost, with same host, removed by wildcard
Case A 4: Page is localhost and localhost:port, removed by host
Case A 5: Page is a `file://` type address, removed by empty host
Cases A 1,2,3 will be tried with and without bookmarks added (which prevent page deletion)
Case B: Tests in which no pages are removed (Inverses)
Case B 1 (inverse): Page has no visits in timeframe, and nothing is deleted
Case B 2: Page has single uri, not removed since hostname is different
Case B 3: Page has multiple subhosts, not removed since wildcard doesn't match
Case C: Combinations tests
Case C 1: Single hostname, multiple visits, at least one in timeframe and hostname
Case C 2: Random subhosts, multiple visits, at least one in timeframe and hostname-wildcard
*/
add_task(async function test_removeByFilter() {
// Cleanup
await PlacesUtils.history.clear();
await PlacesUtils.bookmarks.eraseEverything();
// Adding a witness URI
let witnessURI = NetUtil.newURI("http://witnessmozilla.org/test_browserhistory/test_removeByFilter" + Math.random());
await PlacesTestUtils.addVisits(witnessURI);
Assert.ok((await PlacesTestUtils.isPageInDB(witnessURI)), "Witness URI is in database");
let removeByFilterTester = async function(visits, filter, checkBeforeRemove, checkAfterRemove, useCallback, bookmarkedUri) {
// Add visits for URIs
await PlacesTestUtils.addVisits(visits);
if (bookmarkedUri !== null && visits.map(v => v.uri).includes(bookmarkedUri)) {
await PlacesUtils.bookmarks.insert({
parentGuid: PlacesUtils.bookmarks.unfiledGuid,
url: bookmarkedUri,
title: "test bookmark",
});
}
await checkBeforeRemove();
// Take care of any observers (due to bookmarks)
let { observer, promiseObserved } = getObserverPromise(bookmarkedUri);
if (observer) {
PlacesUtils.history.addObserver(observer, false);
}
// Perfom delete operation on database
let removed = false;
if (useCallback) {
// The amount of callbacks will be the unique URIs to remove from the database
let netCallbacksRequired = (new Set(visits.map(v => v.uri))).size;
removed = await PlacesUtils.history.removeByFilter(filter, pageInfo => {
Assert.ok(PlacesUtils.validatePageInfo(pageInfo, false), "pageInfo should follow a basic format");
Assert.ok(netCallbacksRequired > 0, "Callback called as many times as required");
netCallbacksRequired--;
});
} else {
removed = await PlacesUtils.history.removeByFilter(filter);
}
await checkAfterRemove();
await promiseObserved;
if (observer) {
PlacesUtils.history.removeObserver(observer);
// Remove the added bookmarks as they interfere with following tests
await PlacesUtils.bookmarks.eraseEverything();
}
Assert.ok((await PlacesTestUtils.isPageInDB(witnessURI)), "Witness URI is still in database");
return removed;
};
const remoteUriList = [ "http://mozilla.org/test_browserhistory/test_removeByFilter/" + Math.random(),
"http://subdomain1.mozilla.org/test_browserhistory/test_removeByFilter/" + Math.random(),
"http://subdomain2.mozilla.org/test_browserhistory/test_removeByFilter/" + Math.random(),
];
const localhostUriList = [ "http://localhost:4500/" + Math.random(), "http://localhost/" + Math.random() ];
const fileUriList = [ "file:///home/user/files" + Math.random() ];
const title = "Title " + Math.random();
let sameHostVisits = [
{
uri: remoteUriList[0],
title,
visitDate: new Date(2005, 1, 1) * 1000,
},
{
uri: remoteUriList[0],
title,
visitDate: new Date(2005, 3, 3) * 1000,
},
{
uri: remoteUriList[0],
title,
visitDate: new Date(2007, 1, 1) * 1000,
},
];
let randomHostVisits = [
{
uri: remoteUriList[0],
title,
visitDate: new Date(2005, 1, 1) * 1000,
},
{
uri: remoteUriList[1],
title,
visitDate: new Date(2005, 3, 3) * 1000,
},
{
uri: remoteUriList[2],
title,
visitDate: new Date(2007, 1, 1) * 1000,
},
];
let localhostVisits = [
{
uri: localhostUriList[0],
title,
},
{
uri: localhostUriList[1],
title,
},
];
let fileVisits = [
{
uri: fileUriList[0],
title,
},
];
let assertInDB = async function(aUri) {
Assert.ok((await PlacesTestUtils.isPageInDB(aUri)));
};
let assertNotInDB = async function(aUri) {
Assert.ok(!(await PlacesTestUtils.isPageInDB(aUri)));
};
for (let callbackUse of [true, false]) {
// Case A Positives
for (let bookmarkUse of [true, false]) {
let bookmarkedUri = (arr) => undefined;
let checkableArray = (arr) => arr;
let checkClosure = assertNotInDB;
if (bookmarkUse) {
bookmarkedUri = (arr) => arr[0];
checkableArray = (arr) => arr.slice(1);
checkClosure = function(aUri) { };
}
// Case A 1: Dates
await removeByFilterTester(sameHostVisits,
{ beginDate: new Date(2004, 1, 1), endDate: new Date(2006, 1, 1) },
() => assertInDB(remoteUriList[0]),
() => checkClosure(remoteUriList[0]),
callbackUse, bookmarkedUri(remoteUriList));
// Case A 2: Single Sub-host
await removeByFilterTester(sameHostVisits, { host: "mozilla.org" },
() => assertInDB(remoteUriList[0]),
() => checkClosure(remoteUriList[0]),
callbackUse, bookmarkedUri(remoteUriList));
// Case A 3: Multiple subhost
await removeByFilterTester(randomHostVisits, { host: ".mozilla.org" },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
async () => { for (let uri of checkableArray(remoteUriList)) await checkClosure(uri); },
callbackUse, bookmarkedUri(remoteUriList));
}
// Case A 4: Localhost
await removeByFilterTester(localhostVisits, { host: "localhost" },
async () => { for (let uri of localhostUriList) await assertInDB(uri); },
async () => { for (let uri of localhostUriList) await assertNotInDB(uri); },
callbackUse);
// Case A 5: Local Files
await removeByFilterTester(fileVisits, { host: "." },
async () => { for (let uri of fileUriList) await assertInDB(uri); },
async () => { for (let uri of fileUriList) await assertNotInDB(uri); },
callbackUse);
// Case B: Tests which do not remove anything (inverses)
// Case B 1: Date
await removeByFilterTester(sameHostVisits,
{ beginDate: new Date(2001, 1, 1), endDate: new Date(2002, 1, 1) },
() => assertInDB(remoteUriList[0]),
() => assertInDB(remoteUriList[0]),
callbackUse);
// Case B 2 : Single subhost
await removeByFilterTester(sameHostVisits, { host: "notthere.org" },
() => assertInDB(remoteUriList[0]),
() => assertInDB(remoteUriList[0]),
callbackUse);
// Case B 3 : Multiple subhosts
await removeByFilterTester(randomHostVisits, { host: ".notthere.org" },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
callbackUse);
// Case B 4 : invalid local subhost
await removeByFilterTester(randomHostVisits, { host: ".org" },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
callbackUse);
// Case C: Combination Cases
// Case C 1: single subhost
await removeByFilterTester(sameHostVisits,
{ host: "mozilla.org",
beginDate: new Date(2004, 1, 1),
endDate: new Date(2006, 1, 1) },
() => assertInDB(remoteUriList[0]),
() => assertNotInDB(remoteUriList[0]),
callbackUse);
// Case C 2: multiple subhost
await removeByFilterTester(randomHostVisits,
{ host: ".mozilla.org",
beginDate: new Date(2005, 1, 1),
endDate: new Date(2017, 1, 1) },
async () => { for (let uri of remoteUriList) await assertInDB(uri); },
async () => { for (let uri of remoteUriList) await assertNotInDB(uri); },
callbackUse);
}
});
// Test various error cases
add_task(async function test_error_cases() {
Assert.throws(
() => PlacesUtils.history.removeByFilter(),
/TypeError: Expected a filter/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter("obviously, not a filter"),
/TypeError: Expected a filter/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({}),
/TypeError: Expected a non-empty filter/
);
Assert.throws(
() => PlacesUtils.history.removeVisitsByFilter({beginDate: "now"}),
/TypeError: Expected a Date/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({beginDate: Date.now()}),
/TypeError: Expected a Date/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({beginDate: new Date()}, "obviously, not a callback"),
/TypeError: Invalid function/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({beginDate: new Date(1000), endDate: new Date(0)}),
/TypeError: `beginDate` should be at least as old/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "#"}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "www..org"}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: {}}),
/TypeError: `host` should be a string/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "*.mozilla.org"}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "*"}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "local.host."}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: "(local files)"}),
/TypeError: Expected well formed hostname string for/
);
Assert.throws(
() => PlacesUtils.history.removeByFilter({host: ""}),
/TypeError: Expected a non-empty filter/
);
});
// Helper functions
function getObserverPromise(bookmarkedUri) {
if (!bookmarkedUri) {
return { observer: null, promiseObserved: Promise.resolve() };
}
let observer;
let promiseObserved = new Promise((resolve, reject) => {
observer = {
onBeginUpdateBatch() {},
onEndUpdateBatch() {},
onTitleChanged(aUri) {
reject(new Error("Unexpected call to onTitleChanged"));
},
onClearHistory() {
reject(new Error("Unexpected call to onClearHistory"));
},
onPageChanged(aUri) {
reject(new Error("Unexpected call to onPageChanged"));
},
onFrecencyChanged(aURI) {},
onManyFrecenciesChanged() {},
onDeleteURI(aURI) {
try {
Assert.notEqual(aURI.spec, bookmarkedUri, "Bookmarked URI should not be deleted");
} finally {
resolve();
}
},
onDeleteVisits(aURI, aPartialRemoval) {
try {
Assert.equal(aPartialRemoval, false, "Observing onDeleteVisits deletes all visits");
Assert.equal(aURI.spec, bookmarkedUri, "Bookmarked URI should have all visits removed but not the page itself");
} finally {
resolve();
}
},
};
});
return { observer, promiseObserved };
}