summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_media_wakelock_PIP.js
blob: d550fc2ffa7cb428cead7920f87c523f87788bc1 (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
/**
 * Test the wakelock usage for video being used in the picture-in-picuture (PIP)
 * mode. When video is playing in PIP window, we would always request a video
 * wakelock, and request audio wakelock only when video is audible.
 */
add_task(async function testCheckWakelockForPIPVideo() {
  await checkWakelockWhenChangeTabVisibility({
    description: "playing a PIP video",
    lockAudio: true,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing a muted PIP video",
    additionalParams: {
      muted: true,
    },
    lockAudio: false,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing a volume=0 PIP video",
    additionalParams: {
      volume: 0.0,
    },
    lockAudio: false,
    lockVideo: true,
  });
});

/**
 * Following are helper functions and variables.
 */
const PAGE_URL =
  "https://example.com/browser/toolkit/content/tests/browser/file_video.html";
const AUDIO_WAKELOCK_NAME = "audio-playing";
const VIDEO_WAKELOCK_NAME = "video-playing";
const TEST_VIDEO_ID = "v";

// Import this in order to use `triggerPictureInPicture()`.
Services.scriptloader.loadSubScript(
  "chrome://mochitests/content/browser/toolkit/components/pictureinpicture/tests/head.js",
  this
);

async function checkWakelockWhenChangeTabVisibility({
  description,
  additionalParams,
  lockAudio,
  lockVideo,
}) {
  const originalTab = gBrowser.selectedTab;
  info(`start a new tab for '${description}'`);
  const tab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    PAGE_URL
  );

  info(`wait for PIP video starting playing`);
  await startPIPVideo(tab, additionalParams);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: true,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: true,
  });

  info(
    `switch tab to background and still own foreground locks due to visible PIP video`
  );
  await BrowserTestUtils.switchTab(window.gBrowser, originalTab);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: true,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: true,
  });

  info(`pausing PIP video should release all locks`);
  await pausePIPVideo(tab);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: false,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: false,
  });

  info(`resuming PIP video should request locks again`);
  await resumePIPVideo(tab);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: true,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: true,
  });

  info(`switch tab to foreground again`);
  await BrowserTestUtils.switchTab(window.gBrowser, tab);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: true,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: true,
  });

  info(`remove tab`);
  await BrowserTestUtils.closeWindow(tab.PIPWindow);
  BrowserTestUtils.removeTab(tab);
}

async function startPIPVideo(tab, { muted, volume } = {}) {
  tab.PIPWindow = await triggerPictureInPicture(
    tab.linkedBrowser,
    TEST_VIDEO_ID
  );
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [muted, volume, TEST_VIDEO_ID],
    async (muted, volume, Id) => {
      const video = content.document.getElementById(Id);
      if (muted) {
        video.muted = muted;
      }
      if (volume !== undefined) {
        video.volume = volume;
      }
      ok(
        await video.play().then(
          () => true,
          () => false
        ),
        `video started playing.`
      );
    }
  );
}

function pausePIPVideo(tab) {
  return SpecialPowers.spawn(tab.linkedBrowser, [TEST_VIDEO_ID], Id => {
    content.document.getElementById(Id).pause();
  });
}

function resumePIPVideo(tab) {
  return SpecialPowers.spawn(tab.linkedBrowser, [TEST_VIDEO_ID], async Id => {
    await content.document.getElementById(Id).play();
  });
}