I'm trying to test launching alarm with AlarmManager and accepting it with BroadcastReceiver. I've found this sources , and do everything like there. But it works so-so and fires from time to time (like 1 successful test from 20 attempts):
@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
}
Launch default alarm is wrapper around this func:
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);
}
Parts of the broadcast receiver:
@Inject RealmWrapper mRealmWrapper;
@Override public void onReceive(Context context, Intent intent) {
((Questions) context.getApplicationContext()).getQuestionNotificationReceiverComponent()
.inject(this);
onPostInjectReceive(context, intent);
}
Also here is @Before method:
@Before public void setUp() throws Exception {
mQuestionNotificationReceiver = new QuestionNotificationReceiver();
mQuestions.registerReceiver(mQuestionNotificationReceiver,
new IntentFilter(QuestionNotificationReceiver.getAction(mQuestions)));
}
Sometimes it works, it shows, that code is almost right, but it's very very flaky. If I start to use custom RetryRule
to retry test if it's failed, test becomes very long, since Thread.sleep() takes about 10 seconds. How to manage this situation?
使用Calendar.HOUR或Calendar.HOUR_OF_DAY,不能同时使用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.