Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View height and width are always zero #819

Closed
christopherperry opened this issue Nov 7, 2013 · 9 comments
Closed

View height and width are always zero #819

christopherperry opened this issue Nov 7, 2013 · 9 comments

Comments

@christopherperry
Copy link

Failing test:

@Test
public void heightAndWidth_shouldNotBeZero() {
  TestActivity testActivity =
    Robolectric.buildActivity(TestActivity.class)
      .create().start().resume().visible().get();

  View contentView = testActivity.findViewById(69);
  assertThat(contentView.getWidth()).isNotZero();
  assertThat(contentView.getHeight()).isNotZero();
}

private static class TestActivity extends Activity {  
  @Override 
  protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  
      LinearLayout contentView = new LinearLayout(this);  
      contentView.setId(69);    
      contentView.setLayoutParams(new LayoutParams(666, 666));
      setContentView(contentView);  
  }  
}  
@christopherperry
Copy link
Author

Anyone have input on this? Seems like a major oversight to me.... but I guess this goes in with the same category as some of my other recent issues dealing with resource loading, and config stuff relating to screen size etc.

@erd
Copy link
Member

erd commented Nov 28, 2013

Robolectric isn't trying to emulate Android - just fake enough of it so you can write reasonable unit tests.

It doesn't surprise me that views don't have a width or height, since we're not doing anything that would cause a layout pass to happen. What are you trying to test that requires your views to have been measured?

@christopherperry
Copy link
Author

Hi @erd, I'm testing code for drag and drop, and the drag code implements bounding within the parent View hence it needs valid dimensions.

@ry4n1m3
Copy link
Member

ry4n1m3 commented Oct 28, 2014

One workaround here is that you can manually call View.measure(int, int) and View.layout(int, int, int, int) on your content in your test.

@kingargyle
Copy link
Contributor

Another option is to use Mockito to mock out the views and have the mocks return the values you would expect. Otherwise you are getting into more integration testing instead of unit testing.

@erd
Copy link
Member

erd commented Apr 15, 2015

Thank you for the suggestion, but I don't think we're going to implement this.

@erd erd closed this as completed Apr 15, 2015
@jpshelley
Copy link

@erd Do you have a good way to accomplish this? Would you recommend the mockito suggestion or the .measure and .layout approach?

@jongerrish
Copy link
Contributor

Could we make Robolectric call into doTraversals() as part of the activity
life cycle?
On Apr 29, 2015 11:14 PM, "John Shelley" notifications@github.com wrote:

@erd https://github.com/erd Do you have a good way to accomplish this?
Would you recommend the mockito suggestion or the .measure and .layout
approach?


Reply to this email directly or view it on GitHub
#819 (comment)
.

@erd
Copy link
Member

erd commented Apr 30, 2015

Would you recommend the mockito suggestion or the .measure and .layout approach?

I would go with the measure and layout approach if you really need to have your components sized / positioned correctly.

Could we make Robolectric call into doTraversals() as part of the activity life cycle?

It would make tests slower. Is it worth the trade-off?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants