android / android-test

An extensive framework for testing Android apps
https://android.github.io/android-test
Apache License 2.0
1.14k stars 306 forks source link

ActivityScenario fails to open an Intent with Action #496

Closed Sloy closed 4 years ago

Sloy commented 4 years ago

Description

When I try to launch an Activity via an Intent with Action and Uri, ActivityScenario will timeout and fail with the message: Activity never becomes requested state "[CREATED, STARTED, RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")

I created this sample repository to demonstrate the issue: https://github.com/Sloy/activityscenario-intent-bug

I tried to dig into the code and the issue seems to be in the activityLifecycleObserver, as the Intent.filterEqual() method always returns false. https://github.com/android/android-test/blob/master/core/java/androidx/test/core/app/ActivityScenario.java#L357

Here's the comparison in the debugger: Screenshot 2020-01-29 at 14 15 53 It seems that the actual Intent that opens the Activity has the ComponentName set, whereas the original intent created in the test does not.

Manually adding the ComponentName to the intent makes the test pass of course, but that's an unintuitive behavior of ActivityScenario. It also forces the test to be coupled to the receiving Activity class, which is inconvenient for testing Deep Links.

I'm not sure what's the best solution here, to be honest. Any ideas?

Steps to Reproduce

Launch an activity without component name:

@Test
fun intent_with_action() {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse("scenario://test"))

    ActivityScenario.launch<MainActivity>(intent)
}

Expected Results

ActivityScenario detects the CREATED stage change and the test continues.

Actual Results

ActivityScenario waits indefinitely until it timeouts and fails with the error: java.lang.AssertionError: Activity never becomes requested state "[CREATED, STARTED, RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")

AndroidX Test and Android OS Versions

Link to a public git repo demonstrating the problem:

https://github.com/Sloy/activityscenario-intent-bug

brettchabot commented 4 years ago

Thanks for the detailed report! We'll take a look

radityagumay commented 3 years ago

@brettchabot

hi i am still facing this issue

Activity never becomes requested state "[DESTROYED, RESUMED, STARTED, CREATED]" (last lifecycle transition = "PRE_ON_CREATE")

i am using "androidx.test:core:1.3.0"

homandiy commented 3 years ago

Check activity.

kabirnayeem99 commented 3 years ago

Facing the same problem with androidx.test:core:1.3.0.

java.lang.AssertionError: Activity never becomes requested state "[CREATED, RESUMED, STARTED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE") at androidx.test.core.app.ActivityScenario.waitForActivityToBecomeAnyOf(ActivityScenario.java:338) at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:272) at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226) at com.androiddevs.shoppinglisttestingyt.data.local.ShoppingDaoTest.testFragmentInHiltContainer(GithubClassPostingDaoTest.kt:117) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80) at androidx.test.internal.runner.junit4.statement.RunAfters.evaluate(RunAfters.java:61) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:61) at dagger.hilt.android.internal.testing.MarkThatRulesRanRule$1.evaluate(MarkThatRulesRanRule.java:106) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56) at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2204) `

softartdev commented 3 years ago

It looks like my recently created issue #940 has the same cause, but it's not for sure yet.

ryanholden8 commented 8 months ago

Hi @brettchabot, considering this is still occurring can we re-open this ticket?

brettchabot commented 8 months ago

If you are still having issues I recommend opening a new ticket with a repro sample. The repro sample posted in the original description does not work for me, and there are several unit tests in place for implicit intents with ActivityScenario https://github.com/android/android-test/blob/515980bdc6f0e8520eabb6daa99b5d10e7eb661a/core/javatests/androidx/test/core/app/ActivityScenarioTest.java#L527