Home > Blockchain >  Android: How to perform click action on EditTextPreference using Espresso
Android: How to perform click action on EditTextPreference using Espresso

Time:11-26

I am new at UI testing, especially using Espresso. In my use case, i need to check the if the app accepts the input text on a EditTextPreference that's inside a PreferenceScreen fragment, but before doing so i need to perform a click so that the edit dialog appears and then enter the text.

This is how the preference fragment has the edit widgets:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditTextPreference
        android:key="@string/pref_stream_target_key"
        android:title="@string/pref_stream_target_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_resolution_key"
        android:title="@string/pref_stream_resolution_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_bitrate_key"
        android:title="@string/pref_stream_bitrate_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_framerate_key"
        android:title="@string/pref_stream_framerate_label"
        app:useSimpleSummaryProvider="true" />

    <EditTextPreference
        android:key="@string/pref_stream_peerlatency_key"
        android:title="@string/pref_stream_peerlatency_label"
        app:useSimpleSummaryProvider="true" />

</PreferenceScreen>

And this is how i've tried to perform the click

with(launchFragmentInContainer<StreamSettingsFragment>()) {
    onFragment { fragment ->
        Espresso.onData(PreferenceMatchers.withTitle(R.string.pref_stream_target_label))
            .perform(ViewActions.click())
    }
}

I am not sure if the action of making the click is necessary in order to check the logic behind the change listener that's in the EditTextPreference.

Running the test with this will just keep the app running and do nothing. Is there something i am missing?

EDIT: The solution for making espresso work without doing nothing was to take the code out of the onFragment block, however i am still not able to perform an action to the EditTextPreference widget.

This is the error being thrown:

androidx.test.espresso.NoMatchingViewException: No views in hierarchy found matching: is assignable from class <class android.widget.AdapterView>

View Hierarchy:
 >DecorView{id=-1, visibility=VISIBLE, width=1080, height=2218, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params={(0,0)(fillxfill) ty=BASE_APPLICATION hwFlags=#10 isEmuiStyle=0 statusBarColor=#ff000000 navigationBarColor=#fffcfcfc wanim=0x1030001
  fl=LAYOUT_IN_SCREEN LAYOUT_INSET_DECOR SPLIT_TOUCH HARDWARE_ACCELERATED}, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
|
 ->ActionBarOverlayLayout{id=16908903, res-name=decor_content_parent, visibility=VISIBLE, width=1080, height=2218, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@aa563f7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
 -->FrameLayout{id=16908290, res-name=content, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@4eb0264, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=252.0, child-count=1} 
|
 --->LinearLayout{id=-1, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@2f83982, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
 ---->FrameLayout{id=16908351, res-name=list_container, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@1c24893, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=1} 
|
 ----->RecyclerView{id=2131296596, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@687e0d0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@2746bce, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3bedaef, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@2d239fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
 ------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@615a885, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
 -------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=400, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@c6f76da, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream target, input-type=0, ime-target=false, has-links=false} 
|
 -------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@848370b, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
 ------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@506f9e8, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@ade26a6, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=241.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@ac138e7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@d45cc94, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
 ------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@985633d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
 -------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=539, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@360732, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream Resolution, input-type=0, ime-target=false, has-links=false} 
|
 -------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@fde7c83, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
 ------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@bfd1e00, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@eb9647e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=482.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@9025ddf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@8161a2c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
 ------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@76abcf5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
 -------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=420, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@8834a8a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream Bitrate, input-type=0, ime-target=false, has-links=false} 
|
 -------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@158f8fb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
 ------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@be0ad18, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@4538556, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=723.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@b429d7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@4bf82c4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
 ------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@6fa95ad, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
 -------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=514, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@31aa0e2, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream framerate, input-type=0, ime-target=false, has-links=false} 
|
 -------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@278c73, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
 ------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@1940730, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@245e92e, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=964.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@c647ccf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@bea665c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 
|
 ------->RelativeLayout{id=-1, visibility=VISIBLE, width=876, height=241, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@525cd65, tag=null, root-is-layout-requested=false, has-input-connection=false, x=186.0, y=0.0, child-count=2} 
|
 -------->TextView{id=16908310, res-name=title, visibility=VISIBLE, width=761, height=88, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@6cb6a3a, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=48.0, text=Stream peer latency (SRT), input-type=0, ime-target=false, has-links=false} 
|
 -------->TextView{id=16908304, res-name=summary, visibility=VISIBLE, width=136, height=57, 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=false, is-selected=false, layout-params=android.widget.RelativeLayout$LayoutParams@1d616eb, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=136.0, text=Not set, input-type=0, ime-target=false, has-links=false} 
