gpt4 book ai didi

android - 有没有办法使用 espresso 来测试 View 的大小(高度和宽度)以进行 android Instrumentation 测试?

转载 作者:行者123 更新时间:2023-12-04 23:43:08 25 4
gpt4 key购买 nike

I want to check if the visibility and size of the adFrame changes after clicking the buttonShow


onView(withId(buttonShow)).perform(click());
onView(withId(adFrame)).check(matches(isDisplayed()));
onView(withId(adFrame)).check(?);

我正在寻找 的解决方案这个 ? .

Do we have an assertion that can test the size of the adFrame using the espresso for this Android Instrumentation test?

最佳答案

给定以下匹配器:

class WidthMatcher(private val width: Int = ViewGroup.LayoutParams.MATCH_PARENT) : TypeSafeMatcher<View>() {
companion object {
fun withWidth(width: Int) = WidthMatcher(width)
}

override fun describeTo(description: Description?) {
description?.appendText("View with width: ${width}px")
}

override fun matchesSafely(item: View): Boolean {
return item.layoutParams?.width == width
}
}

class HeightMatcher(private val height: Int = ViewGroup.LayoutParams.MATCH_PARENT) : TypeSafeMatcher<View>() {
companion object {
fun withHeight(height: Int) = HeightMatcher(height)
}

override fun describeTo(description: Description?) {
description?.appendText("View with height: ${height}px")
}

override fun matchesSafely(item: View): Boolean {
return item.layoutParams?.height == height
}
}
你只需要检查如下:
onView(withId(adFrame)).check(matches(withHeight(ViewGroup.LayoutParams.MATCH_PARENT)))
您还可以检查以 px 为单位的任何大小。您可以使用以下方法将 dp 转换为 px:
/**
* Converts dp unit to its equivalent in pixels, based on device density.
*
* @param dp The value in dp (density independent pixels) unit aim to convert into pixels
* @return The equivalent int value in px of the provided dp value depending on device density
*/
fun dpToPx(dp: Int): Int {
(dp * Resources.getSystem().displayMetrics.density).roundToInt()
}

/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @return The equivalent int value in dp of the provided px value depending on device density
*/
fun pxToDp(px: Int): Int {
(px / Resources.getSystem().displayMetrics.density).roundToInt()
}

关于android - 有没有办法使用 espresso 来测试 View 的大小(高度和宽度)以进行 android Instrumentation 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51101563/

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