Permalink
Browse files

Merge remote-tracking branch 'upstream/master'

* upstream/master:
  You can now tell that a geocoder resolution happened
  Enhance Geocoder to allow testing of unsuccessful forward geocoding
  Record dialog start up in the correct order (the order in which show() methods were called)
  ShadowTextView.append updates the selectionStart and selectionEnd Added getPreviousKeyCode and getPreviousKeyEvent to ShadowTextView Move support for selectionStart and selectionEnd from ShadowEditText to ShadowTextView
  • Loading branch information...
Savvas Dalkitsis
Savvas Dalkitsis committed Mar 6, 2012
2 parents abbd39b + 898eed9 commit e032d8bdabcc6f0853b9ac2a2383ea9e3be99b81
@@ -14,6 +14,7 @@
import android.graphics.drawable.*;
import android.hardware.Camera;
import android.hardware.SensorManager;
+import android.location.Address;
import android.location.Geocoder;
import android.location.LocationManager;
import android.media.AudioManager;
@@ -352,6 +353,10 @@ public static ShadowAdapterView shadowOf(AdapterView instance) {
return (ShadowAdapterView) shadowOf_(instance);
}
+ public static ShadowAddress shadowOf(Address instance) {
+ return (ShadowAddress) shadowOf_(instance);
+ }
+
public static ShadowAlarmManager shadowOf(AlarmManager instance) {
return (ShadowAlarmManager) Robolectric.shadowOf_(instance);
}
@@ -15,6 +15,8 @@
private String countryCode;
private double longitude;
private double latitude;
+ private boolean hasLatitude;
+ private boolean hasLongitude;
@Implementation
public double getLatitude() {
@@ -85,4 +87,19 @@ public String getCountryCode() {
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
+
+ @Implementation
+ public boolean hasLatitude() {
+ return hasLatitude;
+ }
+
+ @Implementation
+ public boolean hasLongitude() {
+ return hasLongitude;
+ }
+
+ public void setSimulatedHasLatLong(boolean hasLatitude, boolean hasLongitude) {
+ this.hasLatitude = hasLatitude;
+ this.hasLongitude = hasLongitude;
+ }
}
@@ -102,6 +102,8 @@ public void onBackPressed() {
@Implementation
public void show() {
+ setLatestDialog(this);
+ shownDialogs.add(realDialog);
isShowing = true;
try {
if (!hasShownBefore) {
@@ -117,8 +119,6 @@ public void show() {
throw new RuntimeException(e);
}
hasShownBefore = true;
- setLatestDialog(this);
- shownDialogs.add(realDialog);
}
@Implementation
@@ -15,8 +15,6 @@
public class ShadowEditText extends ShadowTextView {
private int maxLength = Integer.MAX_VALUE;
- private int selectionStart = 0;
- private int selectionEnd = 0;
public ShadowEditText() {
focusable = true;
@@ -48,24 +46,15 @@ public Editable getText() {
return (Editable) text;
}
+ @Override
@Implementation
public void setSelection(int index) {
- setSelection(index, index);
+ super.setSelection(index);
}
+ @Override
@Implementation
public void setSelection(int start, int end) {
- selectionStart = start;
- selectionEnd = end;
- }
-
- @Implementation
- public int getSelectionStart() {
- return selectionStart;
- }
-
- @Implementation
- public int getSelectionEnd() {
- return selectionEnd;
+ super.setSelection(start, end);
}
}
@@ -10,6 +10,8 @@
import java.util.List;
import java.util.Locale;
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+
/**
* A shadow for Geocoder that supports simulated responses and failures
*/
@@ -28,6 +30,10 @@
private double simulatedLatitude;
private double simulatedLongitude;
private boolean shouldSimulateGeocodeException;
+ private boolean hasLatitude;
+ private boolean hasLongitude;
+ private boolean returnNoResults = false;
+ private boolean didResolution;
@Implementation
public List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException {
@@ -37,7 +43,7 @@
if (shouldSimulateGeocodeException) {
throw new IOException("Simulated geocode exception");
}
- Address address = new Address(Locale.getDefault());
+ Address address = makeAddress();
address.setAddressLine(0, addressLine1);
address.setLocality(city);
address.setAdminArea(state);
@@ -48,14 +54,25 @@
@Implementation
public List<Address> getFromLocationName(String locationName, int maxResults) throws IOException {
+ didResolution = true;
this.lastLocationName = locationName;
if (shouldSimulateGeocodeException) {
throw new IOException("Simulated geocode exception");
}
+ if (returnNoResults) {
+ return new ArrayList<Address>();
+ } else {
+ Address address = makeAddress();
+ address.setLatitude(simulatedLatitude);
+ address.setLongitude(simulatedLongitude);
+ return oneElementList(address);
+ }
+ }
+
+ private Address makeAddress() {
Address address = new Address(Locale.getDefault());
- address.setLatitude(simulatedLatitude);
- address.setLongitude(simulatedLongitude);
- return oneElementList(address);
+ shadowOf(address).setSimulatedHasLatLong(hasLatitude, hasLongitude);
+ return address;
}
/**
@@ -123,4 +140,17 @@ public String getLastLocationName() {
addresses.add(address);
return addresses;
}
+
+ public void setSimulatedHasLatLong(boolean hasLatitude, boolean hasLongitude) {
+ this.hasLatitude = hasLatitude;
+ this.hasLongitude = hasLongitude;
+ }
+
+ public void setReturnNoResults(boolean returnNoResults) {
+ this.returnNoResults = returnNoResults;
+ }
+
+ public boolean didResolution() {
+ return didResolution;
+ }
}
@@ -41,8 +41,12 @@
private int textAppearanceId;
private TransformationMethod transformationMethod;
private int inputType;
+ protected int selectionStart = 0;
+ protected int selectionEnd = 0;
private List<TextWatcher> watchers = new ArrayList<TextWatcher>();
+ private List<Integer> previousKeyCodes = new ArrayList<Integer>();
+ private List<KeyEvent> previousKeyEvents = new ArrayList<KeyEvent>();
@Override
public void applyAttributes() {
@@ -71,16 +75,24 @@ public void setText(CharSequence text) {
@Implementation
public final void append(CharSequence text) {
+ boolean isSelectStartAtEnd = selectionStart == this.text.length();
+ boolean isSelectEndAtEnd = selectionEnd == this.text.length();
CharSequence oldValue = this.text;
StringBuffer sb = new StringBuffer(this.text);
sb.append(text);
sendBeforeTextChanged(sb.toString());
this.text = sb.toString();
+ if (isSelectStartAtEnd) {
+ selectionStart = this.text.length();
+ }
+ if (isSelectEndAtEnd) {
+ selectionEnd = this.text.length();
+ }
+
sendOnTextChanged(oldValue);
sendAfterTextChanged();
-
}
@Implementation
@@ -247,6 +259,8 @@ public int getCompoundDrawablePadding() {
@Implementation
public boolean onKeyDown(int keyCode, KeyEvent event) {
+ previousKeyCodes.add(keyCode);
+ previousKeyEvents.add(event);
if (onKeyListener != null) {
return onKeyListener.onKey(realView, keyCode, event);
} else {
@@ -256,13 +270,23 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
@Implementation
public boolean onKeyUp(int keyCode, KeyEvent event) {
+ previousKeyCodes.add(keyCode);
+ previousKeyEvents.add(event);
if (onKeyListener != null) {
return onKeyListener.onKey(realView, keyCode, event);
} else {
return false;
}
}
+ public int getPreviousKeyCode(int index) {
+ return previousKeyCodes.get(index);
+ }
+
+ public KeyEvent getPreviousKeyEvent(int index) {
+ return previousKeyEvents.get(index);
+ }
+
@Implementation
public int getGravity() {
return gravity;
@@ -426,6 +450,25 @@ public TextPaint getPaint() {
return new TextPaint();
}
+ public void setSelection(int index) {
+ setSelection(index, index);
+ }
+
+ public void setSelection(int start, int end) {
+ selectionStart = start;
+ selectionEnd = end;
+ }
+
+ @Implementation
+ public int getSelectionStart() {
+ return selectionStart;
+ }
+
+ @Implementation
+ public int getSelectionEnd() {
+ return selectionEnd;
+ }
+
/**
* @return the list of currently registered watchers/listeners
*/
@@ -139,6 +139,13 @@ public void shouldKeepListOfOpenedDialogs() throws Exception {
assertEquals(0, ShadowDialog.getShownDialogs().size());
}
+ @Test
+ public void shouldPopulateListOfRecentDialogsInCorrectOrder() throws Exception {
+ new NestingTestDialog().show();
+
+ assertEquals(TestDialog.class, ShadowDialog.getLatestDialog().getClass());
+ }
+
private static class TestDialog extends Dialog {
boolean onStartCalled = false;
boolean wasDismissed = false;
@@ -158,4 +165,15 @@ protected void onStart() {
}
}
+ private static class NestingTestDialog extends Dialog {
+ public NestingTestDialog() {
+ super(null);
+ };
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ new TestDialog().show();
+ }
+ }
}
@@ -1,23 +1,63 @@
package com.xtremelabs.robolectric.shadows;
import android.app.Activity;
+import android.location.Address;
import android.location.Geocoder;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.util.List;
+
import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+import static junit.framework.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
@RunWith(WithTestDefaultsRunner.class)
public class GeocoderTest {
+ private Geocoder geocoder;
+
+ @Before
+ public void setUp() throws Exception {
+ geocoder = new Geocoder(new Activity());
+ }
+
@Test
public void shouldRecordLastLocationName() throws Exception {
- Geocoder geocoder = new Geocoder(new Activity());
geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
String lastLocationName = shadowOf(geocoder).getLastLocationName();
assertEquals("731 Market St, San Francisco, CA 94103", lastLocationName);
}
+
+ @Test
+ public void setsUpHasLocationInAddressFromLocationName() throws Exception {
+ shadowOf(geocoder).setSimulatedHasLatLong(true, true);
+ Address address = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1).get(0);
+ assertTrue(address.hasLatitude());
+ assertTrue(address.hasLongitude());
+ shadowOf(geocoder).setSimulatedHasLatLong(false, false);
+ address = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1).get(0);
+ assertFalse(address.hasLatitude());
+ assertFalse(address.hasLongitude());
+ }
+
+ @Test
+ public void canReturnNoAddressesOnRequest() throws Exception {
+ shadowOf(geocoder).setReturnNoResults(true);
+ List<Address> result = geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
+ assertEquals(0, result.size());
+ }
+
+
+ @Test
+ public void answersWhetherResolutionHappened() throws Exception {
+ assertFalse(shadowOf(geocoder).didResolution());
+ shadowOf(geocoder).setReturnNoResults(true);
+ geocoder.getFromLocationName("731 Market St, San Francisco, CA 94103", 1);
+ assertTrue(shadowOf(geocoder).didResolution());
+ }
}
Oops, something went wrong.

0 comments on commit e032d8b

Please sign in to comment.