-
-
Save daj/7b48f1b8a92abf960e7b to your computer and use it in GitHub Desktop.
Revisions
-
daj revised this gist
Sep 19, 2015 . No changes.There are no files selected for viewing
-
daj revised this gist
May 5, 2015 . 2 changed files with 6 additions and 11 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> <!-- Put this file in the "debug" folder so it only gets merged into debug builds --> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cookbrite"> 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,16 @@ apply plugin: 'android' android { productFlavors { dev { applicationId "com.cookbrite.dev" } production { applicationId "com.cookbrite.android" } } // ... // Get the path to ADB. Required when running tests directly from Android Studio. @@ -23,10 +23,6 @@ afterEvaluate { commandLine "$adb shell pm grant $android.productFlavors.dev.applicationId android.permission.SET_ANIMATION_SCALE".split(' ') } task grantAnimationPermissionProduction(type: Exec, dependsOn: 'installProductionDebug') { commandLine "$adb shell pm grant $android.productFlavors.production.applicationId android.permission.SET_ANIMATION_SCALE".split(' ') } @@ -36,8 +32,6 @@ afterEvaluate { tasks.each { task -> if (task.name.startsWith('assembleDevDebugAndroidTest')) { task.dependsOn grantAnimationPermissionDev } else if (task.name.startsWith('assembleProductionDebugAndroidTest')) { task.dependsOn grantAnimationPermissionProduction } -
daj revised this gist
May 5, 2015 . 6 changed files with 68 additions and 59 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ public abstract class BaseStatelessBlackBoxEspressoTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> { private SystemAnimations mSystemAnimations; public BaseStatelessBlackBoxEspressoTest(Class clazz) { super(clazz); } @Override protected void setUp() throws Exception { super.setUp(); mSystemAnimations = new SystemAnimations(getInstrumentation().getContext()); mSystemAnimations.disableAll(); getActivity(); } @Override protected void tearDown() throws Exception { super.tearDown(); // If you want to test this entire Gist is working, comment out this line and // run a test. Then check these options in Settings -> Developer: // -Window animation scale // -Transition animation scale // -Animator duration scale // They should all show "Animation off". Then re-enable this line and run a // test again, this time it will show "Animation scale 1x" mSystemAnimations.enableAll(); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +0,0 @@ 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,9 @@ /** * Disable animations so that they do not interfere with Espresso tests. * * Source: https://code.google.com/p/android-test-kit/wiki/DisablingAnimations */ public final class SystemAnimations extends AndroidJUnitRunner { 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; 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cookbrite"> <!-- Disable animations on debug builds so that the animations do not interfere with Espresso tests. Adding this permission to the manifest is not sufficient - you must also grant the permission over adb! --> <uses-permission android:name="android.permission.SET_ANIMATION_SCALE" /> </manifest> 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 charactersOriginal file line number Diff line number Diff line change @@ -13,36 +13,33 @@ android { } // ... // Get the path to ADB. Required when running tests directly from Android Studio. // Source: http://stackoverflow.com/a/26771087/112705 def adb = android.getAdbExe().toString() // Source: http://stackoverflow.com/q/29908110/112705 afterEvaluate { task grantAnimationPermissionDev(type: Exec, dependsOn: 'installDevDebug') { commandLine "$adb shell pm grant $android.productFlavors.dev.applicationId android.permission.SET_ANIMATION_SCALE".split(' ') } task grantAnimationPermissionStaging(type: Exec, dependsOn: 'installStagingDebug') { commandLine "$adb shell pm grant $android.productFlavors.staging.applicationId android.permission.SET_ANIMATION_SCALE".split(' ') } task grantAnimationPermissionProduction(type: Exec, dependsOn: 'installProductionDebug') { commandLine "$adb shell pm grant $android.productFlavors.production.applicationId android.permission.SET_ANIMATION_SCALE".split(' ') } // When launching individual tests from Android Studio, it seems that only the assemble tasks // get called directly, not the install* versions tasks.each { task -> if (task.name.startsWith('assembleDevDebugAndroidTest')) { task.dependsOn grantAnimationPermissionDev } else if (task.name.startsWith('assembleStagingDebugAndroidTest')) { task.dependsOn grantAnimationPermissionStaging } else if (task.name.startsWith('assembleProductionDebugAndroidTest')) { task.dependsOn grantAnimationPermissionProduction } } } -
xrigau revised this gist
May 7, 2014 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,6 @@ apply plugin: 'android' android { // ... productFlavors { dev { // The one for development/testing @@ -31,7 +27,7 @@ def copyAndReplaceText(source, dest, Closure replaceText) { dest.write(replaceText(source.text)) } // Override Data in Manifest - This can be done using different Manifest files for each flavor, this way there's no need to modify the manifest android.applicationVariants.all { variant -> if (variant.name.startsWith('dev')) { // Where dev is the one you'll use to run Espresso tests System.out.println("Not removing the SET_ANIMATION_SCALE permission for $variant.name") -
xrigau revised this gist
May 7, 2014 . 2 changed files with 22 additions and 23 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ public class MyInstrumentationTestCase extends ActivityInstrumentationTestCase2<MyActivity> { private SystemAnimations systemAnimations; public MyInstrumentationTestCase() { super(MyActivity.class); } @Override protected void setUp() throws Exception { super.setUp(); systemAnimations = new SystemAnimations(getInstrumentation().getContext()); systemAnimations.disableAll(); getActivity(); } @Override protected void tearDown() throws Exception { super.tearDown(); systemAnimations.enableAll(); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -1,23 +0,0 @@ -
xrigau revised this gist
Apr 25, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -42,7 +42,7 @@ private void setSystemAnimationsScale(float animationScale) { } setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales}); } catch (Exception e) { Log.e("SystemAnimations", "Could not change animation scale to " + animationScale + " :'("); } } } -
xrigau revised this gist
Apr 25, 2014 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.novoda.espresso"> <!-- For espresso testing purposes, this is removed in live builds, but not in dev builds --> <uses-permission android:name="android.permission.SET_ANIMATION_SCALE" /> <!-- ... --> </manifest> -
xrigau created this gist
Apr 25, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ public class MyInstrumentationTestRunner extends GoogleInstrumentationTestRunner { private SystemAnimations systemAnimations; @Override public void onCreate(Bundle arguments) { super.onCreate(arguments); systemAnimations = new SystemAnimations(getContext()); } @Override public void callActivityOnCreate(Activity activity, Bundle bundle) { systemAnimations.disableAll(); super.callActivityOnCreate(activity, bundle); } @Override public void callActivityOnDestroy(Activity activity) { super.callActivityOnDestroy(activity); systemAnimations.enableAll(); } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ 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("Could not change animation scale to " + animationScale + " :'("); } } } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,52 @@ apply plugin: 'android' android { // ... defaultConfig { // ... testInstrumentationRunner "com.novoda.espresso.MyInstrumentationTestRunner" } productFlavors { dev { // The one for development/testing } live { // The flavour for releasing } } } // ... task grantAnimationPermission(type: Exec, dependsOn: 'installDevDebug') { // or install{productFlavour}{buildType} commandLine "adb shell pm grant $android.defaultConfig.packageName android.permission.SET_ANIMATION_SCALE".split(' ') } tasks.whenTaskAdded { task -> if (task.name.startsWith('connectedAndroidTest')) { task.dependsOn grantAnimationPermission } } def copyAndReplaceText(source, dest, Closure replaceText) { dest.write(replaceText(source.text)) } // Override Data in Manifest android.applicationVariants.all { variant -> if (variant.name.startsWith('dev')) { // Where dev is the one you'll use to run Espresso tests System.out.println("Not removing the SET_ANIMATION_SCALE permission for $variant.name") return } System.out.println("Removing the SET_ANIMATION_SCALE permission for $variant.name") variant.processManifest.doLast { copyAndReplaceText(manifestOutputFile, manifestOutputFile) { def replaced = it.replace('<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>', ''); if (replaced.contains('SET_ANIMATION_SCALE')) { // For security, imagine an extra space is added before closing tag, then the replace would fail - TODO use regex throw new RuntimeException("Don't ship with this permission! android.permission.SET_ANIMATION_SCALE") } replaced } } }