content/media/webspeech/recognition/test/head.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 "use strict";
michael@0 2
michael@0 3 const DEFAULT_AUDIO_SAMPLE_FILE = "hello.ogg";
michael@0 4 const SPEECH_RECOGNITION_TEST_REQUEST_EVENT_TOPIC = "SpeechRecognitionTest:RequestEvent";
michael@0 5 const SPEECH_RECOGNITION_TEST_END_TOPIC = "SpeechRecognitionTest:End";
michael@0 6
michael@0 7 var errorCodes = {
michael@0 8 NO_SPEECH : "no-speech",
michael@0 9 ABORTED : "aborted",
michael@0 10 AUDIO_CAPTURE : "audio-capture",
michael@0 11 NETWORK : "network",
michael@0 12 NOT_ALLOWED : "not-allowed",
michael@0 13 SERVICE_NOT_ALLOWED : "service-not-allowed",
michael@0 14 BAD_GRAMMAR : "bad-grammar",
michael@0 15 LANGUAGE_NOT_SUPPORTED : "language-not-supported"
michael@0 16 };
michael@0 17
michael@0 18 var Services = SpecialPowers.Cu.import("resource://gre/modules/Services.jsm").Services;
michael@0 19
michael@0 20 function EventManager(sr) {
michael@0 21 var self = this;
michael@0 22 var nEventsExpected = 0;
michael@0 23 self.eventsReceived = [];
michael@0 24
michael@0 25 var allEvents = [
michael@0 26 "audiostart",
michael@0 27 "soundstart",
michael@0 28 "speechstart",
michael@0 29 "speechend",
michael@0 30 "soundend",
michael@0 31 "audioend",
michael@0 32 "result",
michael@0 33 "nomatch",
michael@0 34 "error",
michael@0 35 "start",
michael@0 36 "end"
michael@0 37 ];
michael@0 38
michael@0 39 var eventDependencies = {
michael@0 40 "speechend": "speechstart",
michael@0 41 "soundent": "soundstart",
michael@0 42 "audioend": "audiostart"
michael@0 43 };
michael@0 44
michael@0 45 var isDone = false;
michael@0 46
michael@0 47 // AUDIO_DATA events are asynchronous,
michael@0 48 // so we queue events requested while they are being
michael@0 49 // issued to make them seem synchronous
michael@0 50 var isSendingAudioData = false;
michael@0 51 var queuedEventRequests = [];
michael@0 52
michael@0 53 // register default handlers
michael@0 54 for (var i = 0; i < allEvents.length; i++) {
michael@0 55 (function (eventName) {
michael@0 56 sr["on" + eventName] = function (evt) {
michael@0 57 var message = "unexpected event: " + eventName;
michael@0 58 if (eventName == "error") {
michael@0 59 message += " -- " + evt.message;
michael@0 60 }
michael@0 61
michael@0 62 ok(false, message);
michael@0 63 if (self.doneFunc && !isDone) {
michael@0 64 isDone = true;
michael@0 65 self.doneFunc();
michael@0 66 }
michael@0 67 };
michael@0 68 })(allEvents[i]);
michael@0 69 }
michael@0 70
michael@0 71 self.expect = function EventManager_expect(eventName, cb) {
michael@0 72 nEventsExpected++;
michael@0 73
michael@0 74 sr["on" + eventName] = function(evt) {
michael@0 75 self.eventsReceived.push(eventName);
michael@0 76 ok(true, "received event " + eventName);
michael@0 77
michael@0 78 var dep = eventDependencies[eventName];
michael@0 79 if (dep) {
michael@0 80 ok(self.eventsReceived.indexOf(dep) >= 0,
michael@0 81 eventName + " must come after " + dep);
michael@0 82 }
michael@0 83
michael@0 84 cb && cb(evt, sr);
michael@0 85 if (self.doneFunc && !isDone &&
michael@0 86 nEventsExpected === self.eventsReceived.length) {
michael@0 87 isDone = true;
michael@0 88 self.doneFunc();
michael@0 89 }
michael@0 90 }
michael@0 91 }
michael@0 92
michael@0 93 self.requestFSMEvent = function EventManager_requestFSMEvent(eventName) {
michael@0 94 if (isSendingAudioData) {
michael@0 95 info("Queuing event " + eventName + " until we're done sending audio data");
michael@0 96 queuedEventRequests.push(eventName);
michael@0 97 return;
michael@0 98 }
michael@0 99
michael@0 100 var subject = null;
michael@0 101
michael@0 102 if (eventName === "EVENT_AUDIO_DATA") {
michael@0 103 isSendingAudioData = true;
michael@0 104 var audioTag = document.createElement("audio");
michael@0 105 audioTag.src = self.audioSampleFile;
michael@0 106
michael@0 107 subject = audioTag.mozCaptureStreamUntilEnded();
michael@0 108 audioTag.addEventListener("ended", function() {
michael@0 109 info("Sample stream ended, requesting queued events");
michael@0 110 isSendingAudioData = false;
michael@0 111 while (queuedEventRequests.length) {
michael@0 112 self.requestFSMEvent(queuedEventRequests.shift());
michael@0 113 }
michael@0 114 });
michael@0 115
michael@0 116 audioTag.play();
michael@0 117 }
michael@0 118
michael@0 119 info("requesting " + eventName);
michael@0 120 Services.obs.notifyObservers(subject,
michael@0 121 SPEECH_RECOGNITION_TEST_REQUEST_EVENT_TOPIC,
michael@0 122 eventName);
michael@0 123 }
michael@0 124
michael@0 125 self.requestTestEnd = function EventManager_requestTestEnd() {
michael@0 126 Services.obs.notifyObservers(null, SPEECH_RECOGNITION_TEST_END_TOPIC, null);
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 function buildResultCallback(transcript) {
michael@0 131 return (function(evt) {
michael@0 132 is(evt.results[0][0].transcript, transcript, "expect correct transcript");
michael@0 133 });
michael@0 134 }
michael@0 135
michael@0 136 function buildErrorCallback(errcode) {
michael@0 137 return (function(err) {
michael@0 138 is(err.error, errcode, "expect correct error code");
michael@0 139 });
michael@0 140 }
michael@0 141
michael@0 142 function performTest(options) {
michael@0 143 var prefs = options.prefs;
michael@0 144
michael@0 145 prefs.unshift(
michael@0 146 ["media.webspeech.recognition.enable", true],
michael@0 147 ["media.webspeech.test.enable", true]
michael@0 148 );
michael@0 149
michael@0 150 SpecialPowers.pushPrefEnv({set: prefs}, function() {
michael@0 151 var sr = new SpeechRecognition();
michael@0 152 var em = new EventManager(sr);
michael@0 153
michael@0 154 for (var eventName in options.expectedEvents) {
michael@0 155 var cb = options.expectedEvents[eventName];
michael@0 156 em.expect(eventName, cb);
michael@0 157 }
michael@0 158
michael@0 159 em.doneFunc = function() {
michael@0 160 em.requestTestEnd();
michael@0 161 if (options.doneFunc) {
michael@0 162 options.doneFunc();
michael@0 163 }
michael@0 164 }
michael@0 165
michael@0 166 em.audioSampleFile = DEFAULT_AUDIO_SAMPLE_FILE;
michael@0 167 if (options.audioSampleFile) {
michael@0 168 em.audioSampleFile = options.audioSampleFile;
michael@0 169 }
michael@0 170
michael@0 171 for (var i = 0; i < options.eventsToRequest.length; i++) {
michael@0 172 em.requestFSMEvent(options.eventsToRequest[i]);
michael@0 173 }
michael@0 174 });
michael@0 175 }

mercurial