使用Android的ActivityInstrumentationTestCase2 ActionBar的单元测试 [英] android actionbar unit test using ActivityInstrumentationTestCase2

查看:376
本文介绍了使用Android的ActivityInstrumentationTestCase2 ActionBar的单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序编写的ActionBar导航单元测试。有两个选项卡(家庭和提及)。主页选项卡应弹出回家片段和提到选项卡应弹出提到片段。我使用的是fragmentmanager获得在该活动的当前片段,然后用instanceof来看看他们是否属于正确的类。

I am writing a unit test for actionbar navigation in my android app. There are two tabs (home and mentions). The home tab should bring up the home fragment and the mentions tab should bring up the mentions fragment. I am using the fragmentmanager to get the current fragment in the activity and then using instanceof to see if they belong to the right class.

 @UiThreadTest
    public void testNavigationBar() {
        assertEquals(ActionBar.NAVIGATION_MODE_TABS, actionBar.getNavigationMode());
        assertEquals(2, actionBar.getNavigationItemCount());

        Tab tab0 = actionBar.getTabAt(0);
        assertNotNull(tab0);
        assertNotNull(tab0.getText());
        assertEquals(mActivity.getBaseContext().getString(R.string.navigation_home), tab0.getText());

        Tab tab1 = actionBar.getTabAt(1);
        assertNotNull(tab1);
        assertNotNull(tab1.getText());
        assertEquals(mActivity.getBaseContext().getString(R.string.navigation_mention),
                tab1.getText());

        FragmentManager sfm = mActivity.getSupportFragmentManager();

        actionBar.selectTab(tab0);
        assertEquals(0, actionBar.getSelectedNavigationIndex());
        Fragment currentFragment = sfm.findFragmentById(R.id.frameContainer);
        assertNotNull(currentFragment);
        assertTrue(currentFragment instanceof HomeTimeLineFragment);

        actionBar.selectTab(tab1);
        assertEquals(1, actionBar.getSelectedNavigationIndex());
        Log.d(tag, String.valueOf(actionBar.getSelectedTab().getText()));
        currentFragment = sfm.findFragmentById(R.id.frameContainer);
        assertNotNull(currentFragment);
        assertTrue(currentFragment instanceof MentionsFragment);
    }

测试失败的currentFragment仍然是一个instanceof HomeTimeLineFragment。顺便说一句的log.d声明打印出提到了。所以提到选项卡在导航栏中选择绝对

The test fails as the currentFragment is still an instanceof HomeTimeLineFragment. Btw the log.d statement prints out "Mentions". So the mentions tab is definitely selected in the navigation bar.

推荐答案

尝试添加

sfm.executePendingTransactions();

actionBar.selectTab(tab1);

这篇关于使用Android的ActivityInstrumentationTestCase2 ActionBar的单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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