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
17package com.android.gallery3d.exif;
18
19import android.test.InstrumentationTestRunner;
20import android.test.InstrumentationTestSuite;
21import android.util.Log;
22
23import com.android.gallery3d.tests.R;
24
25import junit.framework.TestCase;
26import junit.framework.TestSuite;
27
28import java.lang.reflect.InvocationTargetException;
29import java.lang.reflect.Method;
30
31public class ExifTestRunner extends InstrumentationTestRunner {
32    private static final String TAG = "ExifTestRunner";
33
34    private static final int[] IMG_RESOURCE = {
35        R.raw.galaxy_nexus
36    };
37    private static final int[] EXIF_DATA_RESOURCE = {
38        R.xml.galaxy_nexus
39    };
40
41    @Override
42    public TestSuite getAllTests() {
43        TestSuite suite = new InstrumentationTestSuite(this);
44        suite.addTestSuite(ExifDataTest.class);
45        suite.addTestSuite(ExifTagTest.class);
46        addAllTestsFromExifTestCase(ExifParserTest.class, suite);
47        addAllTestsFromExifTestCase(ExifReaderTest.class, suite);
48        addAllTestsFromExifTestCase(ExifOutputStreamTest.class, suite);
49        return suite;
50    }
51
52    private void addAllTestsFromExifTestCase(Class<? extends ExifXmlDataTestCase> testClass,
53            TestSuite suite) {
54        for (Method method : testClass.getDeclaredMethods()) {
55            if (method.getName().startsWith("test") && method.getParameterTypes().length == 0) {
56                for (int i = 0; i < IMG_RESOURCE.length; i++) {
57                    TestCase test;
58                    try {
59                        test = testClass.getDeclaredConstructor(int.class, int.class).
60                                newInstance(IMG_RESOURCE[i], EXIF_DATA_RESOURCE[i]);
61                        test.setName(method.getName());
62                        suite.addTest(test);
63                    } catch (IllegalArgumentException e) {
64                        Log.e(TAG, "Failed to create test case", e);
65                    } catch (InstantiationException e) {
66                        Log.e(TAG, "Failed to create test case", e);
67                    } catch (IllegalAccessException e) {
68                        Log.e(TAG, "Failed to create test case", e);
69                    } catch (InvocationTargetException e) {
70                        Log.e(TAG, "Failed to create test case", e);
71                    } catch (NoSuchMethodException e) {
72                        Log.e(TAG, "Failed to create test case", e);
73                    }
74                }
75            }
76        }
77    }
78
79    @Override
80    public ClassLoader getLoader() {
81        return ExifTestRunner.class.getClassLoader();
82    }
83}
84