package org.robolectric.shadows;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.robolectric.Shadows.shadowOf;

import android.location.Address;
import android.location.Geocoder;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

/** Unit test for {@link ShadowGeocoder}. */
@RunWith(RobolectricTestRunner.class)
public class ShadowGeocoderTest {

  @Test
  public void isPresentReturnsTrueByDefault() {
    assertThat(Geocoder.isPresent()).isTrue();
  }

  @Test
  public void isPresentReturnsFalseWhenOverridden() {
    ShadowGeocoder.setIsPresent(false);

    assertThat(Geocoder.isPresent()).isFalse();
  }

  @Test
  public void getFromLocationReturnsAnEmptyArrayByDefault() throws IOException {
    Geocoder geocoder = new Geocoder(RuntimeEnvironment.application.getApplicationContext());
    assertThat(geocoder.getFromLocation(90.0,90.0,1)).hasSize(0);
  }

  @Test
  public void getFromLocationReturnsTheOverwrittenListLimitingByMaxResults() throws IOException {
    Geocoder geocoder = new Geocoder(RuntimeEnvironment.application.getApplicationContext());
    ShadowGeocoder shadowGeocoder = shadowOf(geocoder);

    List<Address> list = Arrays.asList(new Address(Locale.getDefault()), new Address(Locale.CANADA));
    shadowGeocoder.setFromLocation(list);

    List<Address> result = geocoder.getFromLocation(90.0, 90.0, 1);
    assertThat(result).hasSize(1);

    result = geocoder.getFromLocation(90.0, 90.0, 2);
    assertThat(result).hasSize(2);

    result = geocoder.getFromLocation(90.0, 90.0, 3);
    assertThat(result).hasSize(2);
  }

  @Test
  public void getFromLocation_throwsExceptionForInvalidLatitude() throws IOException {
    Geocoder geocoder = new Geocoder(RuntimeEnvironment.application.getApplicationContext());

    IllegalArgumentException thrown =
        assertThrows(IllegalArgumentException.class, () -> geocoder.getFromLocation(91.0, 90.0, 1));
    assertThat(thrown).hasMessageThat().contains(Double.toString(91.0));
  }

  @Test
  public void getFromLocation_throwsExceptionForInvalidLongitude() throws IOException {
    Geocoder geocoder = new Geocoder(RuntimeEnvironment.application.getApplicationContext());

    IllegalArgumentException thrown =
        assertThrows(
            IllegalArgumentException.class, () -> geocoder.getFromLocation(15.0, -211.0, 1));
    assertThat(thrown).hasMessageThat().contains(Double.toString(-211.0));
  }

  @Test
  public void resettingShadowRestoresDefaultValueForIsPresent() {
    ShadowGeocoder.setIsPresent(false);
    ShadowGeocoder.reset();
    assertThat(Geocoder.isPresent()).isTrue();
  }
}

@@ -114,28 +114,20 @@
 .Assert.
-assertThrows
+fail
 ;%0Aimport
@@ -1995,32 +1995,97 @@
 ontext());%0A%0A    
+try %7B%0A      geocoder.getFromLocation(91.0, 90.0, 1);%0A      fail(%22
 IllegalArgumentE
@@ -2097,37 +2097,34 @@
 ion 
+not 
 thrown
- =%0A        assertThrows
+%22);%0A    %7D catch 
 (Ill
@@ -2148,64 +2148,21 @@
 tion
-.class, () -%3E geocoder.getFromLocation(91.0, 90.0, 1));%0A
+ thrown) %7B%0A  
     
@@ -2226,24 +2226,30 @@
 ing(91.0));%0A
+    %7D%0A
   %7D%0A%0A  @Test
@@ -2432,24 +2432,91 @@
 xt());%0A%0A    
+try %7B%0A      geocoder.getFromLocation(15.0, -211.0, 1);%0A      fail(%22
 IllegalArgum
@@ -2532,133 +2532,72 @@
 ion 
+not 
 thrown
- =%0A        assertThrows(%0A            IllegalArgumentException.class, () -%3E geocoder.getFromLocation(15.0, -211.0, 1));%0A
+%22);%0A    %7D catch (IllegalArgumentException thrown) %7B%0A  
     
@@ -2667,16 +2667,22 @@
 11.0));%0A
+    %7D%0A
   %7D%0A%0A  @

Remove assertThrows usage