diff options
Diffstat (limited to 'tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java')
-rw-r--r-- | tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java | 108 |
1 files changed, 72 insertions, 36 deletions
diff --git a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java index ad603df39..8c98fa037 100644 --- a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java @@ -19,6 +19,8 @@ package com.android.gallery3d.exif; import android.graphics.Bitmap; import android.graphics.BitmapFactory; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -26,48 +28,82 @@ import java.io.IOException; import java.io.InputStream; public class ExifOutputStreamTest extends ExifXmlDataTestCase { - public ExifOutputStreamTest(int imageResourceId, int xmlResourceId) { - super(imageResourceId, xmlResourceId); + + private File mTmpFile; + + @Override + public void setUp() throws Exception { + super.setUp(); + mTmpFile = File.createTempFile("exif_test", ".jpg"); + } + + public ExifOutputStreamTest(int imgRes, int xmlRes) { + super(imgRes, xmlRes); } - public void testExifOutputStream() throws IOException, ExifInvalidFormatException { - File file = File.createTempFile("exif_test", ".jpg"); + public ExifOutputStreamTest(String imgPath, String xmlPath) { + super(imgPath, xmlPath); + } + + public void testExifOutputStream() throws Exception { InputStream imageInputStream = null; InputStream exifInputStream = null; FileInputStream reDecodeInputStream = null; FileInputStream reParseInputStream = null; try { - // Read the image - imageInputStream = getInstrumentation() - .getContext().getResources().openRawResource(mImageResourceId); - Bitmap bmp = BitmapFactory.decodeStream(imageInputStream); - - // Read exif data - exifInputStream = getInstrumentation() - .getContext().getResources().openRawResource(mImageResourceId); - ExifData exifData = new ExifReader().read(exifInputStream); - - // Encode the image with the exif data - FileOutputStream outputStream = new FileOutputStream(file); - ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream); - exifOutputStream.setExifData(exifData); - bmp.compress(Bitmap.CompressFormat.JPEG, 100, exifOutputStream); - exifOutputStream.close(); - - // Re-decode the temp file and check the data. - reDecodeInputStream = new FileInputStream(file); - Bitmap decodedBmp = BitmapFactory.decodeStream(reDecodeInputStream); - assertNotNull(decodedBmp); - - // Re-parse the temp file the check EXIF tag - reParseInputStream = new FileInputStream(file); - ExifData reExifData = new ExifReader().read(reParseInputStream); - assertEquals(exifData, reExifData); - } finally { - Util.closeSilently(imageInputStream); - Util.closeSilently(exifInputStream); - Util.closeSilently(reDecodeInputStream); - Util.closeSilently(reParseInputStream); + try { + byte[] imgData = readToByteArray(getImageInputStream()); + imageInputStream = new ByteArrayInputStream(imgData); + exifInputStream = new ByteArrayInputStream(imgData); + + // Read the image data + Bitmap bmp = BitmapFactory.decodeStream(imageInputStream); + // The image is invalid + if (bmp == null) return; + + // Read exif data + ExifData exifData = new ExifReader().read(exifInputStream); + + // Encode the image with the exif data + FileOutputStream outputStream = new FileOutputStream(mTmpFile); + ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream); + exifOutputStream.setExifData(exifData); + bmp.compress(Bitmap.CompressFormat.JPEG, 100, exifOutputStream); + exifOutputStream.close(); + + // Re-decode the temp file and check the data. + reDecodeInputStream = new FileInputStream(mTmpFile); + Bitmap decodedBmp = BitmapFactory.decodeStream(reDecodeInputStream); + assertNotNull(getImageTitle(), decodedBmp); + + // Re-parse the temp file the check EXIF tag + reParseInputStream = new FileInputStream(mTmpFile); + ExifData reExifData = new ExifReader().read(reParseInputStream); + assertEquals(getImageTitle(), exifData, reExifData); + } finally { + Util.closeSilently(imageInputStream); + Util.closeSilently(exifInputStream); + Util.closeSilently(reDecodeInputStream); + Util.closeSilently(reParseInputStream); + } + } catch (Exception e) { + throw new Exception(getImageTitle(), e); + } + } + + private byte[] readToByteArray(InputStream is) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + int len; + byte[] buf = new byte[1024]; + while ((len = is.read(buf)) > -1) { + bos.write(buf, 0, len); } + bos.flush(); + return bos.toByteArray(); + } + + @Override + public void tearDown() { + mTmpFile.delete(); } -}
\ No newline at end of file +} |