Department Location Service

I have a location tracking service that I am trying to use unit test. For this, I'm trying to use the locationManager.addTestProvider and setTestProviderLocation methods. However, it seems that I can not find any place to go through the provider and hit my LocationListener. Here is my code:

public void testGettingLocations() { mLocationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE); if (mLocationManager.getProvider(TEST_PROVIDER) != null) { mLocationManager.removeTestProvider(TEST_PROVIDER); } mLocationManager.addTestProvider(TEST_PROVIDER, false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE); mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true); mLocationManager.setTestProviderStatus(TEST_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis()); locations = new ArrayList<Location>(); mLocationManager.requestLocationUpdates(TEST_PROVIDER, 0, 0, ll); for (int i = 0; i<130; i++) { Location loc = new Location(TEST_PROVIDER); loc.setLatitude(0 + i); loc.setLongitude(130 - i); loc.setTime(System.currentTimeMillis()); loc.setSpeed(0); loc.setAccuracy(25); loc.setAltitude(0); loc.setBearing(0); mLocationManager.setTestProviderLocation(TEST_PROVIDER, loc); } assertEquals(130, locations.size()); } LocationListener ll = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) {} @Override public void onProviderDisabled(String provider) {} @Override public void onLocationChanged(Location location) { locations.add(location); Log.d("TEST", "lat: " + location.getLatitude() + ", lon: " + location.getLongitude()); } }; 

The statement fails because places.size () returns 0 and the log statement in my locationlistener is never printed.

Has anyone had success using these methods for unit test location services?

+8
android unit-testing geolocation
source share
2 answers

I managed to get the @ingsaurabh example working in unit test after making the following changes:

 import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.Socket; import java.net.UnknownHostException; public final class TestLocationProvider { static void sendLocation(double latitude, double longitude) { try { Socket socket = new Socket("10.0.2.2", 5554); // usually 5554 socket.setKeepAlive(true); String str = "geo fix " + longitude + " " + latitude ; Writer w = new OutputStreamWriter(socket.getOutputStream()); w.write(str + "\r\n"); w.flush(); } catch (UnknownHostException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } } 

An important detail is the IP address. The emulator does not recognize "localhost" as a valid host name.

+5
source share

Hi, in my first GPS-based project, I ran into the same problem, so I am writing Java code that will send a GPS Fix to my Android emulator below is a snippet.

 import java.io.IOException; import java.io.PrintStream; import java.net.Socket; import java.net.UnknownHostException; public class Main { static final int PAUSE = 4000; // ms static final float START_LONGITUDE = 51, START_LATITUDE = -1.3f; static final int NO_SAMPLE = 100; static final float DELTA_LONGITUDE = 0.000005f, DELTA_LADITUDE = 0.000005f; public static void main(String[] args) { try { Socket socket = new Socket("localhost", 5554); // usually 5554 PrintStream out = new PrintStream(socket.getOutputStream()); float longitude = START_LONGITUDE, latitude = START_LATITUDE; String str; for (int i = 0; i < NO_SAMPLE; i++) { str = "geo fix " + latitude + " " + longitude + "\n"; out.print(str); System.out.print(str); Thread.sleep(PAUSE); longitude += DELTA_LONGITUDE; latitude += DELTA_LADITUDE; } } catch (UnknownHostException e) { System.exit(-1); } catch (IOException e) { System.exit(-1); } catch (InterruptedException e) { System.exit(-1); } } } 
+4
source share

All Articles