Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric returns 0 height for a view with wrap content height

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

like image 297
Elad Benda Avatar asked May 06 '19 17:05

Elad Benda


1 Answers

Use @TextLayoutMode(REALISTIC) annotation above your test method.

like image 106
charlie Avatar answered Nov 02 '22 12:11

charlie