1/*
2 * Copyright (C) 2017 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.rs.testbackward19;
18
19import com.android.rs.unittest.*;
20
21import android.content.Context;
22import android.support.test.InstrumentationRegistry;
23import android.support.test.filters.MediumTest;
24import android.util.Log;
25
26import org.junit.Assert;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.junit.runners.Parameterized;
30import org.junit.runners.Parameterized.Parameter;
31import org.junit.runners.Parameterized.Parameters;
32
33import java.util.ArrayList;
34import java.util.List;
35
36/**
37 * RSTestBackward19, functional test for platform RenderScript APIs.
38 *
39 * RSTestBackward19 is necessary in addition to RSTestBackward because setting the target api
40 * to anything above 19 causes all tests to fail on API 19 and below
41 *
42 * However, setting the target api to 19 causes many tests to fail to compile, so backward
43 * compatibility testing needs to be separated to pre-19 and post-19 API versions
44 *
45 * To run the test, please use command
46 *
47 * adb shell am instrument -w com.android.rs.testbackward19/android.support.test.runner.AndroidJUnitRunner
48 */
49@RunWith(Parameterized.class)
50public class RSBackward19CompatibilityTests {
51    private static final String TAG = RSBackward19CompatibilityTests.class.getSimpleName();
52
53    /**
54     * Returns the list of subclasses of UnitTest to run.
55     */
56    @Parameters(name = "{0}")
57    public static Iterable<?> getParams() throws Exception {
58        int thisApiVersion = android.os.Build.VERSION.SDK_INT;
59
60        int minApiVersion = 17;
61        if (thisApiVersion < minApiVersion) {
62            Log.w(TAG,
63                String.format("API version is less than %d, no tests running", minApiVersion));
64        }
65
66        int maxApiVersion = 19;
67        if (thisApiVersion > maxApiVersion) {
68            Log.w(TAG,
69                String.format("API version is greater than %d, please use RSTestBackward",
70                    maxApiVersion));
71        }
72
73        Context ctx = InstrumentationRegistry.getTargetContext();
74
75        List<UnitTest> validUnitTests = new ArrayList<>();
76
77        Iterable<Class<? extends UnitTest>> testClasses =
78            RSTests.getTestClassesForCurrentAPIVersion();
79        for (Class<? extends UnitTest> testClass : testClasses) {
80            UnitTest test = testClass.getDeclaredConstructor(Context.class).newInstance(ctx);
81            validUnitTests.add(test);
82        }
83
84        UnitTest.checkDuplicateNames(validUnitTests);
85
86        return validUnitTests;
87    }
88
89
90    @Parameter(0)
91    public UnitTest mTest;
92
93    @Test
94    @MediumTest
95    public void testRSUnitTest() throws Exception {
96        mTest.logStart(TAG, "RenderScript Backward Compatibility 19 Testing");
97        mTest.runTest();
98        mTest.logEnd(TAG);
99        Assert.assertTrue(mTest.getSuccess());
100    }
101}
102