|
| 1 | +package de.taimos.gpsd4java.api; |
| 2 | + |
| 3 | +import de.taimos.gpsd4java.types.TPVObject; |
| 4 | +import org.junit.Assert; |
| 5 | +import org.junit.Test; |
| 6 | +import org.mockito.Mockito; |
| 7 | + |
| 8 | +public class DistanceListenerTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void testDistanceListener() { |
| 12 | + |
| 13 | + final boolean[] called = new boolean[1]; |
| 14 | + |
| 15 | + final DistanceListener distanceListener = |
| 16 | + new DistanceListener(100.0) { |
| 17 | + @Override |
| 18 | + protected void handleLocation(final TPVObject tpv) { |
| 19 | + called[0] = true; |
| 20 | + } |
| 21 | + }; |
| 22 | + |
| 23 | + final TPVObject tpvObject1 = Mockito.mock(TPVObject.class); |
| 24 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0); |
| 25 | + Mockito.when(tpvObject1.getLatitude()).thenReturn(1.0); |
| 26 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0); |
| 27 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(1.0); |
| 28 | + |
| 29 | + final TPVObject tpvObject2 = Mockito.mock(TPVObject.class); |
| 30 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0); |
| 31 | + Mockito.when(tpvObject1.getLatitude()).thenReturn(2.0); |
| 32 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0); |
| 33 | + Mockito.when(tpvObject1.getLongitude()).thenReturn(2.0); |
| 34 | + |
| 35 | + // first call, lastPosition is null, should call handleLocation |
| 36 | + called[0] = false; |
| 37 | + distanceListener.handleTPV(tpvObject1); |
| 38 | + Assert.assertTrue(called[0]); |
| 39 | + |
| 40 | + // second call, lastPosition is set, same position so no call |
| 41 | + called[0] = false; |
| 42 | + distanceListener.handleTPV(tpvObject1); |
| 43 | + Assert.assertFalse(called[0]); |
| 44 | + |
| 45 | + // third call, lastPosition is set, other position so should call |
| 46 | + called[0] = false; |
| 47 | + distanceListener.handleTPV(tpvObject2); |
| 48 | + Assert.assertTrue(called[0]); |
| 49 | + } |
| 50 | +} |
0 commit comments