我有一个活动,有 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)
我是否不明白如何测试标签?