-1

I have an Activity with android.support.v4.FragmentTabHost which I want to test.

My layout:

<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>

and this is how I initialize the 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); 
} 

Now in my TestCase, test_fragment() method:

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);

It works and the TabHost has switch to tab 4.

How can I get the content of TabHost which should be a Fragment.

user3875388
  • 541
  • 1
  • 6
  • 19

1 Answers1

0

Create a new class that extends from fragment with a corresponding xml layout file, and in the onCreateView of the fragment class inflate the corresponding layout:

@Override
public View onCreateView()
{
     View view = inflator.inflate(R.id.your_layout_xml);
     return view;
}
eyadMhanna
  • 2,412
  • 3
  • 31
  • 49