Modified unit test code for Alarm
This commit is contained in:
parent
2a5b6db25c
commit
7d4b0c0dff
@ -1,7 +1,5 @@
|
|||||||
package com.philliphsu.clock;
|
package com.philliphsu.clock;
|
||||||
|
|
||||||
import android.support.annotation.NonNull;
|
|
||||||
|
|
||||||
import com.google.auto.value.AutoValue;
|
import com.google.auto.value.AutoValue;
|
||||||
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@ -153,8 +151,8 @@ public abstract class Alarm {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
public abstract Builder recurringDays(boolean[] recurringDays);
|
public abstract Builder recurringDays(boolean[] recurringDays);
|
||||||
public abstract Builder label(@NonNull String label);
|
public abstract Builder label(String label);
|
||||||
public abstract Builder ringtone(@NonNull String ringtone);
|
public abstract Builder ringtone(String ringtone);
|
||||||
public abstract Builder vibrates(boolean vibrates);
|
public abstract Builder vibrates(boolean vibrates);
|
||||||
// To enforce preconditions, split the build method into two. autoBuild() is hidden from
|
// 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
|
// callers and is generated. You implement the public build(), which calls the generated
|
||||||
@ -178,8 +176,4 @@ public abstract class Alarm {
|
|||||||
if (day < SUNDAY || day > SATURDAY)
|
if (day < SUNDAY || day > SATURDAY)
|
||||||
throw new IllegalArgumentException("Invalid day " + day);
|
throw new IllegalArgumentException("Invalid day " + day);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,14 +5,11 @@ import org.junit.Test;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
import static java.util.Calendar.DAY_OF_MONTH;
|
import static java.lang.System.out;
|
||||||
import static java.util.Calendar.DAY_OF_WEEK;
|
|
||||||
import static java.util.Calendar.HOUR_OF_DAY;
|
import static java.util.Calendar.HOUR_OF_DAY;
|
||||||
import static java.util.Calendar.MILLISECOND;
|
import static java.util.Calendar.MILLISECOND;
|
||||||
import static java.util.Calendar.MINUTE;
|
import static java.util.Calendar.MINUTE;
|
||||||
import static java.util.Calendar.MONTH;
|
|
||||||
import static java.util.Calendar.SECOND;
|
import static java.util.Calendar.SECOND;
|
||||||
import static java.util.Calendar.YEAR;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
@ -23,7 +20,7 @@ import static org.junit.Assert.assertTrue;
|
|||||||
public class AlarmTest {
|
public class AlarmTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRecurrence() {
|
public void setRecurringDays_VerifyElementsSetCorrectly() {
|
||||||
Alarm alarm = Alarm.builder().build();
|
Alarm alarm = Alarm.builder().build();
|
||||||
|
|
||||||
// Some true, some false
|
// Some true, some false
|
||||||
@ -39,6 +36,13 @@ public class AlarmTest {
|
|||||||
assertFalse(alarm.isRecurring(i));
|
assertFalse(alarm.isRecurring(i));
|
||||||
}
|
}
|
||||||
assertFalse(alarm.hasRecurrence());
|
assertFalse(alarm.hasRecurrence());
|
||||||
|
|
||||||
|
try {
|
||||||
|
alarm.setRecurring(7, true);
|
||||||
|
alarm.setRecurring(-3, false);
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
out.println("Caught setting recurrence for invalid days");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -46,7 +50,7 @@ public class AlarmTest {
|
|||||||
GregorianCalendar now = new GregorianCalendar();
|
GregorianCalendar now = new GregorianCalendar();
|
||||||
for (int h = 0; h < 24; h++) {
|
for (int h = 0; h < 24; h++) {
|
||||||
for (int m = 0; m < 60; m++) {
|
for (int m = 0; m < 60; m++) {
|
||||||
System.out.println(String.format("Testing %02d:%02d", h, m));
|
out.println(String.format("Testing %02d:%02d", h, m));
|
||||||
int hC = now.get(HOUR_OF_DAY); // Current hour
|
int hC = now.get(HOUR_OF_DAY); // Current hour
|
||||||
int mC = now.get(MINUTE); // Current minute
|
int mC = now.get(MINUTE); // Current minute
|
||||||
Alarm a = Alarm.builder().hour(h).minutes(m).build();
|
Alarm a = Alarm.builder().hour(h).minutes(m).build();
|
||||||
@ -79,36 +83,27 @@ public class AlarmTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void snoozeAlarm_AssertEquals_SnoozingUntilMillis_CorrespondsToWallClock() {
|
public void snoozeAlarm_AssertEquals_SnoozingUntilMillis_CorrespondsToWallClock() {
|
||||||
// Expected
|
|
||||||
Calendar cal = new GregorianCalendar();
|
Calendar cal = new GregorianCalendar();
|
||||||
cal.add(MINUTE, 10);
|
cal.add(MINUTE, 10);
|
||||||
// Actual
|
|
||||||
Calendar snoozeCal = new GregorianCalendar();
|
|
||||||
Alarm alarm = Alarm.builder().build();
|
Alarm alarm = Alarm.builder().build();
|
||||||
alarm.snooze(10);
|
alarm.snooze(10);
|
||||||
snoozeCal.setTimeInMillis(alarm.snoozingUntil());
|
// Unlike ring times, the snoozingUntilMillis has seconds and millis components.
|
||||||
|
// Due to the overhead of computation, the two time values will inherently have some
|
||||||
assertEquals(cal.get(YEAR), snoozeCal.get(YEAR));
|
// millis difference. However, if the difference is meaningfully small enough, then
|
||||||
assertEquals(cal.get(MONTH), snoozeCal.get(MONTH));
|
// for all practical purposes, we can consider them equal.
|
||||||
assertEquals(cal.get(DAY_OF_MONTH), snoozeCal.get(DAY_OF_MONTH));
|
assertTrue(Math.abs(alarm.snoozingUntil() - cal.getTimeInMillis()) <= 10);
|
||||||
assertEquals(cal.get(DAY_OF_WEEK), snoozeCal.get(DAY_OF_WEEK));
|
|
||||||
assertEquals(cal.get(HOUR_OF_DAY), snoozeCal.get(HOUR_OF_DAY));
|
|
||||||
assertEquals(cal.get(MINUTE), snoozeCal.get(MINUTE));
|
|
||||||
assertEquals(cal.get(SECOND), snoozeCal.get(SECOND));
|
|
||||||
// Milliseconds not required to be equal, because they will always
|
|
||||||
// have some difference
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void snoozeAlarm_IsSnoozed_ReturnsTrue_ForAllMillisUpToButExcluding_SnoozingUntilMillis() {
|
public void snoozeAlarm_IsSnoozed_ReturnsTrue_ForAllMillisUpToButExcluding_SnoozingUntilMillis() {
|
||||||
Alarm alarm = Alarm.builder().build();
|
Alarm alarm = Alarm.builder().build();
|
||||||
alarm.snooze(1);
|
alarm.snooze(1);
|
||||||
// If all iterations leading up to 20ms before the target time evaluate to true,
|
// Stop 10ms early so System.currentTimeMillis() doesn't exceed the target time in the middle
|
||||||
// that is good enough and we don't really care about the last 20ms.
|
// of an iteration.
|
||||||
while (alarm.snoozingUntil() - System.currentTimeMillis() > 20) {
|
while (System.currentTimeMillis() < alarm.snoozingUntil() - 10) {
|
||||||
assertTrue(alarm.isSnoozed());
|
assertTrue(alarm.isSnoozed());
|
||||||
}
|
}
|
||||||
// Wait long enough so the target time passes.
|
// Wait just in case so the target time passes.
|
||||||
try {
|
try {
|
||||||
Thread.sleep(20);
|
Thread.sleep(20);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user