我正在尝试测试应用程序的进度指示器,它在 View 模型获取数据时显示不确定的进度指示器。

为了测试这一点,我模拟了返回数据的提供者并使其阻塞,直到我的测试告诉它继续进行。基本设置如下所示:

@Test
public void testProgressIndicator() {
    injectProvider(mockProvider);
    startTestActivity();

    // The mock provider now runs on a worker thread and won't finish
    // until we tell it to.

    // We should now see a progress indicator.
    onView(withId(R.id.progress_indicator)).check(
        matches(withEffectiveVisibility(Visibility.VISIBLE)));

    // Tell the worker thread to finish up.
    mockProvider.setResult();

    // The worker thread now returns a result, the progress indicator
    // should be gone.
    onView(withId(R.id.progress_indicator)).check(
        matches(withEffectiveVisibility(Visibility.GONE)));
}

这段代码很旧,所以提供者正在使用 AsyncTask 在工作线程上使用阻塞代码。

但是,Espresso 通常会等待所有工作人员完成,以确保结果不依赖于时间。特别是,它使用 AsyncTaskPoolMonitor 来等待所有挂起的 AsyncTask 对象。这通常很好,但就我而言,我希望这个线程在 Espresso 继续时保持忙碌。我怎样才能告诉 Espresso 不要等待这个特定的线程?

廉价的 cop-out 将只是一个 Thread 并通过 Handler 或类似的东西进行通信,但是在使用 AsyncTask 保留设置的同时找到解决方案会很好。

当我闯入调试器时,我看到我的测试运行程序线程卡在第一个 check() 上:
wait:-1, Object (java.lang)
parkFor$:2137, Thread (java.lang)
park:358, Unsafe (sun.misc)
park:190, LockSupport (java.util.concurrent.locks)
await:2059, AbstractQueuedSynchronizer$ConditionObject (java.util.concurrent.locks)
take:442, LinkedBlockingQueue (java.util.concurrent)
gatherAnyResult:83, InteractionResultsHandler (androidx.test.espresso)
gatherAnyResult:52, InteractionResultsHandler (androidx.test.espresso)
waitForAndHandleInteractionResults:314, ViewInteraction (androidx.test.espresso)
check:300, ViewInteraction (androidx.test.espresso)
testProgressIndicator:76, MyFragmentTest (com.my.test)  <<< ***********************
invoke:-1, Method (java.lang.reflect)
runReflectiveCall:50, FrameworkMethod$1 (org.junit.runners.model)
run:12, ReflectiveCallable (org.junit.internal.runners.model)
invokeExplosively:47, FrameworkMethod (org.junit.runners.model)
evaluate:17, InvokeMethod (org.junit.internal.runners.statements)
evaluate:80, RunBefores (androidx.test.internal.runner.junit4.statement)
evaluate:531, ActivityTestRule$ActivityStatement (androidx.test.rule)
evaluate:20, RunRules (org.junit.rules)
runLeaf:325, ParentRunner (org.junit.runners)
runChild:78, BlockJUnit4ClassRunner (org.junit.runners)
runChild:57, BlockJUnit4ClassRunner (org.junit.runners)
run:290, ParentRunner$3 (org.junit.runners)
schedule:71, ParentRunner$1 (org.junit.runners)
runChildren:288, ParentRunner (org.junit.runners)
access$000:58, ParentRunner (org.junit.runners)
evaluate:268, ParentRunner$2 (org.junit.runners)
run:363, ParentRunner (org.junit.runners)
run:104, AndroidJUnit4 (androidx.test.ext.junit.runners)
runChild:128, Suite (org.junit.runners)
runChild:27, Suite (org.junit.runners)
run:290, ParentRunner$3 (org.junit.runners)
schedule:71, ParentRunner$1 (org.junit.runners)
runChildren:288, ParentRunner (org.junit.runners)
access$000:58, ParentRunner (org.junit.runners)
evaluate:268, ParentRunner$2 (org.junit.runners)
run:363, ParentRunner (org.junit.runners)
run:137, JUnitCore (org.junit.runner)
run:115, JUnitCore (org.junit.runner)
execute:56, TestExecutor (androidx.test.internal.runner)
onStart:388, AndroidJUnitRunner (androidx.test.runner)
run:2075, Instrumentation$InstrumentationThread (android.app)

最佳答案

你应该你 CountingIdlingResource,
在 AsyncTask 的 onPreExecute() 中调用 increment() 并在 onPostExecute() 中调用 decrement()

https://developer.android.com/reference/androidx/test/espresso/idling/CountingIdlingResource.html

关于java - 如何在 Espresso 中允许繁忙的工作线程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56317901/

10-11 22:49
查看更多