aboutsummaryrefslogtreecommitdiff
path: root/test-app/src/androidTest/java/com/google/android/renderscript_test/RenderScriptToolkitTest.kt
blob: 0afa54b8b9825e46648fe9daaf7d26f19c84ccb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (C) 2021 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.google.android.renderscript_test

import android.content.Context
import android.util.Log
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.LargeTest
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
@LargeTest
class RenderScriptToolkitTest {
    @ExperimentalUnsignedTypes
    @get:Rule
    var activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

    @Parameterized.Parameter(value = 0)
    lateinit var intrinsic: Intrinsic

    @ExperimentalUnsignedTypes
    @Test
    fun test() {
        lateinit var thread: Thread
        var success = false
        activityScenarioRule.scenario.onActivity { activity ->
            // Run the test in a new thread to avoid blocking the main thread.
            thread = Thread{
                success = testIntrinsic(activity, intrinsic, validate = true)
            }.apply { start() }
        }
        thread.join()
        assertTrue(success)
    }

    @ExperimentalUnsignedTypes
    private fun testIntrinsic(context: Context, intrinsic: Intrinsic, validate: Boolean): Boolean {
        val tester = Tester(context, validate)
        val numberOfIterations = if (validate) 1 else 12
        val timer = TimingTracker(numberOfIterations, numberOfIterationsToIgnore = 0)
        val results = Array(numberOfIterations) { i ->
            Log.i(TAG, "*** Starting iteration ${i + 1} of $numberOfIterations ****")
            val success = tester.testOne(intrinsic, timer)
            Log.i(TAG, if (success) timer.report() else "FAILED! FAILED! FAILED! FAILED!")
            timer.nextIteration()
            success
        }
        tester.destroy()
        return results.all { it }
    }

    companion object {
        private val TAG = "RenderScriptToolkitTest"

        @JvmStatic
        @Parameterized.Parameters(name = "{0}")
        fun data(): List<Array<Any>> = Intrinsic.values().map { arrayOf(it) }
    }
}