由 Handler.postDelayed 启动的 Android 测试活动

问题描述

我是使用 Roboelectric 测试框架的新手,我遇到的问题是无法构建活动。我在每个课程中都使用了相同的精确测试,它们按预期工作,除了依赖于 Handler.postDelayed 调用的 CallingActivity,我认为这是问题所在。

这是将创建 CallingActivity 的代码

btnCall.setonClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run() {
                        Intent intent = new Intent(FakePhoneCallMenuActivity.this,CallingActivity.class);
                        intent.putExtra("callerName",nameEntryBox.getText().toString());
                        intent.putExtra("callerPhoneNum",phoneEntryBox.getText().toString());
                        intent.putExtra("callerVoice",voice);
                        startActivity(intent);
                    }
                },timetoStart);
            }
        });

这是对出现问题的 CallingActivity 的测试:

@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.O_MR1})
public class CallingTest {
    private CallingActivity activity;

    @Before
    public void setUp() throws Exception {
        activity = Robolectric.buildActivity(CallingActivity.class)
                .create()
                .start()
                .resume()
                .get();
    }

    @Test
    public void acceptCallButtonClickShouldStartNewActivity() throws Exception {
        ShadowActivity shadowActivity = shadowOf(activity);
        ImageButton button = (ImageButton) shadowActivity.getContentView().findViewById(R.id.btnAcceptCall);
        button.callOnClick();
        Intent startedIntent = shadowActivity.getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(startedIntent);
        assertEquals(FakePhoneCallMenuActivity.class,shadowIntent.getIntentClass());
    }
}

我在尝试运行此测试后收到的问题是:

java.lang.Exception: Main looper has queued unexecuted runnables.

对此的帮助将不胜感激!

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)