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

"use strict";

/**
 * Tests that the Picture-in-Picture toggle is hidden when videos
 * are laid out with dimensions smaller than MIN_VIDEO_DIMENSION (a
 * constant that is also defined in videocontrols.js).
 */
add_task(async () => {
  // Most of the Picture-in-Picture tests run with the always-show
  // preference set to true to avoid the toggle visibility heuristics.
  // Since this test actually exercises those heuristics, we have
  // to temporarily disable that pref.
  //
  // We also reduce the minimum video length for displaying the toggle
  // to 5 seconds to avoid having to include or generate a 45 second long
  // video (which is the default minimum length).
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "media.videocontrols.picture-in-picture.video-toggle.always-show",
        false,
      ],
      ["media.videocontrols.picture-in-picture.video-toggle.min-video-secs", 5],
    ],
  });

  // This is the minimum size of the video in either width or height for
  // which we will show the toggle. See videocontrols.js.
  const MIN_VIDEO_DIMENSION = 140; // pixels

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

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE_WITH_SOUND,
        gBrowser,
      },
      async browser => {
        // Shrink the video down to less than MIN_VIDEO_DIMENSION.
        let targetWidth = MIN_VIDEO_DIMENSION - 1;
        await SpecialPowers.spawn(
          browser,
          [videoID, targetWidth],
          async (videoID, targetWidth) => {
            let video = content.document.getElementById(videoID);
            let shadowRoot = video.openOrClosedShadowRoot;
            let resizePromise = ContentTaskUtils.waitForEvent(
              shadowRoot.firstChild,
              "resizevideocontrols"
            );
            video.style.width = targetWidth + "px";
            await resizePromise;
          }
        );

        // The toggle should be hidden.
        await testToggleHelper(browser, videoID, false);

        // Now re-expand the video.
        await SpecialPowers.spawn(browser, [videoID], async videoID => {
          let video = content.document.getElementById(videoID);
          let shadowRoot = video.openOrClosedShadowRoot;
          let resizePromise = ContentTaskUtils.waitForEvent(
            shadowRoot.firstChild,
            "resizevideocontrols"
          );
          video.style.width = "";
          await resizePromise;
        });

        // The toggle should be visible.
        await testToggleHelper(browser, videoID, true);
      }
    );
  }
});

/**
 * Tests that when using the experimental toggle variations, videos
 * under 320px width are given the "small-video" attribute.
 */
add_task(async () => {
  const TOGGLE_SMALL = {
    rootID: "pictureInPictureToggle",
    stages: {
      hoverVideo: {
        opacities: {
          ".pip-wrapper": DEFAULT_TOGGLE_OPACITY,
        },
        hidden: [],
      },

      hoverToggle: {
        opacities: {
          ".pip-wrapper": 1.0,
        },
        hidden: [".pip-expanded"],
      },
    },
  };

  const TOGGLE_LARGE = {
    rootID: "pictureInPictureToggle",
    stages: {
      hoverVideo: {
        opacities: {
          ".pip-small": 0.0,
          ".pip-wrapper": DEFAULT_TOGGLE_OPACITY,
          ".pip-expanded": 0.0,
        },
        hidden: [".pip-explainer", ".pip-icon-label > .pip-icon"],
      },
      hoverToggle: {
        opacities: {
          ".pip-small": 0.0,
          ".pip-wrapper": 1.0,
        },
        hidden: [
          ".pip-explainer",
          ".pip-icon-label > .pip-icon",
          ".pip-expanded",
        ],
      },
    },
  };

  // Most of the Picture-in-Picture tests run with the always-show
  // preference set to true to avoid the toggle visibility heuristics.
  // Since this test actually exercises those heuristics, we have
  // to temporarily disable that pref.
  //
  // We also reduce the minimum video length for displaying the toggle
  // to 5 seconds to avoid having to include or generate a 45 second long
  // video (which is the default minimum length).
  await SpecialPowers.pushPrefEnv({
    set: [
      [
        "media.videocontrols.picture-in-picture.video-toggle.always-show",
        false,
      ],
      ["media.videocontrols.picture-in-picture.video-toggle.min-video-secs", 5],
      ["media.videocontrols.picture-in-picture.video-toggle.mode", 1],
    ],
  });

  // Videos that are thinner than MIN_VIDEO_WIDTH should have the small-video
  // attribute set on the experimental toggle.
  const MIN_VIDEO_WIDTH = 320; // pixels

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

    await BrowserTestUtils.withNewTab(
      {
        url: TEST_PAGE_WITH_SOUND,
        gBrowser,
      },
      async browser => {
        // Shrink the video down to less than MIN_VIDEO_WIDTH.
        let targetWidth = MIN_VIDEO_WIDTH - 1;
        let isSmallVideo = await SpecialPowers.spawn(
          browser,
          [videoID, targetWidth],
          async (videoID, targetWidth) => {
            let video = content.document.getElementById(videoID);
            let shadowRoot = video.openOrClosedShadowRoot;
            let resizePromise = ContentTaskUtils.waitForEvent(
              shadowRoot.firstChild,
              "resizevideocontrols"
            );
            video.style.width = targetWidth + "px";
            await resizePromise;
            let toggle = shadowRoot.getElementById("pictureInPictureToggle");
            return toggle.hasAttribute("small-video");
          }
        );

        Assert.ok(isSmallVideo, "Video should have small-video attribute");

        await testToggleHelper(browser, videoID, true, undefined, TOGGLE_SMALL);

        // Now re-expand the video.
        isSmallVideo = await SpecialPowers.spawn(
          browser,
          [videoID],
          async videoID => {
            let video = content.document.getElementById(videoID);
            let shadowRoot = video.openOrClosedShadowRoot;
            let resizePromise = ContentTaskUtils.waitForEvent(
              shadowRoot.firstChild,
              "resizevideocontrols"
            );
            video.style.width = "";
            await resizePromise;
            let toggle = shadowRoot.getElementById("pictureInPictureToggle");
            return toggle.hasAttribute("small-video");
          }
        );

        Assert.ok(!isSmallVideo, "Video should not have small-video attribute");

        await testToggleHelper(browser, videoID, true, undefined, TOGGLE_LARGE);
      }
    );
  }
});