summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_https_only_section.js
blob: a43448adf50280f052f2bee6f7fa80dba866c5c5 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Bug 1671122 - Fixed bug where second click on HTTPS-Only Mode enable-checkbox disables it again.
// https://bugzilla.mozilla.org/bug/1671122
"use strict";

const HTTPS_ONLY_ENABLED = "enabled";
const HTTPS_ONLY_PBM_ONLY = "privateOnly";
const HTTPS_ONLY_DISABLED = "disabled";

add_task(async function httpsOnlyRadioGroupIsWorking() {
  // Make sure HTTPS-Only mode is only enabled for PBM

  registerCleanupFunction(async function () {
    Services.prefs.clearUserPref("dom.security.https_only_mode");
    Services.prefs.clearUserPref("dom.security.https_only_mode_pbm");
  });

  await SpecialPowers.setBoolPref("dom.security.https_only_mode", false);
  await SpecialPowers.setBoolPref("dom.security.https_only_mode_pbm", true);

  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });

  const doc = gBrowser.selectedBrowser.contentDocument;
  const radioGroup = doc.getElementById("httpsOnlyRadioGroup");
  const enableAllRadio = doc.getElementById("httpsOnlyRadioEnabled");
  const enablePbmRadio = doc.getElementById("httpsOnlyRadioEnabledPBM");
  const disableRadio = doc.getElementById("httpsOnlyRadioDisabled");

  // Check if UI
  check(radioGroup, HTTPS_ONLY_PBM_ONLY);

  // Check if UI updated on pref-change
  await SpecialPowers.setBoolPref("dom.security.https_only_mode_pbm", false);
  check(radioGroup, HTTPS_ONLY_DISABLED);

  // Check if prefs change if clicked on radio button
  enableAllRadio.click();
  check(radioGroup, HTTPS_ONLY_ENABLED);

  // Check if prefs stay the same if clicked on same
  // radio button again (see bug 1671122)
  enableAllRadio.click();
  check(radioGroup, HTTPS_ONLY_ENABLED);

  // Check if prefs are set correctly for PBM-only mode.
  enablePbmRadio.click();
  check(radioGroup, HTTPS_ONLY_PBM_ONLY);

  // Check if prefs are set correctly when disabled again.
  disableRadio.click();
  check(radioGroup, HTTPS_ONLY_DISABLED);

  BrowserTestUtils.removeTab(gBrowser.selectedTab);
});

function check(radioGroupElement, expectedValue) {
  is(
    radioGroupElement.value,
    expectedValue,
    "Radio Group value should match expected value"
  );
  is(
    SpecialPowers.getBoolPref("dom.security.https_only_mode"),
    expectedValue === HTTPS_ONLY_ENABLED,
    "HTTPS-Only pref should match expected value."
  );
  is(
    SpecialPowers.getBoolPref("dom.security.https_only_mode_pbm"),
    expectedValue === HTTPS_ONLY_PBM_ONLY,
    "HTTPS-Only PBM pref should match expected value."
  );
}