diff options
Diffstat (limited to 'tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java')
-rw-r--r-- | tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java | 67 |
1 files changed, 48 insertions, 19 deletions
diff --git a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java index 789fd3563..8c4fc3dea 100644 --- a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java @@ -20,17 +20,18 @@ 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; -import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; public class ExifOutputStreamTest extends ExifXmlDataTestCase { private File mTmpFile; + private ExifInterface mInterface; + @Override public void setUp() throws Exception { super.setUp(); @@ -39,10 +40,12 @@ public class ExifOutputStreamTest extends ExifXmlDataTestCase { public ExifOutputStreamTest(int imgRes, int xmlRes) { super(imgRes, xmlRes); + mInterface = new ExifInterface(); } public ExifOutputStreamTest(String imgPath, String xmlPath) { super(imgPath, xmlPath); + mInterface = new ExifInterface(); } public void testExifOutputStream() throws Exception { @@ -50,58 +53,84 @@ public class ExifOutputStreamTest extends ExifXmlDataTestCase { InputStream exifInputStream = null; FileInputStream reDecodeInputStream = null; FileInputStream reParseInputStream = null; + + InputStream dangerInputStream = null; + OutputStream dangerOutputStream = null; try { try { - byte[] imgData = readToByteArray(getImageInputStream()); + byte[] imgData = Util.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; + if (bmp == null) { + return; + } // Read exif data - ExifData exifData = new ExifReader().read(exifInputStream); + ExifData exifData = new ExifReader(mInterface).read(exifInputStream); // Encode the image with the exif data FileOutputStream outputStream = new FileOutputStream(mTmpFile); - ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream); + ExifOutputStream exifOutputStream = new ExifOutputStream(outputStream, mInterface); exifOutputStream.setExifData(exifData); - bmp.compress(Bitmap.CompressFormat.JPEG, 100, exifOutputStream); + bmp.compress(Bitmap.CompressFormat.JPEG, 90, exifOutputStream); exifOutputStream.close(); + exifOutputStream = null; // Re-decode the temp file and check the data. reDecodeInputStream = new FileInputStream(mTmpFile); Bitmap decodedBmp = BitmapFactory.decodeStream(reDecodeInputStream); assertNotNull(getImageTitle(), decodedBmp); + reDecodeInputStream.close(); // Re-parse the temp file the check EXIF tag reParseInputStream = new FileInputStream(mTmpFile); - ExifData reExifData = new ExifReader().read(reParseInputStream); + ExifData reExifData = new ExifReader(mInterface).read(reParseInputStream); + assertEquals(getImageTitle(), exifData, reExifData); + reParseInputStream.close(); + + // Try writing exif to file with existing exif. + dangerOutputStream = (OutputStream) new FileOutputStream(mTmpFile); + exifOutputStream = new ExifOutputStream(dangerOutputStream, mInterface); + exifOutputStream.setExifData(exifData); + exifOutputStream.write(imgData); + // exifOutputStream.write(strippedImgData); + exifOutputStream.close(); + exifOutputStream = null; + + // Make sure it still can be parsed into a bitmap. + dangerInputStream = (InputStream) new FileInputStream(mTmpFile); + decodedBmp = null; + decodedBmp = BitmapFactory.decodeStream(dangerInputStream); + assertNotNull(getImageTitle(), decodedBmp); + dangerInputStream.close(); + dangerInputStream = null; + + // Make sure exif is still well-formatted. + dangerInputStream = (InputStream) new FileInputStream(mTmpFile); + reExifData = null; + reExifData = new ExifReader(mInterface).read(dangerInputStream); assertEquals(getImageTitle(), exifData, reExifData); + dangerInputStream.close(); + dangerInputStream = null; + } finally { Util.closeSilently(imageInputStream); Util.closeSilently(exifInputStream); Util.closeSilently(reDecodeInputStream); Util.closeSilently(reParseInputStream); + + Util.closeSilently(dangerInputStream); + Util.closeSilently(dangerOutputStream); } } 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() throws Exception { super.tearDown(); |