<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
createHTML({
  bug: "1291715",
  title: "Test insertDTMF on sender",
  visible: true
});

function insertdtmftest(pc) {
  ok(pc.getSenders().length, "have senders");
  var sender = pc.getSenders()[0];
  ok(sender.dtmf, "sender has dtmf object");

  ok(sender.dtmf.toneBuffer === "", "sender should start with empty tonebuffer");

  // These will trigger assertions on debug builds if we do not enforce the
  // specified minimums and maximums for duration and interToneGap.
  sender.dtmf.insertDTMF("A", 10);
  sender.dtmf.insertDTMF("A", 10000);
  sender.dtmf.insertDTMF("A", 70, 10);

  var threw = false;
  try {
    sender.dtmf.insertDTMF("bad tones");
  } catch (ex) {
    threw = true;
    is(ex.code, DOMException.INVALID_CHARACTER_ERR, "Expected InvalidCharacterError");
  }
  ok(threw, "Expected exception");

  sender.dtmf.insertDTMF("A");
  sender.dtmf.insertDTMF("B");
  ok(!sender.dtmf.toneBuffer.includes("A"), "calling insertDTMF should replace current characters");

  sender.dtmf.insertDTMF("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
  ok(sender.dtmf.toneBuffer.includes("A"), "lowercase characters should be normalized");

  pc.removeTrack(sender);
  threw = false;
  try {
    sender.dtmf.insertDTMF("AAA");
  } catch (ex) {
    threw = true;
    is(ex.code, DOMException.INVALID_STATE_ERR, "Expected InvalidStateError");
  }
  ok(threw, "Expected exception");
}

runNetworkTest(() => {
  test = new PeerConnectionTest();
  test.setMediaConstraints([{audio: true}], [{audio: true}]);
  test.chain.removeAfter("PC_REMOTE_WAIT_FOR_MEDIA_FLOW");

  // Test sender dtmf.
  test.chain.append([
    function PC_LOCAL_INSERT_DTMF(test) {
      // We want to call removeTrack
      test.pcLocal.expectNegotiationNeeded();
      return insertdtmftest(test.pcLocal._pc);
    }
  ]);

  return pushPrefs(['media.peerconnection.dtmf.enabled', true])
    .then(() => test.run());
});

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