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

Added unit tests for MainPresenter

- Also refactored MainPresenter, FacilityPresenter, and all the
model classes to make writing tests easier
parent e1439e76
......@@ -8,6 +8,18 @@ import io.realm.annotations.PrimaryKey;
public class Facility extends RealmObject {
public Facility(String name, String location, MainSchedule mainSchedule,
boolean isOpen, boolean isFavorited) {
mName = name;
mLocation = location;
mMainSchedule = mainSchedule;
this.isOpen = isOpen;
this.isFavorited = isFavorited;
}
public Facility() {
}
@PrimaryKey
@SerializedName("name")
private String mName;
......
......@@ -7,6 +7,13 @@ import io.realm.RealmObject;
public class MainSchedule extends RealmObject {
public MainSchedule(RealmList<OpenTimes> openTimesList) {
mOpenTimesList = openTimesList;
}
public MainSchedule() {
}
@SerializedName("open_times")
private RealmList<OpenTimes> mOpenTimesList;
......
......@@ -5,6 +5,17 @@ import com.google.gson.annotations.SerializedName;
import io.realm.RealmObject;
public class OpenTimes extends RealmObject {
public OpenTimes(int startDay, int endDay, String startTime, String endTime) {
this.startDay = startDay;
this.endDay = endDay;
this.startTime = startTime;
this.endTime = endTime;
}
public OpenTimes() {
}
@Override
public String toString() {
return "OpenTimes{" +
......
......@@ -10,6 +10,8 @@ import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import java.util.Calendar;
import butterknife.BindView;
import butterknife.ButterKnife;
import io.realm.Realm;
......@@ -71,7 +73,7 @@ public class DetailActivity extends AppCompatActivity implements FacilityView{
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.action_favorite:
mPresenter.toggleFavorite();
mPresenter.toggleFavorite(mFacility);
return true;
case R.id.action_settings:
return true;
......@@ -117,10 +119,11 @@ public class DetailActivity extends AppCompatActivity implements FacilityView{
String statusText = mFacility.isOpen() ? "Open" : "Closed";
openStatusTextView.setText(statusText);
openDurationTextView.setText(mPresenter.getStatusDuration());
openDurationTextView.setText(mPresenter.getStatusDuration(mFacility,
Calendar.getInstance()));
locationTextView.setText(mFacility.getLocation());
scheduleTextView.setText(Html.fromHtml(mPresenter.getSchedule()));
scheduleTextView.setText(Html.fromHtml(mPresenter.getSchedule(mFacility)));
}
}
......@@ -108,7 +108,7 @@ public class FacilityListAdapter extends
// toggles favorite status
@OnClick(R.id.favorite_button)
public void setFavorite() {
mPresenter.toggleFavorite();
mPresenter.toggleFavorite(data);
}
@Override
......
......@@ -18,22 +18,19 @@ import srct.whatsopen.ui.FacilityView;
public class FacilityPresenter {
private FacilityView mFacilityView;
private Facility mFacility;
public void attachView(FacilityView view, Facility facility) {
mFacilityView = view;
mFacility = facility;
}
public void detachView() {
mFacilityView = null;
mFacility = null;
}
// Asynchronously updates the Realm object's favorite status
// and updates the favorite status in SharedPreferences
public void toggleFavorite() {
final boolean status = !mFacility.isFavorited();
public void toggleFavorite(Facility facility) {
final boolean status = !facility.isFavorited();
mFacilityView.changeFavoriteIcon(status);
// Get Realm instance and SharedPreferences
......@@ -43,15 +40,15 @@ public class FacilityPresenter {
final SharedPreferences.Editor editor = pref.edit();
final String facilityName = mFacility.getName();
final String facilityName = facility.getName();
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm bgRealm) {
// have to requery for the object as it was created on a separate thread
Facility facility = bgRealm.where(Facility.class)
Facility f = bgRealm.where(Facility.class)
.equalTo("mName", facilityName).findFirst();
facility.setFavorited(status);
f.setFavorited(status);
editor.putBoolean(facilityName, status);
editor.apply();
}
......@@ -61,9 +58,8 @@ public class FacilityPresenter {
}
// Finds the next time the facility closes or opens and returns it
public String getStatusDuration() {
Calendar now = Calendar.getInstance();
RealmList<OpenTimes> openTimesList = mFacility.getMainSchedule().getOpenTimesList();
public String getStatusDuration(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
if(openTimesList.size() == 0)
return "No open time on schedule";
......@@ -71,7 +67,7 @@ public class FacilityPresenter {
int currentDay = (5 + now.get(Calendar.DAY_OF_WEEK)) % 7;
String durationMessage;
if(mFacility.isOpen()) {
if(facility.isOpen()) {
String closingTime = openTimesList.get(currentDay).getEndTime();
closingTime = parseTo12HourTime(closingTime);
durationMessage = "Closes at " + closingTime;
......@@ -146,8 +142,8 @@ public class FacilityPresenter {
}
// Parses the schedule into an HTML string
public String getSchedule() {
RealmList<OpenTimes> openTimesList = mFacility.getMainSchedule().getOpenTimesList();
public String getSchedule(Facility facility) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
if(openTimesList.size() == 0)
return "No schedule available";
......
......@@ -53,7 +53,7 @@ public class MainPresenter {
for(Facility facility : facilities) {
// Query SharedReferences for each Facility's favorite status. defaults to false
facility.setFavorited(pref.getBoolean(facility.getName(), false));
facility.setOpen(getOpenStatus(facility));
facility.setOpen(getOpenStatus(facility, Calendar.getInstance()));
}
realm.beginTransaction();
......@@ -72,8 +72,7 @@ public class MainPresenter {
}
// Uses the device time to determine which facilities should be open
private boolean getOpenStatus(Facility facility) {
Calendar now = Calendar.getInstance();
public boolean getOpenStatus(Facility facility, Calendar now) {
RealmList<OpenTimes> openTimesList = facility.getMainSchedule().getOpenTimesList();
// have to mess with the current day value, as Calender.DAY_OF_WEEK
......
package srct.whatsopen;
import org.junit.Test;
import srct.whatsopen.ui.presenters.FacilityPresenter;
import static org.junit.Assert.*;
public class FacilityPresenterUnitTest {
FacilityPresenter mPresenter;
}
package srct.whatsopen;
import org.junit.Before;
import org.junit.Test;
import java.util.Calendar;
import io.realm.RealmList;
import srct.whatsopen.model.Facility;
import srct.whatsopen.model.MainSchedule;
import srct.whatsopen.model.OpenTimes;
import srct.whatsopen.ui.presenters.MainPresenter;
import static org.junit.Assert.*;
public class MainPresenterUnitTest {
Calendar now;
MainPresenter mPresenter;
Facility mFacility;
@Before
public void setUp() {
mPresenter = new MainPresenter();
// Set up the Facility
OpenTimes o1 = new OpenTimes(0, 0, "11:00:00", "18:00:00"); // Mondays 11:00-18:00
OpenTimes o2 = new OpenTimes(1, 1, "13:00:00", "18:00:00"); // Tuesdays 13:00-18:00
RealmList<OpenTimes> openTimesList = new RealmList<>();
openTimesList.add(o1);
openTimesList.add(o2);
MainSchedule mainSchedule = new MainSchedule(openTimesList);
mFacility = new Facility("The French Laundry", "Johnson Center",
mainSchedule, false, true);
}
@Test
public void testFacilityIsOpen() {
// Set date
now = Calendar.getInstance();
now.set(2017, 0, 9, 12, 0); // Monday, 1/9/2017, 12:00:00
assertTrue(mPresenter.getOpenStatus(mFacility, now));
}
@Test
public void testFacilityIsClosed() {
// Set date
now = Calendar.getInstance();
now.set(2017, 1, 10, 12, 0); // Tuesday, 1/10/2017, 12:00:00
assertFalse(mPresenter.getOpenStatus(mFacility, now));
}
}
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