diff options
author | Hung-ying Tyan <tyanh@google.com> | 2012-10-16 23:21:19 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-10-16 23:21:20 -0700 |
commit | 58fb8e9280d4446451108270fd78b0afa81e1089 (patch) | |
tree | c41073589d504ca409b5f2738fbc633f6676bfbc | |
parent | decf20d88726b828fe1d41c65913485623d716f7 (diff) | |
parent | cdd116de25c9a9cc14704cb7348077ed99c7ade9 (diff) | |
download | android_packages_apps_Snap-58fb8e9280d4446451108270fd78b0afa81e1089.tar.gz android_packages_apps_Snap-58fb8e9280d4446451108270fd78b0afa81e1089.tar.bz2 android_packages_apps_Snap-58fb8e9280d4446451108270fd78b0afa81e1089.zip |
Merge "Read exif test images from the external storage of the test package" into gb-ub-photos-bryce
6 files changed, 166 insertions, 76 deletions
diff --git a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java index ad603df39..1950c7e86 100644 --- a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java @@ -19,32 +19,39 @@ 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; import java.io.IOException; import java.io.InputStream; +import java.util.List; +import java.util.Map; public class ExifOutputStreamTest extends ExifXmlDataTestCase { - public ExifOutputStreamTest(int imageResourceId, int xmlResourceId) { - super(imageResourceId, xmlResourceId); + public ExifOutputStreamTest(int imgRes, int xmlRes) { + super(imgRes, xmlRes); } - public void testExifOutputStream() throws IOException, ExifInvalidFormatException { + public ExifOutputStreamTest(String imgPath, String xmlPath) { + super(imgPath, xmlPath); + } + + public void testExifOutputStream() throws Exception { File file = File.createTempFile("exif_test", ".jpg"); 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); + byte[] imgData = readToByteArray(getImageInputStream()); + imageInputStream = new ByteArrayInputStream(imgData); + exifInputStream = new ByteArrayInputStream(imgData); + // Read the image data + 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 @@ -70,4 +77,15 @@ public class ExifOutputStreamTest extends ExifXmlDataTestCase { Util.closeSilently(reParseInputStream); } } -}
\ No newline at end of file + + 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(); + } +} diff --git a/tests/src/com/android/gallery3d/exif/ExifParserTest.java b/tests/src/com/android/gallery3d/exif/ExifParserTest.java index 8d4bc3db3..f89ac29ff 100644 --- a/tests/src/com/android/gallery3d/exif/ExifParserTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifParserTest.java @@ -20,31 +20,30 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.IOException; -import java.io.InputStream; import java.util.List; import java.util.Map; public class ExifParserTest extends ExifXmlDataTestCase { private static final String TAG = "ExifParserTest"; - private List<Map<Short, String>> mGroundTruth; - - private InputStream mImageInputStream; + public ExifParserTest(int imgRes, int xmlRes) { + super(imgRes, xmlRes); + } - public ExifParserTest(int imageResourceId, int xmlResourceId) { - super(imageResourceId, xmlResourceId); + public ExifParserTest(String imgPath, String xmlPath) { + super(imgPath, xmlPath); } - @Override - protected void setUp() throws Exception { - mImageInputStream = getInstrumentation() - .getContext().getResources().openRawResource(mImageResourceId); + private List<Map<Short, String>> mGroundTruth; - mGroundTruth = ExifXmlReader.readXml(getInstrumentation().getContext(), mXmlResourceId); + @Override + public void setUp() throws Exception { + super.setUp(); + mGroundTruth = ExifXmlReader.readXml(getXmlParser()); } - public void testParse() throws IOException, ExifInvalidFormatException { - ExifParser parser = ExifParser.parse(mImageInputStream); + public void testParse() throws Exception { + ExifParser parser = ExifParser.parse(getImageInputStream()); int event = parser.next(); while (event != ExifParser.EVENT_END) { switch (event) { @@ -72,7 +71,6 @@ public class ExifParserTest extends ExifXmlDataTestCase { } } - private void checkTag(ExifTag tag) { // Ignore offset tags since the ground-truth from exiftool doesn't have it. // We can verify it by examining the sub-IFD or thumbnail itself. @@ -92,7 +90,7 @@ public class ExifParserTest extends ExifXmlDataTestCase { throws IOException, ExifInvalidFormatException { Map<Short, String> expectedResult = mGroundTruth.get(ifd); int numOfTag = 0; - ExifParser parser = ExifParser.parse(mImageInputStream, options); + ExifParser parser = ExifParser.parse(getImageInputStream(), options); int event = parser.next(); while(event != ExifParser.EVENT_END) { switch (event) { @@ -127,24 +125,24 @@ public class ExifParserTest extends ExifXmlDataTestCase { assertEquals(expectedResult.size(), numOfTag); } - public void testOnlyExifIfd() throws IOException, ExifInvalidFormatException { + public void testOnlyExifIfd() throws Exception { parseOneIfd(IfdId.TYPE_IFD_EXIF, ExifParser.OPTION_IFD_EXIF); } - public void testOnlyIfd0() throws IOException, ExifInvalidFormatException { + public void testOnlyIfd0() throws Exception { parseOneIfd(IfdId.TYPE_IFD_0, ExifParser.OPTION_IFD_0); } - public void testOnlyIfd1() throws IOException, ExifInvalidFormatException { + public void testOnlyIfd1() throws Exception { parseOneIfd(IfdId.TYPE_IFD_1, ExifParser.OPTION_IFD_1); } - public void testOnlyInteroperabilityIfd() throws IOException, ExifInvalidFormatException { + public void testOnlyInteroperabilityIfd() throws Exception { parseOneIfd(IfdId.TYPE_IFD_INTEROPERABILITY, ExifParser.OPTION_IFD_INTEROPERABILITY); } - public void testOnlyReadSomeTag() throws IOException, ExifInvalidFormatException { - ExifParser parser = ExifParser.parse(mImageInputStream, ExifParser.OPTION_IFD_0); + public void testOnlyReadSomeTag() throws Exception { + ExifParser parser = ExifParser.parse(getImageInputStream(), ExifParser.OPTION_IFD_0); int event = parser.next(); boolean isTagFound = false; while (event != ExifParser.EVENT_END) { @@ -176,8 +174,8 @@ public class ExifParserTest extends ExifXmlDataTestCase { assertTrue(isTagFound); } - public void testReadThumbnail() throws ExifInvalidFormatException, IOException { - ExifParser parser = ExifParser.parse(mImageInputStream, + public void testReadThumbnail() throws Exception { + ExifParser parser = ExifParser.parse(getImageInputStream(), ExifParser.OPTION_IFD_1 | ExifParser.OPTION_THUMBNAIL); int event = parser.next(); @@ -206,10 +204,4 @@ public class ExifParserTest extends ExifXmlDataTestCase { assertNotNull(bmp); } } - - @Override - protected void tearDown() throws IOException { - mImageInputStream.close(); - mGroundTruth.clear(); - } } diff --git a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java index 1300af0e9..3a78e091b 100644 --- a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java @@ -16,42 +16,28 @@ package com.android.gallery3d.exif; -import android.content.res.XmlResourceParser; import android.graphics.BitmapFactory; -import java.io.IOException; -import java.io.InputStream; import java.util.List; import java.util.Map; public class ExifReaderTest extends ExifXmlDataTestCase { private static final String TAG = "ExifReaderTest"; - private List<Map<Short, String>> mGroundTruth; - - private InputStream mImageInputStream; - - public ExifReaderTest(int imageResourceId, int xmlResourceId) { - super(imageResourceId, xmlResourceId); + public ExifReaderTest(int imgRes, int xmlRes) { + super(imgRes, xmlRes); } - @Override - public void setUp() throws Exception { - mImageInputStream = getInstrumentation() - .getContext().getResources().openRawResource(mImageResourceId); - - XmlResourceParser parser = - getInstrumentation().getContext().getResources().getXml(mXmlResourceId); - - mGroundTruth = ExifXmlReader.readXml(getInstrumentation().getContext(), mXmlResourceId); - parser.close(); + public ExifReaderTest(String imgPath, String xmlPath) { + super(imgPath, xmlPath); } - public void testRead() throws ExifInvalidFormatException, IOException { + public void testRead() throws Exception { ExifReader reader = new ExifReader(); - ExifData exifData = reader.read(mImageInputStream); + ExifData exifData = reader.read(getImageInputStream()); + List<Map<Short, String>> groundTruth = ExifXmlReader.readXml(getXmlParser()); for (int i = 0; i < IfdId.TYPE_IFD_COUNT; i++) { - checkIfd(exifData.getIfdData(i), mGroundTruth.get(i)); + checkIfd(exifData.getIfdData(i), groundTruth.get(i)); } checkThumbnail(exifData); } @@ -124,9 +110,4 @@ public class ExifReaderTest extends ExifXmlDataTestCase { } assertEquals(ifdValue.size(), size); } - - @Override - public void tearDown() throws Exception { - mImageInputStream.close(); - } } diff --git a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java index 57a7111ad..519e87107 100644 --- a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java +++ b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java @@ -16,6 +16,8 @@ package com.android.gallery3d.exif; +import android.content.Context; +import android.os.Environment; import android.test.InstrumentationTestRunner; import android.test.InstrumentationTestSuite; import android.util.Log; @@ -25,8 +27,11 @@ import com.android.gallery3d.tests.R; import junit.framework.TestCase; import junit.framework.TestSuite; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; public class ExifTestRunner extends InstrumentationTestRunner { private static final String TAG = "ExifTestRunner"; @@ -34,12 +39,17 @@ public class ExifTestRunner extends InstrumentationTestRunner { private static final int[] IMG_RESOURCE = { R.raw.galaxy_nexus }; + private static final int[] EXIF_DATA_RESOURCE = { R.xml.galaxy_nexus }; + private static List<String> mTestImgPath = new ArrayList<String>(); + private static List<String> mTestXmlPath = new ArrayList<String>(); + @Override public TestSuite getAllTests() { + getTestImagePath(); TestSuite suite = new InstrumentationTestSuite(this); suite.addTestSuite(ExifDataTest.class); suite.addTestSuite(ExifTagTest.class); @@ -49,6 +59,25 @@ public class ExifTestRunner extends InstrumentationTestRunner { return suite; } + private void getTestImagePath() { + Context context = getContext(); + File imgDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); + File xmlDir = new File(context.getExternalFilesDir(null).getPath(), "Xml"); + + if (imgDir != null && xmlDir != null) { + String[] imgs = imgDir.list(); + if (imgs == null) return; + for (String imgName: imgs) { + String xmlName = imgName.substring(0, imgName.lastIndexOf('.')) + ".xml"; + File xmlFile = new File(xmlDir, xmlName); + if (xmlFile.exists()) { + mTestImgPath.add(new File(imgDir, imgName).getAbsolutePath()); + mTestXmlPath.add(xmlFile.getAbsolutePath()); + } + } + } + } + private void addAllTestsFromExifTestCase(Class<? extends ExifXmlDataTestCase> testClass, TestSuite suite) { for (Method method : testClass.getDeclaredMethods()) { @@ -72,6 +101,25 @@ public class ExifTestRunner extends InstrumentationTestRunner { Log.e(TAG, "Failed to create test case", e); } } + for (int i = 0, n = mTestImgPath.size(); i < n; i++) { + TestCase test; + try { + test = testClass.getDeclaredConstructor(String.class, String.class). + newInstance(mTestImgPath.get(i), mTestXmlPath.get(i)); + test.setName(method.getName()); + suite.addTest(test); + } catch (IllegalArgumentException e) { + Log.e(TAG, "Failed to create test case", e); + } catch (InstantiationException e) { + Log.e(TAG, "Failed to create test case", e); + } catch (IllegalAccessException e) { + Log.e(TAG, "Failed to create test case", e); + } catch (InvocationTargetException e) { + Log.e(TAG, "Failed to create test case", e); + } catch (NoSuchMethodException e) { + Log.e(TAG, "Failed to create test case", e); + } + } } } } diff --git a/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java b/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java index 41b315181..16a7c6821 100644 --- a/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java +++ b/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java @@ -17,14 +17,65 @@ package com.android.gallery3d.exif; +import android.content.res.Resources; import android.test.InstrumentationTestCase; +import android.util.Xml; + +import org.xmlpull.v1.XmlPullParser; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.InputStreamReader; public class ExifXmlDataTestCase extends InstrumentationTestCase { - protected final int mImageResourceId; - protected final int mXmlResourceId; + + private InputStream mImageInputStream; + private InputStream mXmlInputStream; + private XmlPullParser mXmlParser; + private final String mImagePath; + private final String mXmlPath; + private final int mImageResourceId; + private final int mXmlResourceId; public ExifXmlDataTestCase(int imageRes, int xmlRes) { + mImagePath = null; + mXmlPath = null; mImageResourceId = imageRes; mXmlResourceId = xmlRes; } + + public ExifXmlDataTestCase(String imagePath, String xmlPath) { + mImagePath = imagePath; + mXmlPath = xmlPath; + mImageResourceId = 0; + mXmlResourceId = 0; + } + + protected InputStream getImageInputStream() { + return mImageInputStream; + } + + protected XmlPullParser getXmlParser() { + return mXmlParser; + } + + @Override + public void setUp() throws Exception { + if (mImagePath != null) { + mImageInputStream = new FileInputStream(mImagePath); + mXmlInputStream = new FileInputStream(mXmlPath); + mXmlParser = Xml.newPullParser(); + mXmlParser.setInput(new InputStreamReader(mXmlInputStream)); + } else { + Resources res = getInstrumentation().getContext().getResources(); + mImageInputStream = res.openRawResource(mImageResourceId); + mXmlParser = res.getXml(mXmlResourceId); + } + } + + @Override + public void tearDown() throws Exception { + Util.closeSilently(mImageInputStream); + Util.closeSilently(mXmlInputStream); + } } diff --git a/tests/src/com/android/gallery3d/exif/ExifXmlReader.java b/tests/src/com/android/gallery3d/exif/ExifXmlReader.java index 8e2ad3f30..139b0ca47 100644 --- a/tests/src/com/android/gallery3d/exif/ExifXmlReader.java +++ b/tests/src/com/android/gallery3d/exif/ExifXmlReader.java @@ -16,13 +16,10 @@ package com.android.gallery3d.exif; -import android.content.Context; - import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; -import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -36,7 +33,7 @@ public class ExifXmlReader { private static final String EXIF_IFD = "ExifIFD"; private static final String GPS_IFD = "GPS"; private static final String IFD1 = "IFD1"; - private static final String PREFIX_INTEROP_IFD = "InteropIFD"; + private static final String INTEROP_IFD = "InteropIFD"; private static final String ATTR_ID = "id"; private static final String ATTR_IFD = "ifd"; @@ -47,9 +44,8 @@ public class ExifXmlReader { * @throws XmlPullParserException * @throws IOException */ - static public List<Map<Short, String>> readXml(Context context, int xmlResId) + static public List<Map<Short, String>> readXml(XmlPullParser parser) throws XmlPullParserException, IOException { - XmlPullParser parser = context.getResources().getXml(xmlResId); List<Map<Short, String>> exifData = new ArrayList<Map<Short, String>>(IfdId.TYPE_IFD_COUNT); @@ -80,7 +76,11 @@ public class ExifXmlReader { parser.next(); } - exifData.get(ifdId).put(id, value); + if (ifdId < 0) { + // TODO: the MarkerNote segment. + } else { + exifData.get(ifdId).put(id, value); + } parser.require(XmlPullParser.END_TAG, null, null); } @@ -96,7 +96,7 @@ public class ExifXmlReader { return IfdId.TYPE_IFD_GPS; } else if (IFD1.equals(prefix)) { return IfdId.TYPE_IFD_1; - } else if (PREFIX_INTEROP_IFD.equals(prefix)) { + } else if (INTEROP_IFD.equals(prefix)) { return IfdId.TYPE_IFD_INTEROPERABILITY; } else { assert(false); |