Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>BrowsingData Settings 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 {Downloads} = ChromeUtils.import("resource://gre/modules/Downloads.jsm");
16
17
const OLD_NAMES = {[Downloads.PUBLIC]: "old-public", [Downloads.PRIVATE]: "old-private"};
18
const RECENT_NAMES = {[Downloads.PUBLIC]: "recent-public", [Downloads.PRIVATE]: "recent-private"};
19
const REFERENCE_DATE = new Date();
20
const OLD_DATE = new Date(Number(REFERENCE_DATE) - 10000);
21
22
async function downloadExists(list, path) {
23
let listArray = await list.getAll();
24
return listArray.some(i => i.target.path == path);
25
}
26
27
async function checkDownloads(expectOldExists = true, expectRecentExists = true) {
28
for (let listType of [Downloads.PUBLIC, Downloads.PRIVATE]) {
29
let downloadsList = await Downloads.getList(listType);
30
is(
31
(await downloadExists(downloadsList, OLD_NAMES[listType])),
32
expectOldExists,
33
`Fake old download ${(expectOldExists) ? "was found" : "was removed"}.`);
34
is(
35
(await downloadExists(downloadsList, RECENT_NAMES[listType])),
36
expectRecentExists,
37
`Fake recent download ${(expectRecentExists) ? "was found" : "was removed"}.`);
38
}
39
}
40
41
async function setupDownloads() {
42
let downloadsList = await Downloads.getList(Downloads.ALL);
43
await downloadsList.removeFinished();
44
45
for (let listType of [Downloads.PUBLIC, Downloads.PRIVATE]) {
46
downloadsList = await Downloads.getList(listType);
47
let download = await Downloads.createDownload({
48
source: {
50
isPrivate: listType == Downloads.PRIVATE},
51
target: OLD_NAMES[listType],
52
});
53
download.startTime = OLD_DATE;
54
download.canceled = true;
55
await downloadsList.add(download);
56
57
download = await Downloads.createDownload({
58
source: {
60
isPrivate: listType == Downloads.PRIVATE},
61
target: RECENT_NAMES[listType],
62
});
63
download.startTime = REFERENCE_DATE;
64
download.canceled = true;
65
await downloadsList.add(download);
66
}
67
68
// Confirm everything worked.
69
downloadsList = await Downloads.getList(Downloads.ALL);
70
is((await downloadsList.getAll()).length, 4, "4 fake downloads added.");
71
checkDownloads();
72
}
73
74
add_task(async function testDownloads() {
75
function background() {
76
browser.test.onMessage.addListener(async (msg, options) => {
77
if (msg == "removeDownloads") {
78
await browser.browsingData.removeDownloads(options);
79
} else {
80
await browser.browsingData.remove(options, {downloads: true});
81
}
82
browser.test.sendMessage("downloadsRemoved");
83
});
84
}
85
86
let extension = ExtensionTestUtils.loadExtension({
87
background,
88
manifest: {
89
permissions: ["browsingData"],
90
},
91
});
92
93
async function testRemovalMethod(method) {
94
// Clear downloads with no since value.
95
await setupDownloads();
96
extension.sendMessage(method, {});
97
await extension.awaitMessage("downloadsRemoved");
98
await checkDownloads(false, false);
99
100
// Clear downloads with recent since value.
101
await setupDownloads();
102
extension.sendMessage(method, {since: REFERENCE_DATE});
103
await extension.awaitMessage("downloadsRemoved");
104
await checkDownloads(true, false);
105
106
// Clear downloads with old since value.
107
await setupDownloads();
108
extension.sendMessage(method, {since: REFERENCE_DATE - 100000});
109
await extension.awaitMessage("downloadsRemoved");
110
await checkDownloads(false, false);
111
}
112
113
await extension.startup();
114
115
await testRemovalMethod("removeDownloads");
116
await testRemovalMethod("remove");
117
118
await extension.unload();
119
});
120
121
</script>
122
</body>
123
</html>