无法在 45 秒内启动 Intent。也许主线程在合理的时间内没有空闲?

Could not launch intent within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time?

我正在写 Espresso 测试。这是测试用例

点击 Fab,应用启动测验Activity。

让我向您解释一下我的应用程序。

所以应用要求是 -

  1. 资产文件夹中有 JSON 个文件
  2. 我必须解析它并将数据存储在数据库中
  3. On Main Activity 将此数据从数据库加载到 recyclerview。有 Fab 按钮,点击它应用程序将随机数据列表(我已在 recyclerview 中加载)传递给 QuizActivity

这是我编码的方式 -

  1. 在 MainActivity 的 onCreate() 中仅使用 AsyncTask 解析数据并将数据插入数据库一次。
  2. 数据可用后,通过 AsyncTaskLoader 将其加载到 recyclerview 中
  3. 在 Fab 上设置 Onclick 侦听器。并将所需数据传递给 QuizActivity.

下面是我的测试

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
   /* @Rule
    public IntentsTestRule<MainActivity> intentsTestRule =
            new IntentsTestRule<MainActivity>(MainActivity.class);
*/

    @Rule
    public ActivityTestRule<MainActivity> intentsTestRule =
            new ActivityTestRule<MainActivity>(MainActivity.class,false,true);

    @Test
    public void fabTest(){
        onView(withId(R.id.fab)).perform(click());
        //Check if the text_question textview is displayed
        onView(withId(R.id.text_question)).check(matches(isDisplayed()));
    }

   /* @Test
    public void clickFloatingActionButton() {
        onView(withId(R.id.fab))
                .perform(click());
        intended(hasComponent(new ComponentName(getTargetContext(), QuizActivity.class)));
    }*/
}

我的做法是-

  1. 寻找工厂
  2. 执行点击
  3. 检查是否显示 text_question 文本视图。请注意此文本视图在测验Activity.

在我 运行 我得到的测试之后

java.lang.RuntimeException: Could not launch intent Intent { act=android.intent.action.MAIN flg=0x14000000 cmp=my_package_name/.MainActivity } within 45 seconds. Perhaps the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen. Or the activity is doing network calls on creation? See the threaddump logs. For your reference the last time the event queue was idle before your activity launch request was 1505287862579 and now the last time the queue went idle was: 1505287862579. If these numbers are the same your activity might be hogging the event queue.

PS - 我已经关闭了所有的动画。我在代码中没有进度条。

另外请注意,如果我在 onCreate() 方法中注释掉 AsyncTask、AsyncTaskLoader、RecyclerView 部分,则测试通过。

我怀疑它可能是由于后台任务引起的。

有人遇到过类似的问题吗?如果您知道解决方案,请告诉我。这两天我很挣扎。我在 Whosebug 上搜索了类似的帖子,但对我没有任何帮助。

终于找到答案了!

实际上,罪魁祸首是我的适配器中的自定义视图。

Espresso 一直等到 AsyncTaskAsyncTaskLoader 加载数据,但它看起来像自定义视图导致此问题,如异常中所述:

the main thread has not gone idle within a reasonable amount of time? There could be an animation or something constantly repainting the screen.

无论如何要克服这个问题,我需要做的是-

"If the test is running set the visibility of Custom view to GONE"

你会怎么做?

 private AtomicBoolean isTestRunning;

    public synchronized boolean isTestRunning () {
        if (null == isTestRunning) {
            boolean istest;

            try {

                Class.forName ("Full path of your TestName"); // for e.g. com.example.MyTest

                istest = true;
            } catch (ClassNotFoundException e) {
                istest = false;
            }

            isTestRunning = new AtomicBoolean (istest);
        }

        return isRunningTest.get ();
    }

现在我在这里使用 Recyclerview 所以在 ViewHolder,

public InsectHolder(View itemView)
        {
            super(itemView);
                       customView = (DangerLevelView) itemView.findViewById(R.id.danger_level_tv);

            if(isRunningTest()) {
                customView.setVisibility(View.GONE);
            }
        }

耶!你的考试会通过的。

希望有人觉得它有用!

如果您有任何问题,请在下面的评论部分告诉我!