Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@originx
Created November 23, 2016 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save originx/1890599b57b0ee3e14a85a4732301cd9 to your computer and use it in GitHub Desktop.
Save originx/1890599b57b0ee3e14a85a4732301cd9 to your computer and use it in GitHub Desktop.
public class CustomAndroidJUnitRunner extends AndroidJUnitRunner {
private PowerManager.WakeLock wakeLock;
@Override
public void onCreate(Bundle arguments) {
MultiDex.install(getTargetContext());
super.onCreate(arguments);
ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(new ActivityLifecycleCallback() {
@Override
public void onActivityLifecycleChanged(Activity activity, Stage stage) {
if (stage == Stage.PRE_ON_CREATE) {
activity.getWindow().addFlags(FLAG_DISMISS_KEYGUARD | FLAG_TURN_SCREEN_ON | FLAG_KEEP_SCREEN_ON);
}
}
});
//hook up schedulers to rxjava so espresso idling resouces can fetch it properly
RxJavaHooks.setOnComputationScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
RxJavaHooks.setOnIOScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
RxJavaHooks.setOnNewThreadScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
}
@Override
public void onStart() {
String name = PaybackAndroidJUnitRunner.class.getSimpleName();
Context app = getTargetContext().getApplicationContext();
// Unlock the device so that the tests can input keystrokes.
KeyguardManager keyguard = (KeyguardManager) app.getSystemService(KEYGUARD_SERVICE);
keyguard.newKeyguardLock(name).disableKeyguard();
// Wake up the screen.
PowerManager power = (PowerManager) app.getSystemService(POWER_SERVICE);
wakeLock = power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, name);
wakeLock.acquire();
super.onStart();
}
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, TestPBApplication.class.getName(), context);
}
}
<application
android:name=".CustomApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name_country"
android:supportsRtl="false"
android:theme="@style/Theme.CustomApp"
tools:replace="label">
public class CustomApplication extends CustomApplication {
/*
..other methods and classes
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
try {
MultiDex.install(this);
} catch (RuntimeException e) {
if (PAYBACKCoreSDK.runUnderTest()) {
Log.e(CustomApplication.class.getSimpleName(), "ignoring failed multidex installation in test flight", e);
return;
}
throw e;
}
}
}
public class TestCustomApplication extends CustomApplication {
@Override
public void onCreate() {
super.onCreate();
instantiateClipboardUIManagerToPreventLeaking();
}
private void instantiateClipboardUIManagerToPreventLeaking() {
try {
Class cls = Class.forName("android.sec.clipboard.ClipboardUIManager");
Method m = cls.getDeclaredMethod("getInstance", Context.class);
m.setAccessible(true);
m.invoke(null, this);
} catch (Exception e) {
//not on a samsung device. go on.
}
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
try {
MultiDex.install(this);
} catch (RuntimeException e) {
if (CoreSDK.runUnderTest()) {
Log.e(CustomApplication.class.getSimpleName(), "ignoring Failed multidex installation in test flight", e);
return;
}
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment