summaryrefslogtreecommitdiffstats
path: root/toolkit/components/antitracking/bouncetrackingprotection/test/browser/browser_bouncetracking_schemes.js
blob: 21b72cdc45f7ab430a5d5562155bbf6ba3ccf08e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Any copyright is dedicated to the Public Domain.
   https://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

let bounceTrackingProtection;

add_setup(async function () {
  bounceTrackingProtection = Cc[
    "@mozilla.org/bounce-tracking-protection;1"
  ].getService(Ci.nsIBounceTrackingProtection);
  bounceTrackingProtection.clearAll();
});

async function testInteractWithSite(origin, expectRecorded) {
  is(
    bounceTrackingProtection.testGetUserActivationHosts({}).length,
    0,
    "No user activation hosts initially"
  );

  let baseDomain;
  let scheme;

  await BrowserTestUtils.withNewTab(origin, async browser => {
    baseDomain = browser.contentPrincipal.baseDomain;
    scheme = browser.contentPrincipal.URI.scheme;

    info(
      `Trigger a user activation, which should ${
        expectRecorded ? "" : "not "
      }be recorded.`
    );
    // We intentionally turn off this a11y check, because the following click
    // is purposefully sent on an arbitrary web content that is not expected
    // to be tested by itself with the browser mochitests, therefore this rule
    // check shall be ignored by a11y_checks suite.
    AccessibilityUtils.setEnv({ mustHaveAccessibleRule: false });
    await BrowserTestUtils.synthesizeMouseAtPoint(50, 50, {}, browser);
    AccessibilityUtils.resetEnv();
  });
  if (expectRecorded) {
    Assert.deepEqual(
      bounceTrackingProtection
        .testGetUserActivationHosts({})
        .map(entry => entry.siteHost),
      [baseDomain],
      `User activation should be recorded for ${scheme} scheme.`
    );
  } else {
    Assert.deepEqual(
      bounceTrackingProtection.testGetUserActivationHosts({}),
      [],
      `User activation should not be recorded for ${scheme} scheme.`
    );
  }

  bounceTrackingProtection.clearAll();
}

/**
 * Test that we only record user activation for supported schemes.
 */
add_task(async function test_userActivationSchemes() {
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  await testInteractWithSite("http://example.com", true);
  await testInteractWithSite("https://example.com", true);

  await testInteractWithSite("about:blank", false);
  await testInteractWithSite("about:robots", false);
  await testInteractWithSite(
    "file://" + Services.dirsvc.get("TmpD", Ci.nsIFile).path,
    false
  );
});