Skip to content

Instantly share code, notes, and snippets.

@fejd
Last active September 21, 2016 08:28
Show Gist options
  • Save fejd/66d7f5f687020e57eb676caf5f40c39f to your computer and use it in GitHub Desktop.
Save fejd/66d7f5f687020e57eb676caf5f40c39f to your computer and use it in GitHub Desktop.
An example of using @ClassRule to set up the device to disable animations before running Espresso tests
// Example of disabling animations and restoring after a suite has finished
// SystemAnimations.java -> https://github.com/reark/reark/blob/master/app/src/androidTest/java/io/reark/rxgithubapp/activities/utils/SystemAnimations.java
import org.junit.ClassRule;
import org.junit.rules.ExternalResource;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
TestClassA.class,
TestClassB.class
};
public class EspressoSuite {
@ClassRule
public static ExternalResource prepareDevice() {
return new ExternalResource() {
float[] mAnimationScales;
@Override
protected void before() throws Throwable {
super.before();
// Set animations to off before running tests, otherwise test will fail.
mAnimationScales = SystemAnimations.disableAll();
}
@Override
protected void after() {
// Tests are done. Reset animation values.
if (mAnimationScales != null) {
SystemAnimations.enableAll(mAnimationScales);
}
super.after();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment