Bug 1809700 - Refactor tests for detect-only pref. r=timhuang,settings-reviewers

Differential Revision: https://phabricator.services.mozilla.com/D166832
This commit is contained in:
Paul Zuehlcke
2023-01-19 17:12:00 +00:00
parent 2071ac660b
commit 19eaf572de
8 changed files with 84 additions and 68 deletions

View File

@@ -15,7 +15,6 @@ const {
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
MODE_UNSET,
} = Ci.nsICookieBannerService;
@@ -37,20 +36,20 @@ const {
function cookieBannerSectionIsVisible({
featureMode,
featureModePBM,
detectOnly,
visibilityPref,
testPBM,
}) {
if (!visibilityPref) {
return false;
}
if (detectOnly) {
return false;
}
return (
(testPBM &&
featureModePBM != MODE_DISABLED &&
featureModePBM != MODE_DETECT_ONLY) ||
(!testPBM &&
featureMode != MODE_DISABLED &&
featureMode != MODE_DETECT_ONLY)
(testPBM && featureModePBM != MODE_DISABLED) ||
(!testPBM && featureMode != MODE_DISABLED)
);
}
@@ -162,21 +161,26 @@ add_task(async function test_section_visibility() {
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
]) {
for (let featureModePBM of [
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
]) {
await testSectionVisibility({
win,
featureMode,
featureModePBM,
testPBM,
visibilityPref: true,
});
for (let detectOnly of [false, true]) {
// Testing detect only mode for normal browsing is sufficient.
if (detectOnly && featureModePBM != MODE_DISABLED) {
continue;
}
await testSectionVisibility({
win,
featureMode,
featureModePBM,
detectOnly,
testPBM,
visibilityPref: true,
});
}
}
}

View File

@@ -7,6 +7,7 @@
const FEATURE_PREF = "cookiebanners.ui.desktop.enabled";
const MODE_PREF = "cookiebanners.service.mode";
const DETECT_ONLY_PREF = "cookiebanners.service.detectOnly";
const GROUPBOX_ID = "cookieBannerHandlingGroup";
const CHECKBOX_ID = "handleCookieBanners";
@@ -71,12 +72,13 @@ add_task(async function test_checkbox_unchecked_disabled_mode() {
await SpecialPowers.popPrefEnv();
});
// Test the checkbox is unchecked in DETECT_ONLY mode.
// Test the checkbox is unchecked in detect-only mode.
add_task(async function test_checkbox_unchecked_detect_only_mode() {
await SpecialPowers.pushPrefEnv({
set: [
[FEATURE_PREF, true],
[MODE_PREF, Ci.nsICookieBannerService.MODE_DETECT_ONLY],
[MODE_PREF, Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT],
[DETECT_ONLY_PREF, true],
],
});
@@ -84,7 +86,7 @@ add_task(async function test_checkbox_unchecked_detect_only_mode() {
{ gBrowser, url: "about:preferences#privacy" },
async function(browser) {
let checkbox = browser.contentDocument.getElementById(CHECKBOX_ID);
ok(!checkbox.checked, "checkbox is not checked in DETECT_ONLY mode");
ok(!checkbox.checked, "checkbox is not checked in detect-only mode");
}
);

View File

@@ -9,13 +9,16 @@ add_setup(clickTestSetup);
* Test that the banner clicking won't click banner if the service is disabled or in detect-only mode.
*/
add_task(async function test_cookie_banner_service_disabled() {
for (let mode of [
Ci.nsICookieBannerService.MODE_DISABLED,
Ci.nsICookieBannerService.MODE_DETECT_ONLY,
for (let [serviceMode, detectOnly] of [
[Ci.nsICookieBannerService.MODE_DISABLED, false],
[Ci.nsICookieBannerService.MODE_DISABLED, true],
[Ci.nsICookieBannerService.MODE_REJECT, true],
[Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT, true],
]) {
await SpecialPowers.pushPrefEnv({
set: [
["cookiebanners.service.mode", mode],
["cookiebanners.service.mode", serviceMode],
["cookiebanners.service.detectOnly", detectOnly],
[
"cookiebanners.service.mode.privateBrowsing",
Ci.nsICookieBannerService.MODE_DISABLED,
@@ -30,6 +33,8 @@ add_task(async function test_cookie_banner_service_disabled() {
visible: true,
expected: "NoClick",
});
await SpecialPowers.popPrefEnv();
}
});

View File

@@ -9,9 +9,11 @@ add_setup(clickTestSetup);
* Triggers cookie banner clicking and tests the events dispatched.
* @param {*} options - Test options.
* @param {nsICookieBannerService::Modes} options.mode - The cookie banner service mode to test with.
* @param {boolean} options.detectOnly - Whether the service should be enabled
* in detection only mode, where it does not handle banners.
* @param {*} options.openPageOptions - Options to overwrite for the openPageAndVerify call.
*/
async function runTest({ mode, openPageOptions = {} }) {
async function runTest({ mode, detectOnly = false, openPageOptions = {} }) {
let initFn = () => {
// Insert rules only if the feature is enabled.
if (Services.cookieBanners.isEnabled) {
@@ -19,7 +21,8 @@ async function runTest({ mode, openPageOptions = {} }) {
}
};
let shouldHandleBanner = mode == Ci.nsICookieBannerService.MODE_REJECT;
let shouldHandleBanner =
mode == Ci.nsICookieBannerService.MODE_REJECT && !detectOnly;
let testURL = openPageOptions.testURL || TEST_PAGE_A;
let triggerFn = async () => {
await openPageAndVerify({
@@ -33,7 +36,7 @@ async function runTest({ mode, openPageOptions = {} }) {
});
};
await runEventTest({ mode, initFn, triggerFn, testURL });
await runEventTest({ mode, detectOnly, initFn, triggerFn, testURL });
// Clean up the test tab opened by openPageAndVerify.
BrowserTestUtils.removeTab(gBrowser.selectedTab);
@@ -47,19 +50,27 @@ add_task(async function test_events_mode_reject() {
});
/**
* Test the banner clicking events with MODE_DETECT_ONLY.
* Test the banner clicking events with detect-only mode.
*/
add_task(async function test_events_mode_detect_only() {
await runTest({ mode: Ci.nsICookieBannerService.MODE_DETECT_ONLY });
await runTest({
mode: Ci.nsICookieBannerService.MODE_REJECT,
detectOnly: true,
});
await runTest({
mode: Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
detectOnly: true,
});
});
/**
* Test the banner clicking events with MODE_DETECT_ONLY with a click rule that
* only supports opt-in..
* Test the banner clicking events with detect-only mode with a click rule that
* only supports opt-in.
*/
add_task(async function test_events_mode_detect_only_opt_in_rule() {
await runTest({
mode: Ci.nsICookieBannerService.MODE_DETECT_ONLY,
mode: Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
detectOnly: true,
openPageOptions: {
// We only have an opt-in rule for DOMAIN_B. This ensures we still fire
// detection events for that case.
@@ -72,7 +83,7 @@ add_task(async function test_events_mode_detect_only_opt_in_rule() {
});
/**
* Test the banner clicking events with MODE_DETECT_ONLY.
* Test the banner clicking events with detect-only mode.
*/
add_task(async function test_events_mode_disabled() {
await runTest({ mode: Ci.nsICookieBannerService.MODE_DISABLED });

View File

@@ -11,7 +11,6 @@ const {
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
MODE_UNSET,
} = Ci.nsICookieBannerService;
@@ -19,7 +18,6 @@ const TEST_MODES = [
MODE_DISABLED,
MODE_REJECT,
MODE_REJECT_OR_ACCEPT,
MODE_DETECT_ONLY,
MODE_UNSET, // Should be recorded as invalid.
99, // Invalid
-1, // Invalid
@@ -33,8 +31,6 @@ function convertModeToTelemetryString(mode) {
return "reject";
case MODE_REJECT_OR_ACCEPT:
return "reject_or_accept";
case MODE_DETECT_ONLY:
return "detect_only";
}
return "invalid";
@@ -168,13 +164,7 @@ add_task(async function test_service_mode_telemetry() {
service.observe(null, "idle-daily", null);
// Verify the telemetry value.
for (let label of [
"disabled",
"reject",
"reject_or_accept",
"detect_only",
"invalid",
]) {
for (let label of ["disabled", "reject", "reject_or_accept", "invalid"]) {
let expected = convertModeToTelemetryString(mode) == label;
let expectedPBM = convertModeToTelemetryString(modePBM) == label;

View File

@@ -162,22 +162,6 @@ add_task(async function test_enabled_pref() {
Array.isArray(rules),
"Rules getter should not throw but return an array."
);
info("Enabling cookie banner service. MODE_DETECT_ONLY");
await SpecialPowers.pushPrefEnv({
set: [
[
"cookiebanners.service.mode",
Ci.nsICookieBannerService.MODE_DETECT_ONLY,
],
],
});
rules = Services.cookieBanners.rules;
ok(
Array.isArray(rules),
"Rules getter should not throw but return an array."
);
});
/**
@@ -189,7 +173,6 @@ add_task(async function test_enabled_pref_pbm_combinations() {
Ci.nsICookieBannerService.MODE_DISABLED,
Ci.nsICookieBannerService.MODE_REJECT,
Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT,
Ci.nsICookieBannerService.MODE_DETECT_ONLY,
];
// Test all pref combinations

View File

@@ -82,14 +82,28 @@ add_task(async function test_cookie_injector_disabled() {
* by pref, but the cookie banner service is disabled or in detect-only mode.
*/
add_task(async function test_cookie_banner_service_disabled() {
for (let mode of [
Ci.nsICookieBannerService.MODE_DISABLED,
Ci.nsICookieBannerService.MODE_DETECT_ONLY,
// Enable in PBM so the service is always initialized.
await SpecialPowers.pushPrefEnv({
set: [
[
"cookiebanners.service.mode.privateBrowsing",
Ci.nsICookieBannerService.MODE_REJECT,
],
],
});
for (let [serviceMode, detectOnly] of [
[Ci.nsICookieBannerService.MODE_DISABLED, false],
[Ci.nsICookieBannerService.MODE_DISABLED, true],
[Ci.nsICookieBannerService.MODE_REJECT, true],
[Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT, true],
]) {
info(`Testing with serviceMode=${serviceMode}; detectOnly=${detectOnly}`);
await SpecialPowers.pushPrefEnv({
set: [
["cookiebanners.service.mode", mode],
["cookiebanners.service.mode", serviceMode],
["cookiebanners.cookieInjector.enabled", true],
["cookiebanners.service.detectOnly", detectOnly],
],
});
@@ -97,6 +111,7 @@ add_task(async function test_cookie_banner_service_disabled() {
assertNoCookies();
await SiteDataTestUtils.clear();
await SpecialPowers.popPrefEnv();
}
});

View File

@@ -434,6 +434,8 @@ async function testClickResultTelemetry(expected, resetFOG = true) {
* @param {*} options - Test options.
* @param {nsICookieBannerService::Modes} options.mode - The cookie banner
* service mode to test with.
* @param {boolean} options.detectOnly - Whether the service should be enabled
* in detection only mode, where it does not handle banners.
* @param {function} options.initFn - Function to call for test initialization.
* @param {function} options.triggerFn - Function to call to trigger the banner
* handling feature.
@@ -442,17 +444,21 @@ async function testClickResultTelemetry(expected, resetFOG = true) {
* @returns {Promise} Resolves when the test completes, after cookie banner
* events.
*/
async function runEventTest({ mode, initFn, triggerFn, testURL }) {
async function runEventTest({ mode, detectOnly, initFn, triggerFn, testURL }) {
await SpecialPowers.pushPrefEnv({
set: [["cookiebanners.service.mode", mode]],
set: [
["cookiebanners.service.mode", mode],
["cookiebanners.service.detectOnly", detectOnly],
],
});
await initFn();
let expectEventDetected = mode != Ci.nsICookieBannerService.MODE_DISABLED;
let expectEventHandled =
mode == Ci.nsICookieBannerService.MODE_REJECT ||
mode == Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT;
!detectOnly &&
(mode == Ci.nsICookieBannerService.MODE_REJECT ||
mode == Ci.nsICookieBannerService.MODE_REJECT_OR_ACCEPT);
let eventObservedDetected = false;
let eventObservedHandled = false;