ActivityScenario hangs on EmptyActivity after testing activity with launchMode=singleInstance
Description
When testing an Activity that has launchMode=singleInstance with an ActivityScenarioRule, after the test is completed the text executor gets stuck in EmptyActivity for ~45 seconds before progressing to the next test.
Steps to Reproduce
- Create a new Android project in Android Studio using the project wizard, using e.g. the "Blank Activity" template.
- Add
android:launchMode="singleInstance"to the MainActivity's entry in AndroidManifest.xml - Add
@get:Rule val rule = ActivityScenarioRule(MainActivity::class.java)to theExampleInstrumentedTest - Run
ExampleInstrumentedTest
Expected Results
Test runs and quits immediately after
Actual Results
Test runs and gets stuck in EmptyActivity for about ~45 seconds
AndroidX Test and Android OS Versions
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
(physical) Pixel C, Android 8.1.0 (emulator) Pixel 2, API 30
Seems to be the same issue as in https://stackoverflow.com/questions/65112750/android-test-with-activityscenariorule-hanging-forever
Also, some commenters on https://github.com/android/android-test/issues/676 seem to be running into this issue. Indeed, when changing the launchMode to singleTask as per https://github.com/android/android-test/issues/676#issuecomment-896067367, the test terminates immediately after execution.
I'm hitting this issue too in a compose app. One of the app dependencies requires the activity have a launchMode of "singleInstance" though so changing to singleTask is no good for us. Has anyone found a solution to this?
@brettchabot Do you have an idea what might be causing this?
@yuuki3655
If it helps anyone, this is my workaround. It's definitely not a permanent solution, so if anyone has a better approach, feel free to share!
Basically, I detect based on the gradle tasks to be executed if we're running the ui tests. Then with this information I set a placeholder that will be used in the AndroidManifest.xml. The BuildConfig field is also useful if you need to, for example, involve IdlingResource in your non-test code.
def instrumentationTestTask = gradle.startParameter.taskNames.find { it == ":app:connectedDebugAndroidTest" }
if (instrumentationTestTask != null) {
println "Building instrumentation APK"
manifestPlaceholders += [activityLaunchMode: "singleTask"]
buildConfigField "boolean", "INSTRUMENTATION_MODE", "true"
} else {
manifestPlaceholders += [activityLaunchMode: "singleInstance"]
buildConfigField "boolean", "INSTRUMENTATION_MODE", "false"
}
<activity
...
android:launchMode="${activityLaunchMode}"
... />
We tried both and it still hangs for a good 45 seconds, seems unrelated from what we can see, it is a lock in the getResult call from what we can see.