summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_improved_controls.js
blob: deadc8d53b85cb65a47304366e2b3237f2792949 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { TelemetryTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/TelemetryTestUtils.sys.mjs"
);

const TEST_PAGE_LONG = TEST_ROOT + "test-video-selection.html";

const IMPROVED_CONTROLS_ENABLED_PREF =
  "media.videocontrols.picture-in-picture.improved-video-controls.enabled";

async function getVideoCurrentTime(browser, videoID) {
  return SpecialPowers.spawn(browser, [videoID], async videoID => {
    return content.document.getElementById(videoID).currentTime;
  });
}

async function getVideoDuration(browser, videoID) {
  return SpecialPowers.spawn(browser, [videoID], async videoID => {
    return content.document.getElementById(videoID).duration;
  });
}

async function timestampUpdated(timestampEl, expectedTimestamp) {
  await BrowserTestUtils.waitForMutationCondition(
    timestampEl,
    { childList: true },
    () => {
      return expectedTimestamp === timestampEl.textContent;
    }
  );
}

function checkTimeCloseEnough(actual, expected, message) {
  let equal = Math.abs(actual - expected);
  if (equal <= 0.5) {
    is(equal <= 0.5, true, message);
  } else {
    is(actual, expected, message);
  }
}

/**
 * Tests the functionality of improved Picture-in-picture
 * playback controls.
 */
add_task(async () => {
  let videoID = "with-controls";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      let waitForVideoEvent = eventType => {
        return BrowserTestUtils.waitForContentEvent(browser, eventType, true);
      };

      await ensureVideosReady(browser);
      await SpecialPowers.spawn(browser, [videoID], async videoID => {
        await content.document.getElementById(videoID).play();
      });

      await SpecialPowers.pushPrefEnv({
        set: [[IMPROVED_CONTROLS_ENABLED_PREF, true]],
      });

      // Open the video in PiP
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      let fullscreenButton = pipWin.document.getElementById("fullscreen");
      let seekForwardButton = pipWin.document.getElementById("seekForward");
      let seekBackwardButton = pipWin.document.getElementById("seekBackward");

      // Try seek forward button
      let seekedForwardPromise = waitForVideoEvent("seeked");
      EventUtils.synthesizeMouseAtCenter(seekForwardButton, {}, pipWin);
      ok(await seekedForwardPromise, "The Forward button triggers");

      // Try seek backward button
      let seekedBackwardPromise = waitForVideoEvent("seeked");
      EventUtils.synthesizeMouseAtCenter(seekBackwardButton, {}, pipWin);
      ok(await seekedBackwardPromise, "The Backward button triggers");

      // The Fullsreen button appears when the pref is enabled and the fullscreen hidden property is set to false
      Assert.ok(!fullscreenButton.hidden, "The Fullscreen button is visible");

      // The seek Forward button appears when the pref is enabled and the seek forward button hidden property is set to false
      Assert.ok(!seekForwardButton.hidden, "The Forward button is visible");

      // The seek Backward button appears when the pref is enabled and the seek backward button hidden property is set to false
      Assert.ok(!seekBackwardButton.hidden, "The Backward button is visible");

      // CLose the PIP window
      let pipClosed = BrowserTestUtils.domWindowClosed(pipWin);
      let closeButton = pipWin.document.getElementById("close");
      EventUtils.synthesizeMouseAtCenter(closeButton, {}, pipWin);
      await pipClosed;

      await SpecialPowers.pushPrefEnv({
        set: [[IMPROVED_CONTROLS_ENABLED_PREF, false]],
      });

      // Open the video in PiP
      pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      fullscreenButton = pipWin.document.getElementById("fullscreen");
      seekForwardButton = pipWin.document.getElementById("seekForward");
      seekBackwardButton = pipWin.document.getElementById("seekBackward");

      // The Fullsreen button disappears when the pref is disabled and the fullscreen hidden property is set to true
      Assert.ok(
        fullscreenButton.hidden,
        "The Fullscreen button is not visible"
      );

      // The seek Forward button disappears when the pref is disabled and the seek forward button hidden property is set to true
      Assert.ok(seekForwardButton.hidden, "The Forward button is not visible");

      // The seek Backward button disappears when the pref is disabled and the seek backward button hidden property is set to true
      Assert.ok(
        seekBackwardButton.hidden,
        "The Backward button is not visible"
      );
    }
  );
});

/**
 * Tests the functionality of Picture-in-picture
 * video scrubber
 */
