2015-11-03 23 views
12

Ứng dụng của tôi tải lại dữ liệu khi vuốt xuống được thực hiện trên SwipeRefreshLayout. Bây giờ tôi cố gắng để kiểm tra điều này với Android Test Kit/Espresso như thế này:Espresso: Cách kiểm tra tính năng SwipeRefreshLayout?

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

Thật không may này không thành công với

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

Tất nhiên, cách bố trí có thể nhìn thấy và trượt tay làm việc, nhưng tôi không chắc chắn nếu tôi làm gì sai? Bố cục kéo dài toàn bộ màn hình, vì vậy, nên thực sự có thể cho Espresso thực hiện một số hành động trên đó.

Trả lời

30

Ngủ đôi khi có ích. Lý do cơ bản là chế độ xem được vuốt chỉ hiển thị 89% cho người dùng, trong khi hành động vuốt của Espresso nội bộ yêu cầu 90%. Vì vậy, giải pháp là để bọc các hành động swipe thành hành động khác và ghi đè lên những khó khăn này bằng tay, như thế này:

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

này sau đó có thể được gọi như thế này:

onView(withId(R.id.my_refresh_layout)) 
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85)); 
Các vấn đề liên quan