<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1150315
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 1150315: Check that successive cancel/speak calls work</title>
  <script type="application/javascript">
    window.SimpleTest = parent.SimpleTest;
    window.info = parent.info;
    window.is = parent.is;
    window.isnot = parent.isnot;
    window.ok = parent.ok;
  </script>
  <script type="application/javascript" src="common.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1150315">Mozilla Bug 1150315</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 1150315 **/

function testFunc(done_cb) {
  var gotEndEvent = false;
  // A long utterance that we will interrupt.
  var utterance = new SpeechSynthesisUtterance("Donec ac nunc feugiat, posuere " +
    "mauris id, pharetra velit. Donec fermentum orci nunc, sit amet maximus" +
    "dui tincidunt ut. Sed ultricies ac nisi a laoreet. Proin interdum," +
    "libero maximus hendrerit posuere, lorem risus egestas nisl, a" +
    "ultricies massa justo eu nisi. Duis mattis nibh a ligula tincidunt" +
    "tincidunt non eu erat. Sed bibendum varius vulputate. Cras leo magna," +
    "ornare ac posuere vel, luctus id metus. Mauris nec quam ac augue" +
    "consectetur bibendum. Integer a commodo tortor. Duis semper dolor eu" +
    "facilisis facilisis. Etiam venenatis turpis est, quis tincidunt velit" +
    "suscipit a. Cras semper orci in sapien rhoncus bibendum. Suspendisse" +
    "eu ex lobortis, finibus enim in, condimentum quam. Maecenas eget dui" +
    "ipsum. Aliquam tortor leo, interdum eget congue ut, tempor id elit.");
  utterance.addEventListener('start', function(e) {
    ok(true, 'start utterance 1');
    speechSynthesis.cancel();
    info('cancel!');
    speechSynthesis.speak(utterance2);
    info('speak??');
  });

  var utterance2 = new SpeechSynthesisUtterance("Proin ornare neque vitae " +
    "risus mattis rutrum. Suspendisse a velit ut est convallis aliquet." +
    "Nullam ante elit, malesuada vel luctus rutrum, ultricies nec libero." +
    "Praesent eu iaculis orci. Sed nisl diam, sodales ac purus et," +
    "volutpat interdum tortor. Nullam aliquam porta elit et maximus. Cras" +
    "risus lectus, elementum vel sodales vel, ultricies eget lectus." +
    "Curabitur velit lacus, mollis vel finibus et, molestie sit amet" +
    "sapien. Proin vitae dolor ac augue posuere efficitur ac scelerisque" +
    "diam. Nulla sed odio elit.");
  utterance2.addEventListener('start', function() {
    info('start');
    speechSynthesis.cancel();
    speechSynthesis.speak(utterance3);
  });
  utterance2.addEventListener('end', function(e) {
    gotEndEvent = true;
  });

  var utterance3 = new SpeechSynthesisUtterance("Hello, world 3!");
  utterance3.addEventListener('start', function() {
    ok(gotEndEvent, "didn't get start event for this utterance");
  });
  utterance3.addEventListener('end', done_cb);

  // Speak/cancel while paused (Bug 1187105)
  speechSynthesis.pause();
  speechSynthesis.speak(new SpeechSynthesisUtterance("hello."));
  ok(speechSynthesis.pending, "paused speechSynthesis has an utterance queued.");
  speechSynthesis.cancel();
  ok(!speechSynthesis.pending, "paused speechSynthesis has no utterance queued.");
  speechSynthesis.resume();

  speechSynthesis.speak(utterance);
  ok(!speechSynthesis.speaking, "speechSynthesis is not speaking yet.");
  ok(speechSynthesis.pending, "speechSynthesis has an utterance queued.");
}

// Run test with no global queue, and then run it with a global queue.
testFunc(function() {
  SpecialPowers.pushPrefEnv(
    { set: [['media.webspeech.synth.force_global_queue', true]] }, function() {
      testFunc(SimpleTest.finish)
    });
});

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