summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/media-source/mediasource-multiple-attach.html
blob: 4a95a42e83404bace5cb964f5767158c8c3b9f53 (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
<!DOCTYPE html>
<!-- Copyright © 2016 Chromium authors and World Wide Web Consortium, (Massachusetts Institute of Technology, ERCIM, Keio University, Beihang). -->
<html>
    <head>
        <title>Test Attaching a MediaSource to multiple HTMLMediaElements.</title>
        <script src="/resources/testharness.js"></script>
        <script src="/resources/testharnessreport.js"></script>
        <script src="mediasource-util.js"></script>
    </head>
    <body>
        <div id="log"></div>
        <script>
          function twoMediaElementTest(testFunction, description)
          {
              media_test(function(test)
              {
                  var firstMediaTag = document.createElement('video');
                  var secondMediaTag = document.createElement('video');
                  document.body.appendChild(firstMediaTag);
                  document.body.appendChild(secondMediaTag);

                  // Overload done() so that elements added to the document can be
                  // removed.
                  var removeMediaElements = true;
                  var oldTestDone = test.done.bind(test);
                  test.done = function()
                  {
                      if (removeMediaElements) {
                          document.body.removeChild(secondMediaTag);
                          document.body.removeChild(firstMediaTag);
                          removeMediaElements = false;
                      }
                      oldTestDone();
                  };

                  testFunction(test, firstMediaTag, secondMediaTag);
              }, description);
          }

          twoMediaElementTest(function(test, firstMediaTag, secondMediaTag)
          {
              // When attachment of mediaSource to two MediaElements is done
              // without an intervening stable state, exactly one of the two
              // MediaElements should successfully attach, and the other one
              // should get error event due to mediaSource already in 'open'
              // readyState.
              var mediaSource = new MediaSource();
              var mediaSourceURL = URL.createObjectURL(mediaSource);
              var gotSourceOpen = false;
              var gotError = false;
              var doneIfFinished = test.step_func(function()
              {
                  if (gotSourceOpen && gotError)
                      test.done();
              });
              var errorHandler = test.step_func(function(e)
              {
                  firstMediaTag.removeEventListener('error', errorHandler);
                  secondMediaTag.removeEventListener('error', errorHandler);

                  var eventTarget = e.target;
                  var otherTarget;
                  if (eventTarget == firstMediaTag) {
                      otherTarget = secondMediaTag;
                  } else {
                      assert_equals(eventTarget, secondMediaTag, 'Error target check');
                      otherTarget = firstMediaTag;
                  }

                  assert_true(eventTarget.error != null, 'Error state on one tag');
                  assert_equals(eventTarget.error.code, MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED, 'Expected error code');
                  assert_equals(otherTarget.error, null, 'No error on other tag');

                  assert_equals(eventTarget.networkState, HTMLMediaElement.NETWORK_NO_SOURCE,
                                'Tag with error state networkState');
                  assert_equals(otherTarget.networkState, HTMLMediaElement.NETWORK_LOADING,
                                'Tag without error state networkState');

                  gotError = true;
                  doneIfFinished();
              });

              test.expectEvent(mediaSource, 'sourceopen', 'An attachment succeeded');
              firstMediaTag.addEventListener('error', errorHandler);
              secondMediaTag.addEventListener('error', errorHandler);

              firstMediaTag.src = mediaSourceURL;
              secondMediaTag.src = mediaSourceURL;

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, 'open', 'Source is opened');
                  gotSourceOpen = true;
                  doneIfFinished();
              });
          }, 'Test exactly one succeeds when two MediaElements attach to same MediaSource');

          mediasource_test(function(test, mediaElement, mediaSource) {
              assert_equals(mediaSource.readyState, 'open', 'Source open');
              // Set the tag's src attribute.  This should close mediaSource,
              // reattach it to the tag, and initiate source reopening.
              test.expectEvent(mediaSource, 'sourceopen', 'Source attached again');
              mediaElement.src = URL.createObjectURL(mediaSource);
              assert_equals(mediaSource.readyState, 'closed', 'Source closed');

              test.waitForExpectedEvents(function()
              {
                  assert_equals(mediaSource.readyState, 'open', 'Source reopened');
                  test.done();
              });
          }, 'Test that MediaSource can reattach if closed first');
        </script>
    </body>
</html>