Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.content.Context;
  3. import android.os.PowerManager;
  4. import android.os.PowerManager.WakeLock;
  5. import android.test.ActivityInstrumentationTestCase2;
  6.  
  7. /**
  8. * This requires to be declared in the effective manifest:
  9. * <uses-permission android:name="android.permission.WAKE_LOCK"/>
  10. *
  11. * If you are using Gradle and don't want this permission it in your main application
  12. * you can decleare this in a debug build config manifest (ex: app/src/debug/AndroidManifest.xml).
  13. */
  14. public abstract class EspressoTestCase<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
  15. private final long DEFAULT_WAKE_LOCK_DURATION = 30 * 1000; // 30 seconds
  16.  
  17. private WakeLock wakeLock;
  18.  
  19. protected EspressoTestCase(Class<T> activityClass) {
  20. super(activityClass);
  21. }
  22.  
  23. /**
  24. * By default the test case will keep the device awake for at least 30 seconds.
  25. * If you need a different period of time you can override this method.
  26. */
  27. protected long keepDeviceAwakeForAtLeastMilliseconds() {
  28. return DEFAULT_WAKE_LOCK_DURATION;
  29. }
  30.  
  31. /**
  32. * This **must** be called by implementations if they override setUp().
  33. */
  34. @Override
  35. public void setUp() throws Exception {
  36. super.setUp();
  37.  
  38. // Espresso will not launch our activity for us, we must launch it via getActivity().
  39. getActivity();
  40.  
  41. // Addresses an issue where if the screen is off or locked Espresso will fail with this error:
  42. // com.google.android.apps.common.testing.ui.espresso.NoActivityResumedException: No activities in stage RESUMED. Did you forget to launch the activity. (test.getActivity() or similar)?
  43. // The API docs recommend using LayoutParams.FLAG_TURN_SCREEN_ON, etc, but by the time we
  44. // have a chance to set them Activity.onCreate() has already been called and the flags will
  45. // not have an effect on the activity (they must be set prior to calling
  46. PowerManager pm = (PowerManager) getInstrumentation().getTargetContext().getSystemService(Context.POWER_SERVICE);
  47. wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, EspressoTestCase.class.getSimpleName());
  48. wakeLock.acquire(keepDeviceAwakeForAtLeastMilliseconds());
  49. }
  50.  
  51. /**
  52. * This **must** be called by implementations if they override tearDown().
  53. */
  54. @Override
  55. protected void tearDown() throws Exception {
  56. if (wakeLock != null && wakeLock.isHeld()) {
  57. wakeLock.release();
  58. }
  59.  
  60. super.tearDown();
  61. }
  62.  
  63. protected void runOnUiThread(final UIThreadRunner<T> runner) {
  64. getActivity().runOnUiThread(new Runnable() {
  65. @Override
  66. public void run() {
  67. runner.onUiThread(getActivity());
  68. }
  69. });
  70. }
  71.  
  72. public interface UIThreadRunner<T> {
  73. public void onUiThread(T activity);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement