package com.ibm.emb.test.mfb;

// java stuff
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.emb.Media;
import javax.emb.MediaBean;
import javax.emb.MediaException;
import javax.emb.MediaFormat;
import javax.emb.MediaFormatRegistry;

import org.objectweb.jonas.emb.mfb.formats.audio.WavAudioFormat;

import com.ibm.emb.junit.EMBTestRunner;

public class WavFormatTest extends EmbeddedMediaFormatTest {

    /**
     * constructor
     */
    public WavFormatTest(String name) throws MediaException {
        super(name);
    }

    /**
     * main method
     */
    public static void main(String args[]) {
        EMBTestRunner.run(WavFormatTest.class);
    }

    /**
     * creates an instance of BmpFormat with the given Parameters used in super
     * class of BmpFormatTest
     */
    public MediaFormat createMediaFormat() {
        return new WavAudioFormat();
    }

    /**
     * do initialisation here
     */
    protected void setUp() {
    }

    /**
     * freeup resources
     */
    protected void tearDown() {
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: extractHeader(Media)
     *        Testcase Number: TEMB0074SBA
     *
     *        Test 1:
     *        call extractHeader passing a wav-audio media
     *        --&gt; check result to be an instance of com.ibm.emb.meb.WavHeader
     *
     *
     *
     * </pre>
     */
    public void testTEMB0074SBA() throws MediaException, IOException {

        //
        // test 1
        //

        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("wav", new WavAudioFormat());

        // full path of the mpeg video sample
        String mediaWavFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getWavSoundName1();

        // creates media bean
        File mediaWavFile = new File(mediaWavFullPath);
        MediaBean mediaBean = new MediaBean(mediaWavFile, "wav");

        // creates wav audio format
        WavAudioFormat wavFormat = new WavAudioFormat();

        // creates mpegAudioHeader
//      TODO : wavHeader does not exist any more
//        InputStream mediaWavInputStream = new FileInputStream(mediaWavFile);
//        WavHeader wavHeader = (WavHeader) wavFormat.extractHeader(mediaWavInputStream);
//        mediaWavInputStream.close();
//        // test
//        assertTrue("test 1", wavHeader instanceof WavHeader);

        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: extractSurrogate(Media)
     *        Testcase Number: TEMB0057SBA
     *
     *        Test 1:
     *        call extractSurrogate passing a wav audio media
     *        check result to be an instance of Media
     *        access the content of the resulting media ----&gt;  to verify it is accessible
     *
     *
     *
     * </pre>
     */
    public void testTEMB0057SBA() throws MediaException, IOException {
        //
        // test 1
        //

        // register media format
        MediaFormatRegistry formatRegistry = MediaFormatRegistry.SINGLETON;
        formatRegistry.rebind("wav", new WavAudioFormat());

        // full path of the wav audio sample
        String mediaWavFullPath = embTestConfig.getMediaDir() + File.separator + embTestConfig.getWavSoundName1();

        // creates media bean
        File mediaWavFile = new File(mediaWavFullPath);
        MediaBean mediaBean = new MediaBean(mediaWavFile, "wav");

        InputStream mediaWavInputStream = new FileInputStream(mediaWavFile);
        // creates wav audio format
        WavAudioFormat wavFormat = new WavAudioFormat();
        // gets surrogate
        MediaBean mediaBeanSurrogate = (MediaBean) wavFormat.extractProxy(mediaWavInputStream);
        mediaWavInputStream.close();

        // check access to content and check format
        byte[] content = mediaBeanSurrogate.getContent();
        byte[] content_ = new byte[content.length];
        System.arraycopy(content, 0, content_, 0, content.length);

        assertTrue("test 1", mediaBeanSurrogate instanceof Media);
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: getDefaultMimeType()
     *        Testcase Number: TEMB0075SBA
     *
     *        Test 1:
     *        call getDefaultMimeType
     *        ---&gt;check if result equals &quot;audio/x-pn-wav&quot;
     *
     *
     *
     * </pre>
     */
    public void testTEMB0075SBA() throws MediaException {
        //
        // test 1
        //

        // creates mpeg video format
        WavAudioFormat wavFormat = new WavAudioFormat();

        assertEquals("test 1: ", "audio/x-pn-wav", wavFormat.getDefaultMimeType());
        testTrace("test 1 passed");

        succeed();
    }

    /**
     * <pre>
     *
     *
     *
     *        Testcase Name: isStreamingDesirable()
     *        Testcase Number: TEMB0076SBA
     *
     *        Test 1:
     *        call isStreamingDesirable
     *        ---&gt;check if result is true
     *
     *
     *
     * </pre>
     */
    public void testTEMB0076SBA() throws MediaException {
        //
        // test 1
        //

        // creates mpeg video format
        WavAudioFormat wavFormat = new WavAudioFormat();

        assertTrue("test 1: ", wavFormat.isStreamingDesirable());
        testTrace("test 1 passed");

        succeed();
    }

}