summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_delay_autoplay_media_pausedAfterPlay.js
blob: 09dc5f9691ef3e6492ff2c505a9f7af3ad1078c4 (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
const PAGE =
  "https://example.com/browser/toolkit/content/tests/browser/file_nonAutoplayAudio.html";

add_task(async function setup_test_preference() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["media.useAudioChannelService.testing", true],
      ["media.block-autoplay-until-in-foreground", true],
    ],
  });
});

/**
 * When media starts in an unvisited tab, we would delay its playback and resume
 * media playback when the tab goes to foreground first time. There are two test
 * cases are used to check different situations.
 *
 * The first one is used to check if the delayed media has been paused before
 * the tab goes to foreground. Then, when the tab goes to foreground, media
 * should still be paused.
 *
 * The second one is used to check if the delayed media has been paused, but
 * it eventually was started again before the tab goes to foreground. Then, when
 * the tab goes to foreground, media should be resumed.
 */
add_task(async function testShouldNotResumePausedMedia() {
  info("- open new background tab and wait until it finishes loading -");
  const tab = BrowserTestUtils.addTab(window.gBrowser, PAGE);
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  info("- play media and then immediately pause it -");
  await doPlayThenPauseOnMedia(tab);

  info("- show delay media playback icon on tab -");
  await waitForTabBlockEvent(tab, true);

  info("- selecting tab as foreground tab would resume the tab -");
  await BrowserTestUtils.switchTab(window.gBrowser, tab);

  info("- resuming tab should dismiss delay autoplay icon -");
  await waitForTabBlockEvent(tab, false);

  info("- paused media should still be paused -");
  await checkAudioPauseState(tab, true /* should be paused */);

  info("- paused media won't generate tab playing icon -");
  await waitForTabPlayingEvent(tab, false);

  info("- remove tab -");
  BrowserTestUtils.removeTab(tab);
});

add_task(async function testShouldResumePlayedMedia() {
  info("- open new background tab and wait until it finishes loading -");
  const tab = BrowserTestUtils.addTab(window.gBrowser, PAGE);
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  info("- play media, pause it, then play it again -");
  await doPlayPauseThenPlayOnMedia(tab);

  info("- show delay media playback icon on tab -");
  await waitForTabBlockEvent(tab, true);

  info("- select tab as foreground tab -");
  await BrowserTestUtils.switchTab(window.gBrowser, tab);

  info("- resuming tab should dismiss delay autoplay icon -");
  await waitForTabBlockEvent(tab, false);

  info("- played media should still be played -");
  await checkAudioPauseState(tab, false /* should be played */);

  info("- played media would generate tab playing icon -");
  await waitForTabPlayingEvent(tab, true);

  info("- remove tab -");
  BrowserTestUtils.removeTab(tab);
});

/**
 * Helper functions.
 */
async function checkAudioPauseState(tab, expectedPaused) {
  await SpecialPowers.spawn(
    tab.linkedBrowser,
    [expectedPaused],
    expectedPaused => {
      const audio = content.document.getElementById("testAudio");
      if (!audio) {
        ok(false, "Can't get the audio element!");
      }

      is(audio.paused, expectedPaused, "The pause state of audio is corret.");
    }
  );
}

async function doPlayThenPauseOnMedia(tab) {
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    const audio = content.document.getElementById("testAudio");
    if (!audio) {
      ok(false, "Can't get the audio element!");
    }

    audio.play();
    audio.pause();
  });
}

async function doPlayPauseThenPlayOnMedia(tab) {
  await SpecialPowers.spawn(tab.linkedBrowser, [], () => {
    const audio = content.document.getElementById("testAudio");
    if (!audio) {
      ok(false, "Can't get the audio element!");
    }

    audio.play();
    audio.pause();
    audio.play();
  });
}