summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_resizeVideo.js
blob: 9254ca10cca488f79207afd447b3fb5e2af09b97 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Global values for the left and top edge pixel coordinates. These will be written to
// during the add_setup function in this test file.
let gLeftEdge = 0;
let gTopEdge = 0;

/**
 * Run the resize test on a player window.
 *
 * @param browser (xul:browser)
 *   The browser that has the source video.
 *
 * @param videoID (string)
 *   The id of the video in the browser to test.
 *
 * @param pipWin (player window)
 *   A player window to run the tests on.
 *
 * @param opts (object)
 *   The options for the test.
 *
 *   pinX (boolean):
 *     If true, the video's X position shouldn't change when resized.
 *
 *   pinY (boolean):
 *     If true, the video's Y position shouldn't change when resized.
 */
async function testVideo(browser, videoID, pipWin, { pinX, pinY } = {}) {
  async function switchVideoSource(src) {
    let videoResized = BrowserTestUtils.waitForEvent(pipWin, "resize");
    await ContentTask.spawn(
      browser,
      { src, videoID },
      async ({ src, videoID }) => {
        let doc = content.document;
        let video = doc.getElementById(videoID);
        video.src = src;
      }
    );
    await videoResized;
  }

  /**
   * Check the new screen position against the previous one. When
   * pinX or pinY is true then the top left corner is checked in that
   * dimension. Otherwise, the bottom right corner is checked.
   *
   * The video position is determined by the screen edge it's closest
   * to, so in the default bottom right its bottom right corner should
   * match the previous video's bottom right corner. For the top left,
   * the top left corners should match.
   */
  function checkPosition(
    previousScreenX,
    previousScreenY,
    previousWidth,
    previousHeight,
    newScreenX,
    newScreenY,
    newWidth,
    newHeight
  ) {
    if (pinX || previousScreenX == gLeftEdge) {
      Assert.equal(
        previousScreenX,
        newScreenX,
        "New video is still in the same X position"
      );
    } else {
      Assert.less(
        Math.abs(previousScreenX + previousWidth - (newScreenX + newWidth)),
        2,
        "New video ends at the same screen X position (within 1 pixel)"
      );
    }
    if (pinY) {
      Assert.equal(
        previousScreenY,
        newScreenY,
        "New video is still in the same Y position"
      );
    } else {
      Assert.equal(
        previousScreenY + previousHeight,
        newScreenY + newHeight,
        "New video ends at the same screen Y position"
      );
    }
  }

  Assert.ok(pipWin, "Got PiP window.");

  let initialWidth = pipWin.innerWidth;
  let initialHeight = pipWin.innerHeight;
  let initialAspectRatio = initialWidth / initialHeight;
  Assert.equal(
    Math.floor(initialAspectRatio * 100),
    177, // 16 / 9 = 1.777777777
    "Original aspect ratio is 16:9"
  );

  // Store the window position for later.
  let initialScreenX = pipWin.mozInnerScreenX;
  let initialScreenY = pipWin.mozInnerScreenY;

  await switchVideoSource("test-video-cropped.mp4");

  let resizedWidth = pipWin.innerWidth;
  let resizedHeight = pipWin.innerHeight;
  let resizedAspectRatio = resizedWidth / resizedHeight;
  Assert.equal(
    Math.floor(resizedAspectRatio * 100),
    133, // 4 / 3 = 1.333333333
    "Resized aspect ratio is 4:3"
  );
  Assert.less(resizedWidth, initialWidth, "Resized video has smaller width");
  Assert.equal(
    resizedHeight,
    initialHeight,
    "Resized video is the same vertically"
  );

  let resizedScreenX = pipWin.mozInnerScreenX;
  let resizedScreenY = pipWin.mozInnerScreenY;
  checkPosition(
    initialScreenX,
    initialScreenY,
    initialWidth,
    initialHeight,
    resizedScreenX,
    resizedScreenY,
    resizedWidth,
    resizedHeight
  );

  await switchVideoSource("test-video-vertical.mp4");

  let verticalWidth = pipWin.innerWidth;
  let verticalHeight = pipWin.innerHeight;
  let verticalAspectRatio = verticalWidth / verticalHeight;

  if (verticalWidth == 136) {
    // The video is minimun width allowed
    Assert.equal(
      Math.floor(verticalAspectRatio * 100),
      56, // 1 / 2 = 0.5
      "Vertical aspect ratio is 1:2"
    );
  } else {
    Assert.equal(
      Math.floor(verticalAspectRatio * 100),
      50, // 1 / 2 = 0.5
      "Vertical aspect ratio is 1:2"
    );
  }

  Assert.less(verticalWidth, resizedWidth, "Vertical video width shrunk");
  Assert.equal(
    verticalHeight,
    initialHeight,
    "Vertical video height matches previous height"
  );

  let verticalScreenX = pipWin.mozInnerScreenX;
  let verticalScreenY = pipWin.mozInnerScreenY;
  checkPosition(
    resizedScreenX,
    resizedScreenY,
    resizedWidth,
    resizedHeight,
    verticalScreenX,
    verticalScreenY,
    verticalWidth,
    verticalHeight
  );

  await switchVideoSource("test-video.mp4");

  let restoredWidth = pipWin.innerWidth;
  let restoredHeight = pipWin.innerHeight;
  let restoredAspectRatio = restoredWidth / restoredHeight;
  Assert.equal(
    Math.floor(restoredAspectRatio * 100),
    177,
    "Restored aspect ratio is still 16:9"
  );
  Assert.less(
    Math.abs(initialWidth - pipWin.innerWidth),
    2,
    "Restored video has its original width"
  );
  Assert.equal(
    initialHeight,
    pipWin.innerHeight,
    "Restored video has its original height"
  );

  let restoredScreenX = pipWin.mozInnerScreenX;
  let restoredScreenY = pipWin.mozInnerScreenY;
  checkPosition(
    initialScreenX,
    initialScreenY,
    initialWidth,
    initialHeight,
    restoredScreenX,
    restoredScreenY,
    restoredWidth,
    restoredHeight
  );
}

