Android Open Source - speech_trainer Interactive Training Controller Test






From Project

Back to project page speech_trainer.

License

The source code is released under:

GNU General Public License

If you think the Android project speech_trainer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * This file is part of Speech Trainer./*from   w w  w.  j  ava 2  s  .c  o  m*/
 * Copyright (C) 2011 Jan Wrobel <wrr@mixedbit.org>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package mixedbit.speechtrainer.controller;

import mixedbit.speechtrainer.controller.RecordPlayTaskManager.RecordPlayTaskState;

public class InteractiveTrainingControllerTest extends TrainingControllerTest {
    private InteractiveTrainingController trainingController;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        trainingController = new InteractiveTrainingController(mockRecordPlayTaskManager,
                audioBufferAllocator);
    }

    @Override
    protected TrainingController getTrainingController() {
        return trainingController;
    }

    public void testRecordTerminatesCurrentTaskAndStartsRecordingTask() {
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);
        replayAll();

        trainingController.record();
        verifyAll();
    }

    public void testPlayTerminatesCurrentTaskAndStartsPlayingTask() {
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.PLAY, trainingController);
        replayAll();

        trainingController.startTraining();
        trainingController.play();
        verifyAll();
    }

    public void testRecordingAndPlaying() {
        final int numberOfBuffersToRecord = 7;
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);

        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.PLAY, trainingController);
        replayAll();

        trainingController.startTraining();
        // Start recording and record numberOfBuffersToRecord audio buffers.
        trainingController.record();
        for (short i = 0; i < numberOfBuffersToRecord; i++) {
            assertEquals(RecordPlayTaskState.RECORD, trainingController.handleRecord(testRecorder));
            assertEquals(i + 1, testRecorder.getRecordedBuffersCount());
        }

        // Start playing. Make sure handlePlay requests playing to continue
        // until all recorded buffers are played.
        trainingController.play();
        for (short i = 0; i < numberOfBuffersToRecord; i++) {
            assertEquals(RecordPlayTaskState.PLAY, trainingController.handlePlay(testPlayer));
            // Make sure one buffer was played.
            assertEquals(i + 1, testPlayer.getPlayedBuffersCount());
            // Make sure played buffer was a correct one (audio data contains
            // correct id).
            assertEquals(1, testPlayer.getLastPlayedAudioDataCopy().length);
            assertEquals(i, testPlayer.getLastPlayedAudioDataCopy()[0]);
        }

        verifyAll();
    }

    public void testReplay() {
        // The test is similar to the previous one, but play is invoked twice.
        // Second invocation should still play the recorded data.
        final int numberOfBuffersToRecord = 7;
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);

        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.PLAY, trainingController);

        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.PLAY, trainingController);

        replayAll();

        trainingController.startTraining();
        trainingController.record();
        for (short i = 0; i < numberOfBuffersToRecord; i++) {
            assertEquals(RecordPlayTaskState.RECORD, trainingController.handleRecord(testRecorder));
            assertEquals(i + 1, testRecorder.getRecordedBuffersCount());
        }

        trainingController.play();
        trainingController.play();

        for (short i = 0; i < numberOfBuffersToRecord; i++) {
            assertEquals(RecordPlayTaskState.PLAY, trainingController.handlePlay(testPlayer));
            assertEquals(i + 1, testPlayer.getPlayedBuffersCount());
            assertEquals(1, testPlayer.getLastPlayedAudioDataCopy().length);
            assertEquals(i, testPlayer.getLastPlayedAudioDataCopy()[0]);
        }

        verifyAll();
    }

    public void testPlayingTerminatesWhenNoRecordedBuffers() {
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.PLAY, trainingController);
        replayAll();

        trainingController.startTraining();
        trainingController.play();
        // No audio buffers recorded, handlePlay should request RecordPlayTask
        // to terminate (return TERMINATE status).
        assertEquals(RecordPlayTaskState.TERMINATE, trainingController.handlePlay(testPlayer));
        assertEquals(0, testPlayer.getPlayedBuffersCount());
        verifyAll();
    }

    public void testRecordingTerminatesWhenNoMoreAudioBuffers() {
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);
        replayAll();

        trainingController.startTraining();
        trainingController.record();
        for (int i = 0; i < NUMBER_OF_AUDIO_BUFFERS; ++i) {
            // As long as there are audio buffers available, handleRecord should
            // request RecordPlayTask to continue recording (return RECORD
            // status).
            assertEquals(RecordPlayTaskState.RECORD, trainingController.handleRecord(testRecorder));
            // handleRecord should invoke Recorder to record each allocated
            // audio buffer.
            assertEquals(i + 1, testRecorder.getRecordedBuffersCount());
        }
        // No more audio buffers, handleRecord should request RecordPlayTask to
        // terminate (return TERMINATE status).
        assertEquals(RecordPlayTaskState.TERMINATE, trainingController.handleRecord(testRecorder));
        assertEquals(NUMBER_OF_AUDIO_BUFFERS, testRecorder.getRecordedBuffersCount());
        verifyAll();
    }

    public void testRecordingTerminatesWhenReadAudioBufferFails() {
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);
        replayAll();

        trainingController.startTraining();
        trainingController.record();
        assertEquals(RecordPlayTaskState.RECORD, trainingController.handleRecord(testRecorder));
        // The next readAudioBuffer call should fail and handleRecord should
        // request RecordPlayTask to terminate.
        testRecorder.setReadAudioBufferResult(false);
        assertEquals(RecordPlayTaskState.TERMINATE, trainingController.handleRecord(testRecorder));

        verifyAll();
    }
    public void testRecordingDiscardsAllPreviouslyRecordedAudioData() {
        // Start recording task twice and make sure that data recorded by the
        // first task is discarded when the second task is started.
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);
        mockRecordPlayTaskManager.terminateTaskIfRunning();
        mockRecordPlayTaskManager.startTask(RecordPlayTaskState.RECORD, trainingController);
        replayAll();

        trainingController.startTraining();
        trainingController.record();
        for (int i = 0; i < NUMBER_OF_AUDIO_BUFFERS; ++i) {
            assertEquals(RecordPlayTaskState.RECORD, trainingController.handleRecord(testRecorder));
        }
        assertEquals(NUMBER_OF_AUDIO_BUFFERS, testRecorder.getRecordedBuffersCount());
        // All audio buffers should be allocated.
        assertNull(audioBufferAllocator.allocateAudioBuffer());

        // Start recording again.
        trainingController.record();
        // All audio buffers should be released.
        audioBufferAllocator.assertAllAudioBuffersAvailable();
        verifyAll();
    }
}




