我有一个活动,有2个标签,每个标签指向一个活动,所以我总共有3个。
LoggedInActivity> MyProfile或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);
}
}
我的问题是,我如何设置一些Robolectric测试来断言当你点击标签标签时,它会显示正确的标签活动?
在以前的活动断言活动中,我做过类似的事情:
@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()));
}
但我找不到任何关于如何在Robolectric中测试标签的帮助/提示/教程,即使JavaDocs似乎有点稀疏
非常感谢任何帮助
编辑1: 在指向API中更合适的类之后,这是我到目前为止所提出的:
@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()));
}
发生以下故障:
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)
我无法理解如何测试标签吗?