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