summaryrefslogtreecommitdiff
path: root/dom/media/webspeech/synth/test/common.js
blob: 0ce9ec51b7a902ae8ef0f686f95e7b17b413f4a0 (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
function synthTestQueue(aTestArgs, aEndFunc) {
  var utterances = [];
  for (var i in aTestArgs) {
    var uargs = aTestArgs[i][0];
    var win = uargs.win || window;
    var u = new win.SpeechSynthesisUtterance(uargs.text);

    if (uargs.args) {
      for (var attr in uargs.args)
        u[attr] = uargs.args[attr];
    }

    function onend_handler(e) {
      is(e.target, utterances.shift(), "Target matches utterances");
      ok(!speechSynthesis.speaking, "speechSynthesis is not speaking.");

      if (utterances.length) {
        ok(speechSynthesis.pending, "other utterances queued");
      } else {
        ok(!speechSynthesis.pending, "queue is empty, nothing pending.");
        if (aEndFunc)
          aEndFunc();
      }
    }

    u.addEventListener('start',
      (function (expectedUri) {
        return function (e) {
          if (expectedUri) {
            var chosenVoice = SpecialPowers.wrap(e).target.chosenVoiceURI;
            is(chosenVoice, expectedUri, "Incorrect URI is used");
          }
        };
      })(aTestArgs[i][1] ? aTestArgs[i][1].uri : null));

    u.addEventListener('end', onend_handler);
    u.addEventListener('error', onend_handler);

    u.addEventListener('error',
      (function (expectedError) {
        return function onerror_handler(e) {
          ok(expectedError, "Error in speech utterance '" + e.target.text + "'");
        };
      })(aTestArgs[i][1] ? aTestArgs[i][1].err : false));

    utterances.push(u);
    win.speechSynthesis.speak(u);
  }

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

function loadFrame(frameId) {
  return new Promise(function(resolve, reject) {
    var frame = document.getElementById(frameId);
    frame.addEventListener('load', function (e) {
      frame.contentWindow.document.title = frameId;
      resolve(frame);
    });
    frame.src = 'data:text/html,' + encodeURI('<html><head></head><body></body></html>');
  });
}

function waitForVoices(win) {
  return new Promise(resolve => {
    function resolver() {
      if (win.speechSynthesis.getVoices().length) {
        win.speechSynthesis.removeEventListener('voiceschanged', resolver);
        resolve();
      }
    }

    win.speechSynthesis.addEventListener('voiceschanged', resolver);
    resolver();
  });
}

function loadSpeechTest(fileName, prefs, frameId="testFrame") {
  loadFrame(frameId).then(frame => {
    waitForVoices(frame.contentWindow).then(
      () => document.getElementById("testFrame").src = fileName);
  });
}

function testSynthState(win, expectedState) {
  for (var attr in expectedState) {
    is(win.speechSynthesis[attr], expectedState[attr],
      win.document.title + ": '" + attr + '" does not match');
  }
}