I have an android customView which I want to test.
Few of the tests simulate a scroll of N dps when N is a subView height.
I run the tests with robolectric, but I always get in runtime that subView.getHeight() == 0
That's becuause the subView's height is defined as wrap_content
and I guess robolectric doesn't inflate all the views to get this accurate info.
For example the subView.getWidth > 0
as its defined as match parent
.
Here is my test @before
method:
@Before
public void setUp() {
myView.getHeaderView().getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
myHeight =
myView.getHeaderView().getHeight();
}
});
activityScenarioRule
.getScenario()
.onActivity(activity -> activity.setContentView(myView));
activityScenarioRule.getScenario().moveToState(State.RESUMED);
}
How can I still get the wrap content height at test runtime?
The code does call onGlobalLayout
but returns height==0
Use @TextLayoutMode(REALISTIC) annotation above your test method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With