[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Got AssertionError while launching a activity by ActivityScenario.launch #1199

Open
sayasheng opened this issue Nov 9, 2021 · 27 comments
Open

Comments

@sayasheng
Copy link

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

@wangprofessor
Copy link

me too

1 similar comment
@biezhihua
Copy link

me too

@utzcoz
Copy link
Contributor
utzcoz commented Dec 10, 2021

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

@biezhihua
Copy link

@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
Copy link

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
Copy link

@utzcoz Have you made any progress?

@utzcoz
Copy link
Contributor
utzcoz commented Dec 13, 2021

@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
Copy link
Contributor
utzcoz commented Dec 13, 2021

@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
Copy link
Contributor
utzcoz commented Dec 13, 2021

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
Copy link
Contributor
utzcoz commented Dec 13, 2021

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
Copy link
Contributor
utzcoz commented Dec 13, 2021

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
Copy link
Contributor
utzcoz commented Dec 13, 2021

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
Copy link
Contributor
utzcoz commented Dec 14, 2021

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

@utzcoz
Copy link
Contributor
utzcoz commented Dec 14, 2021

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.

@biezhihua
Copy link

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
Copy link

You can try it again.

@biezhihua
Copy link

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
Copy link

@utzcoz

@utzcoz
Copy link
Contributor
utzcoz commented Dec 17, 2021

@biezhihua I will check your new sample this weekend.

@utzcoz
Copy link
Contributor
utzcoz commented Dec 19, 2021

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.

@biezhihua
Copy link

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.

@utzcoz
Copy link
Contributor
utzcoz commented Dec 20, 2021

@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.

@biezhihua
Copy link

@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.

@brettchabot
Copy link
Collaborator
brettchabot commented Dec 27, 2021

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?

@TWiStErRob

This comment has been minimized.

@brettchabot
Copy link
Collaborator

oops thanks

@oliverspryn
Copy link

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. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants