Skip to content

Instantly share code, notes, and snippets.

@vincetreur
Created May 26, 2016 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vincetreur/2cf5f1ac10c1422da8c07137de564a5a to your computer and use it in GitHub Desktop.
Save vincetreur/2cf5f1ac10c1422da8c07137de564a5a to your computer and use it in GitHub Desktop.
An Android Espresso ViewAction that flashes the background color of a View.
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