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.design.testutils; 18 19import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 20 21import android.content.res.ColorStateList; 22import android.support.annotation.ColorInt; 23import android.support.annotation.DrawableRes; 24import android.support.design.widget.CoordinatorLayout; 25import android.support.design.widget.FloatingActionButton; 26import android.support.test.espresso.UiController; 27import android.support.test.espresso.ViewAction; 28import android.view.View; 29 30import org.hamcrest.Matcher; 31 32public class FloatingActionButtonActions { 33 34 public static ViewAction setBackgroundTintColor(@ColorInt final int color) { 35 return setBackgroundTintList(ColorStateList.valueOf(color)); 36 } 37 38 public static ViewAction setBackgroundTintList(@ColorInt final ColorStateList tint) { 39 return new ViewAction() { 40 @Override 41 public Matcher<View> getConstraints() { 42 return isAssignableFrom(FloatingActionButton.class); 43 } 44 45 @Override 46 public String getDescription() { 47 return "Sets FloatingActionButton background tint"; 48 } 49 50 @Override 51 public void perform(UiController uiController, View view) { 52 uiController.loopMainThreadUntilIdle(); 53 54 final FloatingActionButton fab = (FloatingActionButton) view; 55 fab.setBackgroundTintList(tint); 56 57 uiController.loopMainThreadUntilIdle(); 58 } 59 }; 60 } 61 62 public static ViewAction setImageResource(@DrawableRes final int resId) { 63 return new ViewAction() { 64 @Override 65 public Matcher<View> getConstraints() { 66 return isAssignableFrom(FloatingActionButton.class); 67 } 68 69 @Override 70 public String getDescription() { 71 return "Sets FloatingActionButton image resource"; 72 } 73 74 @Override 75 public void perform(UiController uiController, View view) { 76 uiController.loopMainThreadUntilIdle(); 77 78 final FloatingActionButton fab = (FloatingActionButton) view; 79 fab.setImageResource(resId); 80 81 uiController.loopMainThreadUntilIdle(); 82 } 83 }; 84 } 85 86 public static ViewAction setSize(@FloatingActionButton.Size final int size) { 87 return new ViewAction() { 88 @Override 89 public Matcher<View> getConstraints() { 90 return isAssignableFrom(FloatingActionButton.class); 91 } 92 93 @Override 94 public String getDescription() { 95 return "Sets FloatingActionButton size"; 96 } 97 98 @Override 99 public void perform(UiController uiController, View view) { 100 uiController.loopMainThreadUntilIdle(); 101 102 final FloatingActionButton fab = (FloatingActionButton) view; 103 fab.setSize(size); 104 105 uiController.loopMainThreadUntilIdle(); 106 } 107 }; 108 } 109 110 public static ViewAction setCompatElevation(final float size) { 111 return new ViewAction() { 112 @Override 113 public Matcher<View> getConstraints() { 114 return isAssignableFrom(FloatingActionButton.class); 115 } 116 117 @Override 118 public String getDescription() { 119 return "Sets FloatingActionButton elevation"; 120 } 121 122 @Override 123 public void perform(UiController uiController, View view) { 124 uiController.loopMainThreadUntilIdle(); 125 126 final FloatingActionButton fab = (FloatingActionButton) view; 127 fab.setCompatElevation(size); 128 129 uiController.loopMainThreadUntilIdle(); 130 } 131 }; 132 } 133 134 public static ViewAction setLayoutGravity(final int gravity) { 135 return new ViewAction() { 136 @Override 137 public Matcher<View> getConstraints() { 138 return isAssignableFrom(View.class); 139 } 140 141 @Override 142 public String getDescription() { 143 return "Sets Views layout_gravity"; 144 } 145 146 @Override 147 public void perform(UiController uiController, View view) { 148 uiController.loopMainThreadUntilIdle(); 149 150 CoordinatorLayout.LayoutParams lp = 151 (CoordinatorLayout.LayoutParams) view.getLayoutParams(); 152 lp.gravity = gravity; 153 view.requestLayout(); 154 155 uiController.loopMainThreadUntilIdle(); 156 } 157 }; 158 } 159 160 public static ViewAction hideThenShow(final int animDuration) { 161 return new ViewAction() { 162 @Override 163 public Matcher<View> getConstraints() { 164 return isAssignableFrom(FloatingActionButton.class); 165 } 166 167 @Override 168 public String getDescription() { 169 return "Calls hide() then show()"; 170 } 171 172 @Override 173 public void perform(UiController uiController, View view) { 174 uiController.loopMainThreadUntilIdle(); 175 176 FloatingActionButton fab = (FloatingActionButton) view; 177 fab.hide(); 178 fab.show(); 179 180 uiController.loopMainThreadForAtLeast(animDuration + 100); 181 } 182 }; 183 } 184 185 public static ViewAction showThenHide(final int animDuration) { 186 return new ViewAction() { 187 @Override 188 public Matcher<View> getConstraints() { 189 return isAssignableFrom(FloatingActionButton.class); 190 } 191 192 @Override 193 public String getDescription() { 194 return "Calls show() then hide()"; 195 } 196 197 @Override 198 public void perform(UiController uiController, View view) { 199 uiController.loopMainThreadUntilIdle(); 200 201 FloatingActionButton fab = (FloatingActionButton) view; 202 fab.show(); 203 fab.hide(); 204 205 uiController.loopMainThreadForAtLeast(animDuration + 50); 206 } 207 }; 208 } 209 210} 211