Android TestCase如何获取android.support.v4.FragmentTabHost的内容视图

时间:2015-08-19 05:36:42

标签: android testcase

我有Activity android.support.v4.FragmentTabHost我想测试。

我的布局:

<LinearLayout
    android:id="@+id/homeFragement"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/tabContent"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/line_color" />

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <android.support.v4.app.FragmentTabHost
            android:id="@+id/tabHost"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/bottom_bgcolor" />
    </FrameLayout>
</LinearLayout>

这就是我初始化TabHost

的方式
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabContent); 
int count = 4; 
for (int i = 0; i < count; i++) { 
    String title = getString(getTitleForIndex(i)); 
    TabHost.TabSpec tabSpec = mTabHost.newTabSpec(title); 
    tabSpec.setIndicator(createButtonView(i)); 
    mTabHost.addTab(tabSpec, mFragments[i], null); 
} 

现在在我的TestCase中,test_fragment()方法:

ActivityMonitor addMonitor = getInstrumentation().addMonitor(MainEntryActivity.class.getName(), null, false);
MainEntryActivity mainEntryActiviy = (MainEntryActivity) addMonitor.waitForActivityWithTimeout(3000);
android.support.v4.app.FragmentTabHost fragmentTabHost = (FragmentTabHost) mainEntryActiviy.findViewById(R.id.tabHost);
fragmentTabHost.setCurrentTab(4);

可行,TabHost已切换到标签4。

如何获取TabHost的内容Fragment

1 个答案:

答案 0 :(得分:0)

使用相应的xml布局文件创建一个从fragment扩展的新类,并在片段类的onCreateView中展开相应的布局:

@Override
public View onCreateView()
{
     View view = inflator.inflate(R.id.your_layout_xml);
     return view;
}