fune/toolkit/components/antitracking/test/browser/browser_script.js
Ehsan Akhgari e3601d2177 Bug 1486092 - Part 3: Add tests to ensure that the reject tracker cookie behavior also depends on the browser.contentblocking.enabled pref; r=baku
The antitracking test mini-framework is extended to run the non-blocking
callback of the tests three times, each time with a unique combination
of the network.cookie.cookieBehavior and browser.contentblocking.enabled
prefs that would cause the reject tracker cookie behavior feature to get
disabled.  This works seamlessly for all test cases that do not make any
assumptions around how many times their callbacks are executed.

Note that browser_imageCache.js depends on the number of times its
non-blocking callback gets executed.  As a result of this, this test
is split into three tests to test the three possible combinations of
the pref for the non-blocking callback.
2018-08-27 09:22:01 -04:00

132 lines
3.7 KiB
JavaScript

ChromeUtils.import("resource://gre/modules/Services.jsm");
add_task(async function() {
info("Starting subResources test");
await SpecialPowers.flushPrefEnv();
await SpecialPowers.pushPrefEnv({"set": [
["browser.contentblocking.enabled", true],
["network.cookie.cookieBehavior", Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER],
["privacy.trackingprotection.enabled", false],
["privacy.trackingprotection.pbmode.enabled", false],
["privacy.trackingprotection.annotate_channels", true],
]});
await UrlClassifierTestUtils.addTestTrackers();
info("Creating a new tab");
let tab = BrowserTestUtils.addTab(gBrowser, TEST_TOP_PAGE);
gBrowser.selectedTab = tab;
let browser = gBrowser.getBrowserForTab(tab);
await BrowserTestUtils.browserLoaded(browser);
info("Loading tracking scripts");
await ContentTask.spawn(browser, { scriptURL: TEST_DOMAIN + TEST_PATH + "tracker.js",
page: TEST_3RD_PARTY_PAGE,
}, async obj => {
info("Checking if permission is denied");
let callbackBlocked = async _ => {
try {
localStorage.foo = 42;
ok(false, "LocalStorage cannot be used!");
} catch (e) {
ok(true, "LocalStorage cannot be used!");
is(e.name, "SecurityError", "We want a security error message.");
}
};
await new content.Promise(resolve => {
let ifr = content.document.createElement("iframe");
ifr.onload = function() {
info("Sending code to the 3rd party content");
ifr.contentWindow.postMessage(callbackBlocked.toString(), "*");
};
content.addEventListener("message", function msg(event) {
if (event.data.type == "finish") {
content.removeEventListener("message", msg);
resolve();
return;
}
if (event.data.type == "ok") {
ok(event.data.what, event.data.msg);
return;
}
if (event.data.type == "info") {
info(event.data.msg);
return;
}
ok(false, "Unknown message");
});
content.document.body.appendChild(ifr);
ifr.src = obj.page;
});
info("Triggering a 3rd party script...");
let p = new content.Promise(resolve => {
let bc = new content.BroadcastChannel("a");
bc.onmessage = resolve;
});
let src = content.document.createElement("script");
content.document.body.appendChild(src);
src.src = obj.scriptURL;
await p;
info("Checking if permission is granted");
let callbackAllowed = async _ => {
localStorage.foo = 42;
ok(true, "LocalStorage can be used!");
};
await new content.Promise(resolve => {
let ifr = content.document.createElement("iframe");
ifr.onload = function() {
info("Sending code to the 3rd party content");
ifr.contentWindow.postMessage(callbackAllowed.toString(), "*");
};
content.addEventListener("message", function msg(event) {
if (event.data.type == "finish") {
content.removeEventListener("message", msg);
resolve();
return;
}
if (event.data.type == "ok") {
ok(event.data.what, event.data.msg);
return;
}
if (event.data.type == "info") {
info(event.data.msg);
return;
}
ok(false, "Unknown message");
});
content.document.body.appendChild(ifr);
ifr.src = obj.page;
});
});
info("Removing the tab");
BrowserTestUtils.removeTab(tab);
UrlClassifierTestUtils.cleanupTestTrackers();
});
add_task(async function() {
info("Cleaning up.");
await new Promise(resolve => {
Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
});
});