mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Wake up device before running UI tests
This commit is contained in:
@@ -10,6 +10,7 @@ import android.test.suitebuilder.annotation.LargeTest;
|
|||||||
import org.isoron.uhabits.MainActivity;
|
import org.isoron.uhabits.MainActivity;
|
||||||
import org.isoron.uhabits.R;
|
import org.isoron.uhabits.R;
|
||||||
import org.isoron.uhabits.models.Habit;
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -58,6 +59,8 @@ import static org.isoron.uhabits.ui.ShowHabitActivityActions.openHistoryEditor;
|
|||||||
@LargeTest
|
@LargeTest
|
||||||
public class MainTest
|
public class MainTest
|
||||||
{
|
{
|
||||||
|
private SystemHelper sys;
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(
|
public IntentsTestRule<MainActivity> activityRule = new IntentsTestRule<>(
|
||||||
MainActivity.class);
|
MainActivity.class);
|
||||||
@@ -66,13 +69,20 @@ public class MainTest
|
|||||||
public void setup()
|
public void setup()
|
||||||
{
|
{
|
||||||
Context context = InstrumentationRegistry.getInstrumentation().getContext();
|
Context context = InstrumentationRegistry.getInstrumentation().getContext();
|
||||||
SystemHelper sys = new SystemHelper(context);
|
sys = new SystemHelper(context);
|
||||||
sys.disableAllAnimations();
|
sys.disableAllAnimations();
|
||||||
|
sys.acquireWakeLock();
|
||||||
sys.unlockScreen();
|
sys.unlockScreen();
|
||||||
|
|
||||||
skipTutorial();
|
skipTutorial();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown()
|
||||||
|
{
|
||||||
|
sys.releaseWakeLock();
|
||||||
|
}
|
||||||
|
|
||||||
public void skipTutorial()
|
public void skipTutorial()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ 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;
|
||||||
|
import android.os.PowerManager;
|
||||||
import android.support.test.runner.AndroidJUnitRunner;
|
import android.support.test.runner.AndroidJUnitRunner;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -16,24 +17,39 @@ public final class SystemHelper extends AndroidJUnitRunner
|
|||||||
private static final float DEFAULT = 1.0f;
|
private static final float DEFAULT = 1.0f;
|
||||||
|
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
private PowerManager.WakeLock wakeLock;
|
||||||
|
|
||||||
SystemHelper(Context context)
|
SystemHelper(Context context)
|
||||||
{
|
{
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void acquireWakeLock()
|
||||||
|
{
|
||||||
|
PowerManager power = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
|
wakeLock = power.newWakeLock(PowerManager.FULL_WAKE_LOCK |
|
||||||
|
PowerManager.ACQUIRE_CAUSES_WAKEUP |
|
||||||
|
PowerManager.ON_AFTER_RELEASE, getClass().getSimpleName());
|
||||||
|
wakeLock.acquire();
|
||||||
|
}
|
||||||
|
|
||||||
|
void releaseWakeLock()
|
||||||
|
{
|
||||||
|
if(wakeLock != null)
|
||||||
|
wakeLock.release();
|
||||||
|
}
|
||||||
|
|
||||||
void unlockScreen()
|
void unlockScreen()
|
||||||
{
|
{
|
||||||
Log.i("SystemHelper", "Trying to unlock screen");
|
Log.i("SystemHelper", "Trying to unlock screen");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
KeyguardManager mKeyGuardManager = (KeyguardManager) context
|
KeyguardManager mKeyGuardManager =
|
||||||
.getSystemService(Context.KEYGUARD_SERVICE);
|
(KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
|
||||||
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("lock");
|
KeyguardManager.KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("lock");
|
||||||
mLock.disableKeyguard();
|
mLock.disableKeyguard();
|
||||||
Log.e("SystemHelper", "Successfully unlocked screen");
|
Log.e("SystemHelper", "Successfully unlocked screen");
|
||||||
}
|
} catch (Exception e)
|
||||||
catch (Exception e)
|
|
||||||
{
|
{
|
||||||
Log.e("SystemHelper", "Could not unlock screen");
|
Log.e("SystemHelper", "Could not unlock screen");
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -44,10 +60,8 @@ public final class SystemHelper extends AndroidJUnitRunner
|
|||||||
{
|
{
|
||||||
Log.i("SystemHelper", "Trying to disable animations");
|
Log.i("SystemHelper", "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 Log.e("SystemHelper", "Permission denied");
|
||||||
else
|
|
||||||
Log.e("SystemHelper", "Permission denied");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,8 +99,7 @@ public final class SystemHelper extends AndroidJUnitRunner
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.e("SystemHelper",
|
Log.e("SystemHelper", "Could not change animation scale to " + animationScale + " :'(");
|
||||||
"Could not change animation scale to " + animationScale + " :'(");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,5 +23,6 @@
|
|||||||
|
|
||||||
<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"/>
|
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
|
||||||
|
<uses-permission android:name="android.permission.WAKE_LOCK"/>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
|||||||
Reference in New Issue
Block a user