Home | History | Annotate | Download | only in widget
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.swiperefreshlayout.widget;
     18 
     19 import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
     20 
     21 import android.support.test.espresso.UiController;
     22 import android.support.test.espresso.ViewAction;
     23 import android.view.View;
     24 
     25 import org.hamcrest.Matcher;
     26 
     27 
     28 public class SwipeRefreshLayoutActions {
     29     public static ViewAction setRefreshing() {
     30         return new ViewAction() {
     31             @Override
     32             public Matcher<View> getConstraints() {
     33                 return isAssignableFrom(SwipeRefreshLayout.class);
     34             }
     35 
     36             @Override
     37             public String getDescription() {
     38                 return "Set SwipeRefreshLayout refreshing state";
     39             }
     40 
     41             @Override
     42             public void perform(UiController uiController, View view) {
     43                 uiController.loopMainThreadUntilIdle();
     44 
     45                 SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view;
     46                 swipeRefreshLayout.setRefreshing(true);
     47 
     48                 // Intentionally not waiting until idle here because it will not become idle due to
     49                 // the animation.
     50             }
     51         };
     52     }
     53 
     54     public static ViewAction setSize(final int size) {
     55         return new ViewAction() {
     56             @Override
     57             public Matcher<View> getConstraints() {
     58                 return isAssignableFrom(SwipeRefreshLayout.class);
     59             }
     60 
     61             @Override
     62             public String getDescription() {
     63                 return "Set SwipeRefreshLayout size";
     64             }
     65 
     66             @Override
     67             public void perform(UiController uiController, View view) {
     68                 uiController.loopMainThreadUntilIdle();
     69 
     70                 SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view;
     71                 swipeRefreshLayout.setSize(size);
     72 
     73                 uiController.loopMainThreadUntilIdle();
     74             }
     75         };
     76     }
     77 
     78     public static ViewAction setEnabled(final boolean enabled) {
     79         return new ViewAction() {
     80             @Override
     81             public Matcher<View> getConstraints() {
     82                 return isAssignableFrom(SwipeRefreshLayout.class);
     83             }
     84 
     85             @Override
     86             public String getDescription() {
     87                 return "Set SwipeRefreshLayout enabled state";
     88             }
     89 
     90             @Override
     91             public void perform(UiController uiController, View view) {
     92                 uiController.loopMainThreadUntilIdle();
     93 
     94                 SwipeRefreshLayout swipeRefreshLayout = (SwipeRefreshLayout) view;
     95                 swipeRefreshLayout.setEnabled(enabled);
     96 
     97                 uiController.loopMainThreadUntilIdle();
     98             }
     99         };
    100     }
    101 }
    102