Created
May 26, 2016 13:32
-
-
Save vincetreur/2cf5f1ac10c1422da8c07137de564a5a to your computer and use it in GitHub Desktop.
An Android Espresso ViewAction that flashes the background color of a View.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
import android.graphics.drawable.Drawable; | |
import android.os.Build; | |
import android.support.test.espresso.UiController; | |
import android.support.test.espresso.ViewAction; | |
import android.view.View; | |
import org.hamcrest.Matcher; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | |
public class FlashActions { | |
public static ViewAction flashView(int count) { | |
return new FlashViewAction(count); | |
} | |
public static ViewAction flashView() { | |
return new FlashViewAction(4); | |
} | |
static class FlashViewAction implements ViewAction { | |
private int mCount; | |
public FlashViewAction(int count) { | |
mCount = count; | |
} | |
@Override | |
public String getDescription() { | |
return "flash view"; | |
} | |
@Override | |
public Matcher<View> getConstraints() { | |
return isDisplayed(); | |
} | |
@Override | |
public void perform(UiController uic, View view) { | |
Drawable bgDrawable = view.getBackground(); | |
for (int i = 0; i < mCount; i++) { | |
view.setBackgroundColor(Color.RED); | |
uic.loopMainThreadForAtLeast(250); | |
view.setBackgroundColor(Color.GREEN); | |
uic.loopMainThreadForAtLeast(250); | |
} | |
view.setBackgroundColor(Color.TRANSPARENT); | |
if (bgDrawable != null) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | |
view.setBackground(bgDrawable); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment