使用浓缩咖啡测试 Activity 和特定片段
Testing Activity and specific fragment with espresso
我的 Activity 正在托管两个片段。在 onCreate() 中,我确定将显示哪个片段。
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
LogUtils.d(TAG, "handleIntent action=" + intent.getAction());
if (MainIntentService.ACTION_TARGET_OPENER.equals(intent.getAction())) {
loadOpener();
} else if (MainIntentService.ACTION_TARGET_LOGIN.equals(intent.getAction())) {
loadLogin();
} else {
//noop
}
}
private void loadOpener() {
OpenerFragment openerFragment = OpenerFragment.newInstance();
loadFragment(R.id.frame_fragment_container, openerFragment, true);
}
loadFragment() 处理事务和提交片段...
这是我的测试Class:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginScreenTest {
@Rule
public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
new ActivityTestRule<>(LoginActivity.class);
@Test
public void clickAddNoteButton_opensAddNoteUi() throws Exception {
onView(withId(R.id.button_login_submit)).perform(click());
onView(withId(R.id.text_login)).check(matches(isDisplayed()));
}
}
如何在测试中判断 Class 应该显示哪个 Fragment?
实例化您的规则以不自动启动 activity:
@Rule
public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
new ActivityTestRule<>(LoginActivity.class, false, false);
然后手动启动您的 activity 并传入您感兴趣的意图:
Intent intent = new Intent();
intent.setAction(MainIntentService.ACTION_TARGET_OPENER);
mNotesActivityTestRule.launchActivity(intent);
我的 Activity 正在托管两个片段。在 onCreate() 中,我确定将显示哪个片段。
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
handleIntent(getIntent());
}
private void handleIntent(Intent intent) {
LogUtils.d(TAG, "handleIntent action=" + intent.getAction());
if (MainIntentService.ACTION_TARGET_OPENER.equals(intent.getAction())) {
loadOpener();
} else if (MainIntentService.ACTION_TARGET_LOGIN.equals(intent.getAction())) {
loadLogin();
} else {
//noop
}
}
private void loadOpener() {
OpenerFragment openerFragment = OpenerFragment.newInstance();
loadFragment(R.id.frame_fragment_container, openerFragment, true);
}
loadFragment() 处理事务和提交片段...
这是我的测试Class:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginScreenTest {
@Rule
public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
new ActivityTestRule<>(LoginActivity.class);
@Test
public void clickAddNoteButton_opensAddNoteUi() throws Exception {
onView(withId(R.id.button_login_submit)).perform(click());
onView(withId(R.id.text_login)).check(matches(isDisplayed()));
}
}
如何在测试中判断 Class 应该显示哪个 Fragment?
实例化您的规则以不自动启动 activity:
@Rule
public ActivityTestRule<LoginActivity> mNotesActivityTestRule =
new ActivityTestRule<>(LoginActivity.class, false, false);
然后手动启动您的 activity 并传入您感兴趣的意图:
Intent intent = new Intent();
intent.setAction(MainIntentService.ACTION_TARGET_OPENER);
mNotesActivityTestRule.launchActivity(intent);