それぞれがアクティビティを指している 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 でさえ、少しまばらに見えます
どんな助けでも大歓迎です
EDIT 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)
タブをテストする方法を理解できていませんか?