add_setup(async () => {
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    async browser => {
      // Reset the saved PiP location to top-left edge of the screen, wherever
      // that may be. We record the top-left edge of the screen coordinates into
      // global variables to do later coordinate comparisons after resizes.
      let clearWin = await triggerPictureInPicture(browser, "with-controls");
      let initialScreenX = clearWin.mozInnerScreenX;
      let initialScreenY = clearWin.mozInnerScreenY;
      let PiPScreen = PictureInPicture.getWorkingScreen(
        initialScreenX,
        initialScreenY
      );
      [gLeftEdge, gTopEdge] = PictureInPicture.getAvailScreenSize(PiPScreen);
      clearWin.moveTo(gLeftEdge, gTopEdge);

      await BrowserTestUtils.closeWindow(clearWin);
    }
  );
});

/**
 * Tests that if a <video> element is resized the Picture-in-Picture window
 * will be resized to match the new dimensions.
 */
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 pipWin = await triggerPictureInPicture(browser, videoID);

        await testVideo(browser, videoID, pipWin);

        pipWin.moveTo(gLeftEdge, gTopEdge);

        await testVideo(browser, videoID, pipWin, { pinX: true, pinY: true });

        await BrowserTestUtils.closeWindow(pipWin);
      }
    );
  }
});

/**
 * Tests that the RTL video starts on the left and is pinned in the X dimension.
 */
add_task(async () => {
  await SpecialPowers.pushPrefEnv({ set: [["intl.l10n.pseudo", "bidi"]] });

  for (let videoID of ["with-controls", "no-controls"]) {
    info(`Testing ${videoID} case.`);

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE,
        gBrowser,
      },
      async browser => {
        let pipWin = await triggerPictureInPicture(browser, videoID);

        await testVideo(browser, videoID, pipWin, { pinX: true });

        await BrowserTestUtils.closeWindow(pipWin);
      }
    );
  }
  await SpecialPowers.popPrefEnv();
});