2016-08-01 74 views
0

我想添加一個測試來檢查用戶在ViewPager上輕掃或點擊TabView時TabView的標題與片段的標題「同步」。Android Espresso測試檢查ViewPager的片段和TabViews之間的同步

要開始玩它我做的事情比較簡單,但測試結果在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> 

...

我添加標籤dinamically到我正在使用的適配器:

mTabLayout.setupWithViewPager(mPager); 
... 
myAdapter.add(...); 

回答

0

看來主要的問題是@ android.support.test.annotation.UiThreadTest而不是@UiThread。

關於其他問題,我在GitHub上創建了一個小例子來測試TabLayout/ViewPager:https://github.com/MyWay/TabLayoutTest