I am writing an android application with a tabbed interface. I am using a FragmentActivity
with a ActionBar.Tab
tabs, with each tab displaying a ListFragment
.
In my test case, I am using Robotium 3.6
to perform UI interaction, which is needed to test certain aspects of my fragment's
. Each fragment
has a separate test method, which tests that the ListView
for a fragment
has the same content at a set index (0), as the ArrayList<String>
which the data is from.
The problem is that while the first two fragments
pass their respective tests, the final fragment
does not, with the failure trace being that:
junit.framework.AssertionFailedError: AbsListView with index 2 is not available!
at com.jayway.android.robotium.solo.Waiter.waitForAndGetView(Waiter.java:362)
at com.jayway.android.robotium.solo.Clicker.clickInList(Clicker.java:405)
at com.jayway.android.robotium.solo.Solo.clickInList(Solo.java:1004)
at com.******.*******.test.ActivityNavigatorTest.testFragmentSongsList(ActivityNavigatorTest.java:62)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:537)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
Here is the relevant code for the activity being tested:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigator);
PREFERENCES = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
adapterPager = new AdapterPager(getSupportFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(adapterPager);
viewPager.setOnPageChangeListener(new SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
getActionBar().setSelectedNavigationItem(position);
}
});
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.title_fragment_artists).setTag("artists").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_fragment_albums).setTag("albums").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText(R.string.title_fragment_songs).setTag("songs").setTabListener(this));
}
The code for each ListFragment
is just defining an ArrayList<String>
, populating it with dummy content and displaying said content with an ArrayAdapter<String>
. Here is the code for the test case:
package com.******.*******.test;
import android.test.ActivityInstrumentationTestCase2;
import com.******.******.ActivityNavigator;
import com.*******.******.FragmentAlbums;
import com.*******.********.FragmentArtists;
import com.*******.******.FragmentSongs;
import com.jayway.android.robotium.solo.Solo;
public class ActivityNavigatorTest extends ActivityInstrumentationTestCase2<ActivityNavigator> {
private Solo solo;
public ActivityNavigatorTest() {
super(ActivityNavigator.class);
}
protected void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
}
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
}
public void testFragmentArtistsList() {
solo.assertCurrentActivity("Checking loaded activity is the navigator", ActivityNavigator.class);
solo.clickOnText("Artists");
String contentExpected = FragmentArtists.getArtistList().get(0);
String contentActual = solo.clickInList(0, 0).get(0).getText().toString();
assertEquals("Testing index 0 has same content in ArrayList and ListView", contentExpected, contentActual);
int sizeExpected = FragmentArtists.getArtistList().size();
int sizeActual = solo.getCurrentListViews().get(0).getCount();
assertEquals("Testing same quantity of data in ArrayList and ListView", sizeExpected, sizeActual);
}
public void testFragmentAlbumsList() {
solo.assertCurrentActivity("Checking loaded activity is the navigator", ActivityNavigator.class);
solo.clickOnText("Albums");
String contentExpected = FragmentAlbums.getAlbumList().get(0);
String contentActual = solo.clickInList(0, 1).get(0).getText().toString();
assertEquals("Testing index 0 has same content in ArrayList and ListView", contentExpected, contentActual);
int sizeExpected = FragmentAlbums.getAlbumList().size();
int sizeActual = solo.getCurrentListViews().get(1).getCount();
assertEquals("Testing same quantity of data in ArrayList and ListView", sizeExpected, sizeActual);
}
public void testFragmentSongsList() {
solo.assertCurrentActivity("Checking loaded activity is the navigator", ActivityNavigator.class);
solo.clickOnText("Songs");
String contentExpected = FragmentSongs.getSongList().get(0);
String contentActual = solo.clickInList(0, 2).get(0).getText().toString();
assertEquals("Testing index 0 has same content in ArrayList and ListView", contentExpected, contentActual);
int sizeExpected = FragmentSongs.getSongList().size();
int sizeActual = solo.getCurrentListViews().get(2).getCount();
assertEquals("Testing same quantity of data in ArrayList and ListView", sizeExpected, sizeActual);
}
}