/* global CustomizableUI, info, Services */

// Currently Screenshots is disabled in tests.  We want these tests to work under
// either case that Screenshots is disabled or enabled on startup of the browser,
// and that at the end we're reset to the correct state.
let enabledOnStartup = false;

// ScreenshotsEnabled/Disabled promises return true if it was already
// Enabled/Disabled, and false if it need to Enable/Disable.
function promiseScreenshotsEnabled() {
  if (Services.prefs.getPrefType("extensions.screenshots.disabled") == Services.prefs.PREF_INVALID ||
      !Services.prefs.getBoolPref("extensions.screenshots.disabled")) {
    info("Screenshots was already enabled, assuming enabled by default for tests");
    enabledOnStartup = true;
    return Promise.resolve(true);
  }
  info("Screenshots is not enabled");
  return new Promise((resolve, reject) => {
    let listener = {
      onWidgetAfterCreation(widgetid) {
        if (widgetid == "screenshots_mozilla_org-browser-action") {
          info("screenshots_mozilla_org-browser-action button created");
          CustomizableUI.removeListener(listener);
          resolve(false);
        }
      }
    }
    CustomizableUI.addListener(listener);
    Services.prefs.setBoolPref("extensions.screenshots.disabled", false);
  });
}

function promiseScreenshotsDisabled() {
  if (Services.prefs.getPrefType("extensions.screenshots.disabled") != Services.prefs.PREF_INVALID &&
      Services.prefs.getBoolPref("extensions.screenshots.disabled")) {
    info("Screenshots already disabled");
    return Promise.resolve(true);
  }
  return new Promise((resolve, reject) => {
    let listener = {
      onWidgetDestroyed(widgetid) {
        if (widgetid == "screenshots_mozilla_org-browser-action") {
          CustomizableUI.removeListener(listener);
          info("screenshots_mozilla_org-browser-action destroyed");
          resolve(false);
        }
      }
    }
    CustomizableUI.addListener(listener);
    info("Set Screenshots disabled pref");
    // testing/profiles/prefs_general.js uses user_pref to disable pocket, set
    // back to false.
    Services.prefs.setBoolPref("extensions.screenshots.disabled", true);
  });
}

function promiseScreenshotsReset() { // eslint-disable-line no-unused-vars
  if (enabledOnStartup) {
    info("Reset is enabling Screenshots addon");
    return promiseScreenshotsEnabled();
  }
  info("Reset is disabling Screenshots addon");
  return promiseScreenshotsDisabled();
}

@@ -470,110 +470,8 @@
 if (
-Services.prefs.getPrefType(%22extensions.screenshots.disabled%22) == Services.prefs.PREF_INVALID %7C%7C%0A      
 !Ser
@@ -510,32 +510,39 @@
 ons.screenshots.
+system-
 disabled%22)) %7B%0A  
@@ -526,32 +526,39 @@
 system-disabled%22
+, false
 )) %7B%0A    info(%22S
@@ -1120,24 +1120,77 @@
 (listener);%0A
+    info(%22Set Screenshots disabled pref to false.%22);%0A
     Services
@@ -1224,32 +1224,39 @@
 ons.screenshots.
+system-
 disabled%22, false
@@ -1335,16 +1335,16 @@
 .get
+Bool
 Pref
-Type
 (%22ex
@@ -1356,32 +1356,39 @@
 ons.screenshots.
+system-
 disabled%22) != Se
@@ -1384,110 +1384,15 @@
 led%22
-) != Services.prefs.PREF_INVALID &&%0A      Services.prefs.getBoolPref(%22extensions.screenshots.disabled%22
+, false
 )) %7B
@@ -1895,112 +1895,20 @@
 pref
-%22);%0A    // testing/profiles/prefs_general.js uses user_pref to disable pocket, set%0A    // back to false.
+ to true.%22);
 %0A   
@@ -1959,16 +1959,23 @@
 enshots.
+system-
 disabled

Change the mochitest to work with the system-disabled pref rather than the user facing pref. (#2637)