summaryrefslogtreecommitdiffstats
path: root/toolkit/components/pictureinpicture/tests/browser_saveLastPiPLoc.js
blob: 1e787dafc7cd58b655e43df45b673378f7406f0d (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * This function tests that the browser saves the last location of size of
 * the PiP window and will open the next PiP window in the same location
 * with the size. It adjusts for aspect ratio by keeping the same height and
 * adjusting the width of the PiP window.
 */
async function doTest() {
  await BrowserTestUtils.withNewTab(
    {
      url: TEST_PAGE,
      gBrowser,
    },
    // Function to switch video source.
    async browser => {
      async function switchVideoSource(src) {
        let videoResized = BrowserTestUtils.waitForEvent(pipWin, "resize");
        await ContentTask.spawn(browser, { src }, async ({ src }) => {
          let doc = content.document;
          let video = doc.getElementById("with-controls");
          video.src = src;
        });
        await videoResized;
      }

      function getAvailScreenSize(screen) {
        let screenLeft = {},
          screenTop = {},
          screenWidth = {},
          screenHeight = {};
        screen.GetAvailRectDisplayPix(
          screenLeft,
          screenTop,
          screenWidth,
          screenHeight
        );

        // We have to divide these dimensions by the CSS scale factor for the
        // display in order for the video to be positioned correctly on displays
        // that are not at a 1.0 scaling.
        let scaleFactor =
          screen.contentsScaleFactor / screen.defaultCSSScaleFactor;
        screenWidth.value *= scaleFactor;
        screenHeight.value *= scaleFactor;
        screenLeft.value *= scaleFactor;
        screenTop.value *= scaleFactor;

        return [
          screenLeft.value,
          screenTop.value,
          screenWidth.value,
          screenHeight.value,
        ];
      }

      let screen = Cc["@mozilla.org/gfx/screenmanager;1"]
        .getService(Ci.nsIScreenManager)
        .screenForRect(1, 1, 1, 1);

      let [defaultX, defaultY, defaultWidth, defaultHeight] =
        getAvailScreenSize(screen);

      // Default size of PiP window
      let rightEdge = defaultX + defaultWidth;
      let bottomEdge = defaultY + defaultHeight;

      // tab height
      // Used only for Linux as the PiP window has a tab
      let tabHeight = 35;

      // clear already saved information
      clearSavedPosition();

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

      let defaultPiPWidth = pipWin.innerWidth;
      let defaultPiPHeight = pipWin.innerHeight;

      // Check that it is opened at default location
      isfuzzy(
        pipWin.screenX,
        rightEdge - defaultPiPWidth,
        ACCEPTABLE_DIFFERENCE,
        "Default PiP X location"
      );
      if (AppConstants.platform == "linux") {
        isfuzzy(
          pipWin.screenY,
          bottomEdge - defaultPiPHeight - tabHeight,
          ACCEPTABLE_DIFFERENCE,
          "Default PiP Y location"
        );
      } else {
        isfuzzy(
          pipWin.screenY,
          bottomEdge - defaultPiPHeight,
          ACCEPTABLE_DIFFERENCE,
          "Default PiP Y location"
        );
      }
      isfuzzy(
        pipWin.innerHeight,
        defaultPiPHeight,
        ACCEPTABLE_DIFFERENCE,
        "Default PiP height"
      );
      isfuzzy(
        pipWin.innerWidth,
        defaultPiPWidth,
        ACCEPTABLE_DIFFERENCE,
        "Default PiP width"
      );

      let top = defaultY;
      let left = defaultX;
      pipWin.moveTo(left, top);
      let height = pipWin.innerHeight / 2;
      let width = pipWin.innerWidth / 2;
      pipWin.resizeTo(width, height);

      // CLose first PiP window and open another
      await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
      pipWin = await triggerPictureInPicture(browser, "with-controls");
      ok(pipWin, "Got Picture-in-Picture window.");

      // PiP is opened at 0, 0 with size 1/4 default width and 1/4 default height
      isfuzzy(
        pipWin.screenX,
        left,
        ACCEPTABLE_DIFFERENCE,
        "Opened at last X location"
      );
      isfuzzy(
        pipWin.screenY,
        top,
        ACCEPTABLE_DIFFERENCE,
        "Opened at last Y location"
      );
      isfuzzy(
        pipWin.innerHeight,
        height,
        ACCEPTABLE_DIFFERENCE,
        "Opened with 1/2 default height"
      );
      isfuzzy(
        pipWin.innerWidth,
        width,
        ACCEPTABLE_DIFFERENCE,
        "Opened with 1/2 default width"
      );

      // Mac and Linux did not allow moving to coordinates offscreen so this
      // test is skipped on those platforms
      if (AppConstants.platform == "win") {
        // Move to -1111, -1111 and adjust size to 1/4 width and 1/4 height
        left = -11111;
        top = -11111;
        pipWin.moveTo(left, top);
        pipWin.resizeTo(pipWin.innerWidth / 4, pipWin.innerHeight / 4);

        await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
        pipWin = await triggerPictureInPicture(browser, "with-controls");
        ok(pipWin, "Got Picture-in-Picture window.");

        // because the coordinates are off screen, the default size and location will be used
        isfuzzy(
          pipWin.screenX,
          rightEdge - defaultPiPWidth,
          ACCEPTABLE_DIFFERENCE,
          "Opened at default X location"
        );
        isfuzzy(
          pipWin.screenY,
          bottomEdge - defaultPiPHeight,
          ACCEPTABLE_DIFFERENCE,
          "Opened at default Y location"
        );
        isfuzzy(
          pipWin.innerWidth,
          defaultPiPWidth,
          ACCEPTABLE_DIFFERENCE,
          "Opened at default PiP width"
        );
        isfuzzy(
          pipWin.innerHeight,
          defaultPiPHeight,
          ACCEPTABLE_DIFFERENCE,
          "Opened at default PiP height"
        );
      }

      // Linux doesn't handle switching the video source well and it will
      // cause the tests to failed in unexpected ways. Possibly caused by
      // bug 1594223 https://bugzilla.mozilla.org/show_bug.cgi?id=1594223
      if (AppConstants.platform != "linux") {
        // Save width and height for when aspect ratio is changed
        height = pipWin.innerHeight;
        width = pipWin.innerWidth;

        left = 200;
        top = 100;
        pipWin.moveTo(left, top);

        // Now switch the video so the video ratio is different
        await switchVideoSource("test-video-cropped.mp4");
        await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
        pipWin = await triggerPictureInPicture(browser, "with-controls");
        ok(pipWin, "Got Picture-in-Picture window.");

        isfuzzy(
          pipWin.screenX,
          left,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last X location"
        );
        isfuzzy(
          pipWin.screenY,
          top,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last Y location"
        );
        isfuzzy(
          pipWin.innerHeight,
          height,
          ACCEPTABLE_DIFFERENCE,
          "Opened height with previous width"
        );
        isfuzzy(
          pipWin.innerWidth,
          height * (pipWin.innerWidth / pipWin.innerHeight),
          ACCEPTABLE_DIFFERENCE,
          "Width is changed to adjust for aspect ration"
        );

        left = 300;
        top = 300;
        pipWin.moveTo(left, top);
        pipWin.resizeTo(defaultPiPWidth / 2, defaultPiPHeight / 2);

        // Save height for when aspect ratio is changed
        height = pipWin.innerHeight;

        // Now switch the video so the video ratio is different
        await switchVideoSource("test-video.mp4");
        await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
        pipWin = await triggerPictureInPicture(browser, "with-controls");
        ok(pipWin, "Got Picture-in-Picture window.");

        isfuzzy(
          pipWin.screenX,
          left,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last X location"
        );
        isfuzzy(
          pipWin.screenY,
          top,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last Y location"
        );
        isfuzzy(
          pipWin.innerHeight,
          height,
          ACCEPTABLE_DIFFERENCE,
          "Opened with previous height"
        );
        isfuzzy(
          pipWin.innerWidth,
          height * (pipWin.innerWidth / pipWin.innerHeight),
          ACCEPTABLE_DIFFERENCE,
          "Width is changed to adjust for aspect ration"
        );
      }

      // Move so that part of PiP is off screen (bottom right)

      left = rightEdge - Math.round((3 * pipWin.innerWidth) / 4);
      top = bottomEdge - Math.round((3 * pipWin.innerHeight) / 4);

      let movePromise = BrowserTestUtils.waitForEvent(
        pipWin.windowRoot,
        "MozUpdateWindowPos"
      );
      pipWin.moveTo(left, top);
      await movePromise;

      await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
      pipWin = await triggerPictureInPicture(browser, "with-controls");
      ok(pipWin, "Got Picture-in-Picture window.");

      // Redefine top and left to where the PiP windop will open
      left = rightEdge - pipWin.innerWidth;
      top = bottomEdge - pipWin.innerHeight;

      // await new Promise(r => setTimeout(r, 5000));
      // PiP is opened bottom right but not off screen
      isfuzzy(
        pipWin.screenX,
        left,
        ACCEPTABLE_DIFFERENCE,
        "Opened at last X location but shifted back on screen"
      );
      if (AppConstants.platform == "linux") {
        isfuzzy(
          pipWin.screenY,
          top - tabHeight,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last Y location but shifted back on screen"
        );
      } else {
        isfuzzy(
          pipWin.screenY,
          top,
          ACCEPTABLE_DIFFERENCE,
          "Opened at last Y location but shifted back on screen"
        );
      }

      // Move so that part of PiP is off screen (top left)
      left = defaultX - Math.round(pipWin.innerWidth / 4);
      top = defaultY - Math.round(pipWin.innerHeight / 4);

      movePromise = BrowserTestUtils.waitForEvent(
        pipWin.windowRoot,
        "MozUpdateWindowPos"
      );
      pipWin.moveTo(left, top);
      await movePromise;

      await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
      pipWin = await triggerPictureInPicture(browser, "with-controls");
      ok(pipWin, "Got Picture-in-Picture window.");

      // PiP is opened top left on screen
      isfuzzy(
        pipWin.screenX,
        defaultX,
        ACCEPTABLE_DIFFERENCE,
        "Opened at last X location but shifted back on screen"
      );
      isfuzzy(
        pipWin.screenY,
        defaultY,
        ACCEPTABLE_DIFFERENCE,
        "Opened at last Y location but shifted back on screen"
      );

      if (AppConstants.platform != "linux") {
        // test that if video is on right edge and new video with smaller width
        // is opened next, it is still on the right edge
        left = rightEdge - pipWin.innerWidth;
        top = Math.round(bottomEdge / 4);

        pipWin.moveTo(left, top);

        // Used to ensure that video width decreases for next PiP window
        width = pipWin.innerWidth;
        isfuzzy(
          pipWin.innerWidth + pipWin.screenX,
          rightEdge,
          ACCEPTABLE_DIFFERENCE,
          "Video is on right edge before video is changed"
        );

        // Now switch the video so the video width is smaller
        await switchVideoSource("test-video-cropped.mp4");
        await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
        pipWin = await triggerPictureInPicture(browser, "with-controls");
        ok(pipWin, "Got Picture-in-Picture window.");

        Assert.less(pipWin.innerWidth, width, "New video width is smaller");
        isfuzzy(
          pipWin.innerWidth + pipWin.screenX,
          rightEdge,
          ACCEPTABLE_DIFFERENCE,
          "Video is on right edge after video is changed"
        );
      }

      await ensureMessageAndClosePiP(browser, "with-controls", pipWin, true);
    }
  );
}

add_task(async function test_pip_save_last_loc() {
  await doTest();
});

add_task(async function test_pip_save_last_loc_with_os_zoom() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.textScaleFactor", 120]] });
  await doTest();
});