Espresso 测试在尝试检查是否发送意图时未能记录意图

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

我正在尝试使用 Espresso 对我创建的 AdjustableWebView class 进行集成测试。

我的测试是这样的:

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

我的结果是这样的:

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.check(Intents.java:192)
    at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:2)
    at androidx.test.espresso.ViewInteraction.call(ViewInteraction.java:10)
    at androidx.test.espresso.ViewInteraction.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

我输入了日志语句,并且能够验证我的代码是否使用 startActivityForResult 启动了 activity 并向 activity 提供了 ACTION_CHOOSER 意图,但是由于某种原因,无论我做什么,我都无法记录意图,而且我的测试失败了。即使我删除 intending 行并让 activity 正常打开,并且我可以验证是否创建了正确的意图(使用 Log 语句),activity 正确打开,结果onActivityResult 中传递的代码是 RESULT_CANCELLED(再次使用 Log 语句验证),但我仍然无法记录意图。

有人能为我指明正确的方向,告诉我如何解决这个问题吗?我不想检查打开的 activity,因为我想让我的实现尽可能通用(我想检查的是发送了一些意图,我知道会有一些 activity 来处理它,但我没有指定哪个 activity 正在做这项工作。

我查看了几篇 Whosebug 帖子,但没有任何帮助:(。

我的确切情况是我试图点击一个 HTML 输入标签,然后记录意图。我用的是浓缩咖啡。我的一位同事帮助我解决了这个问题,并且能够通过使用 UIAutomator 而不是 Espresso 来进行测试。