这是来自我的项目的BroadcastReceiver,我希望对它进行单元测试。当用户拨打电话时,它获取电话号码,并设置一个意图启动一个新活动,将电话号码传递进去。
public class OutgoingCallReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context xiContext, Intent xiIntent)
{
if (xiIntent.getAction().equalsIgnoreCase(Intent.ACTION_NEW_OUTGOING_CALL))
{
String phoneNum = xiIntent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Intent intent = new Intent(xiContext, MyActivity.class);
intent.putExtra("phoneNum", phoneNum);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
xiContext.startActivity(intent);
setResultData(null);
}
}
}
到目前为止,我的单元测试看起来像这样:
public class OutgoingCallReceiverTest extends AndroidTestCase
{
private OutgoingCallReceiver mReceiver;
@Override
protected void setUp() throws Exception
{
super.setUp();
mReceiver = new OutgoingCallReceiver();
}
public void testStartActivity()
{
Intent intent = new Intent(Intent.ACTION_NEW_OUTGOING_CALL);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, "01234567890");
mReceiver.onReceive(getContext(), intent);
}
}
这段代码可以运行,但我希望我的测试能够检查是否已发送意图,并检查其上的电话号码。我该怎么做?
我还可以测试电话呼叫是否被取消(因为有 setResultData(null) 这行代码)吗?