1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.gallery3d.exif; 18 19 import android.content.Context; 20 import android.os.Environment; 21 import android.test.InstrumentationTestRunner; 22 import android.test.InstrumentationTestSuite; 23 import android.util.Log; 24 25 import com.android.gallery3d.tests.R; 26 27 import junit.framework.TestCase; 28 import junit.framework.TestSuite; 29 30 import java.io.File; 31 import java.lang.reflect.InvocationTargetException; 32 import java.lang.reflect.Method; 33 import java.util.ArrayList; 34 import java.util.List; 35 36 public class ExifTestRunner extends InstrumentationTestRunner { 37 private static final String TAG = "ExifTestRunner"; 38 39 private static final int[] IMG_RESOURCE = { 40 R.raw.galaxy_nexus 41 }; 42 43 private static final int[] EXIF_DATA_RESOURCE = { 44 R.xml.galaxy_nexus 45 }; 46 47 private static List<String> mTestImgPath = new ArrayList<String>(); 48 private static List<String> mTestXmlPath = new ArrayList<String>(); 49 50 @Override 51 public TestSuite getAllTests() { 52 getTestImagePath(); 53 TestSuite suite = new InstrumentationTestSuite(this); 54 suite.addTestSuite(ExifDataTest.class); 55 suite.addTestSuite(ExifTagTest.class); 56 addAllTestsFromExifTestCase(ExifParserTest.class, suite); 57 addAllTestsFromExifTestCase(ExifReaderTest.class, suite); 58 addAllTestsFromExifTestCase(ExifOutputStreamTest.class, suite); 59 addAllTestsFromExifTestCase(ExifModifierTest.class, suite); 60 addAllTestsFromExifTestCase(ExifInterfaceTest.class, suite); 61 return suite; 62 } 63 64 private void getTestImagePath() { 65 Context context = getContext(); 66 File imgDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES); 67 File xmlDir = new File(context.getExternalFilesDir(null).getPath(), "Xml"); 68 69 if (imgDir != null && xmlDir != null) { 70 String[] imgs = imgDir.list(); 71 if (imgs == null) { 72 return; 73 } 74 for (String imgName : imgs) { 75 String xmlName = imgName.substring(0, imgName.lastIndexOf('.')) + ".xml"; 76 File xmlFile = new File(xmlDir, xmlName); 77 if (xmlFile.exists()) { 78 mTestImgPath.add(new File(imgDir, imgName).getAbsolutePath()); 79 mTestXmlPath.add(xmlFile.getAbsolutePath()); 80 } 81 } 82 } 83 } 84 85 private void addAllTestsFromExifTestCase(Class<? extends ExifXmlDataTestCase> testClass, 86 TestSuite suite) { 87 for (Method method : testClass.getDeclaredMethods()) { 88 if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) { 89 for (int i = 0; i < IMG_RESOURCE.length; i++) { 90 TestCase test; 91 try { 92 test = testClass.getDeclaredConstructor(int.class, int.class). 93 newInstance(IMG_RESOURCE[i], EXIF_DATA_RESOURCE[i]); 94 test.setName(method.getName()); 95 suite.addTest(test); 96 } catch (IllegalArgumentException e) { 97 Log.e(TAG, "Failed to create test case", e); 98 } catch (InstantiationException e) { 99 Log.e(TAG, "Failed to create test case", e); 100 } catch (IllegalAccessException e) { 101 Log.e(TAG, "Failed to create test case", e); 102 } catch (InvocationTargetException e) { 103 Log.e(TAG, "Failed to create test case", e); 104 } catch (NoSuchMethodException e) { 105 Log.e(TAG, "Failed to create test case", e); 106 } 107 } 108 for (int i = 0, n = mTestImgPath.size(); i < n; i++) { 109 TestCase test; 110 try { 111 test = testClass.getDeclaredConstructor(String.class, String.class). 112 newInstance(mTestImgPath.get(i), mTestXmlPath.get(i)); 113 test.setName(method.getName()); 114 suite.addTest(test); 115 } catch (IllegalArgumentException e) { 116 Log.e(TAG, "Failed to create test case", e); 117 } catch (InstantiationException e) { 118 Log.e(TAG, "Failed to create test case", e); 119 } catch (IllegalAccessException e) { 120 Log.e(TAG, "Failed to create test case", e); 121 } catch (InvocationTargetException e) { 122 Log.e(TAG, "Failed to create test case", e); 123 } catch (NoSuchMethodException e) { 124 Log.e(TAG, "Failed to create test case", e); 125 } 126 } 127 } 128 } 129 } 130 131 @Override 132 public ClassLoader getLoader() { 133 return ExifTestRunner.class.getClassLoader(); 134 } 135 } 136