summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-image/ImageCapture-creation.https.html
blob: 81a503893acef22810ea601ce1ea5ab1ca2b9f3a (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
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src=/resources/testdriver.js></script>
<script src=/resources/testdriver-vendor.js></script>
<script src='../mediacapture-streams/permission-helper.js'></script>
<script>

// This test verifies that ImageCapture can be created (or not) with different
// Media Stream Track types (audio, video).

function makeAsyncTest(modifyTrack, message) {
  async_test(function(test) {

    const gotStream = test.step_func(function(stream) {
      assert_equals(stream.getAudioTracks().length, 0);
      assert_equals(stream.getVideoTracks().length, 1);

      var videoTrack = stream.getVideoTracks()[0];
      assert_equals(videoTrack.readyState, 'live');
      assert_true(videoTrack.enabled);
      assert_false(videoTrack.muted);

      var capturer = new ImageCapture(videoTrack);
      assert_equals(capturer.track, videoTrack);

      modifyTrack(videoTrack);

      promise_rejects_dom(test,
                      'InvalidStateError',
                      capturer.grabFrame(),
                      'Should throw InvalidStateError.')
        .then(() => test.done());
    });

    const onError = test.unreached_func('Error creating MediaStream.');
    // both permissions are needed at some point, asking for both at once
    setMediaPermission()
      .then(() => navigator.mediaDevices.getUserMedia({video: true}))
      .then(gotStream)
      .catch(onError);

  }, message);
}

var disableTrack = function(videoTrack) {
  // grabFrame() is rejected if the associated video track is disabled.
  videoTrack.enabled = false;
};

var stopTrack = function(videoTrack) {
  // grabFrame() is rejected if the associated video track is ended.
  videoTrack.stop();
  assert_equals(videoTrack.readyState, 'ended');
};

// Create the rejection tests. Note that grabFrame() would also be rejected if
// the video Track was muted but that's a read-only property of the Track.
makeAsyncTest(disableTrack, 'grabFrame() of a disabled Track');
makeAsyncTest(stopTrack, 'grabFrame() of an ended Track');


var testAudio = async_test(function() {
  navigator.mediaDevices.getUserMedia({audio:true})
  .then(
    this.step_func(function(stream) {
      assert_equals(stream.getAudioTracks().length, 1);
      assert_equals(stream.getVideoTracks().length, 0);
      assert_throws_dom("NotSupportedError",
                        function() {
                          var capturer = new ImageCapture(stream.getAudioTracks()[0]);
                        },
                        'an ImageCapturer can only be created from a video track');

      this.done();
    }))
    .catch(
    this.unreached_func('Error creating MediaStream.'));
}, 'verifies that an ImageCapture cannot be created out of an Audio Track');

var testParameter = test(function() {
  const invalidParameters = [
    "invalid",
    null,
    123,
    {},
    "",
    true
  ];
  assert_throws_js(TypeError,
                   function() { var capturer = new ImageCapture(); },
                   'an ImageCapturer cannot be created with no parameter');
  invalidParameters.map(parameter => {
    assert_throws_js(TypeError,
                     function() { var capturer = new ImageCapture(parameter); },
                     `an ImageCapturer cannot be created with a ${parameter} parameter`);
  });
}, 'throw "TypeError" if parameter is not MediaStreamTrack.');

</script>