1 /*
2  * Copyright (C) 2024 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 android.tools.flicker.junit
18 
19 import android.os.Bundle
20 import android.tools.flicker.legacy.LegacyFlickerTest
21 import androidx.test.platform.app.InstrumentationRegistry
22 import com.android.internal.annotations.VisibleForTesting
23 import org.junit.runner.Runner
24 import org.junit.runners.parameterized.ParametersRunnerFactory
25 import org.junit.runners.parameterized.TestWithParameters
26 
27 /**
28  * A {@code FlickerRunnerFactory} creates a runner for a single {@link TestWithParameters}. Parses
29  * and executes assertions from a flicker DSL
30  */
31 class FlickerParametersRunnerFactory : ParametersRunnerFactory {
32     @VisibleForTesting
createRunnerForTestWithParametersnull33     fun createRunnerForTestWithParameters(test: TestWithParameters, arguments: Bundle): Runner {
34         val simpleClassName = test.testClass.javaClass.simpleName
35         val flickerTest =
36             test.parameters.filterIsInstance<LegacyFlickerTest>().firstOrNull()
37                 ?: error(
38                     "Unable to extract ${LegacyFlickerTest::class.simpleName} " +
39                         "for class $simpleClassName"
40                 )
41         val scenario = flickerTest.initialize(simpleClassName)
42         val newTest =
43             TestWithParameters(
44                 /*name */ "[${scenario.description}]",
45                 /* testClass */ test.testClass,
46                 /* parameters */ test.parameters
47             )
48         return LegacyFlickerJUnit4ClassRunner(newTest, scenario, arguments)
49     }
50 
createRunnerForTestWithParametersnull51     override fun createRunnerForTestWithParameters(test: TestWithParameters) =
52         createRunnerForTestWithParameters(test, arguments = InstrumentationRegistry.getArguments())
53 }
54