从未使用espresso执行点击调用OnClickListener

时间:2016-06-20 09:44:31

标签: android android-espresso

我想第一次在现有项目上测试浓缩咖啡。 在HomeFragment包含的HomeActivity中,我在显示另一个视图的按钮上设置了OnClickListener,我在模拟器中运行应用程序,一切正常。然后我想用浓咖啡测试它,我的OnClickListener似乎永远不会被调用。 我在开发人员选项中删除了我的模拟器中的动画选项,但espresso测试仍然失败,并且日志确认从未调用onClick。

  

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'在屏幕上显示给用户'与所选视图不匹配。

     

预期:屏幕上显示给用户

     

得到:“FilterPopupView {id = 2131558573,res-name = filter_view,visibility = GONE,width = 0,height = 0,has-focus = false,has-focusable = false,has-window-focus = true, is-clickable = false,is-enabled = true,is-focused = false,is-focusable = false,is-layout-requested = true,is-selected = false,root-is-layout-requested = false,has- input-connection = false,x = 0.0,y = 0.0,child-count = 1}“

HomeActivityTest

public class HomeActivityTest
        extends ActivityInstrumentationTestCase2<HomeActivity> {

    public HomeActivityTest() {
        super(HomeActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        getActivity();
    }

    public void testOpenFilter() {
        Log.e("Tests", "Check button_filter is clickable");
        onView(withId(R.id.button_filter)).check(matches(isClickable()));
        Log.e("Tests", "Click on button_filter");
        onView(withId(R.id.button_filter)).perform(click());
        Log.e("Tests", "Check filter_view visibility");
        onView(withId(R.id.filter_view)).check(matches(isDisplayed()));
    }
}

build.gradle

defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 130
        versionName '3.0-v2'
        multiDexEnabled true
        renderscriptTargetApi 20
        renderscriptSupportModeEnabled true
        vectorDrawables.useSupportLibrary = true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

...

dependencies {
    // Espresso core
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2')
        {
            exclude group: 'com.android.support'
        }
    // Android JUnit Runner
    androidTestCompile('com.android.support.test:runner:0.5')
        {
            exclude group: 'com.android.support'
        }
    // JUnit4 Rules
    androidTestCompile('com.android.support.test:rules:0.5')
        {
            exclude group: 'com.android.support'
        }
    ...

}

HomeFragment

void initFilterButton(View view) {
    mFilterButton = (Button) view.findViewById(R.id.button_filter);
    Log.e("Tests", "initFilterButton");
    mFilterButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e("Tests", "FilterButton onClick"); // <---- Never shown in logcat
            mFilterPopupView.setVisibility(View.VISIBLE);
        }
    });
}

在我的布局'home_fragment.xml'

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button_filter"
        style="@style/FilterAccess"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:clickable="true"
        android:text="@string/filter_access" />

    <fr.citylity.citylity.v2.ui.views.FilterPopupView
        android:id="@+id/filter_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="8dp"
        android:visibility="gone" />

</RelativeLayout>

0 个答案:

没有答案