2015-11-03 11 views
12

La mia app ricarica i dati quando un down-swipe viene eseguito su un SwipeRefreshLayout. Ora cerco di testare questo con Test Kit Android/Espresso in questo modo:Espresso: come testare SwipeRefreshLayout?

onView(withId(R.id.my_refresh_layout)).perform(swipeDown()); 

Purtroppo questo non riesce con

android.support.test.espresso.PerformException: Error performing 'fast swipe' 
on view 'with id: my.app.package:id/my_refresh_layout'. 
... 
Caused by: java.lang.RuntimeException: Action will not be performed because the target view 
does not match one or more of the following constraints: 
at least 90 percent of the view's area is displayed to the user. 
Target view: "SwipeRefreshLayout{id=2131689751, res-name=my_refresh_layout, visibility=VISIBLE, 
width=480, height=672, has-focus=false, has-focusable=true, has-window-focus=true, 
is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, 
child-count=2}" 

Naturalmente, il layout è visibile e strisciata manualmente funziona, ma io sono sicuro se sto facendo qualcosa di sbagliato? Il layout si estende su tutto lo schermo, quindi è davvero possibile per Espresso per fare qualche azione su di esso.

risposta

30

Dormire su di esso a volte aiuta. Il motivo di fondo era che la vista da toccare era visibile solo all'89% per l'utente, mentre le azioni di scorrimento di Espresso richiedevano internamente il 90%. Quindi la soluzione è quella di avvolgere l'azione colpo in un'altra azione e ignorare questi vincoli a mano, in questo modo:

public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) { 
    return new ViewAction() { 
     @Override 
     public Matcher<View> getConstraints() { 
      return constraints; 
     } 

     @Override 
     public String getDescription() { 
      return action.getDescription(); 
     } 

     @Override 
     public void perform(UiController uiController, View view) { 
      action.perform(uiController, view); 
     } 
    }; 
} 

Questo può quindi essere chiamato in questo modo:

onView(withId(R.id.my_refresh_layout)) 
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85));