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
1212import com.stepstone.xrunner.internal.ARGUMENT_NOT_PACKAGE
13+ import com.stepstone.xrunner.internal.ARGUMENT_ORCHESTRATOR_DISCOVERY_SERVICE
1314import com.stepstone.xrunner.internal.ARGUMENT_PACKAGE
1415import com.stepstone.xrunner.internal.ARGUMENT_TARGET_PROCESS
1516import com.stepstone.xrunner.internal.OrchestratorListenerWrapper
@@ -33,7 +34,7 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
3334 companion object {
3435 private const val TAG = " XRunner"
3536
36- private const val RUNNER_FIELD_ORCHESTRATOR_LISTENER = " orchestratorListener "
37+ private const val RUNNER_FIELD_TEST_EVENT_CLIENT = " testEventClient "
3738 private const val RUNNER_FIELD_RUNNER_ARGS = " runnerArgs"
3839 private const val RUNNER_METHOD_CREATE_TEST_REQUEST_BUILDER = " createTestRequestBuilder"
3940 private const val RUNNER_ARGS_FIELD_LISTENERS = " listeners"
@@ -61,13 +62,13 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
6162 }
6263
6364 override fun onStart () {
64- val orchestratorListener = SuperReflect .on(this ).get<OrchestratedInstrumentationListener >( RUNNER_FIELD_ORCHESTRATOR_LISTENER )
65+ val testEventClient = SuperReflect .on(this ).get<TestEventClient >( RUNNER_FIELD_TEST_EVENT_CLIENT )
6566
6667 if (shouldUpdateTestSuiteForXRunner()) {
6768 val runCount = getXRunnerCountArgument(modifiedBundle)
68- addXRunnerTestsWithOrchestrator(runCount, modifiedBundle, orchestratorListener )
69+ addXRunnerTestsWithOrchestrator(runCount, modifiedBundle, testEventClient )
6970 } else if (isXRunnerTestExecution(originalBundle)) {
70- updateListenersBeforeXRunnerTestExecution(orchestratorListener )
71+ updateListenersBeforeXRunnerTestExecution(testEventClient )
7172 }
7273 super .onStart()
7374 }
@@ -87,7 +88,7 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
8788 @Suppress(" DEPRECATION" )
8889 private fun isPrimaryInstrumentationProcess (): Boolean = isPrimaryInstrProcess(originalBundle.getString(ARGUMENT_TARGET_PROCESS ))
8990
90- private fun addXRunnerTestsWithOrchestrator (runCount : Int , bundle : Bundle , orchestratorListener : OrchestratedInstrumentationListener ) {
91+ private fun addXRunnerTestsWithOrchestrator (runCount : Int , bundle : Bundle , testEventClient : TestEventClient ) {
9192 val testDescriptionFromBundle = getTestDescriptionFromBundle(bundle)
9293 val testsToRerun = getMethodTestArgumentsFromDescription(testDescriptionFromBundle)
9394
@@ -98,29 +99,33 @@ open class AndroidJUnitXRunner : AndroidJUnitRunner() {
9899 .flatMap { testArg -> (1 until runCount).map { createXRunnerTestDescription(testArg, it) }.asSequence() }
99100 .forEach {
100101 Log .d(TAG , " Adding '$it ' to test suite" )
101- orchestratorListener .addTests(it)
102+ testEventClient .addTests(it)
102103 }
103104 }
104105
105106 private fun shouldUpdateTestSuiteForXRunner (): Boolean {
106107 val listTestsForOrchestrator = (originalBundle.getString(ARGUMENT_LIST_TESTS_FOR_ORCHESTRATOR , false .toString())!! ).toBoolean()
107- return listTestsForOrchestrator && isPrimaryInstrumentationProcess() && shouldUseXRunner(originalBundle)
108+ val testDiscoveryForOrchestratorV2Provided = ! originalBundle.getString(ARGUMENT_ORCHESTRATOR_DISCOVERY_SERVICE ).isNullOrEmpty()
109+ return isPrimaryInstrumentationProcess() &&
110+ (testDiscoveryForOrchestratorV2Provided || listTestsForOrchestrator) &&
111+ shouldUseXRunner(originalBundle)
108112 }
109113
110- private fun updateListenersBeforeXRunnerTestExecution (orchestratorListener : OrchestratedInstrumentationListener ) {
114+ private fun updateListenersBeforeXRunnerTestExecution (testEventClient : TestEventClient ) {
111115 val runnerArgs = getRunnerArgsWithReflection()
112116 SuperReflect .on(runnerArgs).set(
113- RUNNER_ARGS_FIELD_LISTENERS , mutableListOf<RunListener >(
117+ RUNNER_ARGS_FIELD_LISTENERS ,
118+ mutableListOf<RunListener >(
114119 OrchestratorListenerWrapper (
115- orchestratorListener ,
120+ testEventClient.runListener !! ,
116121 originalTestToRun
117122 )
118123 )
119124 )
120- /* We need to set this listener to null as otherwise it would be added in AndroidJUnitRunner#addListeners()
125+ /* We need to set test event client to no-op as otherwise it would be added in AndroidJUnitRunner#addListeners()
121126 * and it would duplicate what SCXRunnerOrchestratorListenerWrapper is already doing.
122127 */
123- SuperReflect .on(this ).set(RUNNER_FIELD_ORCHESTRATOR_LISTENER , null )
128+ SuperReflect .on(this ).set(RUNNER_FIELD_TEST_EVENT_CLIENT , TestEventClient . NO_OP_CLIENT )
124129 }
125130
126131 private fun getRunnerArgsWithReflection () = SuperReflect .on(this ).get<RunnerArgs >(RUNNER_FIELD_RUNNER_ARGS )
0 commit comments