0

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

1 回答 1

0

我有一个理论,但我不确定它是否正确。我相信您的应用程序在视图寻呼机中一次显示一个片段,对吗?

好吧,我相信前两个测试用例可能比任何事情都更偶然。我相信一次应该只显示一个列表视图,并且由于某种原因,robotium 保留了一个额外的列表视图(可能是以前看到的),这意味着计数始终为两个。您可以通过查看 hierarchyviewer 工具并查看应用程序中显示的列表视图来验证这一点。

我为您提供的替代方法是在不同的列表视图上设置好的 id(或标签/内容描述)并使用这些而不是容易出现此类问题的机器人 getCurrentListViews() 方法。

于 2012-12-03T15:41:08.040 回答