1

Given the following sample onCreate()

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null && (null != extras.getString("myVar")))
    {
        setContentView(R.layout.success);
    }
        else
        {
                setContentView(R.layout.failure);
        }

}

What I want to do, is to unit test this activity to make sure I can get either content view set depending on the extras that are passed in when the activity is created.

I have the following so far in my Robolectric test :

@Test
public void testNullExtrasSetsFailureView()
{
    myActivity.onCreate(null);
        // This is the bit I'm struggling with, how to obtain content view?
    assertThat("the view the onCreate loads..", equalTo(R.layout.failure))
}

How can I achieve this? I've had a look through the API and I couldn't see any obvious way of locating the content view, other than findViewById() but I want the layout not an individual view item.

Thanks

Jimmy
  • 16,123
  • 39
  • 133
  • 213

1 Answers1

3

Try the method getContentView() on ShadowActivity:

import com.xtremelabs.robolectric.Robolectric._

assertEquals(R.id.failure, shadowOf(myActivity).getContentView().getId())

Make sure that your layout has an id:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/failure"

Hope that helps.

Stefan De Boey
  • 2,344
  • 16
  • 14