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; 20import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 21 22import android.content.res.ColorStateList; 23import android.graphics.drawable.Drawable; 24import android.support.annotation.DrawableRes; 25import android.support.annotation.IdRes; 26import android.support.annotation.LayoutRes; 27import android.support.annotation.NonNull; 28import android.support.annotation.Nullable; 29import android.support.annotation.StyleRes; 30import android.support.design.widget.NavigationView; 31import android.support.test.espresso.UiController; 32import android.support.test.espresso.ViewAction; 33import android.view.LayoutInflater; 34import android.view.View; 35 36import org.hamcrest.Matcher; 37 38public class NavigationViewActions { 39 /** 40 * Sets item text appearance on the content of the navigation view. 41 */ 42 public static ViewAction setItemTextAppearance(final @StyleRes int resId) { 43 return new ViewAction() { 44 @Override 45 public Matcher<View> getConstraints() { 46 return isDisplayed(); 47 } 48 49 @Override 50 public String getDescription() { 51 return "Set item text appearance"; 52 } 53 54 @Override 55 public void perform(UiController uiController, View view) { 56 uiController.loopMainThreadUntilIdle(); 57 58 NavigationView navigationView = (NavigationView) view; 59 navigationView.setItemTextAppearance(resId); 60 61 uiController.loopMainThreadUntilIdle(); 62 } 63 }; 64 } 65 66 /** 67 * Sets item text color on the content of the navigation view. 68 */ 69 public static ViewAction setItemTextColor(final ColorStateList textColor) { 70 return new ViewAction() { 71 @Override 72 public Matcher<View> getConstraints() { 73 return isDisplayed(); 74 } 75 76 @Override 77 public String getDescription() { 78 return "Set item text color"; 79 } 80 81 @Override 82 public void perform(UiController uiController, View view) { 83 uiController.loopMainThreadUntilIdle(); 84 85 NavigationView navigationView = (NavigationView) view; 86 navigationView.setItemTextColor(textColor); 87 88 uiController.loopMainThreadUntilIdle(); 89 } 90 }; 91 } 92 93 /** 94 * Sets item background on the content of the navigation view. 95 */ 96 public static ViewAction setItemBackground(final @Nullable Drawable itemBackground) { 97 return new ViewAction() { 98 @Override 99 public Matcher<View> getConstraints() { 100 return isDisplayed(); 101 } 102 103 @Override 104 public String getDescription() { 105 return "Set item background"; 106 } 107 108 @Override 109 public void perform(UiController uiController, View view) { 110 uiController.loopMainThreadUntilIdle(); 111 112 NavigationView navigationView = (NavigationView) view; 113 navigationView.setItemBackground(itemBackground); 114 115 uiController.loopMainThreadUntilIdle(); 116 } 117 }; 118 } 119 120 /** 121 * Sets item background on the content of the navigation view. 122 */ 123 public static ViewAction setItemBackgroundResource(final @DrawableRes int resId) { 124 return new ViewAction() { 125 @Override 126 public Matcher<View> getConstraints() { 127 return isDisplayed(); 128 } 129 130 @Override 131 public String getDescription() { 132 return "Set item background"; 133 } 134 135 @Override 136 public void perform(UiController uiController, View view) { 137 uiController.loopMainThreadUntilIdle(); 138 139 NavigationView navigationView = (NavigationView) view; 140 navigationView.setItemBackgroundResource(resId); 141 142 uiController.loopMainThreadUntilIdle(); 143 } 144 }; 145 } 146 147 /** 148 * Sets item icon tint list on the content of the navigation view. 149 */ 150 public static ViewAction setItemIconTintList(final @Nullable ColorStateList tint) { 151 return new ViewAction() { 152 @Override 153 public Matcher<View> getConstraints() { 154 return isDisplayed(); 155 } 156 157 @Override 158 public String getDescription() { 159 return "Set item icon tint list"; 160 } 161 162 @Override 163 public void perform(UiController uiController, View view) { 164 uiController.loopMainThreadUntilIdle(); 165 166 NavigationView navigationView = (NavigationView) view; 167 navigationView.setItemIconTintList(tint); 168 169 uiController.loopMainThreadUntilIdle(); 170 } 171 }; 172 } 173 174 /** 175 * Add the specified view as a header to the navigation view. 176 */ 177 public static ViewAction addHeaderView(final @NonNull LayoutInflater inflater, 178 final @LayoutRes int res) { 179 return new ViewAction() { 180 @Override 181 public Matcher<View> getConstraints() { 182 return isDisplayed(); 183 } 184 185 @Override 186 public String getDescription() { 187 return "Add header view"; 188 } 189 190 @Override 191 public void perform(UiController uiController, View view) { 192 uiController.loopMainThreadUntilIdle(); 193 194 NavigationView navigationView = (NavigationView) view; 195 navigationView.addHeaderView(inflater.inflate(res, null, false)); 196 197 uiController.loopMainThreadUntilIdle(); 198 } 199 }; 200 } 201 202 /** 203 * Inflates a view from the specified layout ID and adds it as a header to the navigation view. 204 */ 205 public static ViewAction inflateHeaderView(final @LayoutRes int res) { 206 return new ViewAction() { 207 @Override 208 public Matcher<View> getConstraints() { 209 return isDisplayed(); 210 } 211 212 @Override 213 public String getDescription() { 214 return "Inflate and add header view"; 215 } 216 217 @Override 218 public void perform(UiController uiController, View view) { 219 uiController.loopMainThreadUntilIdle(); 220 221 NavigationView navigationView = (NavigationView) view; 222 navigationView.inflateHeaderView(res); 223 224 uiController.loopMainThreadUntilIdle(); 225 } 226 }; 227 } 228 229 /** 230 * Removes a previously added header view from the navigation view. 231 */ 232 public static ViewAction removeHeaderView(final @Nullable View headerView) { 233 return new ViewAction() { 234 @Override 235 public Matcher<View> getConstraints() { 236 return isDisplayed(); 237 } 238 239 @Override 240 public String getDescription() { 241 return "Remove header view"; 242 } 243 244 @Override 245 public void perform(UiController uiController, View view) { 246 uiController.loopMainThreadUntilIdle(); 247 248 NavigationView navigationView = (NavigationView) view; 249 navigationView.removeHeaderView(headerView); 250 251 uiController.loopMainThreadUntilIdle(); 252 } 253 }; 254 } 255 256 /** 257 * Sets checked item on the navigation view. 258 */ 259 public static ViewAction setCheckedItem(final @IdRes int id) { 260 return new ViewAction() { 261 @Override 262 public Matcher<View> getConstraints() { 263 return isDisplayed(); 264 } 265 266 @Override 267 public String getDescription() { 268 return "Set checked item"; 269 } 270 271 @Override 272 public void perform(UiController uiController, View view) { 273 uiController.loopMainThreadUntilIdle(); 274 275 NavigationView navigationView = (NavigationView) view; 276 navigationView.setCheckedItem(id); 277 278 uiController.loopMainThreadUntilIdle(); 279 } 280 }; 281 } 282 283 /** 284 * Sets icon for the menu item of the navigation view. 285 */ 286 public static ViewAction setIconForMenuItem(final @IdRes int menuItemId, 287 final Drawable iconDrawable) { 288 return new ViewAction() { 289 @Override 290 public Matcher<View> getConstraints() { 291 return isDisplayed(); 292 } 293 294 @Override 295 public String getDescription() { 296 return "Set menu item icon"; 297 } 298 299 @Override 300 public void perform(UiController uiController, View view) { 301 uiController.loopMainThreadUntilIdle(); 302 303 NavigationView navigationView = (NavigationView) view; 304 navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable); 305 306 uiController.loopMainThreadUntilIdle(); 307 } 308 }; 309 } 310 311 /** 312 * Removes the specified menu item from the navigation view. 313 * 314 * @param menuItemId The ID of the menu item to be removed. 315 */ 316 public static ViewAction removeMenuItem(final @IdRes int menuItemId) { 317 return new ViewAction() { 318 @Override 319 public Matcher<View> getConstraints() { 320 return isAssignableFrom(NavigationView.class); 321 } 322 323 @Override 324 public String getDescription() { 325 return "Remove menu item " + menuItemId; 326 } 327 328 @Override 329 public void perform(UiController uiController, View view) { 330 uiController.loopMainThreadUntilIdle(); 331 NavigationView navigationView = (NavigationView) view; 332 navigationView.getMenu().removeItem(menuItemId); 333 uiController.loopMainThreadUntilIdle(); 334 } 335 }; 336 } 337 338} 339