Java Source Code List

mixedbit.speechtrainer.AssertionsTest.java
mixedbit.speechtrainer.Assertions.java
mixedbit.speechtrainer.SpeechTrainerConfig.java
mixedbit.speechtrainer.TrainingApplication.java
mixedbit.speechtrainer.controller.AudioBufferAllocatorTest.java
mixedbit.speechtrainer.controller.AudioBufferAllocator.java
mixedbit.speechtrainer.controller.AudioEventListener.java
mixedbit.speechtrainer.controller.AutomaticTrainingControllerTest.java
mixedbit.speechtrainer.controller.AutomaticTrainingController.java
mixedbit.speechtrainer.controller.ControllerFactory.java
mixedbit.speechtrainer.controller.InteractiveTrainingControllerTest.java
mixedbit.speechtrainer.controller.InteractiveTrainingController.java
mixedbit.speechtrainer.controller.Player.java
mixedbit.speechtrainer.controller.RecordPlayTaskManagerTest.java
mixedbit.speechtrainer.controller.RecordPlayTaskManager.java
mixedbit.speechtrainer.controller.Recorder.java
mixedbit.speechtrainer.controller.SilenceFilterTest.java
mixedbit.speechtrainer.controller.SilenceFilter.java
mixedbit.speechtrainer.controller.SilenceLevelDetectorTest.java
mixedbit.speechtrainer.controller.SilenceLevelDetector.java
mixedbit.speechtrainer.controller.TrainingControllerTest.java
mixedbit.speechtrainer.controller.TrainingController.java
mixedbit.speechtrainer.model.AudioBufferInfo.java
mixedbit.speechtrainer.model.AudioEventCollectorTest.java
mixedbit.speechtrainer.model.AudioEventCollector.java
mixedbit.speechtrainer.model.AudioEventHistory.java
mixedbit.speechtrainer.view.AudioEventView.java
mixedbit.speechtrainer.view.FileViewerActivity.java
mixedbit.speechtrainer.view.TrainingActivity.java
mixedbit.speechtrainer.view.TrainingPreferenceActivity.java