2015-11-03 8 views
12

SwipeRefreshLayoutでダウンスワイプを実行すると、アプリがデータを再読み込みします。今、私はAndroidのテストキットでこれをテストしよう/エスプレッソは次のように:エスプレッソ:SwipeRefreshLayoutのテスト方法

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

を残念ながら、これはもちろん

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

で失敗し、レイアウトが表示され、手動でスワイプして動作しますが、私はわかりませんよ私が何か間違っているのなら?レイアウトは画面全体に広がっているので、でなければなりません。

答えて

30

時には寝ると助けになることがあります。エスプレッソのスワイプアクションは内部的に90%を要求していたのに対し、スワイプされるビューはユーザーにはほんの89%しか見えないという根本的な理由がありました。だから、解決策は、このように、手で別行動にスワイプアクションをラップし、これらの制約を無効にすることです:

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

これは、このように呼び出すことができます。

onView(withId(R.id.my_refresh_layout)) 
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85)); 
関連する問題