summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_media_wakelock.js
blob: da27805a9dc7fe4a454e5bd00ec187af22f1de43 (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
/**
 * Test whether the wakelock state is correct under different situations. However,
 * the lock state of power manager doesn't equal to the actual platform lock.
 * Now we don't have any way to detect whether platform lock is set correctly or
 * not, but we can at least make sure the specific topic's state in power manager
 * is correct.
 */
"use strict";

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

const LOCATION = "https://example.com/browser/toolkit/content/tests/browser/";
const AUDIO_WAKELOCK_NAME = "audio-playing";
const VIDEO_WAKELOCK_NAME = "video-playing";

add_task(async function testCheckWakelockWhenChangeTabVisibility() {
  await checkWakelockWhenChangeTabVisibility({
    description: "playing video",
    url: "file_video.html",
    lockAudio: true,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing muted video",
    url: "file_video.html",
    additionalParams: {
      muted: true,
    },
    lockAudio: false,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing volume=0 video",
    url: "file_video.html",
    additionalParams: {
      volume: 0.0,
    },
    lockAudio: false,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing video without audio in it",
    url: "file_videoWithoutAudioTrack.html",
    lockAudio: false,
    lockVideo: false,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing audio in video element",
    url: "file_videoWithAudioOnly.html",
    lockAudio: true,
    lockVideo: false,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing audio in audio element",
    url: "file_mediaPlayback2.html",
    lockAudio: true,
    lockVideo: false,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing video from media stream with audio and video tracks",
    url: "browser_mediaStreamPlayback.html",
    lockAudio: true,
    lockVideo: true,
  });
  await checkWakelockWhenChangeTabVisibility({
    description: "playing video from media stream without audio track",
    url: "browser_mediaStreamPlaybackWithoutAudio.html",
    lockAudio: true,
    lockVideo: true,
  });
});

/**
 * Following are helper functions.
 */
async function checkWakelockWhenChangeTabVisibility({
  description,
  url,
  additionalParams,
  lockAudio,
  lockVideo,
}) {
  const originalTab = gBrowser.selectedTab;
  info(`start a new tab for '${description}'`);
  const mediaTab = await BrowserTestUtils.openNewForegroundTab(
    window.gBrowser,
    LOCATION + url
  );

  info(`wait for media starting playing`);
  await waitUntilVideoStarted(mediaTab, additionalParams);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: true,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: true,
  });

  info(`switch media tab to background`);
  await BrowserTestUtils.switchTab(window.gBrowser, originalTab);
  await waitForExpectedWakeLockState(AUDIO_WAKELOCK_NAME, {
    needLock: lockAudio,
    isForegroundLock: false,
  });
  await waitForExpectedWakeLockState(VIDEO_WAKELOCK_NAME, {
    needLock: lockVideo,
    isForegroundLock: false,
  });

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

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

async function waitUntilVideoStarted(tab, { muted, volume } = {}) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [muted, volume],
    async (muted, volume) => {
      const video = content.document.getElementById("v");
      if (!video) {
        ok(false, "can't get media element!");
        return;
      }
      if (muted) {
        video.muted = muted;
      }
      if (volume !== undefined) {
        video.volume = volume;
      }
      ok(
        await video.play().then(
          () => true,
          () => false
        ),
        `video started playing.`
      );
    }
  );
}