android-test icon indicating copy to clipboard operation
android-test copied to clipboard

ActivityScenario hangs on EmptyActivity after testing activity with launchMode=singleInstance

Open jfresen opened this issue 4 years ago • 7 comments

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 the ExampleInstrumentedTest
  • 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

jfresen avatar Sep 24 '21 14:09 jfresen

Seems to be the same issue as in https://stackoverflow.com/questions/65112750/android-test-with-activityscenariorule-hanging-forever

jfresen avatar Sep 24 '21 14:09 jfresen

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.

jfresen avatar Sep 24 '21 15:09 jfresen

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?

tomcully avatar Feb 23 '22 11:02 tomcully

@brettchabot Do you have an idea what might be causing this?

jfresen avatar Feb 23 '22 12:02 jfresen

@yuuki3655

brettchabot avatar Feb 28 '22 17:02 brettchabot

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}"
    ... />

smascaro avatar Jul 27 '22 09:07 smascaro

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.

chrisjenx avatar Aug 11 '22 05:08 chrisjenx