summaryrefslogtreecommitdiffstats
path: root/browser/extensions/report-site-issue/test/browser/head.js
blob: da4545badccd5ba83aa8967c149e956afb6f6518 (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
"use strict";

ChromeUtils.defineModuleGetter(
  this,
  "AddonManager",
  "resource://gre/modules/AddonManager.jsm"
);

const { Management } = ChromeUtils.import(
  "resource://gre/modules/Extension.jsm"
);

const PREF_WC_REPORTER_ENABLED = "extensions.webcompat-reporter.enabled";
const PREF_WC_REPORTER_ENDPOINT =
  "extensions.webcompat-reporter.newIssueEndpoint";

const TEST_ROOT = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);
const TEST_PAGE = TEST_ROOT + "test.html";
const FRAMEWORKS_TEST_PAGE = TEST_ROOT + "frameworks.html";
const FASTCLICK_TEST_PAGE = TEST_ROOT + "fastclick.html";
const NEW_ISSUE_PAGE = TEST_ROOT + "webcompat.html";

const WC_ADDON_ID = "webcompat-reporter@mozilla.org";

async function promiseAddonEnabled() {
  const addon = await AddonManager.getAddonByID(WC_ADDON_ID);
  if (addon.isActive) {
    return;
  }
  const pref = SpecialPowers.Services.prefs.getBoolPref(
    PREF_WC_REPORTER_ENABLED,
    false
  );
  if (!pref) {
    SpecialPowers.Services.prefs.setBoolPref(PREF_WC_REPORTER_ENABLED, true);
  }
}

class HelpMenuHelper {
  #popup = null;

  async open() {
    this.popup = document.getElementById("menu_HelpPopup");
    ok(this.popup, "Help menu should exist");

    const menuOpen = BrowserTestUtils.waitForEvent(this.popup, "popupshown");

    // This event-faking method was copied from browser_title_case_menus.js so
    // this can be tested on MacOS (where the actual menus cannot be opened in
    // tests, but we only need the help menu to populate itself).
    this.popup.dispatchEvent(new MouseEvent("popupshowing", { bubbles: true }));
    this.popup.dispatchEvent(new MouseEvent("popupshown", { bubbles: true }));

    await menuOpen;
  }

  async close() {
    if (this.popup) {
      const menuClose = BrowserTestUtils.waitForEvent(
        this.popup,
        "popuphidden"
      );

      // (Also copied from browser_title_case_menus.js)
      // Just for good measure, we'll fire the popuphiding/popuphidden events
      // after we close the menupopups.
      this.popup.dispatchEvent(
        new MouseEvent("popuphiding", { bubbles: true })
      );
      this.popup.dispatchEvent(
        new MouseEvent("popuphidden", { bubbles: true })
      );

      await menuClose;
      this.popup = null;
    }
  }

  isItemHidden() {
    const item = document.getElementById("help_reportSiteIssue");
    return item && item.hidden;
  }

  isItemEnabled() {
    const item = document.getElementById("help_reportSiteIssue");
    return item && !item.hidden && !item.disabled;
  }
}

async function startIssueServer() {
  const landingTemplate = await new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();
    xhr.open("GET", NEW_ISSUE_PAGE);
    xhr.onload = () => {
      resolve(xhr.responseText);
    };
    xhr.onerror = reject;
    xhr.send();
  });

  const { HttpServer } = ChromeUtils.import(
    "resource://testing-common/httpd.js"
  );
  const server = new HttpServer();

  registerCleanupFunction(async function cleanup() {
    await new Promise(resolve => server.stop(resolve));
  });

  server.registerPathHandler("/new", function(request, response) {
    response.setHeader("Content-Type", "text/html", false);
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.write(landingTemplate);
  });

  server.start(-1);
  return `http://localhost:${server.identity.primaryPort}/new`;
}