

package com.twelvemonkeys.imageio.plugins.jpeg;

import com.twelvemonkeys.imageio.metadata.jpeg.JPEG;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegment;
import com.twelvemonkeys.imageio.metadata.jpeg.JPEGSegmentUtil;
import org.junit.Test;
import org.mockito.InOrder;

import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * JFIFThumbnailReaderTest
 *
 * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
 * @author last modified by $Author: haraldk$
 * @version $Id: JFIFThumbnailReaderTest.java,v 1.0 04.05.12 15:56 haraldk Exp$
 */
public class JFIFThumbnailReaderTest extends AbstractThumbnailReaderTest {
    @Override
    protected JFIFThumbnailReader createReader(ThumbnailReadProgressListener progressListener, int imageIndex, int thumbnailIndex, ImageInputStream stream) throws IOException {
        List<JPEGSegment> segments = JPEGSegmentUtil.readSegments(stream, JPEG.APP0, "JFIF");
        stream.close();

        assertNotNull(segments);
        assertFalse(segments.isEmpty());

        JPEGSegment segment = segments.get(0);
        return new JFIFThumbnailReader(progressListener, imageIndex, thumbnailIndex, JFIF.read(new DataInputStream(segment.segmentData()), segment.segmentLength()));
    }

    @Test
    public void testReadRaw() throws IOException {
        ThumbnailReader reader = createReader(mock(ThumbnailReadProgressListener.class), 0, 0, createStream("/jpeg/jfif-jfif-and-exif-thumbnail-sharpshot-iphone.jpg"));

        assertEquals(131, reader.getWidth());
        assertEquals(122, reader.getHeight());

        BufferedImage thumbnail = reader.read();
        assertNotNull(thumbnail);
        assertEquals(131, thumbnail.getWidth());
        assertEquals(122, thumbnail.getHeight());
    }

    @Test
    public void testReadNonSpecGray() throws IOException {
        ThumbnailReader reader = createReader(mock(ThumbnailReadProgressListener.class), 0, 0, createStream("/jpeg/jfif-grayscale-thumbnail.jpg"));

        assertEquals(127, reader.getWidth());
        assertEquals(76, reader.getHeight());

        BufferedImage thumbnail = reader.read();
        assertNotNull(thumbnail);
        assertEquals(BufferedImage.TYPE_BYTE_GRAY, thumbnail.getType());
        assertEquals(127, thumbnail.getWidth());
        assertEquals(76, thumbnail.getHeight());
    }

    @Test
    public void testProgressListenerRaw() throws IOException {
        ThumbnailReadProgressListener listener = mock(ThumbnailReadProgressListener.class);

        createReader(listener, 0, 99, createStream("/jpeg/jfif-jfif-and-exif-thumbnail-sharpshot-iphone.jpg")).read();

        InOrder order = inOrder(listener);
        order.verify(listener).thumbnailStarted(0, 99);
        order.verify(listener, atLeastOnce()).thumbnailProgress(100f);
        order.verify(listener).thumbnailComplete();
    }
}
