<!doctype html>
<html>
<head>
<title>enumerateDevices: test that enumerateDevices is present (legacy Firefox)</title>
<meta name='assert' content='Check that the enumerateDevices() method is present (legacy Firefox).'/>
</head>
<body>
<h1 class="instructions">Description</h1>
<p class="instructions">This is a modified copy of
testing/web-platform/tests/mediacapture-streams/MediaDevices-enumerateDevices.https.html
testing legacy Firefox version 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";

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_greater_than(deviceId.length, 0, "deviceId should be present even if getUserMedia was never called successfully (legacy).");
    assert_equals(label, "", "label should be empty string if getUserMedia was never called successfully.");
    assert_greater_than(groupId.length, 0, "groupId should be present even if getUserMedia was never called successfully (legacy).");
  }
  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_greater_than(deviceId.length, 0, "audio deviceId should not be empty (legacy).");
        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>
