summaryrefslogtreecommitdiffstats
path: root/dom/media/webspeech/recognition/test/test_call_start_from_end_handler.html
blob: 895648ad9e2e1af0dcc96c759944f41b4c6561ba (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=650295
-->
<head>
  <meta charset="utf-8">
  <title>Test for Bug 650295 -- Restart recognition from end handler</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="head.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=650295">Mozilla Bug 650295</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script type="text/javascript">
  SimpleTest.waitForExplicitFinish();

  function createAudioStream() {
    var audioTag = document.createElement("audio");
    audioTag.src = DEFAULT_AUDIO_SAMPLE_FILE;

    var stream = audioTag.mozCaptureStreamUntilEnded();
    audioTag.play();

    return stream;
  }

  var done = false;
  function endHandler(evt, sr) {
    if (done) {
      SimpleTest.finish();
      return;
    }

    try {
      var stream = createAudioStream();
      sr.start(stream); // shouldn't fail
    } catch (err) {
      ok(false, "Failed to start() from end() callback");
    }

    // calling start() may cause some callbacks to fire, but we're
    // no longer interested in them, except for onend, which is where
    // we'll conclude the test.
    sr.onstart = null;
    sr.onaudiostart = null;
    sr.onspeechstart = null;
    sr.onspeechend = null;
    sr.onaudioend = null;
    sr.onresult = null;

    // FIXME(ggp) the state transition caused by start() is async,
    // but abort() is sync (see bug 1055093). until we normalize
    // state transitions, we need to setTimeout here to make sure
    // abort() finds the speech recognition object in the correct
    // state (namely, STATE_STARTING).
    setTimeout(function() {
      sr.abort();
      done = true;
    });

    info("Successfully start() from end() callback");
  }

  function expectExceptionHandler(evt, sr) {
    try {
      sr.start(createAudioStream());
    } catch (err) {
      is(err.name, "InvalidStateError");
      return;
    }

    ok(false, "Calling start() didn't raise InvalidStateError");
  }

  performTest({
    eventsToRequest: [
      'EVENT_RECOGNITIONSERVICE_FINAL_RESULT'
    ],
    expectedEvents: {
      'start': expectExceptionHandler,
      'audiostart': expectExceptionHandler,
      'speechstart': expectExceptionHandler,
      'speechend': expectExceptionHandler,
      'audioend': expectExceptionHandler,
      'result': buildResultCallback("Mock final result"),
      'end': endHandler,
    },
    prefs: [["media.webspeech.test.fake_fsm_events", true],
            ["media.webspeech.test.fake_recognition_service", true],
            ["media.webspeech.recognition.timeout", 100000]]
  });

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