2016-08-01 16 views
0

Ich möchte einen Test hinzufügen zu überprüfen, dass, wenn der Benutzer Swipe mit dem ViewPager oder auf einem TabView tippen, TabView Titel wird „synchronisiert“ mit dem Titel des Fragments.Android Espresso Test Syncness zwischen ViewPager der Fragmente und Registeransichten überprüfen

mit ihm zu spielen beginnen Ich mache etwas einfacher, aber die Testergebnisse in einer Endlos-Schleife an der Linie OnView (...). Hier

ist der Code:

@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"))); 
} 

Und das ist mein 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> 

...

Ich füge Tab dinamically zu der Adapter, wie ich verwende:

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

Antwort

0

Es scheint das Hauptproblem war @ android.support.test.annotation.UiThreadTest statt @UiThread.

In Bezug auf den Rest habe ich ein kleines Beispiel auf GitHub erstellt, um TabLayout/ViewPager zu testen: https://github.com/MyWay/TabLayoutTest