|
 ------->LinearLayout{id=16908312, res-name=widget_frame, 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, layout-params=android.widget.LinearLayout$LayoutParams@7e58c48, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 ---->TextView{id=16908292, res-name=empty, 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, layout-params=android.widget.LinearLayout$LayoutParams@52adae1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false} 
|
 -->ActionBarContainer{id=16908719, res-name=action_bar_container, visibility=VISIBLE, width=1080, height=144, 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=false, is-selected=false, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@e7d36c7, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=108.0, child-count=2} 
|
 --->ActionBarView{id=16908718, res-name=action_bar, visibility=VISIBLE, width=1080, height=144, 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=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@6eb24f4, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
 ---->LinearLayout{id=-1, visibility=VISIBLE, width=620, height=144, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=false, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.app.ActionBar$LayoutParams@19441d, tag=null, root-is-layout-requested=false, has-input-connection=false, x=24.0, y=0.0, child-count=2} 
|
 ----->HomeView{id=-1, visibility=VISIBLE, width=120, height=144, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@8f10692, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=2} 
|
 ------>ImageView{id=16909572, res-name=up, 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, layout-params=android.widget.FrameLayout$LayoutParams@58c7863, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0} 
|
 ------>ImageView{id=16908332, res-name=home, visibility=VISIBLE, width=96, height=96, 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=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@90f9c60, tag=null, root-is-layout-requested=false, has-input-connection=false, x=12.0, y=24.0} 
|
 ----->LinearLayout{id=-1, visibility=VISIBLE, width=500, height=73, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@414f9de, tag=null, root-is-layout-requested=false, has-input-connection=false, x=120.0, y=35.0, child-count=2} 
|
 ------>TextView{id=16908722, res-name=action_bar_title, visibility=VISIBLE, width=476, height=73, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@c437bf, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Genius Live Camera, input-type=0, ime-target=false, has-links=false} 
|
 ------>TextView{id=16908721, res-name=action_bar_subtitle, 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, layout-params=android.widget.LinearLayout$LayoutParams@9421e8c, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false} 
|
 ---->ActionMenuView{id=-1, visibility=VISIBLE, 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=false, is-selected=false, layout-params=android.view.ViewGroup$LayoutParams@2bdd9d5, tag=null, root-is-layout-requested=false, has-input-connection=false, x=1080.0, y=72.0, child-count=0} 
|
 --->ActionBarContextView{id=16908723, res-name=action_context_bar, 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, layout-params=android.widget.FrameLayout$LayoutParams@2f2d5ea, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
|
 -->ActionBarContainer{id=16909433, res-name=split_action_bar, 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, layout-params=com.android.internal.widget.ActionBarOverlayLayout$LayoutParams@b8e90db, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=0} 
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:1730)
    at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:12)
    at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:7)
    at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:8)
    at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:11)
    at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:4)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:1)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher$1.apply(DataInteraction.java:2)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:7)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.<init>(DataInteraction.java:13)
    at androidx.test.espresso.DataInteraction$DisplayDataMatcher.displayDataMatcher(DataInteraction.java:1)
    at androidx.test.espresso.DataInteraction.makeTargetMatcher(DataInteraction.java:2)
    at androidx.test.espresso.DataInteraction.perform(DataInteraction.java:1)
    at com.domain.division.app.fragment.StreamSettingsFragmentTest.validTarget(StreamSettingsFragmentTest.kt:27)

CodePudding user response:

Remove the onFragment bit - that's so you can directly interact with the Fragment in a synchronized fashion on the main thread. Espresso is designed to work on the test thread and handles synchronizing with the UI on its own:

launchFragmentInContainer<StreamSettingsFragment>()

onData(withTitle(R.string.pref_stream_target_label)).perform(ViewActions.click())

EDIT

OK, so now you get No views in hierarchy found matching: is assignable from class <class android.widget.AdapterView> . What does this mean? It means onData attempted to find an object in an AdapterView (like a ListView) but one wasn't found.

OK, so why not? Look at the error report and you will see:

 ----->RecyclerView{id=2131296596, res-name=recycler_view, visibility=VISIBLE, width=1080, height=1966, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=android.widget.FrameLayout$LayoutParams@687e0d0, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=5} 
|
 ------>LinearLayout{id=0, visibility=VISIBLE, width=1080, height=241, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.recyclerview.widget.RecyclerView$LayoutParams@2746bce, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=3} 
|
 ------->LinearLayout{id=2131296469, res-name=icon_frame, visibility=INVISIBLE, width=168, height=24, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@3bedaef, tag=null, root-is-layout-requested=false, has-input-connection=false, x=18.0, y=108.0, child-count=1} 
|
 -------->PreferenceImageView{id=16908294, res-name=icon, visibility=INVISIBLE, 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=false, is-selected=false, layout-params=android.widget.LinearLayout$LayoutParams@2d239fc, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=12.0} 

What does this mean? It means the PreferenceFragment is using a RecyclerView not a ListView for its items. As such, PreferenceMatchers and onData will not work.

OK, so then what do you do? For RecyclerViews you use RecyclerViewActions instead.

Your previous attempt might translate to something like:

// We know the ID from the error stacktrace you were given
onView(withId(R.id.recycler_view)) 
    .perform(actionOnItem<RecyclerView.ViewHolder>(
        // Find the preference that has "target label" text view as a subview
        hasDescendant(withText(R.string.pref_stream_target_label)),
        click() // And click it
    ))
  • Related