Skip to content

Commit

Permalink
Merge pull request gdg-x#703 from gdg-x/test/event-comparator
Browse files Browse the repository at this point in the history
Unit test for TaggedEventDistanceComparator
  • Loading branch information
tasomaniac authored Oct 30, 2016
2 parents d2020f9 + aef557f commit 2204bb9
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.gdg.frisbee.android.api.model;

import android.support.annotation.VisibleForTesting;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

Expand Down Expand Up @@ -60,6 +62,12 @@ public LatLng getLatLng() {
public static class LatLng {
double lat, lng;

@VisibleForTesting
public LatLng(double lat, double lng) {
this.lat = lat;
this.lng = lng;
}

public double getLng() {
return lng;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

class EventDateComparator implements Comparator<EventAdapter.Item> {

public EventDateComparator() {
}

@Override
public int compare(EventAdapter.Item event1, EventAdapter.Item event2) {
if (event1.getEvent().getStart() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.gdg.frisbee.android.eventseries;

import android.location.Location;
import android.support.annotation.Nullable;

import org.gdg.frisbee.android.api.model.Event;

import java.util.Comparator;

class TaggedEventDistanceComparator implements Comparator<EventAdapter.Item> {

@Nullable
private final Location lastLocation;

public TaggedEventDistanceComparator(Location lastLocation) {
TaggedEventDistanceComparator(@Nullable Location lastLocation) {
this.lastLocation = lastLocation;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
Expand All @@ -22,7 +22,7 @@

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricGradleTestRunner.class)
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, application = BaseApp.class)
public class ChapterComparatorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.junit.Assert.assertEquals;

public class EventDateComparatorShould {
public class EventDateComparatorTest {

private EventDateComparator comparator;
private EventAdapter.Item eventInMarch;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.gdg.frisbee.android.eventseries;

import android.location.Location;

import org.gdg.frisbee.android.BuildConfig;
import org.gdg.frisbee.android.api.model.Event;
import org.gdg.frisbee.android.app.BaseApp;
import org.gdg.frisbee.android.eventseries.EventAdapter.Item;
import org.joda.time.DateTime;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, application = BaseApp.class)
public class TaggedEventDistanceComparatorTest {

@Mock Event eventInMarch;
@Mock Event eventInApril;
@Mock Event eventInDecember;

private Location location;

@Before
public void setup() {
initMocks(this);
location = new Location("mock");
location.setLatitude(41d);
location.setLongitude(29d);

when(eventInMarch.getStart()).thenReturn(new DateTime(2014, 3, 1, 0, 0));
when(eventInApril.getStart()).thenReturn(new DateTime(2014, 4, 1, 0, 0));
when(eventInDecember.getStart()).thenReturn(new DateTime(2014, 12, 1, 0, 0));
}

@Test
public void givenUserHasNoLocation_thenComparatorShouldSortUsingDates() {
TaggedEventDistanceComparator comparator = new TaggedEventDistanceComparator(null);

Item eventInMarch = new Item(this.eventInMarch);
Item eventInApril = new Item(this.eventInApril);
Item eventInDecember = new Item(this.eventInDecember);

assertElementsOrderedLikeThis(comparator, eventInMarch, eventInApril, eventInDecember);
}

@Test
public void givenEventsWithNoLocation_thenComparatorShouldSortUsingDates() {
TaggedEventDistanceComparator comparator = new TaggedEventDistanceComparator(location);

Item eventInMarch = new Item(this.eventInMarch);
Item eventInApril = new Item(this.eventInApril);
Item eventInDecember = new Item(this.eventInDecember);

assertElementsOrderedLikeThis(comparator, eventInMarch, eventInApril, eventInDecember);
}

@Test
public void givenEventWithNoLocation_thenWithLocationShouldComeFirst() {
Item eventWithLocation = givenEventWithLocation(41, 29);
Item eventInMarch = new Item(this.eventInMarch);

TaggedEventDistanceComparator comparator = new TaggedEventDistanceComparator(location);

assertElementsOrderedLikeThis(comparator, eventWithLocation, eventInMarch);
}

@Test
public void shouldSortUsingLocation() {
Item eventWithMyLocation = givenEventWithLocation(41, 29);
Item eventWithFurtherLocation = givenEventWithLocation(42d, 30d);

TaggedEventDistanceComparator comparator = new TaggedEventDistanceComparator(location);

assertElementsOrderedLikeThis(comparator, eventWithMyLocation, eventWithFurtherLocation);
}

private static Item givenEventWithLocation(double lat, double lng) {
Event eventWithLocation = mock(Event.class);
when(eventWithLocation.getLatLng()).thenReturn(new Event.LatLng(lat, lng));
return new Item(eventWithLocation);
}

private static void assertElementsOrderedLikeThis(Comparator<? super Item> comparator, Item... elements) {
List<Item> expectedOrder = Arrays.asList(elements);

List<Item> shuffledAndSorted = new ArrayList<>(expectedOrder);
Collections.shuffle(shuffledAndSorted, new Random(0));
Collections.sort(shuffledAndSorted, comparator);
assertEquals(expectedOrder, shuffledAndSorted);

List<Item> reversedAndSorted = new ArrayList<>(expectedOrder);
Collections.reverse(reversedAndSorted);
Collections.sort(reversedAndSorted, comparator);
assertEquals(expectedOrder, reversedAndSorted);
}
}

0 comments on commit 2204bb9

Please sign in to comment.