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

Got AssertionError while launching a activity by ActivityScenario.launch

Open sayasheng opened this issue 4 years ago • 27 comments
trafficstars

Description

Got Error messages as below: java.lang.AssertionError: Activity never becomes requested state "[CREATED, DESTROYED, RESUMED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338)

some message shows in log E/TestRunner( 3422): ----- begin exception ----- E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338) E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272) E/TestRunner( 3422): at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)

Steps to Reproduce

  1. assign a different process name to the test activity (not a launcher category) in AndroidManifest.xml image
  2. run test code image
  3. The activity UI shows on screen but can't be operated by onView and then it shows the error after a few seconds.

AndroidX Test and Android OS Versions

OS version: android 10 androidx.test.espresso core: 3.4.0 androidx.test.rule: 1.4.0

workaround solution

don't give android:process name to the test activity

sayasheng avatar Nov 09 '21 08:11 sayasheng

me too

wangprofessor avatar Dec 03 '21 13:12 wangprofessor

me too

biezhihua avatar Dec 10 '21 03:12 biezhihua

@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.

utzcoz avatar Dec 10 '21 03:12 utzcoz

@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.

https://github.com/biezhihua/ActivityScenarioTestDemo

I provide a demo project, you can reproduce this problem.

biezhihua avatar Dec 10 '21 07:12 biezhihua

java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, CREATED, RESUMED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE") at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:339) at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:273) at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:196) at com.bzh.test.BZHTest.bzhtest(BZHTest.kt:13)

biezhihua avatar Dec 10 '21 07:12 biezhihua

@utzcoz Have you made any progress?

biezhihua avatar Dec 13 '21 03:12 biezhihua

@utzcoz Have you made any progress?

Sorry for that. Looks like Brett and other maintainers are busy recently. I will take look at this issue this week before they have time for it. And I will post updates to here.

utzcoz avatar Dec 13 '21 06:12 utzcoz

@sayasheng @wangprofessor @biezhihua , could you provide a sample to reproduce this problem? It will help to validate and fix this problem.

https://github.com/biezhihua/ActivityScenarioTestDemo

I provide a demo project, you can reproduce this problem.

I can't run your sample at Android Studio. Could you provide one sample with basic Android Studio structure, and without alibaba's gitlab link that I can't access?

utzcoz avatar Dec 13 '21 17:12 utzcoz

E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE")

Okay, I reproduce this problem with my local sample app. It will raise when add process to test Activity's AndroidManifest.xml tag.

utzcoz avatar Dec 13 '21 17:12 utzcoz

Looks like androidx.test core uses MonitoringInstrumentation to receive Activity state from instrumentation, and it doesn't receive state correctly state from Instrumentation when started Activity with specific process.

utzcoz avatar Dec 13 '21 18:12 utzcoz

Found a sample to test multiprocess: https://github.com/android/android-test/tree/9d25d24ec7abe5021672c0423a992e87b44eb261/testapps/multiprocess_testapp. Maybe you can check it whether it can help you.

utzcoz avatar Dec 13 '21 18:12 utzcoz

Found a sample to test multiprocess: https://github.com/android/android-test/tree/9d25d24ec7abe5021672c0423a992e87b44eb261/testapps/multiprocess_testapp. Maybe you can check it whether it can help you.

It looks like related to Robolectric tests. Hi @brettchabot , does ActivityScenario support multiprocess correctly on emulator?

utzcoz avatar Dec 13 '21 18:12 utzcoz

Found another tutorial to test app with multi-process with espresso remote: https://developer.android.com/training/testing/espresso/multiprocess.

utzcoz avatar Dec 14 '21 16:12 utzcoz

Found another tutorial to test app with multi-process with espresso remote: https://developer.android.com/training/testing/espresso/multiprocess.

From testing result, we can start an Activity in default process with ActivityScenario, and start another Activity with custom private process, and Espresso can work correctly with multi-processes based on espresso-remote library. But the problem of using ActivityScenario to start Activity with custom private process directly also exists. One dirty workaround to fix this problem is to add a dummy Activity for androidTest to start Activity with custom private process config, and use ActivityScenario to setup this dummy Activity and use it to trigger another Activity will be started into private process. Also we need to follow above mentioned tutorial to setup Espresso remote for androidTest. After that, we can use Espresso to test Activity UI in private process.

