使用any()时出错; 在Android测试中需要不兼容的类型:Matcher <View> found:Matcher <Object>

UmA*_*orn 3 testing android android-testing android-espresso

我运行下面的代码并返回任何错误();

error: incompatible types
required: Matcher <View>
found:    Matcher <Object>

/** 
     * Perform action of waiting until UI thread is free. <p/> E.g.: onView(isRoot()).perform(waitUntilIdle());
     * @return
     */
    public static ViewAction waitUntilIdle(){
      return new ViewAction(){
        @Override public Matcher<View> getConstraints(){
          return anything();
        }
        @Override public String getDescription(){
          return "wait until UI thread is free";
        }
        @Override public void perform(    final UiController uiController,    final View view){
          uiController.loopMainThreadUntilIdle();
        }
      }
    ;
    }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

yog*_*arl 8

anything()不是一般的方法,所以你总会得到一个Matcher<Object>.

内部anything()使用IsAnything该类.您可以使用自己的anyView()方法返回Matcher<View>.

public static ViewAction waitUntilIdle(){
    return new ViewAction(){
        @Override public Matcher<View> getConstraints(){
            return anyView();
        }

        @NonNull
        private Matcher<View> anyView() {
            return new IsAnything<>();
        }

        @Override public String getDescription(){
            return "wait until UI thread is free";
        }
        @Override public void perform(    final UiController uiController,    final View view){
            uiController.loopMainThreadUntilIdle();
        }
    }
            ;
}
Run Code Online (Sandbox Code Playgroud)