1 /* 2 * Copyright (C) 2016 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.appcompat.testutils; 18 19 import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 20 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 21 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast; 22 23 import static org.hamcrest.core.AllOf.allOf; 24 25 import android.content.res.ColorStateList; 26 import android.graphics.PorterDuff; 27 import android.support.test.espresso.UiController; 28 import android.support.test.espresso.ViewAction; 29 import android.view.View; 30 import android.widget.TextView; 31 32 import androidx.core.view.ViewCompat; 33 34 import org.hamcrest.Matcher; 35 36 public class TestUtilsActions { 37 /** 38 * Sets layout direction on the view. 39 */ 40 public static ViewAction setLayoutDirection(final int layoutDirection) { 41 return new ViewAction() { 42 @Override 43 public Matcher<View> getConstraints() { 44 return isDisplayed(); 45 } 46 47 @Override 48 public String getDescription() { 49 return "set layout direction"; 50 } 51 52 @Override 53 public void perform(UiController uiController, View view) { 54 uiController.loopMainThreadUntilIdle(); 55 56 ViewCompat.setLayoutDirection(view, layoutDirection); 57 58 uiController.loopMainThreadUntilIdle(); 59 } 60 }; 61 } 62 63 /** 64 * Sets text appearance on {@code TextView}. 65 */ 66 public static ViewAction setTextAppearance(final int resId) { 67 return new ViewAction() { 68 @Override 69 public Matcher<View> getConstraints() { 70 return allOf(isDisplayingAtLeast(90), isAssignableFrom(TextView.class)); 71 } 72 73 @Override 74 public String getDescription() { 75 return "TextView set text appearance"; 76 } 77 78 @Override 79 public void perform(UiController uiController, View view) { 80 uiController.loopMainThreadUntilIdle(); 81 82 TextView textView = (TextView) view; 83 textView.setTextAppearance(textView.getContext(), resId); 84 85 uiController.loopMainThreadUntilIdle(); 86 } 87 }; 88 } 89 90 /** 91 * Sets the passed color state list as the background layer on a {@link View} with 92 * {@link ViewCompat#setBackgroundTintList(View, ColorStateList)} API. 93 */ 94 public static ViewAction setBackgroundTintListViewCompat(final ColorStateList colorStateList) { 95 return new ViewAction() { 96 @Override 97 public Matcher<View> getConstraints() { 98 return isDisplayed(); 99 } 100 101 @Override 102 public String getDescription() { 103 return "set background tint list"; 104 } 105 106 @Override 107 public void perform(UiController uiController, View view) { 108 uiController.loopMainThreadUntilIdle(); 109 110 ViewCompat.setBackgroundTintList(view, colorStateList); 111 112 uiController.loopMainThreadUntilIdle(); 113 } 114 }; 115 } 116 117 /** 118 * Sets the passed mode as the background tint mode on a {@link View} with 119 * {@link ViewCompat#setBackgroundTintMode(View, PorterDuff.Mode)} API. 120 */ 121 public static ViewAction setBackgroundTintModeViewCompat(final PorterDuff.Mode tintMode) { 122 return new ViewAction() { 123 @Override 124 public Matcher<View> getConstraints() { 125 return isDisplayed(); 126 } 127 128 @Override 129 public String getDescription() { 130 return "set background tint mode"; 131 } 132 133 @Override 134 public void perform(UiController uiController, View view) { 135 uiController.loopMainThreadUntilIdle(); 136 137 ViewCompat.setBackgroundTintMode(view, tintMode); 138 139 uiController.loopMainThreadUntilIdle(); 140 } 141 }; 142 } 143 144 public static ViewAction setEnabled(final boolean enabled) { 145 return new ViewAction() { 146 @Override 147 public Matcher<View> getConstraints() { 148 return isDisplayed(); 149 } 150 151 @Override 152 public String getDescription() { 153 return "set enabled"; 154 } 155 156 @Override 157 public void perform(UiController uiController, View view) { 158 uiController.loopMainThreadUntilIdle(); 159 160 view.setEnabled(enabled); 161 162 uiController.loopMainThreadUntilIdle(); 163 } 164 }; 165 } 166 167 public static ViewAction setSystemUiVisibility(final int sysUiVisibility) { 168 return new ViewAction() { 169 @Override 170 public Matcher<View> getConstraints() { 171 return isDisplayed(); 172 } 173 174 @Override 175 public String getDescription() { 176 return "Sets system ui visibility"; 177 } 178 179 @Override 180 public void perform(UiController uiController, View view) { 181 uiController.loopMainThreadUntilIdle(); 182 183 view.setSystemUiVisibility(sysUiVisibility); 184 185 uiController.loopMainThreadUntilIdle(); 186 } 187 }; 188 } 189 } 190