使用 AlarmManager 测试 BroadcastReceiver

Testing BroadcastReceiver with AlarmManager

我正在尝试测试使用 AlarmManager 启动警报并使用 BroadcastReceiver 接受它。我找到了这个 sources,并按照那里的方式进行所有操作。但它工作得一般,并且不时触发(比如 20 次尝试中的 1 次成功测试):

@Test public void testOnReceive_defaultAlarm_accepted() throws Exception {

    final SharedPreferences sharedPrefs = mQuestions.getAppComponent().getSharedPrefs();

    final Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(System.currentTimeMillis() + TIME_DELAY_SNOOZE); // time for alarm

    sharedPrefs.edit()
        .putString(mQuestions.getString(R.string.pref_notification_time_key),
            TimePreference.timeToString(calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE)))
        .apply(); // set test prefs to alarm

    QuestionNotificationUtils.launchDefaultAlarm(mQuestions, sharedPrefs); // launch alarm

    Thread.sleep(TIME_DELAY_SNOOZE + TIME_DELAY); // wait for alarm to be fired

    assertNotNull(mQuestionNotificationReceiver.mRealmWrapper); // check if injected, and HERE IT FAILS

    verify(mQuestionNotificationReceiver.mRealmWrapper, times(1)).getQuestions(); // check if fired with Mockito
}

启动默认警报是围绕此功能的包装:

private static void launchAlarm(Context ctx, SharedPreferences prefs, int id) {
    final AlarmManager alarmMgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
    final PendingIntent alarmIntent =
        PendingIntent.getBroadcast(ctx, id, QuestionNotificationReceiver.getIntent(ctx, id),
            PendingIntent.FLAG_UPDATE_CURRENT);

    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());

    final String time = prefs.getString(ctx.getString(R.string.pref_notification_time_key),
        ctx.getString(R.string.questions_preference_fragment_default_notification_time));

    calendar.set(Calendar.HOUR_OF_DAY, TimePreference.parseHour(time));
    calendar.set(Calendar.MINUTE, TimePreference.parseMinute(time));

    alarmMgr.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
        alarmIntent);
}

部分广播接收器:

@Inject RealmWrapper mRealmWrapper;

@Override public void onReceive(Context context, Intent intent) {

    ((Questions) context.getApplicationContext()).getQuestionNotificationReceiverComponent()
        .inject(this);

    onPostInjectReceive(context, intent);
}

这里还有@Before方法:

@Before public void setUp() throws Exception {

    mQuestionNotificationReceiver = new QuestionNotificationReceiver();

    mQuestions.registerReceiver(mQuestionNotificationReceiver,
        new IntentFilter(QuestionNotificationReceiver.getAction(mQuestions)));
}

有时它会工作,它表明该代码几乎是正确的,但它非常不稳定。如果我开始使用自定义 RetryRule 来重试失败的测试,测试会变得很长,因为 Thread.sleep() 大约需要 10 秒。如何处理这种情况?

使用Calendar.HOUR或Calendar.HOUR_OF_DAY,不能同时使用