summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_playback_and_bfcache.html
blob: 3b1236c5307fb648a10ea7f6218c5f9c1d52e182 (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test media playback and bfcache</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    SimpleTest.requestFlakyTimeout("Need some timer to wait for the audio to play");
    SimpleTest.waitForExplicitFinish();
    var duration = 0;

    // The test opens a page and another page with a media element is loaded.
    // The media element plays an audio file and starts again and sends
    // statistics about it and then history.back() is called. The test waits
    // for 1s + duration of the audio file and goes forward. The audio playback
    // shouldn't have progressed while the page was in the bfcache.
    function test() {
      let bc1 = new BroadcastChannel("bc1");
      let pageshow1Count = 0;
      bc1.onmessage = function(e) {
        if (e.data == "pageshow") {
          ++pageshow1Count;
          info("Page 1 pageshow " + pageshow1Count);
          if (pageshow1Count == 1) {
            bc1.postMessage("loadNext");
          } else if (pageshow1Count == 2) {
            setTimeout(function() {
                bc1.postMessage("forward");
                bc1.close();
              }, (Math.round(duration) + 1) * 1000);
          }
        }
      };

      let bc2 = new BroadcastChannel("bc2");
      let pageshow2Count = 0;
      let statisticsCount = 0;
      bc2.onmessage = function(e) {
        if (e.data.event == "pageshow") {
          ++pageshow2Count;
          info("Page 2 pageshow " + pageshow2Count);
          if (pageshow2Count == 2) {
            ok(e.data.persisted, "Should have persisted the page.");
            bc2.postMessage("statistics");
          }
        } else {
          ++statisticsCount;
          if (statisticsCount == 1) {
            duration = e.data.duration;
            bc2.postMessage("back");
          } else {
            is(statisticsCount, 2, "Should got two play events.");
            ok(e.data.currentTime < e.data.duration,
               "Should have stopped the playback while the page was in bfcache." +
               "currentTime: " + e.data.currentTime + " duration: " + e.data.duration);
            bc2.close();
            SimpleTest.finish();
          }
        }
      };

      window.open("file_playback_and_bfcache.html", "", "noopener");
    }
  </script>
</head>
<body onload="test()">
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>