Commit cc731016 authored by Robert Hitt's avatar Robert Hitt
Browse files

Added unit tests for NotificationPresenter

parent 07f860ab
Pipeline #785 failed with stages
in 4 minutes and 53 seconds
......@@ -21,4 +21,31 @@ public class NotificationSettings {
public boolean interval_15;
public boolean interval_30;
public boolean interval_hour;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NotificationSettings)) return false;
NotificationSettings that = (NotificationSettings) o;
if (opening != that.opening) return false;
if (closing != that.closing) return false;
if (interval_on != that.interval_on) return false;
if (interval_15 != that.interval_15) return false;
if (interval_30 != that.interval_30) return false;
return interval_hour == that.interval_hour;
}
@Override
public int hashCode() {
int result = (opening ? 1 : 0);
result = 31 * result + (closing ? 1 : 0);
result = 31 * result + (interval_on ? 1 : 0);
result = 31 * result + (interval_15 ? 1 : 0);
result = 31 * result + (interval_30 ? 1 : 0);
result = 31 * result + (interval_hour ? 1 : 0);
return result;
}
}
......@@ -29,7 +29,6 @@ import srct.whatsopen.views.NotificationView;
public class NotificationPresenter {
private NotificationView mNotificationView;
private Set<String> mNotificationSettings;
private SharedPreferences pref;
public void attachView(NotificationView view) {
......@@ -45,16 +44,17 @@ public class NotificationPresenter {
// Gets the notification settings from SharedPreferences, parses them to booleans,
// and passes them to the View
public void presentNotifications(String name) {
NotificationSettings n = getNotificationsFromPreferences(name);
Set<String> notificationSettingsSet = pref.getStringSet(name+"NotificationSettings", null);
NotificationSettings n = getNotificationSettingsFromSet(notificationSettingsSet);
mNotificationView.setNotificationChecks(n);
}
private NotificationSettings getNotificationsFromPreferences(String name) {
mNotificationSettings = pref.getStringSet(name+"NotificationSettings", null);
public NotificationSettings getNotificationSettingsFromSet(Set<String> set) {
NotificationSettings n = new NotificationSettings();
if(mNotificationSettings != null) {
for (String s : mNotificationSettings) {
if(set != null) {
for (String s : set) {
switch (s) {
case "opening": n.opening = true; break;
case "closing": n.closing = true; break;
......@@ -69,6 +69,20 @@ public class NotificationPresenter {
return n;
}
// Returns a String set parsed from the given NotificationSettings
public Set<String> getSetFromNotificationSettings(NotificationSettings n) {
Set<String> set = new HashSet<>(6);
if(n.opening) set.add("opening");
if(n.closing) set.add("closing");
if(n.interval_on) set.add("interval_on");
if(n.interval_15) set.add("interval_15");
if(n.interval_30) set.add("interval_30");
if(n.interval_hour) set.add("interval_hour");
return set;
}
// Saves the notification settings to SharedPreferences and schedules the Notifications
public void saveNotifications(String name, boolean inEditMode, NotificationSettings n) {
......@@ -95,7 +109,8 @@ public class NotificationPresenter {
// Removes the Notification settings from SharedPreferences
public void removeNotifications(String name, boolean dismiss) {
// Remove the set Notifications
NotificationSettings n = getNotificationsFromPreferences(name);
Set<String> notificationSettingsSet = pref.getStringSet(name+"NotificationSettings", null);
NotificationSettings n = getNotificationSettingsFromSet(notificationSettingsSet);
deleteNotificationsForFacility(name, n);
// Remove the NotificationSettings
......@@ -118,7 +133,7 @@ public class NotificationPresenter {
if((n.opening || n.closing) &&
(n.interval_on || n.interval_15 || n.interval_30 || n.interval_hour)) {
Set<String> set = setFromNotificationSettings(n);
Set<String> set = getSetFromNotificationSettings(n);
SharedPreferences.Editor editor = pref.edit();
editor.putStringSet(name + "NotificationSettings", set);
......@@ -138,20 +153,6 @@ public class NotificationPresenter {
}
}
// Returns a String set parsed from the given booleans
private Set<String> setFromNotificationSettings(NotificationSettings n) {
Set<String> set = new HashSet<>(6);
if(n.opening) set.add("opening");
if(n.closing) set.add("closing");
if(n.interval_on) set.add("interval_on");
if(n.interval_15) set.add("interval_15");
if(n.interval_30) set.add("interval_30");
if(n.interval_hour) set.add("interval_hour");
return set;
}
// Sets Alarms for the Facility with the given name
private void createAlarmsForFacility(String name, NotificationSettings notificationSettings) {
Realm realm = Realm.getDefaultInstance();
......@@ -210,7 +211,7 @@ public class NotificationPresenter {
private void setAlarm(String name, int day, String type, int intervalMin, String time,
String message) {
Long alarmTime = parseTimeString(time, day, timeIsPassed(time, day));
Long alarmTime = parseTimeStringToMs(time, day, Calendar.getInstance());
int interval = intervalMin * 60000; // parse minutes to ms
alarmTime = alarmTime - interval;
......@@ -273,8 +274,14 @@ public class NotificationPresenter {
alarm.cancel(pendingIntent);
}
private Long parseTimeString(String timeString, int day, boolean timeIsPassed) {
Calendar alarmCalendar = Calendar.getInstance();
// Returns the time in ms from epoch for the given time on the next Day
// of the week relative to the given Calendar
public Long parseTimeStringToMs(String timeString, int day, Calendar alarmCalendar) {
// Determine if the time is in the past
boolean hasPassed = timeHasPassed(timeString, day, alarmCalendar);
// Save the current date
int month = alarmCalendar.get(Calendar.MONTH);
int dayOfMonth = alarmCalendar.get(Calendar.DAY_OF_MONTH);
int year = alarmCalendar.get(Calendar.YEAR);
......@@ -290,7 +297,7 @@ public class NotificationPresenter {
alarmCalendar.set(year, month, dayOfMonth);
// to make sure that the alarm isn't set in the past
if(timeIsPassed)
if(hasPassed)
alarmCalendar.add(Calendar.DATE, 1);
// set the day to the next day matching the given day of the week
......@@ -331,9 +338,7 @@ public class NotificationPresenter {
}
// Determines if the given time is earlier in the current day
private boolean timeIsPassed(String time, int day) {
Calendar now = Calendar.getInstance();
public boolean timeHasPassed(String time, int day, Calendar now) {
if(now.get(Calendar.DAY_OF_WEEK) != day)
return false;
......
package srct.whatsopen;
import org.junit.Before;
import org.junit.Test;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Set;
import srct.whatsopen.model.NotificationSettings;
import srct.whatsopen.presenters.NotificationPresenter;
import static org.junit.Assert.*;
public class NotificationPresenterUnitTest {
Calendar now;
NotificationPresenter mPresenter;
@Before
public void setUp() {
mPresenter = new NotificationPresenter();
now = Calendar.getInstance();
now.set(2017, 0, 9, 13, 0); // Monday, 1/9/2017, 13:00:00
}
@Test
public void testTimeHasPassed() {
boolean hasPassed = mPresenter.timeHasPassed("11:00:00", 2, now);
assertTrue(hasPassed);
}
@Test
public void testTimeHasNotPassed() {
boolean hasPassed = mPresenter.timeHasPassed("14:00:00", 2, now);
assertFalse(hasPassed);
}
@Test
public void testParseTimeStringToMs() {
Long timeInMs = mPresenter.parseTimeStringToMs("14:00:00", 3, now);
assertEquals(1484074800000L, timeInMs.longValue());
}
@Test
public void testSetFromNotificationSettings() {
NotificationSettings n = new NotificationSettings(true, true, true, true, true, true);
// Set up expected set
Set<String> expected = new HashSet<>(6);
expected.add("opening");
expected.add("closing");
expected.add("interval_on");
expected.add("interval_15");
expected.add("interval_30");
expected.add("interval_hour");
Set<String> actual = mPresenter.getSetFromNotificationSettings(n);
assertEquals(expected, actual);
}
@Test
public void testNotificationSettingsFromSet() {
NotificationSettings expected = new NotificationSettings(true, true,
true, true, true, true);
// Set up set
Set<String> set = new HashSet<>(6);
set.add("opening");
set.add("closing");
set.add("interval_on");
set.add("interval_15");
set.add("interval_30");
set.add("interval_hour");
NotificationSettings actual = mPresenter.getNotificationSettingsFromSet(set);
assertEquals(expected, actual);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment