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

"use strict";

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

const videoID = "with-controls";

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

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

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

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

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

      let ratio = pipWin.innerWidth / pipWin.innerHeight;
      let height = 750;
      let width = Math.floor(ratio * height);
      if (pipWin.innerHeight < height || pipWin.innerWidth < width) {
        // Resize the PiP window so we know the audio scrubber is visible
        let resizePromise = BrowserTestUtils.waitForEvent(pipWin, "resize");
        pipWin.resizeTo(width, height);
        await resizePromise;
      }

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

      // Volume should be 1 and video should not be muted when opening this video
      let actualVolume = await getVideoVolume(browser, videoID);
      let expectedVolume = 1;

      let actualMuted = await getVideoMuted(browser, videoID);

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      ok(!actualMuted, "Video is not muted");

      let volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );

      // Test that left arrow key changes volume by -0.1
      EventUtils.synthesizeKey("KEY_ArrowLeft", {}, pipWin);

      await volumeChangePromise;

      actualVolume = await getVideoVolume(browser, videoID);
      expectedVolume = 0.9;

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      // Test that right arrow key changes volume by +0.1 and does not exceed 1
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, pipWin);
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, pipWin);
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, pipWin);

      actualVolume = await getVideoVolume(browser, videoID);
      expectedVolume = 1;

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      // Test that the mouse can move the audio scrubber to 0.5
      let rect = audioScrubber.getBoundingClientRect();

      volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );

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

      await volumeChangePromise;

      actualVolume = await getVideoVolume(browser, videoID);
      expectedVolume = 0.5;

      isfuzzy(
        actualVolume,
        expectedVolume,
        0.01,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      // Test muting and unmuting the video
      let muteButton = pipWin.document.getElementById("audio");
      volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );
      muteButton.click();
      await volumeChangePromise;

      ok(
        await getVideoMuted(browser, videoID),
        "The video is muted aftering clicking the mute button"
      );
      is(
        audioScrubber.max,
        "0",
        "The max of the audio scrubber is 0, so the volume slider appears that the volume is 0"
      );

      volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );
      muteButton.click();
      await volumeChangePromise;

      ok(
        !(await getVideoMuted(browser, videoID)),
        "The video is muted aftering clicking the mute button"
      );
      isfuzzy(
        actualVolume,
        expectedVolume,
        0.01,
        `The volume is still ${actualVolume}.`
      );

      volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );

      // Test that the audio scrubber can be dragged from 0.5 to 0 and the video gets muted
      EventUtils.synthesizeMouse(
        audioScrubber,
        rect.width / 2,
        rect.height / 2,
        { type: "mousedown" },
        pipWin
      );
      EventUtils.synthesizeMouse(
        audioScrubber,
        0,
        rect.height / 2,
        { type: "mousemove" },
        pipWin
      );
      EventUtils.synthesizeMouse(
        audioScrubber,
        0,
        rect.height / 2,
        { type: "mouseup" },
        pipWin
      );

      await volumeChangePromise;

      actualVolume = await getVideoVolume(browser, videoID);
      expectedVolume = 0;

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      ok(
        await getVideoMuted(browser, videoID),
        "Video is now muted because slider was moved to 0"
      );

      // Test that the left arrow key does not unmute the video and the volume remains at 0
      EventUtils.synthesizeKey("KEY_ArrowLeft", {}, pipWin);
      EventUtils.synthesizeKey("KEY_ArrowLeft", {}, pipWin);

      actualVolume = await getVideoVolume(browser, videoID);

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      ok(
        await getVideoMuted(browser, videoID),
        "Video is now muted because slider is still at 0"
      );

      volumeChangePromise = BrowserTestUtils.waitForContentEvent(
        browser,
        "volumechange",
        true
      );

      // Test that the right arrow key will increase the volume by +0.1 and will unmute the video
      EventUtils.synthesizeKey("KEY_ArrowRight", {}, pipWin);

      await volumeChangePromise;

      actualVolume = await getVideoVolume(browser, videoID);
      expectedVolume = 0.1;

      isfuzzy(
        actualVolume,
        expectedVolume,
        Number.EPSILON,
        `The actual volume ${actualVolume}. The expected volume ${expectedVolume}`
      );

      ok(
        !(await getVideoMuted(browser, videoID)),
        "Video is no longer muted because we moved the slider"
      );
    }
  );
});