Android UI 测试,已弃用的 ViewAsserts assertHorizontalCenterAligned 的替代方案是什么?
Android UI testing, what is the alternative for deprecated ViewAsserts assertHorizontalCenterAligned?
在 Android UI 测试中,我们有 ViewAsserts class 已在 API 级别 24 中弃用。
class 有一些方法,例如 assertHorizontalCenterAligned 来测试一个视图是否在另一个视图中水平居中。
在新的 Espresso PositionAssertions 中,这种方法的替代方法是什么?
它有左对齐、右对齐、上对齐和下对齐的断言,但没有居中断言。
首先,我的回答不使用 PositionAssertions,而是
我创建了自定义 ViewAsserts,它适用于任何 Android 平台
您的看法
假设您有以下 xml 视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="merchantapp.ul.com.myapplication.MainActivity">
<RelativeLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lll"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
</RelativeLayout>
id 为 lll
的视图位于 id 为 ll
的视图的中心
自定义 ViewAsserts
创建classHorizontalViewAssertion
public class HorizontalViewAssertion extends TypeSafeMatcher<View> {
private final View view;
private HorizontalViewAssertion (View view) {
this.view = view;
}
public static HorizontalViewAssertion alignHorizantalWith(View view) {
return new HorizontalViewAssertion(view);
}
@Override
protected boolean matchesSafely(View item) {
float centerX1 = item.getX() + (((float)item.getWidth())/2.0f);
float centerX2 = this.view.getX() + (((float)this.view.getWidth())/2.0f);
if ( (Math.abs(centerX1 - centerX2)) <= 1)
return true;
return false;
}
@Override
public void describeTo(Description description) {
}
}
您的测试单元
创建您的测试单元
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void testHorizantalCenter() {
onView(withId(R.id.lll)).check(ViewAssertions.matches(alignHorizantalWith(mActivityRule.getActivity().findViewById(R.id.ll))));
}
}
测试会通过
描述
思路很简单,我们有两个视图,我们称它们为view1
和view2
通过获取 x 轴并将其求和为宽度的一半来计算两个视图中心的水平 x
然后检查这两个中心是否相同(好吧,我不是在检查它们是否相等,而是在检查它们之间的差异是否小于 1,那是因为浮点数相除。来自从数学的角度来看,差异永远不会超过 0.5)
我希望这对你有好处
在 Android UI 测试中,我们有 ViewAsserts class 已在 API 级别 24 中弃用。
class 有一些方法,例如 assertHorizontalCenterAligned 来测试一个视图是否在另一个视图中水平居中。
在新的 Espresso PositionAssertions 中,这种方法的替代方法是什么?
它有左对齐、右对齐、上对齐和下对齐的断言,但没有居中断言。
首先,我的回答不使用 PositionAssertions,而是
我创建了自定义 ViewAsserts,它适用于任何 Android 平台
您的看法
假设您有以下 xml 视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="merchantapp.ul.com.myapplication.MainActivity">
<RelativeLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/lll"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerInParent="true"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
</RelativeLayout>
id 为 lll
的视图位于 id 为 ll
自定义 ViewAsserts
创建classHorizontalViewAssertion
public class HorizontalViewAssertion extends TypeSafeMatcher<View> {
private final View view;
private HorizontalViewAssertion (View view) {
this.view = view;
}
public static HorizontalViewAssertion alignHorizantalWith(View view) {
return new HorizontalViewAssertion(view);
}
@Override
protected boolean matchesSafely(View item) {
float centerX1 = item.getX() + (((float)item.getWidth())/2.0f);
float centerX2 = this.view.getX() + (((float)this.view.getWidth())/2.0f);
if ( (Math.abs(centerX1 - centerX2)) <= 1)
return true;
return false;
}
@Override
public void describeTo(Description description) {
}
}
您的测试单元
创建您的测试单元
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void testHorizantalCenter() {
onView(withId(R.id.lll)).check(ViewAssertions.matches(alignHorizantalWith(mActivityRule.getActivity().findViewById(R.id.ll))));
}
}
测试会通过
描述
思路很简单,我们有两个视图,我们称它们为view1
和view2
通过获取 x 轴并将其求和为宽度的一半来计算两个视图中心的水平 x
然后检查这两个中心是否相同(好吧,我不是在检查它们是否相等,而是在检查它们之间的差异是否小于 1,那是因为浮点数相除。来自从数学的角度来看,差异永远不会超过 0.5)
我希望这对你有好处