From d0207ed7b92969d13f1b0b07cb7f4a359146c86b Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Sun, 12 Jun 2016 03:36:55 -0700 Subject: [PATCH] Added ringsAt test that queues recurring days --- .../java/com/philliphsu/clock2/Alarm.java | 8 +-- .../java/com/philliphsu/clock2/AlarmTest.java | 65 +++++++++++++++++++ 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/Alarm.java b/app/src/main/java/com/philliphsu/clock2/Alarm.java index f711286..be19f32 100644 --- a/app/src/main/java/com/philliphsu/clock2/Alarm.java +++ b/app/src/main/java/com/philliphsu/clock2/Alarm.java @@ -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. diff --git a/app/src/test/java/com/philliphsu/clock2/AlarmTest.java b/app/src/test/java/com/philliphsu/clock2/AlarmTest.java index ba2d699..ed32e1c 100644 --- a/app/src/test/java/com/philliphsu/clock2/AlarmTest.java +++ b/app/src/test/java/com/philliphsu/clock2/AlarmTest.java @@ -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: