<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
createHTML({
  bug: "1187775",
  title: "peer connection ICE fails on relay-only without TURN"
});

function PC_LOCAL_NO_CANDIDATES(test) {
  var isnt = can => is(can, null, "No candidates: " + JSON.stringify(can));
  test.pcLocal._pc.addEventListener("icecandidate", e => isnt(e.candidate));
}

function PC_BOTH_WAIT_FOR_ICE_FAILED(test) {
  var isFail = (f, reason, msg) =>
    f().then(() => { throw new Error(msg + " must fail"); },
             e => is(e.message, reason, msg + " must fail with: " + e.message));

  return Promise.all([
    isFail(() => test.pcLocal.waitForIceConnected(), "ICE failed", "Local ICE"),
    isFail(() => test.pcRemote.waitForIceConnected(), "ICE failed", "Remote ICE")
  ])
  .then(() => ok(true, "ICE on both sides must fail."));
}

runNetworkTest(async options => {
  await pushPrefs(
    ['media.peerconnection.ice.stun_client_maximum_transmits', 3],
    ['media.peerconnection.ice.trickle_grace_period', 5000]
  );
  options = options || {};
  options.config_local = options.config_local || {};
  const servers = options.config_local.iceServers || [];
  // remove any turn servers
  options.config_local.iceServers = servers.filter(server =>
      server.urls.every(u => !u.toLowerCase().startsWith('turn')));

  // Here's the setting we're testing. Comment out and this test should fail:
  options.config_local.iceTransportPolicy = "relay";

  const test = new PeerConnectionTest(options);
  test.setMediaConstraints([{audio: true}, {video: true}],
                           [{audio: true}, {video: true}]);
  test.chain.remove("PC_LOCAL_SETUP_ICE_LOGGER");  // Needed to suppress failing
  test.chain.remove("PC_REMOTE_SETUP_ICE_LOGGER"); // on ICE-failure.
  test.chain.insertAfter("PC_LOCAL_SETUP_ICE_HANDLER", PC_LOCAL_NO_CANDIDATES);
  test.chain.replace("PC_LOCAL_WAIT_FOR_ICE_CONNECTED", PC_BOTH_WAIT_FOR_ICE_FAILED);
  test.chain.removeAfter("PC_BOTH_WAIT_FOR_ICE_FAILED");
  await test.run();
});

</script>
</pre>
</body>
</html>
