Skip to content

Commit

Permalink
Attempt to stop tests failing occasionally on travis due to screenlock
Browse files Browse the repository at this point in the history
Travis often fails tests with the following error:

> java.lang.RuntimeException: Waited for the root of the view hierarchy to have window focus and not be requesting layout for over 10 seconds. If you specified a non default root matcher, it may be picking a root that never takes focus. Otherwise, something is seriously wrong.

It seems that this is due to the keypad locking if the emulator starts slowly.
Hopefully this commit will fix the problem.
  • Loading branch information
alxndrsn committed May 2, 2016
1 parent 0330e55 commit 6df7bf6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ public class SettingsDialogActivityTest {
private HttpTestHelper http;

@Before
public void setUp() throws Exception {
public void setUp() throws Throwable {
clearAppSettings();
this.http = new HttpTestHelper();
preventScreenLock(activityTestRule);
}

@After
Expand Down
21 changes: 21 additions & 0 deletions src/androidTest/java/medic/gateway/test/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import android.app.*;
import android.content.*;
import android.support.test.rule.*;
import android.view.*;

import medic.gateway.*;

import java.util.*;
import java.util.regex.*;

import static android.content.Context.KEYGUARD_SERVICE;
import static android.support.test.InstrumentationRegistry.*;
import static org.junit.Assert.*;

Expand Down Expand Up @@ -52,4 +54,23 @@ public void run() {
}
});
}

@SuppressWarnings("deprecation")
public static void preventScreenLock(final ActivityTestRule testRule) throws Throwable {
testRule.runOnUiThread(new Runnable() {
public void run() {
Activity activity = testRule.getActivity();

// prevent lock
KeyguardManager k = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
k.newKeyguardLock(KEYGUARD_SERVICE).disableKeyguard();

// turn on screen and remove any current locks
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
};
});
}
}
7 changes: 7 additions & 0 deletions src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="medic.gateway">
<!-- extra permissions used by instrumentation tests -->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
</manifest>

0 comments on commit 6df7bf6

Please sign in to comment.