Skip to content

Commit b0e514c

Browse files
author
Piotr Zawadzki
committed
Add support for AndroidX Test Runner 1.4.0 as it was incompatible with this library due to internal changes in AndroidJUnitRunner and the fact that rely on reflection.
Fixes issue #6
1 parent 2932acf commit b0e514c

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Here are some samples:
8585

8686
| XRunner version | Test runner version |
8787
|:------------------------------------------------------------------------:|:---------------------------------------:|
88+
| [X.Y.Z](https://github.com/stepstone-tech/AndroidTestXRunner/tree/X.Y.Z) | `androidx.test:runner:1.4.0` |
8889
| [2.1.0](https://github.com/stepstone-tech/AndroidTestXRunner/tree/2.1.0) | `androidx.test:runner:1.2.0` |
8990
| [2.0.0](https://github.com/stepstone-tech/AndroidTestXRunner/tree/2.0.0) | `androidx.test:runner:1.1.0` |
9091
| [1.0.0](https://github.com/stepstone-tech/AndroidTestXRunner/tree/1.0.0) | `com.android.support.test:runner:1.0.2` |

build.gradle

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
buildscript {
44
ext.KOTLIN_VERSION = '1.3.41'
5-
ext.ANDROID_GRADLE_PLUGIN_VERSION = '3.5.0'
5+
ext.ANDROID_GRADLE_PLUGIN_VERSION = '3.5.4'
66
ext.DOKKA_VERSION = '0.9.17'
77
ext.BINTRAY_VERSION = '1.8.4'
88
repositories {
@@ -30,19 +30,19 @@ allprojects {
3030
configure(allprojects) {
3131
ext {
3232
ANDROID_MIN_SDK_VERSION = 19
33-
ANDROID_TARGET_SDK_VERSION = 28
34-
ANDROID_COMPILE_SDK_VERSION = 28
33+
ANDROID_TARGET_SDK_VERSION = 30
34+
ANDROID_COMPILE_SDK_VERSION = 30
3535

36-
TEST_RUNNER_VERSION = '1.2.0'
36+
TEST_RUNNER_VERSION = '1.4.0'
3737
SUPER_REFLEKT_VERSION = '1.0.1'
3838

3939
// Sample app only
40-
TEST_RULES_VERSION = '1.2.0'
41-
TEST_EXT_VERSION = '1.1.1'
42-
TEST_ORCHESTRATOR_VERSION = '1.2.0'
43-
ESPRESSO_VERSION = '3.2.0'
40+
TEST_RULES_VERSION = '1.4.0'
41+
TEST_EXT_VERSION = '1.1.3'
42+
TEST_ORCHESTRATOR_VERSION = '1.4.0'
43+
ESPRESSO_VERSION = '3.4.0'
4444
APPCOMPAT_VERSION = '1.1.0'
45-
MATERIAL_VERSION = '1.1.0-alpha10'
45+
MATERIAL_VERSION = '1.1.0'
4646
CONSTRAINT_VERSION = '1.1.3'
4747
}
4848
}

xrunner-library/src/main/java/com/stepstone/xrunner/AndroidJUnitXRunner.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.stepstone.xrunner
22

33
import android.os.Bundle
4+
import android.util.Log
5+
import androidx.test.internal.events.client.TestEventClient
46
import androidx.test.internal.runner.RunnerArgs
57
import androidx.test.internal.runner.TestRequestBuilder
6-
import androidx.test.orchestrator.instrumentationlistener.OrchestratedInstrumentationListener
78
import androidx.test.runner.AndroidJUnitRunner
8-
import android.util.Log
99
import com.stepstone.xrunner.internal.ARGUMENT_CLASS
1010
import com.stepstone.xrunner.internal.ARGUMENT_LIST_TESTS_FOR_ORCHESTRATOR
1111
import 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)

xrunner-library/src/main/java/com/stepstone/xrunner/internal/OrchestratorListenerWrapper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.stepstone.xrunner.internal
22

3-
import androidx.test.orchestrator.instrumentationlistener.OrchestratedInstrumentationListener
3+
import androidx.test.internal.events.client.OrchestratedInstrumentationListener
44
import org.junit.runner.Description
55
import org.junit.runner.Result
66
import org.junit.runner.notification.Failure
77
import org.junit.runner.notification.RunListener
88

99
/**
10-
* This is a wrapper for the original [OrchestratedInstrumentationListener] set in [android.support.test.runner.AndroidJUnitRunner].
10+
* This is a wrapper for the original [OrchestratedInstrumentationListener] set in [androidx.test.runner.AndroidJUnitRunner].
1111
* It's purpose is to report to Android Test Orchestrator correct XRunner test methods e.g.
1212
* if [com.stepstone.xrunner.AndroidJUnitXRunner] was originally triggered to run "com.android.foo.FooTest#testFoo_XRUNNER_RUN_1"
1313
* this class would make sure that [OrchestratedInstrumentationListener] would get a test description with "_XRUNNER_RUN_1" suffix.
1414
* This is because [com.stepstone.xrunner.AndroidJUnitXRunner] removes this suffix when running the test.
1515
*/
1616
class OrchestratorListenerWrapper(
17-
private val orchestratorListener: OrchestratedInstrumentationListener,
17+
private val orchestratorListener: RunListener,
1818
private val originalTestToRun: String?
1919
) : RunListener() {
2020

0 commit comments

Comments
 (0)