Skip to content

Commit

Permalink
Use Robolectric and add 2 other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tasomaniac committed Oct 30, 2016
1 parent f7d5033 commit 127adea
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
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 @@ -3,10 +3,13 @@
import android.location.Location;

import org.gdg.frisbee.android.api.model.Event;
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 java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -16,55 +19,88 @@
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)
public class TaggedEventDistanceComparatorTest {

@Mock
Event eventInMarch;
@Mock
Event eventInApril;
@Mock
Event eventWithLocation;
@Mock
Location location;
@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() {
when(eventInMarch.getStart()).thenReturn(new DateTime(2014, 3, 1, 0, 0));
when(eventInApril.getStart()).thenReturn(new DateTime(2014, 4, 1, 0, 0));
when(eventInMarch.getLatLng()).thenReturn(null);
when(eventInApril.getLatLng()).thenReturn(null);
when(eventWithLocation.getLatLng()).thenReturn(new Event.LatLng(41, 29));
when(location.getLatitude()).thenReturn(41d);
when(location.getLongitude()).thenReturn(29d);
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);

EventAdapter.Item eventInMarch = new EventAdapter.Item(this.eventInMarch);
EventAdapter.Item eventInApril = new EventAdapter.Item(this.eventInApril);
EventAdapter.Item eventWithLocation = new EventAdapter.Item(this.eventWithLocation);
assertElementsOrderedLikeThis(comparator, eventWithLocation, eventInMarch);
}

assertElementsOrderedLikeThis(comparator, eventInMarch, eventInApril, eventWithLocation, eventInApril);
@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 <T> void assertElementsOrderedLikeThis(
Comparator<? super T> comparator, T... elements) {
List<T> expectedOrder = Arrays.asList(elements);
private static void assertElementsOrderedLikeThis(Comparator<? super Item> comparator, Item... elements) {
List<Item> expectedOrder = Arrays.asList(elements);

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

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

0 comments on commit 127adea

Please sign in to comment.