add_task(async function testVideoScrubber() {
  let videoID = "long";

  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE_LONG,
      gBrowser,
    },
    async browser => {
      await ensureVideosReady(browser);

      await SpecialPowers.pushPrefEnv({
        set: [[IMPROVED_CONTROLS_ENABLED_PREF, true]],
      });

      // Open the video in PiP
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      let scrubber = pipWin.document.getElementById("scrubber");
      scrubber.focus();

      let currentTime = await getVideoCurrentTime(browser, videoID);
      let expectedVideoTime = 0;
      const duration = await getVideoDuration(browser, videoID);
      checkTimeCloseEnough(
        currentTime,
        expectedVideoTime,
        "Video current time is 0"
      );

      let timestampEl = pipWin.document.getElementById("timestamp");
      let expectedTimestamp = "0:00 / 0:08";

      // Wait for the timestamp to update
      await timestampUpdated(timestampEl, expectedTimestamp);
      let actualTimestamp = timestampEl.textContent;
      is(actualTimestamp, expectedTimestamp, "Timestamp reads 0:00 / 0:08");

      EventUtils.synthesizeKey("KEY_ArrowRight", {}, pipWin);

      currentTime = await getVideoCurrentTime(browser, videoID);
      expectedVideoTime = 5;
      checkTimeCloseEnough(
        currentTime,
        expectedVideoTime,
        "Video current time is 5"
      );

      expectedTimestamp = "0:05 / 0:08";
      await timestampUpdated(timestampEl, expectedTimestamp);
      actualTimestamp = timestampEl.textContent;
      is(actualTimestamp, expectedTimestamp, "Timestamp reads 0:05 / 0:08");

      EventUtils.synthesizeKey("KEY_ArrowLeft", {}, pipWin);

      currentTime = await getVideoCurrentTime(browser, videoID);
      expectedVideoTime = 0;
      checkTimeCloseEnough(
        currentTime,
        expectedVideoTime,
        "Video current time is 0"
      );

      expectedTimestamp = "0:00 / 0:08";
      await timestampUpdated(timestampEl, expectedTimestamp);
      actualTimestamp = timestampEl.textContent;
      is(actualTimestamp, expectedTimestamp, "Timestamp reads 0:00 / 0:08");

      let rect = scrubber.getBoundingClientRect();

      EventUtils.synthesizeMouse(
        scrubber,
        rect.width / 2,
        rect.height / 2,
        {},
        pipWin
      );

      expectedVideoTime = duration / 2;
      currentTime = await getVideoCurrentTime(browser, videoID);
      checkTimeCloseEnough(
        currentTime,
        expectedVideoTime,
        "Video current time is 3.98..."
      );

      expectedTimestamp = "0:04 / 0:08";
      await timestampUpdated(timestampEl, expectedTimestamp);
      actualTimestamp = timestampEl.textContent;
      is(actualTimestamp, expectedTimestamp, "Timestamp reads 0:04 / 0:08");

      EventUtils.synthesizeMouse(
        scrubber,
        rect.width / 2,
        rect.height / 2,
        { type: "mousedown" },
        pipWin
      );

      EventUtils.synthesizeMouse(
        scrubber,
        rect.width,
        rect.height / 2,
        { type: "mousemove" },
        pipWin
      );

      EventUtils.synthesizeMouse(
        scrubber,
        rect.width,
        rect.height / 2,
        { type: "mouseup" },
        pipWin
      );

      expectedVideoTime = duration;
      currentTime = await getVideoCurrentTime(browser, videoID);
      checkTimeCloseEnough(
        currentTime,
        expectedVideoTime,
        "Video current time is 7.96..."
      );

      await ensureMessageAndClosePiP(browser, videoID, pipWin, false);
    }
  );
});

/**
 * Tests the behavior of the scrubber and position/duration indicator for a
 * video with an invalid/non-finite duration.
 */
add_task(async function testInvalidDuration() {
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE_WITH_NAN_VIDEO_DURATION,
      gBrowser,
    },
    async browser => {
      const videoID = "nan-duration";

      // This tests skips calling ensureVideosReady, because canplaythrough
      // will never fire for the NaN duration video.

      await SpecialPowers.pushPrefEnv({
        set: [[IMPROVED_CONTROLS_ENABLED_PREF, true]],
      });

      // Open the video in PiP
      let pipWin = await triggerPictureInPicture(browser, videoID);
      ok(pipWin, "Got Picture-in-Picture window.");

      // Both the scrubber and the duration should be hidden.
      let timestampEl = pipWin.document.getElementById("timestamp");
      ok(timestampEl.hidden, "Timestamp in the PIP window should be hidden.");

      let scrubberEl = pipWin.document.getElementById("scrubber");
      ok(
        scrubberEl.hidden,
        "Scrubber control in the PIP window should be hidden"
      );
    }
  );
});