diff options
Diffstat (limited to 'browser/extensions/webcompat/tests')
11 files changed, 366 insertions, 0 deletions
diff --git a/browser/extensions/webcompat/tests/browser/browser.ini b/browser/extensions/webcompat/tests/browser/browser.ini new file mode 100644 index 0000000000..f15b901240 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/browser.ini @@ -0,0 +1,15 @@ +[DEFAULT] +support-files = + head.js + shims_test.js + shims_test_2.js + shims_test_3.js + iframe_test.html + shims_test.html + shims_test_2.html + shims_test_3.html + +[browser_aboutcompat.js] +[browser_shims.js] +https_first_disabled = true +skip-if = verify diff --git a/browser/extensions/webcompat/tests/browser/browser_aboutcompat.js b/browser/extensions/webcompat/tests/browser/browser_aboutcompat.js new file mode 100644 index 0000000000..a448269294 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/browser_aboutcompat.js @@ -0,0 +1,27 @@ +"use strict"; + +add_task(async function test_about_compat_loads_properly() { + const tab = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + opening: "about:compat", + waitForLoad: true, + }); + + await SpecialPowers.spawn(tab.linkedBrowser, [], async function () { + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector("#overrides tr[data-id]"), + "UA overrides are listed" + ); + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector("#interventions tr[data-id]"), + "interventions are listed" + ); + await ContentTaskUtils.waitForCondition( + () => content.document.querySelector("#smartblock tr[data-id]"), + "SmartBlock shims are listed" + ); + ok(true, "Interventions are listed"); + }); + + await BrowserTestUtils.removeTab(tab); +}); diff --git a/browser/extensions/webcompat/tests/browser/browser_shims.js b/browser/extensions/webcompat/tests/browser/browser_shims.js new file mode 100644 index 0000000000..4de900a4c6 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/browser_shims.js @@ -0,0 +1,73 @@ +"use strict"; + +registerCleanupFunction(() => { + UrlClassifierTestUtils.cleanupTestTrackers(); + Services.prefs.clearUserPref(TRACKING_PREF); +}); + +add_setup(async function () { + await UrlClassifierTestUtils.addTestTrackers(); +}); + +add_task(async function test_shim_disabled_by_own_pref() { + // Test that a shim will not apply if disabled in about:config + + Services.prefs.setBoolPref(DISABLE_SHIM1_PREF, true); + Services.prefs.setBoolPref(TRACKING_PREF, true); + + await testShimDoesNotRun(); + + Services.prefs.clearUserPref(DISABLE_SHIM1_PREF); + Services.prefs.clearUserPref(TRACKING_PREF); +}); + +add_task(async function test_shim_disabled_by_global_pref() { + // Test that a shim will not apply if disabled in about:config + + Services.prefs.setBoolPref(GLOBAL_PREF, false); + Services.prefs.setBoolPref(DISABLE_SHIM1_PREF, false); + Services.prefs.setBoolPref(TRACKING_PREF, true); + + await testShimDoesNotRun(); + + Services.prefs.clearUserPref(GLOBAL_PREF); + Services.prefs.clearUserPref(DISABLE_SHIM1_PREF); + Services.prefs.clearUserPref(TRACKING_PREF); +}); + +add_task(async function test_shim_disabled_hosts_notHosts() { + Services.prefs.setBoolPref(TRACKING_PREF, true); + + await testShimDoesNotRun(false, SHIMMABLE_TEST_PAGE_3); + + Services.prefs.clearUserPref(TRACKING_PREF); +}); + +add_task(async function test_shim_disabled_overridden_by_pref() { + Services.prefs.setBoolPref(TRACKING_PREF, true); + + await testShimDoesNotRun(false, SHIMMABLE_TEST_PAGE_2); + + Services.prefs.setBoolPref(DISABLE_SHIM2_PREF, false); + + await testShimRuns(SHIMMABLE_TEST_PAGE_2); + + Services.prefs.clearUserPref(TRACKING_PREF); + Services.prefs.clearUserPref(DISABLE_SHIM2_PREF); +}); + +add_task(async function test_shim() { + // Test that a shim which only runs in strict mode works, and that it + // is permitted to opt into showing normally-blocked tracking content. + + Services.prefs.setBoolPref(TRACKING_PREF, true); + + await testShimRuns(SHIMMABLE_TEST_PAGE); + + // test that if the user opts in on one domain, they will still have to opt + // in on another domain which embeds an iframe to the first one. + + await testShimRuns(EMBEDDING_TEST_PAGE, 0, false, false); + + Services.prefs.clearUserPref(TRACKING_PREF); +}); diff --git a/browser/extensions/webcompat/tests/browser/head.js b/browser/extensions/webcompat/tests/browser/head.js new file mode 100644 index 0000000000..7fe8c3c171 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/head.js @@ -0,0 +1,140 @@ +"use strict"; + +const TEST_ROOT = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "http://example.com" +); + +const THIRD_PARTY_ROOT = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "http://example.net" +); + +const SHIMMABLE_TEST_PAGE = `${TEST_ROOT}shims_test.html`; +const SHIMMABLE_TEST_PAGE_2 = `${TEST_ROOT}shims_test_2.html`; +const SHIMMABLE_TEST_PAGE_3 = `${TEST_ROOT}shims_test_3.html`; +const EMBEDDING_TEST_PAGE = `${THIRD_PARTY_ROOT}iframe_test.html`; + +const BLOCKED_TRACKER_URL = + "//trackertest.org/tests/toolkit/components/url-classifier/tests/mochitest/evil.js"; + +const DISABLE_SHIM1_PREF = "extensions.webcompat.disabled_shims.MochitestShim"; +const DISABLE_SHIM2_PREF = "extensions.webcompat.disabled_shims.MochitestShim2"; +const DISABLE_SHIM3_PREF = "extensions.webcompat.disabled_shims.MochitestShim3"; +const DISABLE_SHIM4_PREF = "extensions.webcompat.disabled_shims.MochitestShim4"; +const GLOBAL_PREF = "extensions.webcompat.enable_shims"; +const TRACKING_PREF = "privacy.trackingprotection.enabled"; + +const { UrlClassifierTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/UrlClassifierTestUtils.sys.mjs" +); + +async function testShimRuns( + testPage, + frame, + trackersAllowed = true, + expectOptIn = true +) { + const tab = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + opening: testPage, + waitForLoad: true, + }); + + const TrackingProtection = + tab.ownerGlobal.gProtectionsHandler.blockers.TrackingProtection; + ok(TrackingProtection, "TP is attached to the tab"); + ok(TrackingProtection.enabled, "TP is enabled"); + + await SpecialPowers.spawn( + tab.linkedBrowser, + [[trackersAllowed, BLOCKED_TRACKER_URL, expectOptIn], frame], + async (args, _frame) => { + const window = _frame === undefined ? content : content.frames[_frame]; + + await SpecialPowers.spawn( + window, + args, + async (_trackersAllowed, trackerUrl, _expectOptIn) => { + const shimResult = await content.wrappedJSObject.shimPromise; + is("shimmed", shimResult, "Shim activated"); + + const optInResult = await content.wrappedJSObject.optInPromise; + is(_expectOptIn, optInResult, "Shim allowed opt in if appropriate"); + + const o = content.document.getElementById("shims"); + const cl = o.classList; + const opts = JSON.parse(o.innerText); + is( + undefined, + opts.branchValue, + "Shim script did not receive option for other branch" + ); + is( + undefined, + opts.platformValue, + "Shim script did not receive option for other platform" + ); + is( + true, + opts.simpleOption, + "Shim script received simple option correctly" + ); + ok(opts.complexOption, "Shim script received complex option"); + is( + 1, + opts.complexOption.a, + "Shim script received complex options correctly #1" + ); + is( + "test", + opts.complexOption.b, + "Shim script received complex options correctly #2" + ); + ok(cl.contains("green"), "Shim affected page correctly"); + } + ); + } + ); + + await BrowserTestUtils.removeTab(tab); +} + +async function testShimDoesNotRun( + trackersAllowed = false, + testPage = SHIMMABLE_TEST_PAGE +) { + const tab = await BrowserTestUtils.openNewForegroundTab({ + gBrowser, + opening: testPage, + waitForLoad: true, + }); + + await SpecialPowers.spawn( + tab.linkedBrowser, + [trackersAllowed, BLOCKED_TRACKER_URL], + async (_trackersAllowed, trackerUrl) => { + const shimResult = await content.wrappedJSObject.shimPromise; + is("did not shim", shimResult, "Shim did not activate"); + + ok( + !content.document.getElementById("shims").classList.contains("green"), + "Shim script did not run" + ); + + is( + _trackersAllowed ? "ALLOWED" : "BLOCKED", + await new Promise(resolve => { + const s = content.document.createElement("script"); + s.src = trackerUrl; + s.onload = () => resolve("ALLOWED"); + s.onerror = () => resolve("BLOCKED"); + content.document.head.appendChild(s); + }), + "Normally-blocked resources blocked if appropriate" + ); + } + ); + + await BrowserTestUtils.removeTab(tab); +} diff --git a/browser/extensions/webcompat/tests/browser/iframe_test.html b/browser/extensions/webcompat/tests/browser/iframe_test.html new file mode 100644 index 0000000000..baf1ee9024 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/iframe_test.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf8" /> + <script> + window.shimPromise = new Promise(resolve => { + window.shimPromiseResolve = resolve; + }); + window.optInPromise = new Promise(resolve => { + window.optInPromiseResolve = resolve; + }); + </script> + </head> + <body> + <iframe + src="http://example.com/browser/browser/extensions/webcompat/tests/browser/shims_test.html" + ></iframe> + </body> +</html> diff --git a/browser/extensions/webcompat/tests/browser/shims_test.html b/browser/extensions/webcompat/tests/browser/shims_test.html new file mode 100644 index 0000000000..ebe877316d --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test.html @@ -0,0 +1,21 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf8" /> + <script> + window.shimPromise = new Promise(resolve => { + window.shimPromiseResolve = resolve; + }); + window.optInPromise = new Promise(resolve => { + window.optInPromiseResolve = resolve; + }); + </script> + <script + onerror="window.shimPromiseResolve('error')" + src="shims_test.js" + ></script> + </head> + <body> + <div id="shims"></div> + </body> +</html> diff --git a/browser/extensions/webcompat/tests/browser/shims_test.js b/browser/extensions/webcompat/tests/browser/shims_test.js new file mode 100644 index 0000000000..4a55bee7ed --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test.js @@ -0,0 +1,11 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +if (window.doingOptIn) { + window.optInPromiseResolve(true); +} else { + window.shimPromiseResolve("did not shim"); +} diff --git a/browser/extensions/webcompat/tests/browser/shims_test_2.html b/browser/extensions/webcompat/tests/browser/shims_test_2.html new file mode 100644 index 0000000000..b080f74f6e --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test_2.html @@ -0,0 +1,21 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf8" /> + <script> + window.shimPromise = new Promise(resolve => { + window.shimPromiseResolve = resolve; + }); + window.optInPromise = new Promise(resolve => { + window.optInPromiseResolve = resolve; + }); + </script> + <script + onerror="window.shimPromiseResolve('error')" + src="shims_test_2.js" + ></script> + </head> + <body> + <div id="shims"></div> + </body> +</html> diff --git a/browser/extensions/webcompat/tests/browser/shims_test_2.js b/browser/extensions/webcompat/tests/browser/shims_test_2.js new file mode 100644 index 0000000000..4a55bee7ed --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test_2.js @@ -0,0 +1,11 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +if (window.doingOptIn) { + window.optInPromiseResolve(true); +} else { + window.shimPromiseResolve("did not shim"); +} diff --git a/browser/extensions/webcompat/tests/browser/shims_test_3.html b/browser/extensions/webcompat/tests/browser/shims_test_3.html new file mode 100644 index 0000000000..bcb6f12043 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test_3.html @@ -0,0 +1,21 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="utf8" /> + <script> + window.shimPromise = new Promise(resolve => { + window.shimPromiseResolve = resolve; + }); + window.optInPromise = new Promise(resolve => { + window.optInPromiseResolve = resolve; + }); + </script> + <script + onerror="window.shimPromiseResolve('error')" + src="shims_test_3.js" + ></script> + </head> + <body> + <div id="shims"></div> + </body> +</html> diff --git a/browser/extensions/webcompat/tests/browser/shims_test_3.js b/browser/extensions/webcompat/tests/browser/shims_test_3.js new file mode 100644 index 0000000000..9acb6cdcf1 --- /dev/null +++ b/browser/extensions/webcompat/tests/browser/shims_test_3.js @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +window.shimPromiseResolve("did not shim"); |