Android Kotlin(Instrumented)测试,断言ImageButton具有正确的资源不起作用

时间:2019-12-22 21:57:51

标签: android android-testing android-imagebutton instrumented-test

我正在使用Kotlin开发一个Android应用程序。我正在为我的应用程序编写检测测试。现在,我在测试ImageButton时遇到问题,因为它无法按预期工作。我想测试在打开活动时是否将ImageButton视图设置为具有正确的可绘制对象。

在我的活动XML布局文件中,我有一个包含以下代码的ImageButton

// Store your relevant keywords in a list of strings
List<string> datatypes = new List<string>{"string", "int"};

// Now loop over the ValueList using a normal for loop
// starting from the second elemend and skipping the next
for(int x = 1; x < ValueList.Count; x+=2)
{
    // Get the current element in the ValueList
    string current = ValueList[x];

    // Verify if it is present in the identifiers list
    if (datatypes.Contains(current)))
    {
        // Yes, then add the element before the current and the current to the MyObject list
        MyObject obj = new MyObject;
        obj.name = ValueList[x - 1];
        obj.type = current;
        listObjects.Add(obj);
    }
}

我编写了一个测试来断言图像按钮视图是使用正确的可绘制资源设置的。以下是我的测试的实施。

<ImageButton
        android:id="@+id/camera_image_btn_capture"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:background="@null"
        android:src="@drawable/camera_capture"
        android:layout_width="85dp"
        android:layout_height="85dp" />

您可以在测试方法中看到,我断言两个可绘制资源是否相等。当我运行测试时,测试总是失败。但它应该过去了。我的代码中缺少什么,我该如何解决?

1 个答案:

答案 0 :(得分:0)

经过一番努力后,我发现了问题。如果有人遇到相同问题,我会发布解决方案。问题是我没有使用正确的方法来检查两个可绘制对象是否相等。为了比较两个可绘制对象,我们必须比较它们的恒定状态。我写了一个测试助手函数来断言这一点。

这是代码。

b