Android 单元测试:检查报警是否已取消
我正在为我的应用程序编写单元测试,我想测试警报是否正确取消,但我找不到正确的解决方案。我熟悉使用PendingEvent.getBroadcast()和FLAG_NO_CREATE标志检查报警是否处于活动状态的方法 在同一测试中,我成功地重复使用了getBroadcast()函数,首先检查应用程序启动时是否设置了报警,然后检查是否设置了报警,这两次都返回了预期的布尔值Android 单元测试:检查报警是否已取消,android,unit-testing,alarmmanager,Android,Unit Testing,Alarmmanager,我正在为我的应用程序编写单元测试,我想测试警报是否正确取消,但我找不到正确的解决方案。我熟悉使用PendingEvent.getBroadcast()和FLAG_NO_CREATE标志检查报警是否处于活动状态的方法 在同一测试中,我成功地重复使用了getBroadcast()函数,首先检查应用程序启动时是否设置了报警,然后检查是否设置了报警,这两次都返回了预期的布尔值 public void testTimerButtonStartProcess(){ Intent i
public void testTimerButtonStartProcess(){
Intent intent = new Intent (appContext, OnAlarmReceiver.class);
intent.putExtra(Scheduler.PERIOD_TYPE, 1);
intent.putExtra(Scheduler.DELAY_COUNT, 0);
intent.setAction(Scheduler.CUSTOM_INTENT_ALARM_PERIOD_END);
boolean alarmUp = (PendingIntent.getBroadcast(appContext, 0, intent,PendingIntent.FLAG_NO_CREATE) != null);
assertFalse("the alarm manager wasnt running when app is lauched", alarmUp);
solo.clickOnView(timerLayout);
instr.waitForIdleSync();
solo.sleep(5000);
alarmUp = (PendingIntent.getBroadcast(appContext, 0, intent,PendingIntent.FLAG_NO_CREATE) != null);
assertTrue("the alarm is set", alarmUp);
}
但是,当我尝试以相反的顺序执行此操作时(首先检查是否设置了报警,然后再检查是否不再设置报警),我的测试失败,因为在第二次检查(当报警应取消时)之后,getBroadcast()返回true(我希望得到false)
在应用程序取消警报后,我还尝试只使用了一次getBroadcast,但仍然返回true
同时,我确信,警报的取消工作与预期一样,因为应用程序在“关闭”后停止警报
public void testTimerButtonLongPress(){
solo.clickOnView(timerLayout);
instr.waitForIdleSync();
Intent intent = new Intent (appContext, OnAlarmReceiver.class);
intent.putExtra(Scheduler.PERIOD_TYPE, 1);
intent.putExtra(Scheduler.DELAY_COUNT, 0);
intent.setAction(Scheduler.CUSTOM_INTENT_ALARM_PERIOD_END);
boolean alarmUp = (PendingIntent.getBroadcast(appContext, 0, intent,PendingIntent.FLAG_NO_CREATE) != null);
assertTrue("the alarm manager is running", alarmUp);
solo.clickLongOnView(timerLayout, 1500);
instr.waitForIdleSync();
solo.sleep(3000);
alarmUp = (PendingIntent.getBroadcast(appContext, 0, intent,PendingIntent.FLAG_NO_CREATE) != null);
assertFalse("the alarm manager was not running anymore", alarmUp);
}