summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/mediacapture-streams/MediaDevices-enumerateDevices.https.html
blob: 88c07048a1b48940787f8659dc32cb78bf041b86 (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
<!doctype html>
<html>
<head>
<title>enumerateDevices: test that enumerateDevices is present</title>
<link rel="author" title="Dr Alex Gouaillard" href="mailto:agouaillard@gmail.com"/>
<link rel="help" href="https://w3c.github.io/mediacapture-main/#enumerating-devices">
<meta name='assert' content='Check that the enumerateDevices() method is present.'/>
</head>
<body>
<h1 class="instructions">Description</h1>
<p class="instructions">This test checks for the presence of the
<code>navigator.mediaDevices.enumerateDevices()</code> method.</p>
<div id='log'></div>
<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=permission-helper.js></script>
<script>
"use strict";

//NOTE ALEX: for completion, a test for ondevicechange event is missing.

promise_test(async () => {
  assert_not_equals(navigator.mediaDevices.enumerateDevices, undefined, "navigator.mediaDevices.enumerateDevices exists");
  const devices = await navigator.mediaDevices.enumerateDevices();
  for (const {kind, deviceId, label, groupId} of devices) {
    assert_in_array(kind, ["videoinput", "audioinput", "audiooutput"]);
    assert_equals(deviceId, "", "deviceId should be empty string if getUserMedia was never called successfully.");
    assert_equals(label, "", "label should be empty string if getUserMedia was never called successfully.");
    assert_equals(groupId, "", "groupId should be empty string if getUserMedia was never called successfully.");
  }
  assert_less_than_equal(devices.filter(({kind}) => kind == "audioinput").length,
                         1, "there should be zero or one audio input device.");
  assert_less_than_equal(devices.filter(({kind}) => kind == "videoinput").length,
                         1, "there should be zero or one video input device.");
  assert_equals(devices.filter(({kind}) => kind == "audiooutput").length,
                0, "there should be no audio output devices.");
  assert_less_than_equal(devices.length, 2,
                         "there should be no more than two devices.");
  if (devices.length > 1) {
    assert_equals(devices[0].kind, "audioinput", "audioinput is first");
    assert_equals(devices[1].kind, "videoinput", "videoinput is second");
  }
}, "mediaDevices.enumerateDevices() is present and working - before capture");

promise_test(async t => {
  await setMediaPermission("granted");
  const stream = await navigator.mediaDevices.getUserMedia({ video: true });
  stream.getTracks()[0].stop();

  const devices = await navigator.mediaDevices.enumerateDevices();
  const kinds = ["audioinput", "videoinput"];
  for (const {kind, deviceId} of devices) {
    assert_in_array(kind, kinds, "camera doesn't expose audiooutput");
    assert_equals(typeof deviceId, "string", "deviceId is a string.");
    switch (kind) {
      case "videoinput":
        assert_greater_than(deviceId.length, 0, "video deviceId should not be empty.");
        break;
      case "audioinput":
        assert_equals(deviceId.length, 0, "audio deviceId should be empty.");
        break;
    }
  }
}, "mediaDevices.enumerateDevices() is working - after video capture");

// This test is designed to come after its video counterpart directly above
promise_test(async t => {
  const devices1 = await navigator.mediaDevices.enumerateDevices();
  const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
  stream.getTracks()[0].stop();
  const devices = await navigator.mediaDevices.enumerateDevices();
  assert_equals(devices.filter(({kind}) => kind == "videoinput").length,
                devices1.filter(({kind}) => kind == "videoinput").length,
                "same number of (previously exposed) videoinput devices");
  assert_greater_than_equal(devices.filter(d => d.kind == "audioinput").length,
                            devices1.filter(d => d.kind == "audioinput").length,
                            "same number or more audioinput devices");
  const order = ["audioinput", "videoinput", "audiooutput"];
  for (const {kind, deviceId} of devices) {
    assert_in_array(kind, order, "expected kind");
    assert_equals(typeof deviceId, "string", "deviceId is a string.");
    switch (kind) {
      case "videoinput":
        assert_greater_than(deviceId.length, 0, "video deviceId should not be empty.");
        break;
      case "audioinput":
        assert_greater_than(deviceId.length, 0, "audio deviceId should not be empty.");
        break;
    }
  }
  const kinds = devices.map(({kind}) => kind);
  const correct = [...kinds].sort((a, b) => order.indexOf(a) - order.indexOf(b));
  assert_equals(JSON.stringify(kinds), JSON.stringify(correct), "correct order");
}, "mediaDevices.enumerateDevices() is working - after video then audio capture");

promise_test(async () => {
  const devices = await navigator.mediaDevices.enumerateDevices();
  for (const mediaInfo of devices) {
    if (mediaInfo.kind == "audioinput" || mediaInfo.kind == "videoinput") {
      assert_true("InputDeviceInfo" in window, "InputDeviceInfo exists");
      assert_true(mediaInfo instanceof InputDeviceInfo);
    } else if (mediaInfo.kind == "audiooutput") {
      assert_true(mediaInfo instanceof MediaDeviceInfo);
    } else {
      assert_unreached("mediaInfo.kind should be one of 'audioinput', 'videoinput', or 'audiooutput'.")
    }
  }
}, "InputDeviceInfo is supported");
</script>
</body>
</html>