Commit ce64bc97 authored by Earl Ou's avatar Earl Ou
Browse files

Tests for ExifData

Bug:7192363

Change-Id: Ic74ecad2eff50e26efa85f928ef7904a2c51ecc7
parent 89591324
/*
* 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));
}
}
......@@ -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 {
......
......@@ -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<Short, String> mIfd0Value = new HashMap<Short, String>();
private HashMap<Short, String> mIfd1Value = new HashMap<Short, String>();
private HashMap<Short, String> mExifIfdValue = new HashMap<Short, String>();
......@@ -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
......
......@@ -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<Short, String> mIfd0Value = new HashMap<Short, String>();
private final HashMap<Short, String> mIfd1Value = new HashMap<Short, String>();
private final HashMap<Short, String> mExifIfdValue = new HashMap<Short, String>();
......@@ -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
......
/*
* 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;
}
}
......@@ -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<? extends InstrumentationTestCase> testClass,
private void addAllTestsFromExifTestCase(Class<? extends ExifXmlDataTestCase> testClass,
TestSuite suite) {
for (Method method : testClass.getDeclaredMethods()) {
if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) {
......
/*
* 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;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment