From 2abb15cd5ac2dc5987c409023af2d1bf7851d74c Mon Sep 17 00:00:00 2001 From: Earl Ou Date: Wed, 19 Sep 2012 11:59:45 +0800 Subject: Tests for ExifData Bug:7192363 Change-Id: Ic74ecad2eff50e26efa85f928ef7904a2c51ecc7 --- .../com/android/gallery3d/exif/ExifDataTest.java | 54 ++++++ .../gallery3d/exif/ExifOutputStreamTest.java | 10 +- .../com/android/gallery3d/exif/ExifParserTest.java | 9 +- .../com/android/gallery3d/exif/ExifReaderTest.java | 9 +- .../com/android/gallery3d/exif/ExifTagTest.java | 207 +++++++++++++++++++++ .../com/android/gallery3d/exif/ExifTestRunner.java | 11 +- .../gallery3d/exif/ExifXmlDataTestCase.java | 30 +++ 7 files changed, 304 insertions(+), 26 deletions(-) create mode 100644 tests/src/com/android/gallery3d/exif/ExifDataTest.java create mode 100644 tests/src/com/android/gallery3d/exif/ExifTagTest.java create mode 100644 tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java (limited to 'tests') diff --git a/tests/src/com/android/gallery3d/exif/ExifDataTest.java b/tests/src/com/android/gallery3d/exif/ExifDataTest.java new file mode 100644 index 000000000..ba656bfa8 --- /dev/null +++ b/tests/src/com/android/gallery3d/exif/ExifDataTest.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.exif; + +import junit.framework.TestCase; + +import java.nio.ByteOrder; + +public class ExifDataTest extends TestCase { + public void testAddTag() { + ExifData exifData = new ExifData(ByteOrder.BIG_ENDIAN); + // IFD0 tag + exifData.addTag(ExifTag.TAG_MAKE).setValue("test"); + exifData.addTag(ExifTag.TAG_IMAGE_WIDTH).setValue(1000); + + // EXIF tag + exifData.addTag(ExifTag.TAG_ISO_SPEED_RATINGS).setValue(1); + + // GPS tag + exifData.addTag(ExifTag.TAG_GPS_ALTITUDE).setValue(new Rational(10, 100)); + + // Interoperability tag + exifData.addInteroperabilityTag(ExifTag.TAG_INTEROPERABILITY_INDEX).setValue("inter_test"); + + // IFD1 tag + exifData.addThumbnailTag(ExifTag.TAG_MAKE).setValue("test_thumb"); + exifData.addThumbnailTag(ExifTag.TAG_IMAGE_WIDTH).setValue(100); + + // check data + assertEquals("test", exifData.getTag(ExifTag.TAG_MAKE).getString()); + assertEquals(1000, exifData.getTag(ExifTag.TAG_IMAGE_WIDTH).getUnsignedLong(0)); + assertEquals(1, exifData.getTag(ExifTag.TAG_ISO_SPEED_RATINGS).getUnsignedShort(0)); + assertEquals(new Rational(10, 100), + exifData.getTag(ExifTag.TAG_GPS_ALTITUDE).getRational(0)); + assertEquals("inter_test", + exifData.getInteroperabilityTag(ExifTag.TAG_INTEROPERABILITY_INDEX).getString()); + assertEquals("test_thumb", exifData.getThumbnailTag(ExifTag.TAG_MAKE).getString()); + assertEquals(100, exifData.getThumbnailTag(ExifTag.TAG_IMAGE_WIDTH).getUnsignedLong(0)); + } +} diff --git a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java index fb85b2c91..ad603df39 100644 --- a/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifOutputStreamTest.java @@ -18,7 +18,6 @@ package com.android.gallery3d.exif; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.test.InstrumentationTestCase; import java.io.File; import java.io.FileInputStream; @@ -26,12 +25,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; -public class ExifOutputStreamTest extends InstrumentationTestCase { - - private final int mImageResourceId; - - public ExifOutputStreamTest(int imageResourceId, int xmlReourceId) { - mImageResourceId = imageResourceId; +public class ExifOutputStreamTest extends ExifXmlDataTestCase { + public ExifOutputStreamTest(int imageResourceId, int xmlResourceId) { + super(imageResourceId, xmlResourceId); } public void testExifOutputStream() throws IOException, ExifInvalidFormatException { diff --git a/tests/src/com/android/gallery3d/exif/ExifParserTest.java b/tests/src/com/android/gallery3d/exif/ExifParserTest.java index 549fb0681..c1ad8341b 100644 --- a/tests/src/com/android/gallery3d/exif/ExifParserTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifParserTest.java @@ -19,18 +19,14 @@ package com.android.gallery3d.exif; import android.content.res.XmlResourceParser; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.test.InstrumentationTestCase; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; -public class ExifParserTest extends InstrumentationTestCase { +public class ExifParserTest extends ExifXmlDataTestCase { private static final String TAG = "ExifParserTest"; - private final int mImageResourceId; - private final int mXmlResourceId; - private HashMap mIfd0Value = new HashMap(); private HashMap mIfd1Value = new HashMap(); private HashMap mExifIfdValue = new HashMap(); @@ -39,8 +35,7 @@ public class ExifParserTest extends InstrumentationTestCase { private InputStream mImageInputStream; public ExifParserTest(int imageResourceId, int xmlResourceId) { - mImageResourceId = imageResourceId; - mXmlResourceId = xmlResourceId; + super(imageResourceId, xmlResourceId); } @Override diff --git a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java index 74b8bd3d8..269120870 100644 --- a/tests/src/com/android/gallery3d/exif/ExifReaderTest.java +++ b/tests/src/com/android/gallery3d/exif/ExifReaderTest.java @@ -18,18 +18,14 @@ package com.android.gallery3d.exif; import android.content.res.XmlResourceParser; import android.graphics.BitmapFactory; -import android.test.InstrumentationTestCase; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; -public class ExifReaderTest extends InstrumentationTestCase { +public class ExifReaderTest extends ExifXmlDataTestCase { private static final String TAG = "ExifReaderTest"; - private final int mImageResourceId; - private final int mXmlResourceId; - private final HashMap mIfd0Value = new HashMap(); private final HashMap mIfd1Value = new HashMap(); private final HashMap mExifIfdValue = new HashMap(); @@ -38,8 +34,7 @@ public class ExifReaderTest extends InstrumentationTestCase { private InputStream mImageInputStream; public ExifReaderTest(int imageResourceId, int xmlResourceId) { - mImageResourceId = imageResourceId; - mXmlResourceId = xmlResourceId; + super(imageResourceId, xmlResourceId); } @Override diff --git a/tests/src/com/android/gallery3d/exif/ExifTagTest.java b/tests/src/com/android/gallery3d/exif/ExifTagTest.java new file mode 100644 index 000000000..128956dee --- /dev/null +++ b/tests/src/com/android/gallery3d/exif/ExifTagTest.java @@ -0,0 +1,207 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.gallery3d.exif; + +import junit.framework.TestCase; + +public class ExifTagTest extends TestCase { + + private static long MAX_UNSIGNED_LONG = (1L << 32) - 1; + private static int MAX_LONG = Integer.MAX_VALUE; + private static int MIN_LONG = Integer.MIN_VALUE; + + private static final ExifTag sTestTags[] = { + ExifTag.buildTag(ExifTag.TAG_EXIF_VERSION), // TYPE_UNDEFINED with 4 components + ExifTag.buildTag(ExifTag.TAG_GPS_VERSION_ID), // TYPE_UNSIGNED_BYTE with 4 components + ExifTag.buildTag(ExifTag.TAG_DATE_TIME), // TYPE_ASCII with 20 components + ExifTag.buildTag(ExifTag.TAG_COMPRESSION), // TYPE_UNSIGNED_SHORT with 1 components + // TYPE_UNSIGNED_LONG with 1 components + ExifTag.buildTag(ExifTag.TAG_JPEG_INTERCHANGE_FORMAT), + ExifTag.buildTag(ExifTag.TAG_GPS_LONGITUDE), // TYPE_UNSIGNED_RATIONAL with 3 components + ExifTag.buildTag(ExifTag.TAG_SHUTTER_SPEED_VALUE), // TYPE_RATIONAL with 1 components + // There is no tag defined with TYPE_LONG. Create a dummy one for testing. + new ExifTag((short) 0, ExifTag.TYPE_LONG, 1, 0) + }; + + public void testValueType() { + for (ExifTag tag: sTestTags) { + int count = tag.getComponentCount(); + int intBuf[] = new int[count]; + long longBuf[] = new long[count]; + byte byteBuf[] = new byte[count]; + Rational rationalBuf[] = new Rational[count]; + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < count; i++) { + intBuf[i] = 0; + longBuf[i] = 0; + byteBuf[i] = 0; + rationalBuf[i] = new Rational(0, 0); + // The string size should equal to component count - 1 + if (i != 0) sb.append("*"); + } + String strBuf = sb.toString(); + + checkTypeByte(tag, byteBuf); + checkTypeAscii(tag, strBuf); + checkTypeUnsignedShort(tag, intBuf); + checkTypeUnsignedLong(tag, intBuf, longBuf); + checkTypeLong(tag, intBuf); + checkTypeRational(tag, rationalBuf); + checkTypeUnsignedRational(tag, rationalBuf); + } + } + + private void checkTypeByte(ExifTag tag, byte[] buf) { + boolean excepThrow = false; + short type = tag.getDataType(); + try { + tag.setValue(buf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + assertTrue("Tag ID: " + tag.getTagId(), + (type == ExifTag.TYPE_UNDEFINED || type == ExifTag.TYPE_UNSIGNED_BYTE) + ^ excepThrow); + } + + private void checkTypeAscii(ExifTag tag, String str) { + boolean excepThrow = false; + try { + tag.setValue(str); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + assertTrue("Tag ID: " + tag.getTagId(), + tag.getDataType() == ExifTag.TYPE_ASCII ^ excepThrow); + } + + private void checkTypeUnsignedShort(ExifTag tag, int[] intBuf) { + boolean excepThrow = false; + short type = tag.getDataType(); + try { + tag.setValue(intBuf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + assertTrue("Tag ID: " + tag.getTagId(), + (type == ExifTag.TYPE_UNSIGNED_SHORT + || type == ExifTag.TYPE_UNSIGNED_LONG || type == ExifTag.TYPE_LONG) ^ excepThrow); + } + + private void checkTypeUnsignedLong(ExifTag tag, int[] intBuf, long[] longBuf) { + + // Test value only for unsigned long. + boolean excepThrow = false; + int count = intBuf.length; + try { + intBuf[count - 1] = MAX_LONG; + tag.setValue(intBuf); + longBuf[count - 1] = MAX_UNSIGNED_LONG; + tag.setValue(longBuf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + intBuf[count - 1] = 0; + assertTrue("Tag ID: " + tag.getTagId(), + tag.getDataType() == ExifTag.TYPE_UNSIGNED_LONG ^ excepThrow); + + + // Test invalid value for all type. + try { + longBuf[count - 1] = MAX_UNSIGNED_LONG + 1; + tag.setValue(longBuf); + fail(); + } catch (IllegalArgumentException expected) {} + longBuf[count - 1] = 0; + } + + private void checkTypeLong(ExifTag tag, int[] intBuf) { + boolean excepThrow = false; + int count = intBuf.length; + try { + intBuf[count - 1] = MAX_LONG; + tag.setValue(intBuf); + intBuf[count - 1] = MIN_LONG; + tag.setValue(intBuf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + intBuf[count - 1] = 0; + assertTrue("Tag ID: " + tag.getTagId(), + tag.getDataType() == ExifTag.TYPE_LONG ^ excepThrow); + } + + private void checkTypeRational(ExifTag tag, Rational rationalBuf[]) { + boolean excepThrow = false; + int count = rationalBuf.length; + Rational r = rationalBuf[count - 1]; + try { + rationalBuf[count - 1] = new Rational(MAX_LONG, MIN_LONG); + tag.setValue(rationalBuf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + assertTrue("Tag ID: " + tag.getTagId(), + tag.getDataType() == ExifTag.TYPE_RATIONAL ^ excepThrow); + + if(tag.getDataType() == ExifTag.TYPE_RATIONAL) { + // check overflow + try { + rationalBuf[count - 1] = new Rational(MAX_LONG + 1L, MIN_LONG); + tag.setValue(rationalBuf); + fail(); + } catch (IllegalArgumentException expected) {} + + try { + rationalBuf[count - 1] = new Rational(MAX_LONG, MIN_LONG - 1L); + tag.setValue(rationalBuf); + fail(); + } catch (IllegalArgumentException expected) {} + } + rationalBuf[count - 1] = r; + } + + private void checkTypeUnsignedRational(ExifTag tag, Rational rationalBuf[]) { + boolean excepThrow = false; + int count = rationalBuf.length; + Rational r = rationalBuf[count - 1]; + try { + rationalBuf[count - 1] = new Rational(MAX_UNSIGNED_LONG, MAX_UNSIGNED_LONG); + tag.setValue(rationalBuf); + } catch (IllegalArgumentException e) { + excepThrow = true; + } + assertTrue("Tag ID: " + tag.getTagId(), + tag.getDataType() == ExifTag.TYPE_UNSIGNED_RATIONAL ^ excepThrow); + + if(tag.getDataType() == ExifTag.TYPE_UNSIGNED_RATIONAL) { + // check overflow + try { + rationalBuf[count - 1] = new Rational(MAX_UNSIGNED_LONG + 1, 0); + tag.setValue(rationalBuf); + fail(); + } catch (IllegalArgumentException expected) {} + + try { + rationalBuf[count - 1] = new Rational(MAX_UNSIGNED_LONG, -1); + tag.setValue(rationalBuf); + fail(); + } catch (IllegalArgumentException expected) {} + } + rationalBuf[count - 1] = r; + } +} diff --git a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java index bcbc9f57b..57a7111ad 100644 --- a/tests/src/com/android/gallery3d/exif/ExifTestRunner.java +++ b/tests/src/com/android/gallery3d/exif/ExifTestRunner.java @@ -16,7 +16,6 @@ package com.android.gallery3d.exif; -import android.test.InstrumentationTestCase; import android.test.InstrumentationTestRunner; import android.test.InstrumentationTestSuite; import android.util.Log; @@ -42,13 +41,15 @@ public class ExifTestRunner extends InstrumentationTestRunner { @Override public TestSuite getAllTests() { TestSuite suite = new InstrumentationTestSuite(this); - getAllTestFromTestCase(ExifParserTest.class, suite); - getAllTestFromTestCase(ExifReaderTest.class, suite); - getAllTestFromTestCase(ExifOutputStreamTest.class, suite); + suite.addTestSuite(ExifDataTest.class); + suite.addTestSuite(ExifTagTest.class); + addAllTestsFromExifTestCase(ExifParserTest.class, suite); + addAllTestsFromExifTestCase(ExifReaderTest.class, suite); + addAllTestsFromExifTestCase(ExifOutputStreamTest.class, suite); return suite; } - private void getAllTestFromTestCase(Class testClass, + private void addAllTestsFromExifTestCase(Class testClass, TestSuite suite) { for (Method method : testClass.getDeclaredMethods()) { if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) { diff --git a/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java b/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java new file mode 100644 index 000000000..41b315181 --- /dev/null +++ b/tests/src/com/android/gallery3d/exif/ExifXmlDataTestCase.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.android.gallery3d.exif; + +import android.test.InstrumentationTestCase; + +public class ExifXmlDataTestCase extends InstrumentationTestCase { + protected final int mImageResourceId; + protected final int mXmlResourceId; + + public ExifXmlDataTestCase(int imageRes, int xmlRes) { + mImageResourceId = imageRes; + mXmlResourceId = xmlRes; + } +} -- cgit v1.2.3