Disable animations when testing

pull/69/head
Alinson S. Xavier 10 years ago
parent 1a18bb939d
commit 9156bba267

@ -0,0 +1,20 @@
language: android
jdk: openjdk7
env:
global:
- ADB_INSTALL_TIMEOUT=8
android:
components:
- build-tools-23.0.1
- android-23
- extra
- addon
- sys-img-armeabi-v7a-android-19
before_script:
- echo no | android create avd --force -n test -t android-19 --abi armeabi-v7a -s "480x800"
- emulator -avd test -no-skin -no-audio -no-window &
- android-wait-for-emulator
- adb shell input keyevent 82 &
script:
- ./gradlew connectedAndroidTest
- cat app/build/reports/androidTests/connected/*html | awk '/<pre>/ { on=1 } /<\/pre>/ { on = 0 } { if(on) print }'

@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "21.1.2"
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "org.isoron.uhabits"
@ -43,3 +43,13 @@ dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}
task grantAnimationPermission(type: Exec, dependsOn: 'installDebug') {
commandLine "adb shell pm grant org.isoron.uhabits android.permission.SET_ANIMATION_SCALE".split(' ')
}
tasks.whenTaskAdded { task ->
if (task.name.startsWith('connected')) {
task.dependsOn grantAnimationPermission
}
}

@ -63,6 +63,18 @@ public class MainTest
MainActivity.class);
@Before
public void setup()
{
disableAnimations();
skipTutorial();
}
public void disableAnimations()
{
Context context = InstrumentationRegistry.getInstrumentation().getContext();
new SystemAnimations(context).disableAll();
}
public void skipTutorial()
{
try

@ -0,0 +1,68 @@
package org.isoron.uhabits.ui;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.IBinder;
import android.support.test.runner.AndroidJUnitRunner;
import android.util.Log;
import java.lang.reflect.Method;
/**
* 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;
private final Context context;
SystemAnimations(Context context) {
this.context = context;
}
void disableAll() {
Log.i("SystemAnimations", "Trying to disable animations");
int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus == PackageManager.PERMISSION_GRANTED) {
setSystemAnimationsScale(DISABLED);
} else {
Log.e("SystemAnimations", "Permission denied");
}
}
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});
Log.i("SystemAnimations", "All animations successfully disabled");
} catch (Exception e) {
Log.e("SystemAnimations", "Could not change animation scale to " + animationScale + " :'(");
}
}
}

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
~
~ This file is part of Loop Habit Tracker.
~
~ Loop Habit Tracker is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by the
~ Free Software Foundation, either version 3 of the License, or (at your
~ option) any later version.
~
~ Loop Habit Tracker is distributed in the hope that it will be useful, but
~ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
~ more details.
~
~ You should have received a copy of the GNU General Public License along
~ with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<manifest
package="org.isoron.uhabits"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>
</manifest>
Loading…
Cancel
Save