3

I've got an activity, that has 2 tabs, each pointing to an activity, so I have 3 in total.

LoggedInActivity > MyProfile OR MyBadges

public class LoggedIn extends TabActivity
{

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

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MyProfile.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("artists").setIndicator("My Profile",
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent().setClass(this, MyBadges.class);
    spec = tabHost.newTabSpec("albums").setIndicator("My Badges",
            res.getDrawable(R.drawable.ic_tab_artists))
            .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

}

My question is, how can I set up some Robolectric tests to assert that when you click on the tab label, it shows the correct tab activity?

In previous activity to activity assertions, I've done something like this :

@Test
public void assertClickingMyProfileLoadsTheMyProfileActivity()
{

    loginButton.performClick();

    ShadowActivity shadowActivity = shadowOf(searchActivity);

    Intent startedIntent = shadowActivity.getNextStartedActivity();
    ShadowIntent shadowIntent = shadowOf(startedIntent);

    // Check the correct intent was initiated
    assertThat(shadowIntent.getComponent().getClassName(), equalTo(LoggedInActivity.class.getName()));

}

But I can't find any help / hints / tutorials on how to test tabs in Robolectric, even the JavaDocs seem a little sparse

Any help would be greatly appreciated

EDIT 1 : After being pointed at a more appropriate class in the API, this is what I've come up with so far :

@Test
    public void assertClickingMyProfileLoadsTheMyProfileActivity()
    {

        ShadowActivity shadowActivity = shadowOf(loggedInActivity);

        ShadowTabHost shadowTabHost = shadowOf(loggedInActivity.getTabHost());
        shadowTabHost.setCurrentTab(1);

        Intent startedIntent = shadowActivity.getNextStartedActivity();
        ShadowIntent shadowIntent = shadowOf(startedIntent);


        // Check the correct intent was initiated
        assertThat(shadowIntent.getComponent().getClassName(), equalTo(MyProfile.class.getName()));

    }

With the following failure :

java.lang.NullPointerException: can't get a shadow for null
    at com.xtremelabs.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:249)
    at com.xtremelabs.robolectric.Robolectric.shadowOf_(Robolectric.java:773)
    at com.xtremelabs.robolectric.Robolectric.shadowOf(Robolectric.java:500)
    at com.mypackage.apps.activity.LoggedInTest.assertClickingMyProfileLoadsTheMyProfileActivity(LoggedInTest.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)

Am I failing to understand how to test tabs?

Jimmy
  • 16,123
  • 39
  • 133
  • 213

0 Answers0