2016-08-01 8 views
0

ユーザーがViewPagerでスワイプしたりTabViewをタップしたりすると、タブビューのタイトルがフラグメントのタイトルと「同期」することを確認するテストを追加します。ViewPagerのフラグメントとタブビューの同期を確認するためのAndroid Espressoテスト

私はもっと簡単なことをやっていますが、テストではonView(...)という行で無限ループが発生します。私はdinamicallyタブを追加している

...

<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> 

...

@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である:ここでは

コードです私が使用しているように、アダプタ:

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

答えて

0

@UiThreadの代わりに@ android.support.test.annotation.UiThreadTestが主な問題だと思われます。

残りについては、TabLayout/ViewPagerをテストするためのGitHubの小さなサンプルを作成しました:https://github.com/MyWay/TabLayoutTest

関連する問題