### Question:
Locations { public static Location getLocationWithAdjustedAltitude(Location loc, PreferenceHelper ph) { if(!loc.hasAltitude()){ return loc; } if(ph.shouldAdjustAltitudeFromGeoIdHeight() && loc.getExtras() != null){ String geoidheight = loc.getExtras().getString(BundleConstants.GEOIDHEIGHT); if (!Strings.isNullOrEmpty(geoidheight)) { loc.setAltitude(loc.getAltitude() - Double.parseDouble(geoidheight)); } else { loc.removeAltitude(); } } if(loc.hasAltitude() && ph.getSubtractAltitudeOffset() != 0){ loc.setAltitude(loc.getAltitude() - ph.getSubtractAltitudeOffset()); } return loc; }  static Location getLocationWithAdjustedAltitude(Location loc, PreferenceHelper ph); static Location getLocationAdjustedForGPSWeekRollover(Location loc);  }### Answer:
@Test public void AdjustLocationAltitude_WhenNoAltitude_NothingAdjusted(){ Location loc = MockLocations.builder("MOCK", 12.193,19.111).build(); PreferenceHelper ph = mock(PreferenceHelper.class); Location actual = Locations.getLocationWithAdjustedAltitude(loc, ph); assertThat("Location without altitude is not adjusted", actual.hasAltitude(), is(actual.hasAltitude())); assertThat("Location without altitude is not adjusted", actual.hasAltitude(), is(false)); verify(loc, times(0)).setAltitude(anyDouble()); }
@Test public void AdjustLocationAltitude_WhenAltitudePresentNoPreferences_NothingAdjusted(){ PreferenceHelper ph = mock(PreferenceHelper.class); when(ph.shouldAdjustAltitudeFromGeoIdHeight()).thenReturn(true); Location loc = MockLocations.builder("MOCK", 12.193,19.111).withAltitude(100).build(); Location actual = Locations.getLocationWithAdjustedAltitude(loc, ph); assertThat("Location with altitude but no preference not adjusted", actual.getAltitude(), is(100d)); verify(loc, times(0)).setAltitude(anyDouble()); }
@Test public void AdjustLocationAltitude_AdjustAltitudeFromGeoIdHeight_AdjustedLocation() { PreferenceHelper ph = mock(PreferenceHelper.class); when(ph.shouldAdjustAltitudeFromGeoIdHeight()).thenReturn(true); Location loc = MockLocations.builder("MOCK", 12.193, 19.111).withAltitude(100).putExtra(BundleConstants.GEOIDHEIGHT, "15").build(); Location actual = Locations.getLocationWithAdjustedAltitude(loc, ph); verify(loc, times(1)).setAltitude(85); }
@Test public void AdjustLocationAltitude_GeoIdHeightNotPresent_RemoveAltitude(){ PreferenceHelper ph = mock(PreferenceHelper.class); when(ph.shouldAdjustAltitudeFromGeoIdHeight()).thenReturn(true); Location loc = MockLocations.builder("MOCK", 12.193,19.111).withAltitude(100).putExtra("a","b").build(); Location actual = Locations.getLocationWithAdjustedAltitude(loc, ph); verify(loc,times(1)).removeAltitude(); }
@Test public void AdjustLocationAltitude_AdjustAltitudeFromUserOffset_AdjustedLocation(){ PreferenceHelper ph = mock(PreferenceHelper.class); when(ph.shouldAdjustAltitudeFromGeoIdHeight()).thenReturn(false); when(ph.getSubtractAltitudeOffset()).thenReturn(20); Location loc = MockLocations.builder("MOCK", 12.193,19.111).withAltitude(100).putExtra(BundleConstants.GEOIDHEIGHT,"15").build(); Location actual = Locations.getLocationWithAdjustedAltitude(loc,ph); verify(loc, times(1)).setAltitude(80); verify(loc, times(0)).setAltitude(85); }