Android Espresso 测试以检查 ViewPager 的片段和 TabView 之间的同步性
Android Espresso test to check syncness between ViewPager's fragments and TabViews
我想添加一个测试来检查当用户使用 ViewPager 滑动或点击 TabView 时,tabview 的标题是 "synced" 和片段的标题。
为了开始使用它,我做了一些更简单的事情,但是测试结果在 onView(...) 行出现无限循环。
代码如下:
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(
MyActivity.class);
@Test
@android.support.test.annotation.UiThreadTest
public void switchTab() {
((MyActivity)mActivityRule.getActivity()).addMyTab("Test 1", true);
onView(withText("Test 1"))
.perform(click())
.check(matches(withText("Test 1")));
}
这是我的 XML:
...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
...
我正在向适配器动态添加制表符,因为我正在使用:
mTabLayout.setupWithViewPager(mPager);
...
myAdapter.add(...);
看来主要问题是@android.support.test.annotation.UiThreadTest而不是@UiThread。
关于其余部分,我在 GitHub 上创建了一个小示例来测试 TabLayout/ViewPager:https://github.com/MyWay/TabLayoutTest
我想添加一个测试来检查当用户使用 ViewPager 滑动或点击 TabView 时,tabview 的标题是 "synced" 和片段的标题。
为了开始使用它,我做了一些更简单的事情,但是测试结果在 onView(...) 行出现无限循环。
代码如下:
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Rule
public ActivityTestRule mActivityRule = new ActivityTestRule<>(
MyActivity.class);
@Test
@android.support.test.annotation.UiThreadTest
public void switchTab() {
((MyActivity)mActivityRule.getActivity()).addMyTab("Test 1", true);
onView(withText("Test 1"))
.perform(click())
.check(matches(withText("Test 1")));
}
这是我的 XML:
...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
...
我正在向适配器动态添加制表符,因为我正在使用:
mTabLayout.setupWithViewPager(mPager);
...
myAdapter.add(...);
看来主要问题是@android.support.test.annotation.UiThreadTest而不是@UiThread。
关于其余部分,我在 GitHub 上创建了一个小示例来测试 TabLayout/ViewPager:https://github.com/MyWay/TabLayoutTest