summaryrefslogtreecommitdiffstats
path: root/browser/extensions/pocket/test/head.js
blob: e044a42c76bac95f03a68dd114ec5736d82a52b9 (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
// 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;

// 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");
  return new Promise((resolve, reject) => {
    let listener = {
      onWidgetAfterCreation(widgetid) {
        if (widgetid == "pocket-button") {
          info("pocket-button created");
          CustomizableUI.removeListener(listener);
          resolve(false);
        }
      }
    }
    CustomizableUI.addListener(listener);
    Services.prefs.setBoolPref("extensions.pocket.enabled", true);
  });
}

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);
  }
  return new Promise((resolve, reject) => {
    let listener = {
      onWidgetDestroyed: function(widgetid) {
        if (widgetid == "pocket-button") {
          CustomizableUI.removeListener(listener);
          info( "pocket-button destroyed");
          // wait for a full unload of pocket
          BrowserTestUtils.waitForCondition(() => {
            return !window.hasOwnProperty("pktUI");
          }, "pocket properties removed from window").then(() => {
            resolve(false);
          })
        }
      }
    }
    CustomizableUI.addListener(listener);
    info("reset pocket enabled pref");
    // testing/profiles/prefs_general.js uses user_pref to disable pocket, set
    // back to false.
    Services.prefs.setBoolPref("extensions.pocket.enabled", false);
  });
}

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