Source code

Revision control

Other Tools

1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
<title>BrowsingData Cookies 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
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
16
17
const COOKIE = {
18
host: "example.com",
19
name: "test_cookie",
20
path: "/",
21
};
22
let since, oldCookie;
23
24
function addCookie(cookie) {
25
let expiry = Date.now() / 1000 + 10000;
26
Services.cookies.add(cookie.host, cookie.path, cookie.name, "test", false, false, false, expiry, {}, Ci.nsICookie.SAMESITE_NONE);
27
ok(Services.cookies.cookieExists(cookie.host, cookie.path, cookie.name, {}), `Cookie ${cookie.name} was created.`);
28
}
29
30
async function setUpCookies() {
31
// Add a cookie which will end up with an older creationTime.
32
oldCookie = Object.assign({}, COOKIE, {name: Date.now()});
33
addCookie(oldCookie);
34
await new Promise(resolve => setTimeout(resolve, 20));
35
since = Date.now();
36
await new Promise(resolve => setTimeout(resolve, 10));
37
38
// Add a cookie which will end up with a more recent creationTime.
39
addCookie(COOKIE);
40
}
41
42
add_task(async function testCookies() {
43
function background() {
44
browser.test.onMessage.addListener(async (msg, options) => {
45
if (msg == "removeCookies") {
46
await browser.browsingData.removeCookies(options);
47
} else {
48
await browser.browsingData.remove(options, {cookies: true});
49
}
50
browser.test.sendMessage("cookiesRemoved");
51
});
52
}
53
54
let extension = ExtensionTestUtils.loadExtension({
55
background,
56
manifest: {
57
permissions: ["browsingData"],
58
},
59
});
60
61
async function testRemovalMethod(method) {
62
// Clear cookies with a recent since value.
63
await setUpCookies();
64
extension.sendMessage(method, {since});
65
await extension.awaitMessage("cookiesRemoved");
66
67
ok(Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was not removed.");
68
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
69
70
// Clear cookies with an old since value.
71
await setUpCookies();
72
addCookie(COOKIE);
73
extension.sendMessage(method, {since: since - 100000});
74
await extension.awaitMessage("cookiesRemoved");
75
76
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), "Old cookie was removed.");
77
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), "Recent cookie was removed.");
78
79
// Clear cookies with no since value and valid originTypes.
80
await setUpCookies();
81
extension.sendMessage(
82
method,
83
{originTypes: {unprotectedWeb: true, protectedWeb: false}});
84
await extension.awaitMessage("cookiesRemoved");
85
86
ok(!Services.cookies.cookieExists(COOKIE.host, COOKIE.path, COOKIE.name, {}), `Cookie ${COOKIE.name} was removed.`);
87
ok(!Services.cookies.cookieExists(oldCookie.host, oldCookie.path, oldCookie.name, {}), `Cookie ${oldCookie.name} was removed.`);
88
}
89
90
await extension.startup();
91
92
await testRemovalMethod("removeCookies");
93
await testRemovalMethod("remove");
94
95
await extension.unload();
96
});
97
98
add_task(async function testCache() {
99
function background() {
100
browser.test.onMessage.addListener(async msg => {
101
if (msg == "removeCache") {
102
await browser.browsingData.removeCache({});
103
} else {
104
await browser.browsingData.remove({}, {cache: true});
105
}
106
browser.test.sendMessage("cacheRemoved");
107
});
108
}
109
110
let extension = ExtensionTestUtils.loadExtension({
111
background,
112
manifest: {
113
permissions: ["browsingData"],
114
},
115
});
116
117
// Returns a promise when 'cacheservice:empty-cache' event is fired
118
function topicObserved() {
119
return new Promise(resolve => {
120
let observe = (subject, topic, data) => {
121
Services.obs.removeObserver(observe, "cacheservice:empty-cache");
122
resolve(data);
123
};
124
Services.obs.addObserver(observe, "cacheservice:empty-cache");
125
});
126
}
127
128
async function testRemovalMethod(method) {
129
// We can assume the notification works properly, so we only need to observe
130
// the notification to know the cache was cleared.
131
let awaitNotification = topicObserved();
132
extension.sendMessage(method);
133
await awaitNotification;
134
await extension.awaitMessage("cacheRemoved");
135
}
136
137
await extension.startup();
138
139
await testRemovalMethod("removeCache");
140
await testRemovalMethod("remove");
141
142
await extension.unload();
143
});
144
</script>
145
146
</body>
147
</html>