Created
November 24, 2014 17:50
-
-
Save xrigau/ea8d306e0a751fafb1e6 to your computer and use it in GitHub Desktop.
Disable Animations for Espresso tests v2.0
This file contains 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
// Your build.gradle ... | |
apply from: "grant-animation-permission.gradle" |
This file contains 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
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 | |
} | |
} |
This file contains 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
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(); | |
} | |
} |
This file contains 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
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