summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/xpcshell/test_RecommendedPreferences.js
blob: d42482d1d82b139492967f5644a75cd4abe4ca98 (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
108
109
110
111
/* 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/. */

const { RecommendedPreferences } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/RecommendedPreferences.sys.mjs"
);

const COMMON_PREF = "toolkit.startup.max_resumed_crashes";

const MARIONETTE_PREF = "dom.disable_beforeunload";
const MARIONETTE_RECOMMENDED_PREFS = new Map([[MARIONETTE_PREF, true]]);

const CDP_PREF = "browser.contentblocking.features.standard";
const CDP_RECOMMENDED_PREFS = new Map([
  [CDP_PREF, "-tp,tpPrivate,cookieBehavior0,-cm,-fp"],
]);

function cleanup() {
  info("Restore recommended preferences and test preferences");
  Services.prefs.clearUserPref("remote.prefs.recommended");
  RecommendedPreferences.restoreAllPreferences();
}

// cleanup() should be called:
// - explicitly after each test to avoid side effects
// - via registerCleanupFunction in case a test crashes/times out
registerCleanupFunction(cleanup);

add_task(async function test_RecommendedPreferences() {
  info("Check initial values for the test preferences");
  checkPreferences({ cdp: false, common: false, marionette: false });

  checkPreferences({ cdp: false, common: false, marionette: false });

  info("Apply recommended preferences for a marionette client");
  RecommendedPreferences.applyPreferences(MARIONETTE_RECOMMENDED_PREFS);
  checkPreferences({ cdp: false, common: true, marionette: true });

  info("Apply recommended preferences for a cdp client");
  RecommendedPreferences.applyPreferences(CDP_RECOMMENDED_PREFS);
  checkPreferences({ cdp: true, common: true, marionette: true });

  info("Restore marionette preferences");
  RecommendedPreferences.restorePreferences(MARIONETTE_RECOMMENDED_PREFS);
  checkPreferences({ cdp: true, common: true, marionette: false });

  info("Restore cdp preferences");
  RecommendedPreferences.restorePreferences(CDP_RECOMMENDED_PREFS);
  checkPreferences({ cdp: false, common: true, marionette: false });

  info("Restore all the altered preferences");
  RecommendedPreferences.restoreAllPreferences();
  checkPreferences({ cdp: false, common: false, marionette: false });

  info("Attemps to restore again");
  RecommendedPreferences.restoreAllPreferences();
  checkPreferences({ cdp: false, common: false, marionette: false });

  cleanup();
});

add_task(async function test_RecommendedPreferences_disabled() {
  info("Disable RecommendedPreferences");
  Services.prefs.setBoolPref("remote.prefs.recommended", false);

  info("Check initial values for the test preferences");
  checkPreferences({ cdp: false, common: false, marionette: false });

  info("Recommended preferences are not applied, applyPreferences is a no-op");
  RecommendedPreferences.applyPreferences(MARIONETTE_RECOMMENDED_PREFS);
  checkPreferences({ cdp: false, common: false, marionette: false });

  cleanup();
});

// Check that protocols can override common preferences.
add_task(async function test_RecommendedPreferences_override() {
  info("Make sure the common preference has no user value");
  Services.prefs.clearUserPref(COMMON_PREF);

  const OVERRIDE_VALUE = 42;
  const OVERRIDE_COMMON_PREF = new Map([[COMMON_PREF, OVERRIDE_VALUE]]);

  info("Apply a map of preferences overriding a common preference");
  RecommendedPreferences.applyPreferences(OVERRIDE_COMMON_PREF);

  equal(
    Services.prefs.getIntPref(COMMON_PREF),
    OVERRIDE_VALUE,
    "The common preference was set to the expected value"
  );

  cleanup();
});

function checkPreferences({ cdp, common, marionette }) {
  checkPreference(COMMON_PREF, { hasValue: common });
  checkPreference(MARIONETTE_PREF, { hasValue: marionette });
  checkPreference(CDP_PREF, { hasValue: cdp });
}

function checkPreference(pref, { hasValue }) {
  equal(
    Services.prefs.prefHasUserValue(pref),
    hasValue,
    hasValue
      ? `The preference ${pref} has a user value`
      : `The preference ${pref} has no user value`
  );
}