title: Testing custom views with Robolectric: Width and height are always 0
body: <p>I have build a custom View (DrawView) for a drawing app. It depends heavily on a real width and height for internal bitmaps. When I try to test it with Robolectric 2.2, the view has a length and width of 0 which crashes my internal logic. How can I mock a real screen size? I use a simple LinearLayout with the DrawView as the only element in it to test. Here is my layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/drawViewLayout"&gt;
      &lt;net.thoster.scribmasterlib.DrawView
          android:id="@+id/drawView"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And this is my Unit Test:</p>
  
  <pre><code>@RunWith(RobolectricTestRunner.class)
  public class DrawViewTest {
    Activity activity;
    DrawView drawView;
    LinearLayout layout;
  
    @Before
    public void setUp() throws Exception {
  
      ActivityController&lt;Activity&gt; activityController = Robolectric.buildActivity(Activity.class).create().start().resume()
          .visible();
  
      Activity activity = activityController.get();
  
      layout = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.test, null);
      drawView = (DrawView) layout.findViewById(R.id.drawView);
  
    }
  
    @Test
    public void testFloodFill() throws Exception {
      System.out.println(drawView.getWidth());
      drawView.floodFill(new Point(1, 1), Color.BLACK, FloodFillMode.PIXEL);
      Bitmap b = drawView.getDrawingAsNewBitmap();
      int pixel = b.getPixel(2, 2);
      assertEquals(pixel, Color.BLACK);
  
    }
  
  }
  </code></pre>