AuditEventLogger { @Nullable Location getMostAccurateLocation(long currentTime) { removeExpiredLocations(currentTime); Location bestLocation = null; if (!locations.isEmpty()) { for (Location location : locations) { if (bestLocation == null || location.getAccuracy() < bestLocation.getAccuracy()) { bestLocation = location; } } } return bestLocation; } AuditEventLogger(File instanceFile, AuditConfig auditConfig); void logEvent(AuditEvent.AuditEventType eventType, boolean writeImmediatelyToDisk); void logEvent(AuditEvent.AuditEventType eventType, FormIndex formIndex,
                         boolean writeImmediatelyToDisk, String questionAnswer); void exitView(); void addLocation(Location location);  }
@Test public void getMostAccurateLocationTest() { Location location1 = mock(Location.class); when(location1.getLatitude()).thenReturn(54.380746599999995); when(location1.getLongitude()).thenReturn(18.606523); when(location1.getAccuracy()).thenReturn(5f); when(location1.getTime()).thenReturn(1548156641000L); AuditEventLogger auditEventLogger = new AuditEventLogger(testInstanceFile, testAuditConfig); auditEventLogger.addLocation(location1); assertEquals(1, auditEventLogger.getLocations().size()); Location location2 = mock(Location.class); when(location2.getLatitude()).thenReturn(54.37971080550665); when(location2.getLongitude()).thenReturn(18.612874470947304); when(location2.getAccuracy()).thenReturn(10f); when(location2.getTime()).thenReturn(1548156655000L); auditEventLogger.addLocation(location2); assertEquals(2, auditEventLogger.getLocations().size()); Location location3 = mock(Location.class); when(location3.getLatitude()).thenReturn(54.3819102504987); when(location3.getLongitude()).thenReturn(18.62025591015629); when(location3.getAccuracy()).thenReturn(12f); when(location3.getTime()).thenReturn(1548156665000L); auditEventLogger.addLocation(location3); assertEquals(3, auditEventLogger.getLocations().size()); Location location4 = mock(Location.class); when(location4.getLatitude()).thenReturn(54.38620882544086); when(location4.getLongitude()).thenReturn(18.62523409008793); when(location4.getAccuracy()).thenReturn(7f); when(location4.getTime()).thenReturn(1548156688000L); auditEventLogger.addLocation(location4); assertEquals(4, auditEventLogger.getLocations().size()); Location location5 = mock(Location.class); when(location5.getLatitude()).thenReturn(54.39070685202294); when(location5.getLongitude()).thenReturn(18.617166005371132); when(location5.getAccuracy()).thenReturn(20f); when(location5.getTime()).thenReturn(1548156710000L); auditEventLogger.addLocation(location5); assertEquals(5, auditEventLogger.getLocations().size()); Location location = auditEventLogger.getMostAccurateLocation(1548156712000L); assertEquals(4, auditEventLogger.getLocations().size()); assertEquals(54.38620882544086, location.getLatitude(), 0); assertEquals(18.62523409008793, location.getLongitude(), 0); assertEquals(7f, location.getAccuracy(), 0); }