11package com.stepstone.xrunner
22
33import android.os.Bundle
4+ import android.util.Log
5+ import androidx.test.internal.events.client.TestEventClient
46import androidx.test.internal.runner.RunnerArgs
57import androidx.test.internal.runner.TestRequestBuilder
6- import androidx.test.orchestrator.instrumentationlistener.OrchestratedInstrumentationListener
78import androidx.test.runner.AndroidJUnitRunner
8- import android.util.Log
99import com.stepstone.xrunner.internal.ARGUMENT_CLASS
1010import com.stepstone.xrunner.internal.ARGUMENT_LIST_TESTS_FOR_ORCHESTRATOR
1111import com.stepstone.xrunner.internal.ARGUMENT_NOT_CLASS
@@ -33,7 +33,7 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
3333 companion object {
3434 private const val TAG = " XRunner"
3535
36- private const val RUNNER_FIELD_ORCHESTRATOR_LISTENER = " orchestratorListener "
36+ private const val RUNNER_FIELD_TEST_EVENT_CLIENT = " testEventClient "
3737 private const val RUNNER_FIELD_RUNNER_ARGS = " runnerArgs"
3838 private const val RUNNER_METHOD_CREATE_TEST_REQUEST_BUILDER = " createTestRequestBuilder"
3939 private const val RUNNER_ARGS_FIELD_LISTENERS = " listeners"
@@ -61,13 +61,13 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
6161 }
6262
6363 override fun onStart () {
64- val orchestratorListener = SuperReflect .on(this ).get<OrchestratedInstrumentationListener >( RUNNER_FIELD_ORCHESTRATOR_LISTENER )
64+ val testEventClient = SuperReflect .on(this ).get<TestEventClient >( RUNNER_FIELD_TEST_EVENT_CLIENT )
6565
6666 if (shouldUpdateTestSuiteForXRunner()) {
6767 val runCount = getXRunnerCountArgument(modifiedBundle)
68- addXRunnerTestsWithOrchestrator(runCount, modifiedBundle, orchestratorListener )
68+ addXRunnerTestsWithOrchestrator(runCount, modifiedBundle, testEventClient )
6969 } else if (isXRunnerTestExecution(originalBundle)) {
70- updateListenersBeforeXRunnerTestExecution(orchestratorListener )
70+ updateListenersBeforeXRunnerTestExecution(testEventClient )
7171 }
7272 super .onStart()
7373 }
@@ -87,7 +87,7 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
8787 @Suppress(" DEPRECATION" )
8888 private fun isPrimaryInstrumentationProcess (): Boolean = isPrimaryInstrProcess(originalBundle.getString(ARGUMENT_TARGET_PROCESS ))
8989
90- private fun addXRunnerTestsWithOrchestrator (runCount : Int , bundle : Bundle , orchestratorListener : OrchestratedInstrumentationListener ) {
90+ private fun addXRunnerTestsWithOrchestrator (runCount : Int , bundle : Bundle , testEventClient : TestEventClient ) {
9191 val testDescriptionFromBundle = getTestDescriptionFromBundle(bundle)
9292 val testsToRerun = getMethodTestArgumentsFromDescription(testDescriptionFromBundle)
9393
@@ -98,29 +98,30 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
9898 .flatMap { testArg -> (1 until runCount).map { createXRunnerTestDescription(testArg, it) }.asSequence() }
9999 .forEach {
100100 Log .d(TAG , " Adding '$it ' to test suite" )
101- orchestratorListener .addTests(it)
101+ testEventClient .addTests(it)
102102 }
103103 }
104104
105105 private fun shouldUpdateTestSuiteForXRunner (): Boolean {
106+ // TODO: 12/08/2021 check for orchestration V2
106107 val listTestsForOrchestrator = (originalBundle.getString(ARGUMENT_LIST_TESTS_FOR_ORCHESTRATOR , false .toString())!! ).toBoolean()
107108 return listTestsForOrchestrator && isPrimaryInstrumentationProcess() && shouldUseXRunner(originalBundle)
108109 }
109110
110- private fun updateListenersBeforeXRunnerTestExecution (orchestratorListener : OrchestratedInstrumentationListener ) {
111+ private fun updateListenersBeforeXRunnerTestExecution (testEventClient : TestEventClient ) {
111112 val runnerArgs = getRunnerArgsWithReflection()
112113 SuperReflect .on(runnerArgs).set(
113114 RUNNER_ARGS_FIELD_LISTENERS , mutableListOf<RunListener >(
114115 OrchestratorListenerWrapper (
115- orchestratorListener ,
116+ testEventClient.runListener !! ,
116117 originalTestToRun
117118 )
118119 )
119120 )
120- /* We need to set this listener to null as otherwise it would be added in AndroidJUnitRunner#addListeners()
121+ /* We need to set test event client to no-op as otherwise it would be added in AndroidJUnitRunner#addListeners()
121122 * and it would duplicate what SCXRunnerOrchestratorListenerWrapper is already doing.
122123 */
123- SuperReflect .on(this ).set(RUNNER_FIELD_ORCHESTRATOR_LISTENER , null )
124+ SuperReflect .on(this ).set(RUNNER_FIELD_TEST_EVENT_CLIENT , TestEventClient . NO_OP_CLIENT )
124125 }
125126
126127 private fun getRunnerArgsWithReflection () = SuperReflect .on(this ).get<RunnerArgs >(RUNNER_FIELD_RUNNER_ARGS )
0 commit comments