Added ringsAt test that queues recurring days

This commit is contained in:
Phillip Hsu 2016-06-12 03:36:55 -07:00
parent b6260c3fb7
commit d0207ed7b9
2 changed files with 67 additions and 6 deletions

View File

@ -92,7 +92,6 @@ public abstract class Alarm implements JsonSerializable {
.label(jsonObject.getString(KEY_LABEL))
.ringtone(jsonObject.getString(KEY_RINGTONE))
.vibrates(jsonObject.getBoolean(KEY_VIBRATES))
.recurrenceIds(recurrenceIds)
.rebuild();
alarm.setEnabled(jsonObject.getBoolean(KEY_ENABLED));
alarm.snoozingUntilMillis = jsonObject.getLong(KEY_SNOOZING_UNTIL_MILLIS);
@ -113,8 +112,7 @@ public abstract class Alarm implements JsonSerializable {
.recurringDays(new boolean[NUM_DAYS])
.label("")
.ringtone("")
.vibrates(false)
.recurrenceIds(new long[NUM_DAYS]);
.vibrates(false);
}
public void snooze(int minutes) {
@ -255,8 +253,7 @@ public abstract class Alarm implements JsonSerializable {
.put(KEY_RECURRING_DAYS, new JSONArray(recurringDays()))
.put(KEY_LABEL, label())
.put(KEY_RINGTONE, ringtone())
.put(KEY_VIBRATES, vibrates())
.put(KEY_RECURRENCE_IDS, new JSONArray(recurrenceIds));
.put(KEY_VIBRATES, vibrates());
} catch (JSONException e) {
throw new RuntimeException(e);
}
@ -290,7 +287,6 @@ public abstract class Alarm implements JsonSerializable {
public abstract Builder label(String label);
public abstract Builder ringtone(String ringtone);
public abstract Builder vibrates(boolean vibrates);
public abstract Builder recurrenceIds(long[] recurrenceIds);
// To enforce preconditions, split the build method into two. autoBuild() is hidden from
// callers and is generated. You implement the public build(), which calls the generated
// autoBuild() and performs your desired validations.

View File

@ -160,6 +160,71 @@ public class AlarmTest {
}
}
/*
* Set recurring days in a queue and test that, regardless, ringsAt() ALWAYS returns the ring time
* that is closest to the current day. In other words, the ring time that comes first in the queue.
*/
@Test
public void alarm_RingsAt_QueueingRecurringDays_ReturnsClosestRingTime() {
Calendar cal = new GregorianCalendar();
int D_C = cal.get(Calendar.DAY_OF_WEEK);
for (int h = 0; h < 24; h++) {
for (int m = 0; m < 60; m++) {
Alarm a = Alarm.builder().hour(h).minutes(m).build();
for (int D = D_C; D <= Calendar.SATURDAY; D++) {
out.println("Testing (h, m, d) = ("+h+", "+m+", "+ (D-1) +")");
int hC = cal.get(HOUR_OF_DAY); // Current hour
int mC = cal.get(MINUTE); // Current minute
a.setRecurring(D - 1, true);
int days = 0;
int hours = 0;
int minutes = 0;
if (D == D_C) {
if (h > hC || (h == hC && m > mC)) {
days = 0;
} else if (h <= hC) {
days = 6;
}
}
if (h <= hC) {
if (m <= mC) {
hours = 23 - hC + h;
minutes = 60 - mC + m;
} else {
minutes = m - mC;
if (h < hC) {
hours = 24 - hC + h;
}
}
} else {
if (m <= mC) {
hours = h - hC - 1;
minutes = 60 - mC + m;
} else {
hours = h - hC;
minutes = m - mC;
}
}
cal.add(HOUR_OF_DAY, 24 * days);
cal.add(HOUR_OF_DAY, hours);
cal.add(MINUTE, minutes);
cal.set(SECOND, 0);
cal.set(MILLISECOND, 0);
long time = cal.getTimeInMillis();
assertEquals(time, a.ringsAt());
// RESET AT END!!!!
cal.setTimeInMillis(System.currentTimeMillis());
}
}
}
}
@Test
public void alarm_RingsAt_AllRecurringDays_ReturnsCorrectRingTime() {
// The results of this test should be the same as the normal ringsAt test: