summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/embedded-content/media-elements/loading-the-media-resource/resource-selection-currentSrc.html
blob: 61902161ed1ed1e0a6b800b3cf9c61dd4ad0c950 (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
<!doctype html>
<title>currentSrc should not be reset when changing source</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<audio src="/media/sine440.mp3"></audio>
<script>
let v;
let t = async_test("Test currentSrc behaviour in various playback scenarios");
v = document.querySelector('audio');
function queueTaskAndStep(f) {
  step_timeout(function() {
    t.step(f);
  }, 0);
}

function next() {
  let testcase = tests.shift();
  if (!testcase) {
    t.done();
    return;
  }
  step_timeout(testcase, 0);
}

let tests = [
  function() {
    v.src = "/media/sound_0.mp3";
    queueTaskAndStep(function() {
      assert_true(v.currentSrc.indexOf("sound_0.mp3") != -1, "currentSrc must be equal to the source after load if present");
      next();
    });
  },
  function() {
    v.src = URL.createObjectURL(new MediaSource());
    queueTaskAndStep(function() {
      assert_not_equals(v.currentSrc, "", "currentSrc must not be equal to the empty string after load if playing a MediaSource from the src attribute");
      next();
    });
  },
  function() {
    fetch('/media/sound_0.mp3')
      .then(function(response) {
        return response.arrayBuffer();
      }).then((b) => {
        v.src = URL.createObjectURL(new Blob([new Uint8Array(b)], ["audio/mpeg"]));
        queueTaskAndStep(function() {
          assert_not_equals(v.currentSrc, "", "currentSrc must be not equal to the empty string after load if playing a Blob from the src attribute");
          next();
        });
    });
  },
  function() {
    v.src = "/media/sound_0.mp3";
    // Source should be ignored when there is an `src`
    let sourceNode = document.createElement("source");
    sourceNode.setAttribute("src", "/media/sine440.mp3");
    sourceNode.setAttribute("type", "audio/mpeg");
    v.appendChild(sourceNode);
    queueTaskAndStep(function() {
      assert_true(v.currentSrc.indexOf("sine440.mp3") == -1, "The src attribute takes precedence over any source child element when both are preset");
      next();
    })
  },
  function() {
    // But taken into account when there is no `src` attribute;
    v.src = "";
    v.removeAttribute("src");
    queueTaskAndStep(function() {
      assert_true(v.currentSrc.indexOf("sine440.mp3") != -1, "The child source element is the current source when no src attribute is present");
      next();
    });
  },
  function() {
    v.firstChild.remove();
    v.src = "https://test:test/";
    queueTaskAndStep(function() {
      assert_true(v.currentSrc.indexOf("sine440.mp3") != -1, "Not reset when a new load errors");
      next();
    });
  },
  function() {
    v.srcObject = new MediaStream();
    queueTaskAndStep(function() {
      assert_equals(v.currentSrc, "", "When playing a MediaStream, currentSrc should also be reset to an empty string");
      next();
    });
  }
];

next();

</script>