summaryrefslogtreecommitdiffstats
path: root/browser/components/pocket/test/head.js
blob: c3ce73e42e464225cdbba2254e96189704390d53 (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
// Currently Pocket is disabled in tests.  We want these tests to work under
// either case that Pocket is disabled or enabled on startup of the browser,
// and that at the end we're reset to the correct state.
let enabledOnStartup = false;

ChromeUtils.defineESModuleGetters(this, {
  pktApi: "chrome://pocket/content/pktApi.sys.mjs",
});

const { sinon } = ChromeUtils.importESModule(
  "resource://testing-common/Sinon.sys.mjs"
);

// PocketEnabled/Disabled promises return true if it was already
// Enabled/Disabled, and false if it need to Enable/Disable.
function promisePocketEnabled() {
  if (
    Services.prefs.getPrefType("extensions.pocket.enabled") !=
      Services.prefs.PREF_INVALID &&
    Services.prefs.getBoolPref("extensions.pocket.enabled")
  ) {
    info("pocket was already enabled, assuming enabled by default for tests");
    enabledOnStartup = true;
    return Promise.resolve(true);
  }
  info("pocket is not enabled");
  Services.prefs.setBoolPref("extensions.pocket.enabled", true);
  return BrowserTestUtils.waitForCondition(() => {
    return !!CustomizableUI.getWidget("save-to-pocket-button");
  });
}

function promisePocketDisabled() {
  if (
    Services.prefs.getPrefType("extensions.pocket.enabled") ==
      Services.prefs.PREF_INVALID ||
    !Services.prefs.getBoolPref("extensions.pocket.enabled")
  ) {
    info("pocket-button already disabled");
    return Promise.resolve(true);
  }
  info("reset pocket enabled pref");
  // testing/profiles/common/user.js uses user_pref to disable pocket, set
  // back to false.
  Services.prefs.setBoolPref("extensions.pocket.enabled", false);
  return BrowserTestUtils.waitForCondition(() => {
    return !CustomizableUI.getWidget("save-to-pocket-button");
  });
}

function promisePocketReset() {
  if (enabledOnStartup) {
    info("reset is enabling pocket addon");
    return promisePocketEnabled();
  }
  info("reset is disabling pocket addon");
  return promisePocketDisabled();
}

function checkElements(expectPresent, l, win = window) {
  for (let id of l) {
    let el =
      win.document.getElementById(id) ||
      win.gNavToolbox.palette.querySelector("#" + id);
    is(
      !!el && !el.hidden,
      expectPresent,
      "element " + id + (expectPresent ? " is" : " is not") + " present"
    );
  }
}

function checkElementsShown(expectPresent, l, win = window) {
  for (let id of l) {
    let el =
      win.document.getElementById(id) ||
      win.gNavToolbox.palette.querySelector("#" + id);
    let elShown = !!el && window.getComputedStyle(el).display != "none";
    is(
      elShown,
      expectPresent,
      "element " + id + (expectPresent ? " is" : " is not") + " present"
    );
  }
}