summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabMediaIndicator/head.js
blob: 8b82e21a43f9710e5824660cf30df07abe8d7f97 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
 * Global variables for testing.
 */
const gEMPTY_PAGE_URL = GetTestWebBasedURL("file_empty.html");

/**
 * Return a web-based URL for a given file based on the testing directory.
 * @param {String} fileName
 *        file that caller wants its web-based url
 * @param {Boolean} cors [optional]
 *        if set, then return a url with different origin
 */
function GetTestWebBasedURL(fileName, cors = false) {
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
  const origin = cors ? "http://example.org" : "http://example.com";
  return (
    getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) +
    fileName
  );
}

/**
 * Wait until tab sound indicator appears on the given tab.
 * @param {tabbrowser} tab
 *        given tab where tab sound indicator should appear
 */
async function waitForTabSoundIndicatorAppears(tab) {
  if (!tab.soundPlaying) {
    info("Tab sound indicator doesn't appear yet");
    await BrowserTestUtils.waitForEvent(
      tab,
      "TabAttrModified",
      false,
      event => {
        return event.detail.changed.includes("soundplaying");
      }
    );
  }
  ok(tab.soundPlaying, "Tab sound indicator appears");
}

/**
 * Wait until tab sound indicator disappears on the given tab.
 * @param {tabbrowser} tab
 *        given tab where tab sound indicator should disappear
 */
async function waitForTabSoundIndicatorDisappears(tab) {
  if (tab.soundPlaying) {
    info("Tab sound indicator doesn't disappear yet");
    await BrowserTestUtils.waitForEvent(
      tab,
      "TabAttrModified",
      false,
      event => {
        return event.detail.changed.includes("soundplaying");
      }
    );
  }
  ok(!tab.soundPlaying, "Tab sound indicator disappears");
}

/**
 * Return a new foreground tab loading with an empty file.
 * @param {boolean} needObserver
 *        If true, sets an observer property on the returned tab. This property
 *        exposes `hasEverUpdated()` which will return a bool indicating if the
 *        sound indicator has ever updated.
 */
async function createBlankForegroundTab({ needObserver } = {}) {
  const tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    gEMPTY_PAGE_URL
  );
  if (needObserver) {
    tab.observer = createSoundIndicatorObserver(tab);
  }
  return tab;
}

function createSoundIndicatorObserver(tab) {
  let hasEverUpdated = false;
  let listener = event => {
    if (event.detail.changed.includes("soundplaying")) {
      hasEverUpdated = true;
    }
  };
  tab.addEventListener("TabAttrModified", listener);
  return {
    hasEverUpdated: () => {
      tab.removeEventListener("TabAttrModified", listener);
      return hasEverUpdated;
    },
  };
}

/**
 * Sythesize mouse hover on the given icon, which would sythesize `mouseover`
 * and `mousemove` event on that. Return a promise that will be resolved when
 * the tooptip element shows.
 * @param {tab icon} icon
 *        the icon on which we want to mouse hover
 * @param {tooltip element} tooltip
 *        the tab tooltip elementss
 */
function hoverIcon(icon, tooltip) {
  disableNonTestMouse(true);

  if (!tooltip) {
    tooltip = document.getElementById("tabbrowser-tab-tooltip");
  }

  let popupShownPromise = BrowserTestUtils.waitForEvent(tooltip, "popupshown");
  EventUtils.synthesizeMouse(icon, 1, 1, { type: "mouseover" });
  EventUtils.synthesizeMouse(icon, 2, 2, { type: "mousemove" });
  EventUtils.synthesizeMouse(icon, 3, 3, { type: "mousemove" });
  EventUtils.synthesizeMouse(icon, 4, 4, { type: "mousemove" });
  return popupShownPromise;
}

/**
 * Leave mouse from the given icon, which would sythesize `mouseout`
 * and `mousemove` event on that.
 * @param {tab icon} icon
 *        the icon on which we want to mouse hover
 * @param {tooltip element} tooltip
 *        the tab tooltip elementss
 */
function leaveIcon(icon) {
  EventUtils.synthesizeMouse(icon, 0, 0, { type: "mouseout" });
  EventUtils.synthesizeMouseAtCenter(document.documentElement, {
    type: "mousemove",
  });
  EventUtils.synthesizeMouseAtCenter(document.documentElement, {
    type: "mousemove",
  });
  EventUtils.synthesizeMouseAtCenter(document.documentElement, {
    type: "mousemove",
  });

  disableNonTestMouse(false);
}

/**
 * Sythesize mouse click on the given icon.
 * @param {tab icon} icon
 *        the icon on which we want to mouse hover
 */
async function clickIcon(icon) {
  await hoverIcon(icon);
  EventUtils.synthesizeMouseAtCenter(icon, { button: 0 });
  leaveIcon(icon);
}

function disableNonTestMouse(disable) {
  let utils = window.windowUtils;
  utils.disableNonTestMouseEvents(disable);
}