Edit: This is an old answer. Please use Roshak's
The bug report mentioned using reflection to change ViewDataBinding.USE_CHOREOGRAPHER
to false
for the tests, so here is the solution I came up with:
public static void setFinalStatic(Field field, Object newValue) throws Exception {
field.setAccessible(true);
Field modifiersField;
try {
modifiersField = Field.class.getDeclaredField("accessFlags");
} catch(NoSuchFieldException e) {
//This is an emulator JVM ¯\_(ツ)_/¯
modifiersField = Field.class.getDeclaredField("modifiers");
}
modifiersField.setAccessible(true);
modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
field.set(null, newValue);
}
Then, just define an ActivityTestRule
for the Activity under test, and override its beforeActivityLaunched()
. It is necessary to do this before the activity is launched (as opposed to in a @Before
annotation) because ViewDataBinding
will initialize a Looper
if it doesn't use CHOREOGRAPHER
.
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();
//Because we are using data-binding, we avoid using CHOREOGRAPHER
try {
ReflectionUtils.setFinalStatic(
ViewDataBinding.class.getDeclaredField("USE_CHOREOGRAPHER"), false);
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
This way, you can get rid of that Thread.sleep()