我想測試與Espresso
Android測試框架,所有預期的標題顯示在ListView中。正確聲稱與濃咖啡我列出的項目ListView
我有活動ListView顯示目錄中的文件。每個ListView項是Item
對象:
/**
* Item
*/
static class Item {
private String title;
private IFileInfo fileInfo;
private int imageResourceId;
public Item(String title, IFileInfo fileInfo, int imageResourceId)
{
this.title = title;
this.fileInfo = fileInfo;
this.imageResourceId = imageResourceId;
}
public IFileInfo getFileInfo() {
return fileInfo;
}
public String getTitle() {
return title;
}
public int getImageResourceId() {
return imageResourceId;
}
}
持有人只是設置標題和圖像的項目:
/**
* Adapter
*/
private class Adapter extends ArrayAdapter<Item> {
private LayoutInflater layoutInflater;
public Adapter(Context context, List<Item> objects) {
super(context, 0, objects);
layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.file_dialog_row, null);
convertView.setTag(new Holder(convertView));
}
Item item = getItem(position);
Holder holder = (Holder) convertView.getTag();
holder.getTitleView().setText(item.getTitle());
holder.getTitleView().setTextColor(itemColorStateList);
holder.getImageView().setImageResource(item.getImageResourceId());
return convertView;
}
}
項目佈局:
<?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="wrap_content">
<ImageView android:layout_alignParentLeft="true" android:id="@+id/fdrowimage"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:paddingRight="3dp" android:paddingLeft="3dp"/>
<TextView android:text="@+id/fdrowtext" android:layout_width="wrap_content"
android:id="@+id/fdrowtext"
android:layout_alignTop="@+id/fdrowimage" android:layout_alignBottom="@+id/fdrowimage"
android:gravity="center_vertical" android:layout_height="wrap_content"
android:textSize="19dp"/>
本來我想斷言與文本「N」的項目被列出(我有一個列表Item
對象,我想爲它顯示的每一個斷言)。
要檢查它的實際工作我已經檢查測試應該失敗一些實際未顯示項目冠軍,但由於某些原因,測試成功:
onData(allOf(is(instanceOf(Item.class)), withText("someNotListedItemTitle"), isDisplayed()));
下面是完整的測試代碼:
...
@Test
public void testShowFiles() throws IOException {
List<File> files = new LinkedList<>();
for (int i = 0; i < 10; i++) {
File tmpFile = new File(rootFilesFolder, generateUniqueString());
files.add(tmpFile);
if (i % 2 == 0)
FileUtils.touch(tmpFile); // create file
else
FileUtils.forceMkdir(tmpFile); // create folder
}
// add not shown for sure in ListView item
File notExistingListed = new File(rootFilesFolder, generateUniqueString());
files.add(notExistingListed);
// activity creates ListView that shows file name for each file in TextView with @+id/fdrowtext
startActivityForPath(rootFilesFolder);
for (File eachFile : files) {
// should fail for `notExistingListed`
onData(anyOf(is(instanceOf(Item.class)), withText(eachFile.getName()), isDisplayed()));
}
}
Espresso的正確斷言是什麼? 我應該爲我的物品創建自定義匹配器嗎?
「ListView」和「RecyclerView」之間存在顯着差異,如下所示:https://google.github.io/android-testing-support-library/docs/espresso/lists/ – 4ntoine