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

"use strict";

async function checkMessages(expectedResult) {
  let onBeforeAddons = false;
  let onProfileAfterChange = false;
  let onBeforeUIStartup = false;
  let onAllWindowsRestored = false;

  let errorListener = {
    observe(subject) {
      let message = subject.wrappedJSObject.arguments[0];
      if (message.includes("_cleanup from onBeforeAddons")) {
        onBeforeAddons = true;
      } else if (message.includes("_cleanup from onProfileAfterChange")) {
        onProfileAfterChange = true;
      } else if (message.includes("_cleanup from onBeforeUIStartup")) {
        onBeforeUIStartup = true;
      } else if (message.includes("_cleanup from onAllWindowsRestored")) {
        onAllWindowsRestored = true;
      }
    },
  };

  Services.console.registerListener(errorListener);

  const ConsoleAPIStorage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(
    Ci.nsIConsoleAPIStorage
  );
  ConsoleAPIStorage.addLogEventListener(
    errorListener.observe,
    Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal)
  );

  await setupPolicyEngineWithJson({
    policies: {},
  });

  equal(
    onBeforeAddons,
    expectedResult,
    "onBeforeAddons should be " + expectedResult
  );
  equal(
    onProfileAfterChange,
    expectedResult,
    "onProfileAfterChange should be" + expectedResult
  );
  equal(
    onBeforeUIStartup,
    expectedResult,
    "onBeforeUIStartup should be" + expectedResult
  );
  equal(
    onAllWindowsRestored,
    expectedResult,
    "onAllWindowsRestored should be" + expectedResult
  );
}

/* If there is no existing policy, cleanup should not run. */
add_task(async function test_cleanup_no_policy() {
  await checkMessages(false);
});

add_task(async function setup_policy() {
  await setupPolicyEngineWithJson({
    policies: {
      BlockAboutConfig: true,
    },
  });
});

/* Since there was a policy, cleanup should run. */
add_task(async function test_cleanup_with_policy() {
  await checkMessages(true);
});

/* Since cleanup was already done, cleanup should not run again. */
add_task(async function test_cleanup_after_policy() {
  await checkMessages(false);
});