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 android.support.design.testutils;
18 
19 import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
20 
21 import android.content.res.ColorStateList;
22 import android.support.annotation.ColorInt;
23 import android.support.annotation.DrawableRes;
24 import android.support.design.widget.FloatingActionButton;
25 import android.support.test.espresso.UiController;
26 import android.support.test.espresso.ViewAction;
27 import android.view.View;
28 
29 import org.hamcrest.Matcher;
30 
31 public class FloatingActionButtonActions {
32 
setBackgroundTintColor(@olorInt final int color)33     public static ViewAction setBackgroundTintColor(@ColorInt final int color) {
34         return new ViewAction() {
35             @Override
36             public Matcher<View> getConstraints() {
37                 return isAssignableFrom(FloatingActionButton.class);
38             }
39 
40             @Override
41             public String getDescription() {
42                 return "Sets FloatingActionButton background tint";
43             }
44 
45             @Override
46             public void perform(UiController uiController, View view) {
47                 uiController.loopMainThreadUntilIdle();
48 
49                 final FloatingActionButton fab = (FloatingActionButton) view;
50                 fab.setBackgroundTintList(ColorStateList.valueOf(color));
51 
52                 uiController.loopMainThreadUntilIdle();
53             }
54         };
55     }
56 
57     public static ViewAction setImageResource(@DrawableRes final int resId) {
58         return new ViewAction() {
59             @Override
60             public Matcher<View> getConstraints() {
61                 return isAssignableFrom(FloatingActionButton.class);
62             }
63 
64             @Override
65             public String getDescription() {
66                 return "Sets FloatingActionButton image resource";
67             }
68 
69             @Override
70             public void perform(UiController uiController, View view) {
71                 uiController.loopMainThreadUntilIdle();
72 
73                 final FloatingActionButton fab = (FloatingActionButton) view;
74                 fab.setImageResource(resId);
75 
76                 uiController.loopMainThreadUntilIdle();
77             }
78         };
79     }
80 
81     public static ViewAction setSize(@FloatingActionButton.Size final int size) {
82         return new ViewAction() {
83             @Override
84             public Matcher<View> getConstraints() {
85                 return isAssignableFrom(FloatingActionButton.class);
86             }
87 
88             @Override
89             public String getDescription() {
90                 return "Sets FloatingActionButton size";
91             }
92 
93             @Override
94             public void perform(UiController uiController, View view) {
95                 uiController.loopMainThreadUntilIdle();
96 
97                 final FloatingActionButton fab = (FloatingActionButton) view;
98                 fab.setSize(size);
99 
100                 uiController.loopMainThreadUntilIdle();
101             }
102         };
103     }
104 
105 }
106