summaryrefslogtreecommitdiffstats
path: root/remote/shared/test/xpcshell/test_RecommendedPreferences.js
blob: 88dc717be828d51dcdfeeb84f18ced00552c2f5b (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
112
113
114
115
116
117
118
119
120
121
122
123
/* 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 PROTOCOL_1_PREF = "dom.disable_beforeunload";
const PROTOCOL_1_RECOMMENDED_PREFS = new Map([[PROTOCOL_1_PREF, true]]);

const PROTOCOL_2_PREF = "browser.contentblocking.features.standard";
const PROTOCOL_2_RECOMMENDED_PREFS = new Map([
  [PROTOCOL_2_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_multipleClients() {
  info("Check initial values for the test preferences");
  checkPreferences({ common: false, protocol_1: false, protocol_2: false });

  checkPreferences({ common: false, protocol_1: false, protocol_2: false });

  info("Apply recommended preferences for a protocol_1 client");
  RecommendedPreferences.applyPreferences(PROTOCOL_1_RECOMMENDED_PREFS);
  checkPreferences({ common: true, protocol_1: true, protocol_2: false });

  info("Apply recommended preferences for a protocol_2 client");
  RecommendedPreferences.applyPreferences(PROTOCOL_2_RECOMMENDED_PREFS);
  checkPreferences({ common: true, protocol_1: true, protocol_2: true });

  info("Restore protocol_1 preferences");
  RecommendedPreferences.restorePreferences(PROTOCOL_1_RECOMMENDED_PREFS);
  checkPreferences({ common: true, protocol_1: false, protocol_2: true });

  info("Restore protocol_2 preferences");
  RecommendedPreferences.restorePreferences(PROTOCOL_2_RECOMMENDED_PREFS);
  checkPreferences({ common: true, protocol_1: false, protocol_2: false });

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

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

  cleanup();
});

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

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

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

  cleanup();
});

add_task(async function test_noCustomPreferences() {
  info("Applying preferences without any custom preference should not throw");

  // First call invokes setting of default preferences
  RecommendedPreferences.applyPreferences();

  // Second call does nothing
  RecommendedPreferences.applyPreferences();

  cleanup();
});

// Check that protocols can override common preferences.
add_task(async function test_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({ common, protocol_1, protocol_2 }) {
  checkPreference(COMMON_PREF, { hasValue: common });
  checkPreference(PROTOCOL_1_PREF, { hasValue: protocol_1 });
  checkPreference(PROTOCOL_2_PREF, { hasValue: protocol_2 });
}

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`
  );
}