All files / components/textToSpeech/js MockTTS.js

80% Statements 32/40
50% Branches 6/12
83.33% Functions 10/12
80% Lines 32/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                            3x                   3x                                                                                                                             3x 28x 28x 28x     3x   3x 4x     3x 12x           3x             3x 12x 12x         3x         3x       3x 12x     3x 4x                     3x                                   3x 12x 12x     3x 4x 4x     3x 4x   4x     4x          
/*
Copyright 2015 OCAD University
 
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
 
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
 
*/
 
/* global fluid */
 
(function () {
    "use strict";
 
    /*********************************************************************************************
     * fluid.mock.textToSpeech component
     *********************************************************************************************/
 
    // Mocks the fluid.textToSpeech component, removing calls to the
    // Web Speech API. This will allow for tests to run in browsers
    // that don't support the Web Speech API.
    fluid.defaults("fluid.mock.textToSpeech", {
        members: {
            // An archive of all the calls to queueSpeech.
            // Will contain an ordered set of objects -- {text: String, options: Object}.
            speechRecord: [],
            // An archive of all the events fired
            // Will contain a key/value pairing where key is the name of the event and the
            // value is the number of times the event was fired.
            eventRecord: {},
            lastUtterance: null
        },
        listeners: {
            // records
            "utteranceOnStart.recordUtterance": {
                funcName: "fluid.set",
                args: ["{that}", "{that}.queue.0"]
            },
            "onStart.recordEvent": {
                listener: "{that}.recordEvent",
                args: ["onStart"]
            },
            "onStop.recordEvent": {
                listener: "{that}.recordEvent",
                args: ["onStop"]
            },
            "onSpeechQueued.recordEvent": {
                listener: "{that}.recordEvent",
                args: ["onSpeechQueued"]
            },
            "onSpeechQueued.recordSpeech": {
                listener: "fluid.mock.textToSpeech.recordSpeech",
                args: ["{that}.speechRecord", "{arguments}.0.text", "{arguments}.1"]
            },
            // remove cleanup
            "onCreate.unloadCleanup": {
                funcName: "fluid.identity"
            },
            "onDestroy.cleanup": {
                funcName: "fluid.identity"
            }
        },
        invokers: {
            invokeSpeechSynthesisFunc: {
                funcName: "fluid.mock.textToSpeech.invokeStub",
                args: ["{that}", "{arguments}.0", "{arguments}.1"]
            },
            recordEvent: {
                funcName: "fluid.mock.textToSpeech.recordEvent",
                args: ["{that}.eventRecord", "{arguments}.0"]
            },
            // override the speak invoker to return the utterance component instead of the SpeechSynthesisUtterance instance
            speak: {
                func: "{that}.invokeSpeechSynthesisFunc",
                args: ["speak", "{that}.queue.0"]
            }
        },
        distributeOptions: {
            record: "fluid.mock.textToSpeech.utterance",
            target: "{that fluid.textToSpeech.utterance}.options.gradeNames",
            namespace: "utteranceMock"
        }
    });
 
    fluid.mock.textToSpeech.invokeStub = function (that, method, args) {
        args = fluid.makeArray(args);
        args.unshift(that);
        fluid.invokeGlobalFunction(["fluid", "mock", "textToSpeech", "stubs", method], args);
    };
 
    fluid.registerNamespace("fluid.mock.textToSpeech.stubs");
 
    fluid.mock.textToSpeech.stubs.speak = function (that, utterance) {
        utterance.speakUtterance();
    };
 
    fluid.mock.textToSpeech.stubs.cancel = function (that) {
        Iif (that.lastUtterance) {
            that.lastUtterance.dispatchEvent("end");
            that.lastUtterance = null;
        }
    };
 
    fluid.mock.textToSpeech.stubs.pause = function (that) {
        var utterance = that.queue[0];
        if (utterance) {
            utterance.dispatchEvent("pause");
        }
    };
 
    fluid.mock.textToSpeech.stubs.resume = function (that) {
        var utterance = that.queue[0];
        Iif (utterance) {
            utterance.speakUtterance(true);
        }
    };
 
    fluid.mock.textToSpeech.voices =  [
        {voiceURI: "Alex", name: "Alex", lang: "en-US", localService: true, default: true},
        {voiceURI: "Alice", name: "Alice", lang: "it-IT", localService: true, default: false}
    ];
 
    fluid.mock.textToSpeech.stubs.getVoices = function () {
        return fluid.mock.textToSpeech.voices;
    };
 
    fluid.mock.textToSpeech.recordEvent = function (eventRecord, name) {
        eventRecord[name] = (eventRecord[name] || 0) + 1;
    };
 
    fluid.mock.textToSpeech.recordSpeech = function (speechRecord, text, interrupt) {
        speechRecord.push({
            interrupt: !!interrupt,
            text: text
        });
    };
 
    /*********************************************************************************************
     * fluid.mock.textToSpeech.utterance component
     *********************************************************************************************/
 
    // to be added to a fluid.textToSpeech.utterance grade when the underlying SpeechSynthesisUtterance should be mocked.
    fluid.defaults("fluid.mock.textToSpeech.utterance", {
        invokers: {
            dispatchEvent: {
                funcName: "fluid.mock.textToSpeech.utterance.dispatchEvent",
                args: ["{that}.utterance", "{arguments}.0"]
            },
            utteranceEnd: {
                funcName: "fluid.mock.textToSpeech.utterance.utterranceEnd",
                args: ["{that}"]
            },
            speakUtterance: {
                funcName: "fluid.mock.textToSpeech.utterance.speakUtterance",
                args: ["{that}", "{arguments}.0"]
            }
        },
        synchronous: false
    });
 
    fluid.mock.textToSpeech.utterance.dispatchEvent = function (utterance, type) {
        var newEvent = new Event(type);
        utterance.dispatchEvent(newEvent);
    };
 
    fluid.mock.textToSpeech.utterance.utterranceEnd = function (that) {
        that.dispatchEvent("boundary");
        that.dispatchEvent("end");
    };
 
    fluid.mock.textToSpeech.utterance.speakUtterance = function (that, resume) {
        that.dispatchEvent(resume ? "resume" : "start");
 
        Iif (that.options.synchronous) {
            that.utterranceEnd();
        } else {
            setTimeout(that.utteranceEnd, that.utterance.text.length);
        }
    };
 
})();