ActivityScenario< MainActivity>挂在perform(click())上 [英] ActivityScenario<MainActivity> hangs on perform(click())

查看:133
本文介绍了ActivityScenario< MainActivity>挂在perform(click())上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总的来说,我在这里是因为不推荐使用真实的 ActivityTestRule<> ,而且显然我需要使用 ActivityScenario<> ActivityScenarioRule<> .

In general, I'm here because tried-and-true ActivityTestRule<> is deprecated, and apparently I need to use ActivityScenario<> or ActivityScenarioRule<>.

但是它们违反了规则对视图的测试应该将视图对象视为对象".我必须有效"地例如,将所有断言都塞入回调中.我无法使 ActivityScenarioRule<> 工作,因为无法填充数据库并延迟对 onCreate()的调用.

But they break the rule "tests on the view should treat view objects like objects." I must "efficiently" cram all assertions into a callback, for example. I can't get ActivityScenarioRule<> working because there's no way to stuff my database and delay the call to onCreate().

这样就剩下 ActivityScenario<> 了,我可以走得很远了:

So that leaves me with ActivityScenario<>, and I can get this far:

    //  stuff database
    ActivityScenario<MainActivity> scenario = ActivityScenario.launch(MainActivity.class);

    scenario.onActivity( (ma) -> {
        //  assertions on ma that pass

        onView(withId(R.id.configure_device_button)).perform(click());
        
        //  ...
    } );

问题是 perform(click())挂断,位于 wait()的深处.

The problem is the perform(click()) hangs up, deep inside of a wait().

我们还在等什么?

当我将视图对象视为对象时, ma.configureDeviceButton.performClick()起作用.因此,这意味着我现在必须重新编写数百个视图测试中的每一个...

When I treat the view objects like objects, ma.configureDeviceButton.performClick() works. So this means I must now re-write every one of my hundreds of view tests...

事实证明,我可以像这样共享我的MainActivity句柄 ma :

It turns out I can share my MainActivity handle, ma, like this:

    activateActivity( (ma) -> {
        TestFixtures.ma = ma;
    } );

MainActivity的寿命比其回调的寿命更长.(这打开了一个问题,如果我们不使用Execute Around模式,为什么要使用回调?)

The MainActivity lives longer than its callback. (Which opens the question Why use a callback if we are not using the Execute Around Pattern?)

因此,我可能不需要重新构造所有内容.每个人仍然需要一个答案,为什么我的书本 perform(click())挂断了,但是...

So I might not need to rearchitect everything. Everyone still needs an answer why my by-the-book perform(click()) hung up, though...

推荐答案

onActivity操作块是在UIThread上执行的,onView将等待UIThread空闲.

onActivity action block is performed on UIThread and onView will wait for UIThread to idle.

您需要在onActivity块之外调用onView,否则它将死锁.从您的代码中

You need to call onView outside the onActivity block or else it will deadlock. From your code

scenario.onActivity( (ma) -> {
   //  Do your UI setup
});
onView(...).perform(...);

这篇关于ActivityScenario&lt; MainActivity&gt;挂在perform(click())上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