summaryrefslogtreecommitdiffstats
path: root/browser/components/protections/test/browser/browser_protections_proxy.js
blob: 840abcab3f326f959194b085060e1101d00ef94a (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* 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";

ChromeUtils.defineESModuleGetters(this, {
  Region: "resource://gre/modules/Region.sys.mjs",
});

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.contentblocking.report.monitor.enabled", false],
      ["browser.contentblocking.report.lockwise.enabled", false],
      ["browser.vpn_promo.enabled", false],
    ],
  });
});

add_task(async function () {
  let tab = await BrowserTestUtils.openNewForegroundTab({
    url: "about:protections",
    gBrowser,
  });

  info("Secure Proxy card should be hidden by default");
  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
    await ContentTaskUtils.waitForCondition(() => {
      const proxyCard = content.document.querySelector(".proxy-card");
      return !proxyCard["data-enabled"];
    }, "Proxy card is not enabled.");

    const proxyCard = content.document.querySelector(".proxy-card");
    ok(ContentTaskUtils.is_hidden(proxyCard), "Proxy card is hidden.");
  });

  info("Enable showing the Secure Proxy card");
  Services.prefs.setBoolPref(
    "browser.contentblocking.report.proxy.enabled",
    true
  );

  info(
    "Check that secure proxy card is hidden if user's language is not en-US"
  );
  Services.prefs.setCharPref("intl.accept_languages", "en-CA");
  await BrowserTestUtils.reloadTab(tab);
  await checkProxyCardVisibility(tab, true);

  info(
    "Check that secure proxy card is shown if user's location is in the US."
  );
  // Set language back to en-US
  Services.prefs.setCharPref("intl.accept_languages", "en-US");
  Region._setHomeRegion("US", false);
  await BrowserTestUtils.reloadTab(tab);
  await checkProxyCardVisibility(tab, false);

  info(
    "Check that secure proxy card is hidden if user's location is not in the US."
  );
  Region._setHomeRegion("CA", false);
  await BrowserTestUtils.reloadTab(tab);
  await checkProxyCardVisibility(tab, true);

  info(
    "Check that secure proxy card is hidden if the extension is already installed."
  );
  // Make sure we set the region back to "US"
  Region._setHomeRegion("US", false);
  const id = "secure-proxy@mozilla.com";
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id } },
      name: "Firefox Proxy",
    },
    useAddonManager: "temporary",
  });
  await extension.startup();
  await BrowserTestUtils.reloadTab(tab);
  await checkProxyCardVisibility(tab, true);
  await extension.unload();

  Services.prefs.setBoolPref(
    "browser.contentblocking.report.proxy.enabled",
    false
  );

  await BrowserTestUtils.removeTab(tab);
});

async function checkProxyCardVisibility(tab, shouldBeHidden) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [{ _shouldBeHidden: shouldBeHidden }],
    async function ({ _shouldBeHidden }) {
      await ContentTaskUtils.waitForCondition(() => {
        const proxyCard = content.document.querySelector(".proxy-card");
        return ContentTaskUtils.is_hidden(proxyCard) === _shouldBeHidden;
      });

      const visibilityState = _shouldBeHidden ? "hidden" : "shown";
      ok(true, `Proxy card is ${visibilityState}.`);
    }
  );
}