utzcoz avatar Dec 14 '21 16:12 utzcoz

E/TestRunner( 3422): java.lang.AssertionError: Activity never becomes requested state "[RESUMED, CREATED, DESTROYED, STARTED]" (last lifecycle transition = "PRE_ON_CREATE")

Okay, I reproduce this problem with my local sample app. It will raise when add process to test Activity's AndroidManifest.xml tag.

Thank your work!

It will raise when not add process to test Activity's AndroidManifest.xml tag.

biezhihua avatar Dec 16 '21 08:12 biezhihua

You can try it again.

biezhihua avatar Dec 16 '21 08:12 biezhihua

I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag.

image

You will get same information.

biezhihua avatar Dec 16 '21 08:12 biezhihua

@utzcoz

biezhihua avatar Dec 16 '21 08:12 biezhihua

@biezhihua I will check your new sample this weekend.

utzcoz avatar Dec 17 '21 16:12 utzcoz

I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag.

image

You will get same information.

You didn't add your test Activity to your AndroidManifest.xml, so system can't find your test Activity, and raised this error to you.

utzcoz avatar Dec 19 '21 16:12 utzcoz

I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem without process tag. image You will get same information.

You didn't add your test Activity to your AndroidManifest.xml, so system can't find your test Activity, and raised this error to you.

Sorry, this is my error, i forget to add Activity to my AndroidManifest.xml.

I updated the sample project : https://github.com/biezhihua/ActivityScenarioTestDemo, you can reproduce the problem.

biezhihua avatar Dec 20 '21 07:12 biezhihua

@biezhihua There are some things about your sample:

  1. You should add AndroidJUnit4 as your TestCase's test runner.
  2. You should avoid add TestActivity to AndroidManifest.xml under androidTest and test directry. From https://issuetracker.google.com/issues/127986458#comment6 we know AGP doesn't merge AndroidManifest.xml under androidTest and test, you definition in androidTest's AndroidManifest.xml doesn't work, and your TestActivity will not found and started by test case, and you will encounter above problem. If you really want to add pure TestActivity, you can follow https://github.com/robolectric/robolectric/pull/4736 to try testapp workaround from Robolectric.

utzcoz avatar Dec 20 '21 16:12 utzcoz

@biezhihua There are some things about your sample:

  1. You should add AndroidJUnit4 as your TestCase's test runner.
  2. You should avoid add TestActivity to AndroidManifest.xml under androidTest and test directry. From https://issuetracker.google.com/issues/127986458#comment6 we know AGP doesn't merge AndroidManifest.xml under androidTest and test, you definition in androidTest's AndroidManifest.xml doesn't work, and your TestActivity will not found and started by test case, and you will encounter above problem. If you really want to add pure TestActivity, you can follow Workaround for AGP not merging test manifest robolectric/robolectric#4736 to try testapp workaround from Robolectric.

Thank you very much.

biezhihua avatar Dec 21 '21 02:12 biezhihua

Sorry for delayed response. ActivityScenario won't support testing an Activity running in a different process than the Instrumentation. At the very least I feel ActivityScenario should detect and report a better error message in this situation.

Does it help if you specify

<instrumentation
    android:name="androidx.test.runner.AndroidJUnitRunner"
    android:targetPackage="com.example.activityscenariotestdemo"
    android:targetProcesses=":welcome"
/>

in your test's AndroidManifest.xml?

brettchabot avatar Dec 27 '21 18:12 brettchabot

@brettchabot in your previous comment did you forget the backticks? because it looks like there might be XML inserted mid-sentence.


I could get it to show by pressing "reply" 🤓:

<instrumentation
    android:name="androidx.test.runner.AndroidJUnitRunner"
    android:targetPackage="com.example.activityscenariotestdemo"
    android:targetProcesses=":welcome"
/>

TWiStErRob avatar Dec 29 '21 08:12 TWiStErRob

oops thanks

brettchabot avatar Dec 29 '21 15:12 brettchabot

I ran into this today for a Jetpack Compose project. I am able to work around this issue by removing testCoverageEnabled true of android.buildTypes.debug. Unfortunately, I need that. :(

oliverspryn avatar Dec 30 '21 22:12 oliverspryn