测试单击按钮时是否启动正确的活动

发布于 2024-11-29 12:16:48 字数 894 浏览 1 评论 0原文

我正在为有几个按钮的活动编写测试, 每一个都启动一个新的活动,

我如何知道按钮是否启动了正确的活动?

这是我到目前为止所拥有的:

public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {

    private Intent mMainIntent;


    public MainActivityTest() {
        super(MainActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mMainIntent = new Intent(Intent.ACTION_MAIN);
    }

    @MediumTest
    public void testButtonActivityA () {
        MainActivity activity = startActivity(mMainIntent, null, null);
        Button buttonActivityA = (Button) activity.findViewById(com.project.R.id.button_activity_a);
        buttonVoice.performClick();
        Intent i = getStartedActivityIntent();
        assertNotNull(i);
        assertTrue(isFinishCalled());
    }
}

PS:“isFinishedCalled()”失败了,如果我引发一个新的全屏活动,这怎么可能? 谢谢,

I'm writing a test for an activity that have a few buttons,
each one of than starts a new Activity,

How can I know if the button is starting the correct activity?

This is what I have so far:

public class MainActivityTest extends ActivityUnitTestCase<MainActivity> {

    private Intent mMainIntent;


    public MainActivityTest() {
        super(MainActivity.class);
    }


    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mMainIntent = new Intent(Intent.ACTION_MAIN);
    }

    @MediumTest
    public void testButtonActivityA () {
        MainActivity activity = startActivity(mMainIntent, null, null);
        Button buttonActivityA = (Button) activity.findViewById(com.project.R.id.button_activity_a);
        buttonVoice.performClick();
        Intent i = getStartedActivityIntent();
        assertNotNull(i);
        assertTrue(isFinishCalled());
    }
}

PS: the 'isFinishedCalled()' is failing, how can this be if I raise a new fullscreen Activity?
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟─花易冷 2024-12-06 12:16:48

它失败是因为未调用 finish() 。

您必须自己完成一项活动,否则当您打开一个新活动时,它会出现在“堆栈”的顶部,并且原始活动已调用 onPause 但仍然“活动”。 finish() 是一个隐式调用,您可以将其编码到应用程序中以销毁活动。

请阅读Android Activity Lifecycle

然后学习调用 < a href="http://developer.android.com/reference/android/app/Activity.html#finish%28%29" rel="nofollow">何时应该调用 finish()

It's failing because finish() isn't called.

you have to finish an activity yourself, otherwise when you open a new one it comes up over the top on the 'stack' and the original activity has onPause called but is still 'alive'. finish() is an implicit call that you can code into your app to destroy an activity.

Please go read about the Android Activity Lifecycle

Then learn to call when you should call finish()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文