Espresso.onData set inAdapterView to be AlertDialog

試著忘記壹切 提交于 2019-12-25 09:22:25

问题


I have an AlertDialog that contains a list of options which is set:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
                .setTitle(args.getString(TITLE))                    
                .setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE, args.getInt(SELECTED_INDEX), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // handle click
                        dismiss();
                    }
                });

I am trying to test clicking on each item. If I run:

onView(allOf(withClassName(Matchers.equalTo(AppCompatCheckedTextView.class.getName())), withText(displayName)))
                .perform(click());

Things work wonderfully, except if the list of options in the AlertDialog is too long to display entirely.

So, I am trying to find the item by using onData instead, but I am unsure how to instruct the DataInteraction object created by onData in what view to look for the data.

The AppCompatCheckedTextView objects that I am trying to click on are contained by an AlertController$RecyleListView with ID select_dialog_listview. So, I have tried:

onData(withText(displayName))
                .inAdapterView(withId(R.select_dialog_listview)) 
                .perform(click());

and

onData(withText(displayName))
                .inAdapterView(withClassName(Matchers.equalTo("android.internal.app.AlertController$RecyleListView")))
                .perform(click());

But receive an error that there is no matching view in the hierarchy.

So, then I tried adding a custom view to the Alert Dialog to see if I could find that by ID.

I have a layout file containing only a LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dialog_view">
</LinearLayout>

Which I add to my AlertDialog builder:

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
                    .setTitle(args.getString(TITLE))
                    .setView(LayoutInflater.from(getContext()).inflate(R.layout.alert_dialog_view, null))                    
                    .setSingleChoiceItems(ARRAY_OF_CHARSEQUENCE, args.getInt(SELECTED_INDEX), new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // handle click
                            dismiss();
                        }
                    });

And then look for in Espresso:

onData(withText(displayName))
                    .inAdapterView(withId(R.dialog_view)) 
                    .perform(click());

But receive the error:

android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: org.OUR.APPLICATION:id/dialog_view'.

If I am correct in using onData to perform the click, what is the proper method of pointing DataInteraction to the AlertDialog view.


回答1:


onData() with "com.android.internal.app.AlertController$RecycleListView" does work for me, note that you have typo in "RecyleListView".

Also, onData() parameter should be "dataMatcher", instanceOf() on the type used as T for ArrayAdapter (String in this case) works.

This works for me:

onData(is(instanceOf(String.class))).inAdapterView(allOf(withClassName(equalTo("com.android.internal.app.AlertController$RecycleListView")), isDisplayed())).atPosition(index).perform(click()); 


来源:https://stackoverflow.com/questions/38959280/espresso-ondata-set-inadapterview-to-be-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!