Robolectric reference

Robolectric Android testing Fragments and more

private ReminderManagerFragment startReminderManagerFragment() {
    final ReminderManagerFragment fragment = new ReminderManagerFragment();
    SupportFragmentTestUtil.startVisibleFragment(fragment);
    return fragment;
}

@Test
public void reminderManagerTest() throws Exception {
    // by default there should be 3 Reminder rows
    ReminderManagerFragment fragment = startReminderManagerFragment();
    assertThat(fragment).isNotNull();
    Button addButton = (Button) fragment.getView().findViewById(R.id.reminder_add_button);
    assertThat(addButton).isNotNull();

    List<Reminder> reminders = getReminders(TestValues.BillyUser);
    assertThat(reminders.size()).isEqualTo(3);

    // add a reminder, assert reminders = 4
    addButton.performClick();

    //assertThat(reminders.size()).isEqualTo(4);
    TimePickerFragment pickerFragment =
            (TimePickerFragment) fragment.getFragmentManager()
                    .findFragmentByTag(ReminderManagerFragment.TIME_DIALOG);
    assertThat(pickerFragment).isNotNull();

    reminders = getReminders(TestValues.BillyUser);
    assertThat(reminders.size()).isEqualTo(4);

    AlertDialog pickerDialog = (AlertDialog) pickerFragment.getDialog();

    assertThat(pickerDialog).isNotNull();
    assertThat(pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE)).isNotNull();

    Button pickerOKButton = (Button) pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE);
    pickerOKButton.performClick();

    // check that an alarm was set
    ShadowAlarmManager shadowAlarmManager = shadowOf(AlarmFactory.getAlarmManager(mCtx));
    // shadow AlarmManager should return 0 alarms
    List<ShadowAlarmManager.ScheduledAlarm> shadowAlarms = shadowAlarmManager.getScheduledAlarms();
    Log.i(TAG, "alarms currently set: " + shadowAlarms.size());
    assertThat(shadowAlarms.size()).isGreaterThan(0);

 

Try changing TimePicker Hour and Minutes and resubmit AlertDialog (TimePickerDialog), they key method being ((TimePickerDialog) pickerDialog).updateTime(hh,mm);

// try adding a second reminder & alarm
addButton.performClick();

pickerFragment =
        (TimePickerFragment) fragment.getFragmentManager()
                .findFragmentByTag(ReminderManagerFragment.TIME_DIALOG);

assertThat(pickerFragment).isNotNull();

reminders = getReminders(TestValues.BillyUser);
assertThat(reminders.size()).isEqualTo(5);

pickerDialog = (AlertDialog) pickerFragment.getDialog();


assertThat(pickerDialog).isNotNull();

((TimePickerDialog) pickerDialog).updateTime(18, 30);

pickerOKButton = (Button) pickerDialog.getButton(AlertDialog.BUTTON_POSITIVE);
pickerOKButton.performClick();

shadowAlarms = shadowAlarmManager.getScheduledAlarms();
for (ShadowAlarmManager.ScheduledAlarm sAlarm :
        shadowAlarms) {
    Log.i(TAG, Dates.getBasicDate(new Date(sAlarm.triggerAtTime)) + " " +
            sAlarm.operation.toString());
}
Log.i(TAG, "alarms currently set: " + shadowAlarms.size());
assertThat(shadowAlarms.size()).isGreaterThan(1);

More to come


Posted

in

,

by

Tags: