Skip to content

Instantly share code, notes, and snippets.

@xrigau
Created November 24, 2014 17:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xrigau/ea8d306e0a751fafb1e6 to your computer and use it in GitHub Desktop.
Save xrigau/ea8d306e0a751fafb1e6 to your computer and use it in GitHub Desktop.
Disable Animations for Espresso tests v2.0
// Your build.gradle ...
apply from: "grant-animation-permission.gradle"
task grantAnimationPermission(type: Exec, dependsOn: 'installDevDebug') { // or install{productFlavour}{buildType}
commandLine "adb shell pm grant $android.defaultConfig.applicationId android.permission.SET_ANIMATION_SCALE".split(' ')
}
tasks.whenTaskAdded { task ->
if (task.name.startsWith('connectedAndroidTest')) {
task.dependsOn grantAnimationPermission
}
}
package com.novoda.espresso;
import android.os.SystemClock;
import android.test.ActivityInstrumentationTestCase2;
import com.mypackage.EntryPointActivity;
public class EspressoInstrumentationTestCase extends ActivityInstrumentationTestCase2<EntryPointActivity> {
private SystemAnimations systemAnimations;
public EspressoInstrumentationTestCase() {
super(EntryPointActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
systemAnimations = new SystemAnimations(getInstrumentation().getContext());
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
systemAnimations.enableAll();
}
}
package com.novoda.espresso;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.util.Log;
import java.lang.reflect.Method;
class SystemAnimations {
private static final String ANIMATION_PERMISSION = "android.permission.SET_ANIMATION_SCALE";
private static final float DISABLED = 0.0f;
private static final float DEFAULT = 1.0f;
private final Context context;
SystemAnimations(Context context) {
this.context = context;
}
void disableAll() {
int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(DISABLED);
}
}
void enableAll() {
int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(DEFAULT);
}
}
private void setSystemAnimationsScale(float animationScale) {
try {
Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");
IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
for (int i = 0; i < currentScales.length; i++) {
currentScales[i] = animationScale;
}
setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
} catch (Exception e) {
Log.e("SystemAnimations", "Could not change animation scale to " + animationScale + " :'(");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment