android – Robolectric,单击列表项的问题

我一直在努力解决这个问题,我想我并没有得到一些关于Robolectric的基本信息.通常一些谷歌搜索可以帮助我解决这类问题的底部,但在此之间,看看示例代码我没有发现任何使用.

我试图模仿列表视图项上的单击并检查单击后是否启动了一个活动.我一直在回想起我正在测试的当前活动是由此产生的活动.我尝试删除所有列表项单击代码并检查生成的活动,然后将其作为我正在测试的InstallationListActivity返回.所以我得出的结论是列表视图项没有被点击,我只是不确定为什么.我在下面的测试代码中设置的系统日志是我期望它们的值.该列表是13项,getChildAt(0)返回标题.我认为获取一个项目(getChildAt(1))并调用它上面的performClick或其子文本视图将启动我的预期活动,但似乎并非如此.无论如何,这是我正在使用的robolectric /测试代码

@Before
    public void setUp() {
        mAppLaunch = new ApplicationLaunchActivity();
        mAppLaunch.onCreate(null);
        mActivity = new InstallationListActivity();
        mActivity.onCreate(null);
    }

    @Test
    public void shouldHaveNonEmptyInstallationList() throws Exception {
        assert(mActivity.installationListCount() > 0);
    }

    @Test
    public void shouldHaveSameNumberOfElements() throws Exception {
        ListView installationListView = (ListView) mActivity.getListView();

        ShadowListView shadowListView = shadowOf(installationListView);

        assert(shadowListView.getChildCount() == mActivity.installationListCount());
    }

    @Test
    public void pressingTheFirstItemInTheListShouldLaunchVenueListActivity() {
        ListView installationListView = (ListView) mActivity.findViewById(android.R.id.list);

        System.out.println("qty: " + installationListView.getChildCount());
        System.out.println("class: " + installationListView.getChildAt(0).getClass());
        System.out.println("class: " + installationListView.getChildAt(1).getClass());
        System.out.println("class: " + installationListView.getChildAt(2).getClass());
        System.out.println("class: " + installationListView.getChildAt(3).getClass());
        System.out.println("class: " + installationListView.getChildAt(4).getClass());

        LinearLayout firstItemLayout = (LinearLayout) installationListView.getChildAt(1);
        TextView firstItem = (TextView) firstItemLayout.getChildAt(0);

        ShadowTextView shadowFirstItem = shadowOf(firstItem);
        ShadowLinearLayout shadowLayout = (ShadowLinearLayout) shadowOf(firstItemLayout);

        shadowLayout.performClick();
        shadowFirstItem.performClick();

        clickOn(firstItem);
        clickOn(firstItemLayout);

        System.out.println("class: " + firstItemLayout.getChildAt(0).getClass());
        System.out.println("Layout shadow" + shadowOf(firstItemLayout).getClass());
        System.out.println("First Item Text: " + shadowFirstItem.getText());

        ShadowActivity shadowActivity = shadowOf(mActivity);
        Intent startedIntent = shadowActivity.getNextStartedActivity();
        assertNotNull(startedIntent);
        ShadowIntent shadowIntent = shadowOf(startedIntent);

        assertthat(shadowIntent.getComponent().getClassName(),equalTo(VenueListActivity.class.getName()));
    }
}

这是我用来构建列表视图的布局:

This is list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:cacheColorHint="#00000000"
              android:background="@drawable/background"/>
</LinearLayout>

list_item.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/list_item"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent">
    <TextView android:id="@+id/list_item_text"
              style="@style/tw_list_font"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textAppearance="?android:attr/textAppearanceLarge"
              android:gravity="center_vertical"
              android:paddingLeft="6dip"
              android:minHeight="?android:attr/listPreferredItemHeight" />
</LinearLayout>

以下是初始化列表的代码

InstallationListActivity.java

setContentView(R.layout.list);
        final ListView installationListView = getListView();
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header,installationListView,false);
        TextView headerText = (TextView) header.findViewById(R.id.header_text);
        headerText.setText("Installations On Live"); // Todo: This should not be hardcoded
        installationListView.addHeaderView(header,null,false);

        setlistadapter(new ArrayAdapter<Installation>(this,R.layout.list_item,R.id.list_item_text,mInstallations));

        // Click event
        installationListView.setClickable(true);
        installationListView.setonItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView,View view,int position,long arg1) {
                Installation installationClicked = (Installation) installationListView.getItemAtPosition(position);
                if (LOCAL_LOG) {
                    Log.d(LOG_TAG,"Installation: " + installationClicked.toString() + " clicked.");
                    Log.d(LOG_TAG,installationClicked.toString() + " has installationDirectory: " +
                            installationClicked.rootDir);
                }
                AppState.installation = installationClicked;
                AppState.serverInstallationName = installationClicked.rootDir;
                Intent venueListIntent = new Intent(InstallationListActivity.this,VenueListActivity.class);
                startActivity(venueListIntent);
            }
        });

任何帮助深表感谢!万分感谢!

解决方法

尝试帮助方法performItemClick:
Robolectric.shadowOf(listView).performItemClick(position);

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...