summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_contextMenu.js
blob: ce6a85e91c721f00a3945ece48c7ba2ed9e7d636 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Opens up the content area context menu on a video loaded in a
 * browser.
 *
 * @param {Element} browser The <xul:browser> hosting the <video>
 *
 * @param {String} videoID The ID of the video to open the context
 * menu with.
 *
 * @returns Promise
 * @resolves With the context menu DOM node once opened.
 */
async function openContextMenu(browser, videoID) {
  let contextMenu = document.getElementById("contentAreaContextMenu");
  let popupShownPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popupshown"
  );
  await BrowserTestUtils.synthesizeMouseAtCenter(
    "#" + videoID,
    { type: "contextmenu", button: 2 },
    browser
  );
  await popupShownPromise;
  return contextMenu;
}

/**
 * Closes the content area context menu.
 *
 * @param {Element} contextMenu The content area context menu opened with
 * openContextMenu.
 *
 * @returns Promise
 * @resolves With undefined
 */
async function closeContextMenu(contextMenu) {
  let popupHiddenPromise = BrowserTestUtils.waitForEvent(
    contextMenu,
    "popuphidden"
  );
  contextMenu.hidePopup();
  await popupHiddenPromise;
}

/**
 * Tests that Picture-in-Picture can be opened and closed through the
 * context menu
 */
add_task(async () => {
  for (const videoId of ["with-controls", "no-controls"]) {
    info(`Testing ${videoId} case.`);

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE,
        gBrowser,
      },
      async browser => {
        let contextMenu = await openContextMenu(browser, videoId);

        info("Context menu is open.");

        const pipMenuItemId = "context-video-pictureinpicture";
        let menuItem = document.getElementById(pipMenuItemId);

        Assert.ok(
          !menuItem.hidden,
          "Should show Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "false",
          "Picture-in-Picture should be unchecked."
        );

        contextMenu.activateItem(menuItem);

        await SpecialPowers.spawn(browser, [videoId], async videoID => {
          let video = content.document.getElementById(videoID);
          await ContentTaskUtils.waitForCondition(() => {
            return video.isCloningElementVisually;
          }, "Video has started being cloned.");
        });

        info("PiP player is now open.");

        contextMenu = await openContextMenu(browser, videoId);

        info("Context menu is open again.");

        contextMenu.activateItem(menuItem);

        await SpecialPowers.spawn(browser, [videoId], async videoID => {
          let video = content.document.getElementById(videoID);
          await ContentTaskUtils.waitForCondition(() => {
            return !video.isCloningElementVisually;
          }, "Video has stopped being cloned.");
        });
      }
    );
  }
});

/**
 * Tests that the Picture-in-Picture context menu is correctly updated
 * based on the Picture-in-Picture state of the video.
 */
add_task(async () => {
  for (let videoID of ["with-controls", "no-controls"]) {
    info(`Testing ${videoID} case.`);

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE,
        gBrowser,
      },
      async browser => {
        let menuItem = document.getElementById(
          "context-video-pictureinpicture"
        );
        let menu = await openContextMenu(browser, videoID);
        Assert.ok(
          !menuItem.hidden,
          "Should show Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "false",
          "Picture-in-Picture should be unchecked."
        );
        await closeContextMenu(menu);

        let pipWin = await triggerPictureInPicture(browser, videoID);
        ok(pipWin, "Got Picture-in-Picture window.");

        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          let video = content.document.getElementById(videoID);
          await ContentTaskUtils.waitForCondition(() => {
            return video.isCloningElementVisually;
          }, "Video has started being cloned.");
        });

        menu = await openContextMenu(browser, videoID);
        Assert.ok(
          !menuItem.hidden,
          "Should show Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "true",
          "Picture-in-Picture should be checked."
        );
        await closeContextMenu(menu);

        let videoNotCloning = SpecialPowers.spawn(
          browser,
          [videoID],
          async videoID => {
            let video = content.document.getElementById(videoID);
            await ContentTaskUtils.waitForCondition(() => {
              return !video.isCloningElementVisually;
            }, "Video has stopped being cloned.");
          }
        );
        pipWin.close();
        await videoNotCloning;

        menu = await openContextMenu(browser, videoID);
        Assert.ok(
          !menuItem.hidden,
          "Should show Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "false",
          "Picture-in-Picture should be unchecked."
        );
        await closeContextMenu(menu);

        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          // Construct a new video element, and capture a stream from it
          // to redirect to the video that we're testing.
          let newVideo = content.document.createElement("video");
          content.document.body.appendChild(newVideo);

          let testedVideo = content.document.getElementById(videoID);
          newVideo.src = testedVideo.src;

          testedVideo.srcObject = newVideo.mozCaptureStream();
          await newVideo.play();
          await testedVideo.play();

          await newVideo.pause();
          await testedVideo.pause();
        });
        menu = await openContextMenu(browser, videoID);
        Assert.ok(
          !menuItem.hidden,
          "Should be showing Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "false",
          "Picture-in-Picture should be unchecked."
        );
        await closeContextMenu(menu);

        pipWin = await triggerPictureInPicture(browser, videoID);
        ok(pipWin, "Got Picture-in-Picture window.");

        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          let video = content.document.getElementById(videoID);
          await ContentTaskUtils.waitForCondition(() => {
            return video.isCloningElementVisually;
          }, "Video has started being cloned.");
        });

        menu = await openContextMenu(browser, videoID);
        Assert.ok(
          !menuItem.hidden,
          "Should show Picture-in-Picture menu item."
        );
        Assert.equal(
          menuItem.getAttribute("checked"),
          "true",
          "Picture-in-Picture should be checked."
        );
        await closeContextMenu(menu);
      }
    );
  }
});