'Espresso tests failing to record intent when trying to check if an intent is sent

I'm trying to use Espresso for integration testing for a AdjustableWebView class that I've created.

My test looks like this:

@Test
public void myTest() {
        // intents.init() is done in the @Before setup() method
        
        // ... some code to set up permissions correctly
        
        // ensure that the activity doesn't actually open
        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT);
        Instrumentation.ActivityResult contentResult = new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, contentIntent);
        Instrumentation.ActivityResult chooserResult = new Instrumentation.ActivityResult(Activity.RESULT_CANCELED, chooserIntent);
        intending(hasAction(Intent.ACTION_GET_CONTENT)).respondWith(contentResult);
        intending(hasAction(Intent.ACTION_CHOOSER)).respondWith(chooserResult);

        // ... some code to load page in webview

        // ... some code to click on the proper element in the webview

        // verify
        intended(anyOf(hasAction(Intent.ACTION_GET_CONTENT), hasAction(Intent.ACTION_CHOOSER)));

        // intents.release() is done in the @After teardown() method
}

My result is this:

IntentMatcher: (has action: is "android.intent.action.GET_CONTENT" or has action: is "android.intent.action.CHOOSER")

Matched intents:[]

Recorded intents:[]
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:1736)
    at androidx.test.espresso.base.AssertionErrorHandler.handleSafely(AssertionErrorHandler.java:3)
    at androidx.test.espresso.base.AssertionErrorHandler.handleSafely(AssertionErrorHandler.java:1)
    at androidx.test.espresso.base.DefaultFailureHandler$TypedFailureHandler.handle(DefaultFailureHandler.java:4)
    at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:5)
    at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:5)
    at androidx.test.espresso.ViewInteraction.check(ViewInteraction.java:12)
    at androidx.test.espresso.intent.Intents.intended(Intents.java:188)
    at androidx.test.espresso.intent.Intents.intended(Intents.java:167)
    at com.mypackage.myapp.webview.tests.AdjustableWebViewTests.testFileInput_intentLaunches(AdjustableWebViewTests.java:246)
    ... 35 trimmed
Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has action: is "android.intent.action.GET_CONTENT" or has action: is "android.intent.action.CHOOSER")

Matched intents:[]

Recorded intents:[]
    at junit.framework.Assert.fail(Assert.java:50)
    at androidx.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:80)
    at androidx.test.espresso.intent.Intents.internalIntended(Intents.java:344)
    at androidx.test.espresso.intent.Intents$2.check(Intents.java:192)
    at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:2)
    at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:10)
    at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:1)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

FAILURES!!!
Tests run: 1,  Failures: 1

I put in log statements and am able to verify that my code starts an activity using startActivityForResult and is providing an ACTION_CHOOSER intent to the activity, but for some reason, whatever I do, I am unable to record the intent, and my test fails. Even if I remove the intending lines and let the activity open normally, and I can verify that the correct intent is created (using Log statements), an activity opens correctly, and the result code passed in onActivityResult is RESULT_CANCELLED (once again, verified using Log statements), but still, I cannot record the intent.

Would someone be able to point me in the right direction in terms of how I would fix this? I don't want to check the activity that opens because I want to keep my implementation as generic as possible (all I want to check is that SOME intent is sent out and I know there will be SOME activity to deal with it but I'm not specifying which activity is doing that work).

I have looked at several StackOverflow posts but nothing has been helping :(.



Solution 1:[1]

My exact situation was that I was trying to click on an HTML input tag and then record the intent. I was using Espresso. A colleague of mine helped me with this, and was able to get the test working by using UIAutomator instead of Espresso.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 k701