如何创建 Android JUnit 测试用例来测试 Activity 中生成的 Intent 的内容?
我有一个包含 EditText 窗口的 Activity,当用户完成输入所需数据时,Activity 会向 IntentService 启动一个 Intent,该 IntentService 记录数据并继续应用程序进程。这是我要测试的类, OnEditorActionListener/PasscodeEditorListener 被创建为一个单独的类:
public class PasscodeActivity extends BaseActivity {
EditText m_textEntry = null;
PasscodeEditorListener m_passcodeEditorListener = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.passcode_activity);
m_passcodeEditorListener = new PasscodeEditorListener();
m_textEntry = (EditText) findViewById(R.id.passcode_activity_edit_text);
m_textEntry.setTag(this);
m_textEntry.setOnEditorActionListener(m_passcodeEditorListener);
}
@Override
protected void onPause() {
super.onPause();
/*
* If we're covered for any reason during the passcode entry,
* exit the activity AND the application...
*/
Intent finishApp = new Intent(this, CoreService.class);
finishApp.setAction(AppConstants.INTENT_ACTION_ACTIVITY_REQUESTS_SERVICE_STOP);
startService(finishApp);
finish();
}
}
class PasscodeEditorListener implements OnEditorActionListener{
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
PasscodeActivity activity = (PasscodeActivity) v.getTag();
boolean imeSaysGo = ((actionId & EditorInfo.IME_ACTION_DONE)!=0)?true:false;
boolean keycodeSaysGo = ((null != event) &&
(KeyEvent.ACTION_DOWN == event.getAction()) &&
(event.getKeyCode() == KeyEvent.KEYCODE_ENTER))?true:false;
if (imeSaysGo || keycodeSaysGo){
CharSequence seq = v.getText();
Intent guidEntry = new Intent(activity, CoreService.class);
guidEntry.setAction(AppConstants.INTENT_ACTION_PASSCODE_INPUT);
guidEntry.putExtra(AppConstants.EXTRA_KEY_GUID, seq.toString());
activity.startService(guidEntry);
return true;
}
return false;
}
}
如何拦截活动生成的两个可能的出站 Intent 并验证其内容?
谢谢