gpt4 book ai didi

android - 如何使用 espresso android 测试 recyclerview 中的切换按钮在特定位置打开或关闭?

转载 作者:行者123 更新时间:2023-11-29 19:11:37 24 4
gpt4 key购买 nike

在 activity_main.xml 中,我使用带有 id 信息的 TextView 来显示带有 id item_name 的 recycler_view 列表中单击文本的信息。 Recycler View 显示了一个包含 100 个项目的列表,其中包含其名称和切换以使其处于 Activity 状态和非 Activity 状态。

这是我的 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.app.recyclerviewtesting.MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:onClick="eraseInfo"
android:padding="20dp"
android:textColor="#000000"
android:textColorHint="#999999" />

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager" />
</LinearLayout>
</layout>

在回收者 View 中使用的项目:这是我的 item.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<data>

<variable
name="item"
type="com.app.recyclerviewtesting.Item" />

<variable
name="listener"
type="com.app.recyclerviewtesting.AdapterItemsClickHandler" />
</data>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">

<android.support.v7.widget.AppCompatTextView
android:id="@+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="@{() -> listener.onItemNameClick(item)}"
android:text="@{item.name}"
android:textSize="20sp" />

<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:checked="@{item.on}"
android:onClick="@{() -> listener.onToggleClick(item)}"
tools:ignore="RelativeOverlap" />
</RelativeLayout>
</layout>

这里是MainActivity.java

public class MainActivity extends AppCompatActivity 
{

public static final String ITEM_NAME_FORMAT = "Item :%d";
public static final String ITEM_SELECTED_FORMAT = "%s is selected.";
public static final String INFO_HINT = "Click on item!";
private ActivityMainBinding mBinding;
private List<Item> mMainList = new ArrayList<>();
private RecyclerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

initViews();
}

private void initViews() {

mBinding.info.setHint(INFO_HINT);

for (int i = 0; i < 100; i++) {
mMainList.add(new Item(String.format(Locale.getDefault(), ITEM_NAME_FORMAT, i), false));
}
mAdapter = new RecyclerAdapter(mMainList, new HandleAdapterActions());
mBinding.recyclerView.setAdapter(mAdapter);
}

public void eraseInfo(View view) {
mBinding.info.setText("");
}

private class HandleAdapterActions implements AdapterItemsClickHandler {

@Override
public void onItemNameClick(Item item) {
mBinding.info.setText(String.format(Locale.getDefault(), ITEM_SELECTED_FORMAT, item.getName()));
}

@Override
public void onToggleClick(Item item) {
item.setOn(!item.getOn());
mAdapter.notifyItemChanged(mMainList.indexOf(item));
}
}
}

这是适配器

public class RecyclerAdapter extends 
RecyclerView.Adapter<RecyclerView.ViewHolder>
{

private final int ITEM_VIEW_TYPE = 1;
private List<Item> mainList;
private AdapterItemsClickHandler mListener;

public RecyclerAdapter(List<Item> mainList, AdapterItemsClickHandler mListener) {
this.mainList = mainList;
this.mListener = mListener;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
if (viewType == ITEM_VIEW_TYPE) {
final ItemBinding binding = ItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
binding.setListener(mListener);
viewHolder = new ItemViewHolder(binding);
} else {
viewHolder = null;
}
return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof ItemViewHolder) {
ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
final ItemBinding binding = itemViewHolder.binding;

Item item = mainList.get(position);
binding.setItem(item);
}
}

@Override
public int getItemCount() {
return mainList.size();
}

@Override
public int getItemViewType(int position) {
return ITEM_VIEW_TYPE;
}

public class ItemViewHolder extends RecyclerView.ViewHolder {

public ItemBinding binding;

ItemViewHolder(ItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}

如果打开或关闭位置 15th 的开关,请帮助我找到编写测试用例的方法。

最佳答案

下面是这个问题的解决方案:

 public static Matcher<View> withToggleMatcher(final int position) {
return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {

@Override
public void describeTo(Description description) {

}

@Override
protected boolean matchesSafely(RecyclerView recyclerView) {
final RecyclerAdapter.ItemViewHolder viewHolder = (RecyclerAdapter.ItemViewHolder) recyclerView.findViewHolderForAdapterPosition(position);
return viewHolder.binding.toggle.isChecked();
}
};
}

@Test
public void checkIfToggleIsOnAfterClickOnIt() {

int position = 15;

String itemName = String.format(Locale.getDefault(), MainActivity.ITEM_NAME_FORMAT, position);

onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.scrollToPosition(position));

onView(allOf(withId(R.id.toggle), hasSibling(withText(itemName))))
.perform(click());

onView(withId(R.id.recyclerView))
.check(matches(withToggleMatcher(position)));
}

关于android - 如何使用 espresso android 测试 recyclerview 中的切换按钮在特定位置打开或关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45094382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com