Created
May 12, 2016 18:10
-
-
Save sveinungkb/7676f6099d591aa319ab120388171694 to your computer and use it in GitHub Desktop.
Convenient parent class for UI automation tests to ensure successful runs. Will wake screen, unlock if necessary and optionally show the home screen.
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 characters
import android.app.Activity; | |
import android.app.Application; | |
import android.app.KeyguardManager; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.PowerManager; | |
import android.support.test.uiautomator.By; | |
import android.support.test.uiautomator.BySelector; | |
import android.support.test.uiautomator.UiDevice; | |
import android.support.test.uiautomator.UiObject2; | |
import android.support.test.uiautomator.UiObjectNotFoundException; | |
import android.support.test.uiautomator.UiSelector; | |
import android.support.test.uiautomator.Until; | |
import android.test.InstrumentationTestCase; | |
public class AutomationTest extens InstrumentationTestCase { | |
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
final Context context = getInstrumentation().getTargetContext(); | |
unlockScreen(context); | |
wakeDevice(context); | |
} | |
private void wakeDevice(Context context) { | |
final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); | |
wakeLock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, getName()); | |
wakeLock.acquire(); | |
} | |
public void unlockScreen(final Context context) { | |
final KeyguardManager keyGuardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); | |
final KeyguardManager.KeyguardLock lock = keyGuardManager.newKeyguardLock("activity_classname"); | |
lock.disableKeyguard(); | |
} | |
// Call from tests where appropriate to reset and dismiss misc dialogs | |
public void showHome() { | |
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); | |
mainIntent.addCategory(Intent.CATEGORY_HOME); | |
startActivity(mainIntent); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment