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 17package android.support.v7.testutils; 18 19import android.content.res.ColorStateList; 20import android.graphics.PorterDuff; 21import android.graphics.drawable.Drawable; 22import android.support.annotation.DrawableRes; 23import android.support.test.espresso.UiController; 24import android.support.test.espresso.ViewAction; 25import android.support.v4.view.TintableBackgroundView; 26import android.support.v4.view.ViewCompat; 27import android.support.v7.widget.AppCompatTextView; 28import android.view.View; 29import org.hamcrest.Matcher; 30 31import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 32import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 33import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast; 34import static org.hamcrest.core.AllOf.allOf; 35 36public class AppCompatTintableViewActions { 37 /** 38 * Sets enabled state on a <code>View</code> that implements the 39 * <code>TintableBackgroundView</code> interface. 40 */ 41 public static ViewAction setEnabled(final boolean enabled) { 42 return new ViewAction() { 43 @Override 44 public Matcher<View> getConstraints() { 45 return allOf(isDisplayingAtLeast(90), TestUtilsMatchers.isTintableBackgroundView()); 46 } 47 48 @Override 49 public String getDescription() { 50 return "set enabled"; 51 } 52 53 @Override 54 public void perform(UiController uiController, View view) { 55 uiController.loopMainThreadUntilIdle(); 56 57 view.setEnabled(enabled); 58 59 uiController.loopMainThreadUntilIdle(); 60 } 61 }; 62 } 63 64 /** 65 * Sets the passed color state list as the background layer on a {@link View} that 66 * implements the {@link TintableBackgroundView} interface. 67 */ 68 public static ViewAction setBackgroundTintList(final ColorStateList colorStateList) { 69 return new ViewAction() { 70 @Override 71 public Matcher<View> getConstraints() { 72 return allOf(isDisplayed(), TestUtilsMatchers.isTintableBackgroundView()); 73 } 74 75 @Override 76 public String getDescription() { 77 return "set background tint list"; 78 } 79 80 @Override 81 public void perform(UiController uiController, View view) { 82 uiController.loopMainThreadUntilIdle(); 83 84 TintableBackgroundView tintableBackgroundView = (TintableBackgroundView) view; 85 tintableBackgroundView.setSupportBackgroundTintList(colorStateList); 86 87 uiController.loopMainThreadUntilIdle(); 88 } 89 }; 90 } 91 92 /** 93 * Sets the passed mode as the background tint mode on a <code>View</code> that 94 * implements the <code>TintableBackgroundView</code> interface. 95 */ 96 public static ViewAction setBackgroundTintMode(final PorterDuff.Mode tintMode) { 97 return new ViewAction() { 98 @Override 99 public Matcher<View> getConstraints() { 100 return allOf(isDisplayed(), TestUtilsMatchers.isTintableBackgroundView()); 101 } 102 103 @Override 104 public String getDescription() { 105 return "set background tint mode"; 106 } 107 108 @Override 109 public void perform(UiController uiController, View view) { 110 uiController.loopMainThreadUntilIdle(); 111 112 TintableBackgroundView tintableBackgroundView = (TintableBackgroundView) view; 113 tintableBackgroundView.setSupportBackgroundTintMode(tintMode); 114 115 uiController.loopMainThreadUntilIdle(); 116 } 117 }; 118 } 119 120 /** 121 * Sets background drawable on a <code>View</code> that implements the 122 * <code>TintableBackgroundView</code> interface. 123 */ 124 public static ViewAction setBackgroundDrawable(final Drawable background) { 125 return new ViewAction() { 126 @Override 127 public Matcher<View> getConstraints() { 128 return allOf(TestUtilsMatchers.isTintableBackgroundView()); 129 } 130 131 @Override 132 public String getDescription() { 133 return "set background drawable"; 134 } 135 136 @Override 137 public void perform(UiController uiController, View view) { 138 uiController.loopMainThreadUntilIdle(); 139 140 view.setBackgroundDrawable(background); 141 142 uiController.loopMainThreadUntilIdle(); 143 } 144 }; 145 } 146 147 /** 148 * Sets background resource on a <code>View</code> that implements the 149 * <code>TintableBackgroundView</code> interface. 150 */ 151 public static ViewAction setBackgroundResource(final @DrawableRes int resId) { 152 return new ViewAction() { 153 @Override 154 public Matcher<View> getConstraints() { 155 return allOf(TestUtilsMatchers.isTintableBackgroundView()); 156 } 157 158 @Override 159 public String getDescription() { 160 return "set background resource"; 161 } 162 163 @Override 164 public void perform(UiController uiController, View view) { 165 uiController.loopMainThreadUntilIdle(); 166 167 view.setBackgroundResource(resId); 168 169 uiController.loopMainThreadUntilIdle(); 170 } 171 }; 172 } 173 174} 175