Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when use anything(); in Android testing incompatible types required: Matcher <View> found: Matcher <Object>

I run the code belows and got error at return anything();

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

Any ideas?

like image 369
UmAnusorn Avatar asked Dec 05 '25 11:12

UmAnusorn


1 Answers

anything() is not a generic method, so you will always get a Matcher<Object>.

Internally anything() uses the IsAnything class. You can make your own anyView() method to return a 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();
        }
    }
            ;
}
like image 148
yogurtearl Avatar answered Dec 07 '25 00:12

yogurtearl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!