Unlock screen before running UI tests

pull/69/head
Alinson S. Xavier 10 years ago
parent 196a79a88c
commit 4cf2b8072b

@ -64,15 +64,13 @@ public class MainTest
@Before @Before
public void setup() public void setup()
{
disableAnimations();
skipTutorial();
}
public void disableAnimations()
{ {
Context context = InstrumentationRegistry.getInstrumentation().getContext(); Context context = InstrumentationRegistry.getInstrumentation().getContext();
new SystemAnimations(context).disableAll(); SystemHelper sys = new SystemHelper(context);
sys.disableAllAnimations();
sys.unlockScreen();
skipTutorial();
} }
public void skipTutorial() public void skipTutorial()

@ -1,5 +1,6 @@
package org.isoron.uhabits.ui; package org.isoron.uhabits.ui;
import android.app.KeyguardManager;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.IBinder; import android.os.IBinder;
@ -8,12 +9,7 @@ import android.util.Log;
import java.lang.reflect.Method; import java.lang.reflect.Method;
/** public final class SystemHelper extends AndroidJUnitRunner
* 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 String ANIMATION_PERMISSION = "android.permission.SET_ANIMATION_SCALE";
private static final float DISABLED = 0.0f; private static final float DISABLED = 0.0f;
@ -21,48 +17,73 @@ public final class SystemAnimations extends AndroidJUnitRunner
private final Context context; private final Context context;
SystemAnimations(Context context) { SystemHelper(Context context)
{
this.context = context; this.context = context;
} }
void disableAll() { void unlockScreen()
{
try
{
KeyguardManager mKeyGuardManager = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("lock");
mLock.disableKeyguard();
}
catch (Exception e)
{
e.printStackTrace();
}
}
void disableAllAnimations()
{
Log.i("SystemAnimations", "Trying to disable animations"); Log.i("SystemAnimations", "Trying to disable animations");
int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION); int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus == PackageManager.PERMISSION_GRANTED) { if (permStatus == PackageManager.PERMISSION_GRANTED)
setSystemAnimationsScale(DISABLED); setSystemAnimationsScale(DISABLED);
} else { else
Log.e("SystemAnimations", "Permission denied"); Log.e("SystemAnimations", "Permission denied");
}
} }
void enableAll() { void enableAllAnimations()
{
int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION); int permStatus = context.checkCallingOrSelfPermission(ANIMATION_PERMISSION);
if (permStatus == PackageManager.PERMISSION_GRANTED) { if (permStatus == PackageManager.PERMISSION_GRANTED)
{
setSystemAnimationsScale(DEFAULT); setSystemAnimationsScale(DEFAULT);
} }
} }
private void setSystemAnimationsScale(float animationScale) { private void setSystemAnimationsScale(float animationScale)
try { {
try
{
Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub"); Class<?> windowManagerStubClazz = Class.forName("android.view.IWindowManager$Stub");
Method asInterface = windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class); Method asInterface =
windowManagerStubClazz.getDeclaredMethod("asInterface", IBinder.class);
Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager"); Class<?> serviceManagerClazz = Class.forName("android.os.ServiceManager");
Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class); Method getService = serviceManagerClazz.getDeclaredMethod("getService", String.class);
Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager"); Class<?> windowManagerClazz = Class.forName("android.view.IWindowManager");
Method setAnimationScales = windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class); Method setAnimationScales =
windowManagerClazz.getDeclaredMethod("setAnimationScales", float[].class);
Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales"); Method getAnimationScales = windowManagerClazz.getDeclaredMethod("getAnimationScales");
IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window"); IBinder windowManagerBinder = (IBinder) getService.invoke(null, "window");
Object windowManagerObj = asInterface.invoke(null, windowManagerBinder); Object windowManagerObj = asInterface.invoke(null, windowManagerBinder);
float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj); float[] currentScales = (float[]) getAnimationScales.invoke(windowManagerObj);
for (int i = 0; i < currentScales.length; i++) { for (int i = 0; i < currentScales.length; i++)
currentScales[i] = animationScale; currentScales[i] = animationScale;
}
setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales}); setAnimationScales.invoke(windowManagerObj, new Object[]{currentScales});
Log.i("SystemAnimations", "All animations successfully disabled"); Log.i("SystemAnimations", "All animations successfully disabled");
} catch (Exception e) { }
Log.e("SystemAnimations", "Could not change animation scale to " + animationScale + " :'("); catch (Exception e)
{
Log.e("SystemAnimations",
"Could not change animation scale to " + animationScale + " :'(");
} }
} }
} }

@ -22,5 +22,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"> xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/> <uses-permission android:name="android.permission.SET_ANIMATION_SCALE"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
</manifest> </manifest>

Loading…
Cancel
Save