Skip to content

Commit

Permalink
Bug 1501992 - Add tests for the cookies subpanel in the control cente…
Browse files Browse the repository at this point in the history
…r. r=Ehsan

Differential Revision: https://phabricator.services.mozilla.com/D13146
  • Loading branch information
Johann Hofmann committed Dec 1, 2018
1 parent a2f9b23 commit 4a0b798
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 132 deletions.
8 changes: 7 additions & 1 deletion browser/base/content/test/trackingUI/benignPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<meta charset="utf8">
</head>
<body>
<iframe src="http://not-tracking.example.com/"></iframe>
<!--TODO: We used to have an iframe here, to double-check that benign-->
<!--iframes may be included in pages. However, the cookie restrictions-->
<!--project introduced a change that declared blockable content to be-->
<!--found on any page that embeds iframes, rendering this unusable for-->
<!--our purposes. That's not ideal and we intend to restore this iframe.-->
<!--(See bug 1511303 for a more detailed technical explanation.)-->
<!--<iframe src="http://not-tracking.example.com/"></iframe>-->
</body>
</html>
1 change: 1 addition & 0 deletions browser/base/content/test/trackingUI/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ support-files =
[browser_trackingUI_animation_2.js]
[browser_trackingUI_appMenu.js]
[browser_trackingUI_categories.js]
[browser_trackingUI_cookies_subview.js]
[browser_trackingUI_fetch.js]
support-files =
file_trackingUI_fetch.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ registerCleanupFunction(function() {

add_task(async function testCategoryLabelsInControlPanel() {
await BrowserTestUtils.withNewTab("http://www.example.com", async function() {
let promisePanelOpen = BrowserTestUtils.waitForEvent(gIdentityHandler._identityPopup, "popupshown");
gIdentityHandler._identityBox.click();
await promisePanelOpen;
await openIdentityPopup();

let preferencesButton = document.getElementById("tracking-protection-preferences-button");
ok(preferencesButton.label, "The preferencesButton label exists");
Expand Down Expand Up @@ -70,3 +68,55 @@ add_task(async function testCategoryLabelsInAppMenu() {
"The appMenuCategory label has been changed to custom");
});
});

add_task(async function testSubcategoryLabels() {
await BrowserTestUtils.withNewTab("http://www.example.com", async function() {
let categoryLabel =
document.getElementById("identity-popup-content-blocking-tracking-protection-state-label");

Services.prefs.setBoolPref(TP_PREF, true);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.trackers.blocked.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.trackers.blocked.label"));

Services.prefs.setBoolPref(TP_PREF, false);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.trackers.allowed.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.trackers.allowed.label"));

categoryLabel =
document.getElementById("identity-popup-content-blocking-cookies-state-label");

Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.cookies.allowed.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.cookies.allowed.label"));

Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.cookies.allBlocked.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.cookies.allBlocked.label"));

Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.cookies.3rdPartyBlocked.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.cookies.3rdPartyBlocked.label"));

Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.cookies.trackersBlocked.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.cookies.trackersBlocked.label"));

Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_LIMIT_FOREIGN);
await TestUtils.waitForCondition(() => categoryLabel.textContent ==
gNavigatorBundle.getString("contentBlocking.cookies.unvisitedBlocked.label"),
"The category label has updated correctly");
is(categoryLabel.textContent, gNavigatorBundle.getString("contentBlocking.cookies.unvisitedBlocked.label"));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable mozilla/no-arbitrary-setTimeout */

"use strict";

const COOKIE_PAGE = "http://not-tracking.example.com/browser/browser/base/content/test/trackingUI/cookiePage.html";

const TPC_PREF = "network.cookie.cookieBehavior";

add_task(async function setup() {
// Avoid the content blocking tour interfering with our tests by popping up.
await SpecialPowers.pushPrefEnv({set: [[ContentBlocking.prefIntroCount, ContentBlocking.MAX_INTROS]]});
await UrlClassifierTestUtils.addTestTrackers();

registerCleanupFunction(() => {
UrlClassifierTestUtils.cleanupTestTrackers();
});
});

async function assertSitesListed(trackersBlocked, thirdPartyBlocked, firstPartyBlocked) {
await BrowserTestUtils.withNewTab(COOKIE_PAGE, async function(browser) {
await openIdentityPopup();

let categoryItem =
document.getElementById("identity-popup-content-blocking-category-cookies");
ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
let cookiesView = document.getElementById("identity-popup-cookiesView");
let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
categoryItem.click();
await viewShown;

ok(true, "Cookies view was shown");

let listHeaders = cookiesView.querySelectorAll(".identity-popup-cookiesView-list-header");
is(listHeaders.length, 1, "We have 1 list header");
is(listHeaders[0].textContent,
gNavigatorBundle.getString(`contentBlocking.cookiesView.trackers.label`),
"The list header is for tracking cookies.");

let listItems = cookiesView.querySelectorAll(".identity-popup-content-blocking-list-item");
is(listItems.length, 1, "We have 1 cookie in the list");

let listItem = listItems[0];
let label = listItem.querySelector(".identity-popup-content-blocking-list-host-label");
is(label.value, "http://trackertest.org", "Has an item for trackertest.org");
ok(BrowserTestUtils.is_visible(listItem), "List item is visible");
is(listItem.classList.contains("allowed"), !trackersBlocked,
"Indicates whether the cookie was blocked or allowed");

let mainView = document.getElementById("identity-popup-mainView");
viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
let backButton = cookiesView.querySelector(".subviewbutton-back");
backButton.click();
await viewShown;

ok(true, "Main view was shown");

let change = waitForSecurityChange();
let timeoutPromise = new Promise(resolve => setTimeout(resolve, 1000));

await ContentTask.spawn(browser, {}, function() {
content.postMessage("third-party-cookie", "*");
});

let result = await Promise.race([change, timeoutPromise]);
is(result, undefined, "No securityChange events should be received");

viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
categoryItem.click();
await viewShown;

ok(true, "Cookies view was shown");

listHeaders = cookiesView.querySelectorAll(".identity-popup-cookiesView-list-header");
is(listHeaders.length, 2, "We now have 2 list headers");
is(listHeaders[1].textContent,
gNavigatorBundle.getString(`contentBlocking.cookiesView.thirdParty.label`),
"The new list header is for third party cookies.");

listItems = cookiesView.querySelectorAll(".identity-popup-content-blocking-list-item");
is(listItems.length, 2, "We have 2 cookies in the list");

listItem = listItems[1];
label = listItem.querySelector(".identity-popup-content-blocking-list-host-label");
is(label.value, "https://test1.example.org", "Has an item for test1.example.org");
ok(BrowserTestUtils.is_visible(listItem), "List item is visible");
is(listItem.classList.contains("allowed"), !thirdPartyBlocked,
"Indicates whether the cookie was blocked or allowed");

viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
backButton.click();
await viewShown;

ok(true, "Main view was shown");

change = waitForSecurityChange();
timeoutPromise = new Promise(resolve => setTimeout(resolve, 1000));

await ContentTask.spawn(browser, {}, function() {
content.postMessage("first-party-cookie", "*");
});

result = await Promise.race([change, timeoutPromise]);
is(result, undefined, "No securityChange events should be received");

viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
categoryItem.click();
await viewShown;

ok(true, "Cookies view was shown");

listHeaders = cookiesView.querySelectorAll(".identity-popup-cookiesView-list-header");
is(listHeaders.length, 3, "We now have 3 list headers");
is(listHeaders[0].textContent,
gNavigatorBundle.getString(`contentBlocking.cookiesView.firstParty.label`),
"The new list header is for first party cookies.");

listItems = cookiesView.querySelectorAll(".identity-popup-content-blocking-list-item");
is(listItems.length, 3, "We have 2 cookies in the list");

listItem = listItems[0];
label = listItem.querySelector(".identity-popup-content-blocking-list-host-label");
is(label.value, "http://not-tracking.example.com", "Has an item for the first party");
ok(BrowserTestUtils.is_visible(listItem), "List item is visible");
is(listItem.classList.contains("allowed"), !firstPartyBlocked,
"Indicates whether the cookie was blocked or allowed");
});
}

add_task(async function testCookiesSubView() {
info("Testing cookies subview with reject tracking cookies.");
Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER);
await assertSitesListed(true, false, false);
info("Testing cookies subview with reject third party cookies.");
Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN);
await assertSitesListed(true, true, false);
info("Testing cookies subview with reject all cookies.");
Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT);
await assertSitesListed(true, true, true);
info("Testing cookies subview with accept all cookies.");
Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT);
await assertSitesListed(false, false, false);

Services.prefs.clearUserPref(TPC_PREF);
});

add_task(async function testCookiesSubViewAllowed() {
Services.prefs.setIntPref(TPC_PREF, Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER);
let principal = Services.scriptSecurityManager.createCodebasePrincipalFromOrigin("http://trackertest.org/");
Services.perms.addFromPrincipal(principal, "cookie", Services.perms.ALLOW_ACTION);

await BrowserTestUtils.withNewTab(COOKIE_PAGE, async function(browser) {
await openIdentityPopup();

let categoryItem =
document.getElementById("identity-popup-content-blocking-category-cookies");
ok(BrowserTestUtils.is_visible(categoryItem), "TP category item is visible");
let cookiesView = document.getElementById("identity-popup-cookiesView");
let viewShown = BrowserTestUtils.waitForEvent(cookiesView, "ViewShown");
categoryItem.click();
await viewShown;

ok(true, "Cookies view was shown");

let listHeaders = cookiesView.querySelectorAll(".identity-popup-cookiesView-list-header");
is(listHeaders.length, 1, "We have 1 list header");
is(listHeaders[0].textContent,
gNavigatorBundle.getString(`contentBlocking.cookiesView.trackers.label`),
"The list header is for tracking cookies.");

let listItems = cookiesView.querySelectorAll(".identity-popup-content-blocking-list-item");
is(listItems.length, 1, "We have 1 cookie in the list");

let listItem = listItems[0];
let label = listItem.querySelector(".identity-popup-content-blocking-list-host-label");
is(label.value, "http://trackertest.org", "Has an item for trackertest.org");
ok(BrowserTestUtils.is_visible(listItem), "List item is visible");
ok(listItem.classList.contains("allowed"), "Indicates whether the cookie was blocked or allowed");

let button = listItem.querySelector(".identity-popup-permission-remove-button");
ok(BrowserTestUtils.is_visible(button), "Permission remove button is visible");
button.click();
is(Services.perms.testExactPermissionFromPrincipal(principal, "cookie"), Services.perms.UNKNOWN_ACTION, "Button click should remove cookie pref.");
ok(!listItem.classList.contains("allowed"), "Has removed the allowed class");
});

Services.prefs.clearUserPref(TPC_PREF);
});
17 changes: 0 additions & 17 deletions browser/base/content/test/trackingUI/browser_trackingUI_fetch.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
const URL = "http://mochi.test:8888/browser/browser/base/content/test/trackingUI/file_trackingUI_fetch.html";

function waitForSecurityChange(numChanges = 1) {
return new Promise(resolve => {
let n = 0;
let listener = {
onSecurityChange() {
n = n + 1;
info("Received onSecurityChange event " + n + " of " + numChanges);
if (n >= numChanges) {
gBrowser.removeProgressListener(listener);
resolve();
}
},
};
gBrowser.addProgressListener(listener);
});
}

add_task(async function test_fetch() {
await SpecialPowers.pushPrefEnv({ set: [
["privacy.trackingprotection.enabled", true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ add_task(async function testOpenPreferencesFromAddBlockingButtons() {
SpecialPowers.pushPrefEnv({set: [
[TP_PREF, false],
[TPC_PREF, Ci.nsICookieService.BEHAVIOR_ACCEPT],
[TP_UI_PREF, true],
[RT_UI_PREF, true],
]});

await BrowserTestUtils.withNewTab(TRACKING_PAGE, async function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ function testTrackingPage(window) {

ok(hidden("#identity-popup-content-blocking-not-detected"), "blocking not detected label is hidden");
ok(!hidden("#identity-popup-content-blocking-detected"), "blocking detected label is visible");

ok(!hidden("#identity-popup-content-blocking-category-list"), "category list is visible");
ok(hidden("#identity-popup-content-blocking-category-tracking-protection > #identity-popup-content-blocking-tracking-protection-label-allowed"),
"TP category item is not showing the allowed label");
ok(!hidden("#identity-popup-content-blocking-category-tracking-protection > #identity-popup-content-blocking-tracking-protection-label-blocked"),
"TP category item is set to blocked");
}

function testTrackingPageUnblocked() {
Expand All @@ -81,12 +75,6 @@ function testTrackingPageUnblocked() {

ok(hidden("#identity-popup-content-blocking-not-detected"), "blocking not detected label is hidden");
ok(!hidden("#identity-popup-content-blocking-detected"), "blocking detected label is visible");

ok(!hidden("#identity-popup-content-blocking-category-list"), "category list is visible");
ok(!hidden("#identity-popup-content-blocking-category-tracking-protection > #identity-popup-content-blocking-tracking-protection-label-allowed"),
"TP category item is showing the allowed label");
ok(hidden("#identity-popup-content-blocking-category-tracking-protection > #identity-popup-content-blocking-tracking-protection-label-blocked"),
"TP category item is not set to blocked");
}

add_task(async function testExceptionAddition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ add_task(async function setup() {
});
});

function openIdentityPopup() {
let mainView = document.getElementById("identity-popup-mainView");
let viewShown = BrowserTestUtils.waitForEvent(mainView, "ViewShown");
gIdentityHandler._identityBox.click();
return viewShown;
}

add_task(async function testReportBreakageVisibility() {
let scenarios = [
{
Expand Down
Loading

0 comments on commit 4a0b798

Please sign in to comment.