Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用浓缩咖啡匹配此表格单元格?_Android_Android Layout_Android Testing_Hamcrest_Android Espresso - Fatal编程技术网

Android 如何使用浓缩咖啡匹配此表格单元格?

Android 如何使用浓缩咖啡匹配此表格单元格?,android,android-layout,android-testing,hamcrest,android-espresso,Android,Android Layout,Android Testing,Hamcrest,Android Espresso,我需要匹配由红色矩形突出显示的视图。我应该为它写哪种意式咖啡 这是一个表格布局,所有单元格都是TextView的实例。单元视图没有唯一的ID。感兴趣的视图中可能有文本,也可能没有文本。我所知道的是,这种观点总是在“食物组”的细胞下面 欢迎提供任何线索。这是我将在您的案例中编写的测试 public void testCellBelowFoodGroup() { getActivity(); onView( allOf( isDescenda

我需要匹配由红色矩形突出显示的视图。我应该为它写哪种意式咖啡

这是一个表格布局,所有单元格都是TextView的实例。单元视图没有唯一的ID。感兴趣的视图中可能有文本,也可能没有文本。我所知道的是,这种观点总是在“食物组”的细胞下面


欢迎提供任何线索。

这是我将在您的案例中编写的测试

public void testCellBelowFoodGroup() {
    getActivity();
    onView(
        allOf(
            isDescendantOfA(isAssignableFrom(TableLayout.class)),
            isInRowBelow(withText("Food Group")),
            hasChildPosition(0)
            )
    ).check(matches(withText("TEXT TO BE FOUND")));
}
因此,我们在给定的TableLayout中寻找一个视图,它位于“Food Group”文本下面的一行中,这是该行最左边的元素。然后,我们可以对该视图执行任何操作,例如,检查其文本

Espresso不提供
isInRowBelow
hasChildPosition
,它们是自定义方法,在使用Espresso进行测试时通常如此:鼓励您创建自己的视图断言和视图匹配器

下面是实现

static Matcher<View> isInRowBelow(final Matcher<View> viewInRowAbove) {
    checkNotNull(viewInRowAbove);
    return new TypeSafeMatcher<View>(){

        @Override
        public void describeTo(Description description) {
            description.appendText("is below a: ");
            viewInRowAbove.describeTo(description);
        }

        @Override
        public boolean matchesSafely(View view) {
            // Find the current row
            ViewParent viewParent = view.getParent();
            if (!(viewParent instanceof TableRow)) {
                return false;
            }
            TableRow currentRow = (TableRow) viewParent;
            // Find the row above
            TableLayout table = (TableLayout) currentRow.getParent();
            int currentRowIndex = table.indexOfChild(currentRow);
            if (currentRowIndex < 1) {
                return false;
            }
            TableRow rowAbove = (TableRow) table.getChildAt(currentRowIndex - 1);
            // Does the row above contains at least one view that matches viewInRowAbove?
            for(int i = 0 ; i < rowAbove.getChildCount() ; i++) {
                if (viewInRowAbove.matches(rowAbove.getChildAt(i))) {
                    return true;
                }
            }
            return false;
        }};
}

static Matcher<View> hasChildPosition(final int i) {
    return new TypeSafeMatcher<View>(){

        @Override
        public void describeTo(Description description) {
            description.appendText("is child #" + i);
        }

        @Override
        public boolean matchesSafely(View view) {
            ViewParent viewParent = view.getParent();
            if (!(viewParent instanceof ViewGroup)) {
                return false;
            }
            ViewGroup viewGroup = (ViewGroup) viewParent;
            return (viewGroup.indexOfChild(view) == i);
        }};
}
静态匹配器如下图所示(最终匹配器视图如上图所示){
checkNotNull(查看如上所示);
返回新的TypeSafeMatcher(){
@凌驾
公共无效说明(说明){
description.appendText(“在a:”下面);
视图如上所示。描述(描述);
}
@凌驾
公共布尔匹配安全(视图){
//查找当前行
ViewParent-ViewParent=view.getParent();
如果(!(viewParent instanceof TableRow)){
返回false;
}
TableRow currentRow=(TableRow)viewParent;
//找到上面的那一行
TableLayout table=(TableLayout)currentRow.getParent();
int currentRowIndex=table.indexOfChild(currentRow);
如果(currentRowIndex<1){
返回false;
}
tableRowOver=(TableRow)table.getChildAt(currentRowIndex-1);
//上面的行是否至少包含一个与上面的ViewInRowOver匹配的视图?
对于(int i=0;i

完整的源代码可以从

下载,这是一个基本想法:我将尝试编写一个匹配器isInTableAtPosition(最终匹配器表匹配器,int行,int列)。如果视图的父级是TableRow,并且它是该行的第th子级,则可以匹配视图,然后检查该TableRow是否是tableMatcher匹配的表的第th子级。不确定TableLayout的内部结构,它可能有一个更复杂的内部布局,但这可以从视图层次结构和Android源代码中看出。听起来是一个需要检查的想法,谢谢!你想写什么样的断言?当您说“感兴趣的视图内部可能有文本,也可能没有文本”时,您是否知道对于给定的测试用例,该视图是否会有文本以及应该是什么?