开发者

Testing GPS in Android

开发者 https://www.devze.com 2022-12-15 04:02 出处:网络
How do you test GPS applications in Android? Can we test it using the Android emulat开发者_开发知识库or?Yes.

How do you test GPS applications in Android? Can we test it using the Android emulat开发者_开发知识库or?


Yes.

If you develop using the Eclipse ADT plugin, open the DDMS perspective after launching the emulator and look for the Emulator Control view. There is a location controls section that allows you to send fixed latitude and longitude coordinates, or GPX (GPS Exchange Format) or KML (Keyhole Markup Language) files if you want to send multiple coordinates at regular intervals (to simulate traveling a specific route).

Alternatively, you can simply telnet to the emulator and use the geo command from the command line, which supports fixed latitude and longitude coordinates as well as NMEA data sentences:

telnet localhost 5554
geo fix -82.411629 28.054553
geo nmea $GPGGA,001431.092,0118.2653,N,10351.1359,E,0,00,,-19.6,M,4.1,M,,0000*5B


Maybe you want to a use a Unit test, so you can try this:

public class GPSPollingServiceTest extends AndroidTestCase {
    private LocationManager locationManager;

    public void testGPS() {
        LocationManager locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
        locationManager.addTestProvider("Test", false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
        locationManager.setTestProviderEnabled("Test", true);

        // Set up your test

        Location location = new Location("Test");
        location.setLatitude(10.0);
        location.setLongitude(20.0);
        locationManager.setTestProviderLocation("Test", location);

        // Check if your listener reacted the right way

        locationManager.removeTestProvider("Test");
    }
}


For this to work you also need a required permission to the AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />


Perhaps this test application or this can help you.


in addition to Mallox code this is the sulotion for mock location in addition to real gps location : TEST_MOCK_GPS_LOCATION is a string

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = null;

    List providers = lm.getAllProviders();
    for (Object provider : providers) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Location loc = lm.getLastKnownLocation((String) provider);
        if  (provider.equals(TEST_MOCK_GPS_LOCATION))  {
            mLastLocation = loc;
        }
    } 

in the test class it is :

public class GPSPollingServiceTest extends AndroidTestCase {
private LocationManager locationManager;

public void testGPS() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    LocationManager locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
    List providers = locationManager.getAllProviders();
    if(!providers.contains(TEST_MOCK_GPS_LOCATION)) {
        locationManager.addTestProvider(TEST_MOCK_GPS_LOCATION, false, false, false, false, false, false, false, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);
        locationManager.setTestProviderEnabled(TEST_MOCK_GPS_LOCATION, true);
        // Set up your test
        Location location = new Location(TEST_MOCK_GPS_LOCATION);
        location.setLatitude(34.1233400);
        location.setLongitude(15.6777880);
        location.setAccuracy(7);
        location.setTime(8);
        location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

        locationManager.setTestProviderLocation(TEST_MOCK_GPS_LOCATION, location);

        Method locationJellyBeanFixMethod = Location.class.getMethod("makeComplete");
        if (locationJellyBeanFixMethod != null) {
            locationJellyBeanFixMethod.invoke(location);
        }
    } else {
        // Check if your listener reacted the right way
        locationManager.removeTestProvider(TEST_MOCK_GPS_LOCATION);
    }
}

}

hope it will help you


In the Extended controls of your emulator (click "..." on the control bar next to your emulator) there is the Location tab, where you can either manually input individual locations (latitude / longitude / altitude) and send them to the virtual device, or setup GPS data playback at a desired speed from a GPX or a KML file you can import.

Edit: it's the default Android Emulator I'm talking about.


I suggest that you write test which is sending location object to the listener class you want to test. With this in mind you will need some helper methods that will be able to produce location objects.

E.g.:

First you send a location with fixed coordinates (e.g. center of New York).

// test if everything is OK

Than you send a coordinate which is 100m appart from the first coordinate. You can do this with function I implemented recently: https://stackoverflow.com/a/17545955/322791

// test if everything is OK

It is common that GPS methods are related to time. I suggest that you implement method getNow() on listener class and call it whenever you need current time.

long getNow() {
    if (isUnitTesting) {
         return unitTestTime;
    } else {
         System.getcurrenttimemillis();
    }
}

In unit test environment you simply set the obj.isUnitTesting=true and obj.unitTestTime to any time you want. After that you can send first location to the listener, then you change time (+x seconds) and send another location ...

It will be much easier to test your application that way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号