Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing custom views with Robolectric: Width and height are always 0

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:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawViewLayout">
    <net.thoster.scribmasterlib.DrawView
        android:id="@+id/drawView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
</LinearLayout>

And this is my Unit Test:

@RunWith(RobolectricTestRunner.class)
public class DrawViewTest {
  Activity activity;
  DrawView drawView;
  LinearLayout layout;

  @Before
  public void setUp() throws Exception {

    ActivityController<Activity> 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);

  }

}
like image 698
Stefan Ostermann Avatar asked Mar 05 '14 14:03

Stefan Ostermann


People also ask

How can I get height and width of a view in android programmatically?

Step 1 − Create a new project in Android Studio,go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above example we have create a dynamic textview and added textview properties like setText(),setPadding(),SetGravity().

How to write test cases in Android Studio?

You write your local unit test class as a JUnit 4 test class. To do so, create a class that contains one or more test methods, usually in module-name/src/test/ . A test method begins with the @Test annotation and contains the code to exercise and verify a single aspect of the component that you want to test.

Which directory is used to store unit tests in your Android project*?

The following is the default directory structure for your application and test code: app/src/main/java - for your source code of your main application build. app/src/test/java - for any unit test which can run on the JVM. app/src/androidTest/java - for any test which should run on an Android device.


1 Answers

As erd points out at https://github.com/robolectric/robolectric/issues/819 ...

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.

Please see the bug for potential work arounds.

like image 83
spaaarky21 Avatar answered Oct 13 '22 13:10

spaaarky21