Unit test gps functionality

Hi
I have a Android app which uses gps for various functionality.
How do i unit test the gps?
And can it be done without running a device?

Regards
Jakob

Tagged:

Answers

  • YkshLeoYkshLeo USMember ✭✭✭✭
    edited February 2016

    You can try something like this :

    public class GPSPollingServiceTest
    {
        LocationManager locationManager;
    
        public void TestGPS()
        {
            Context context;
            var locationManager = context.GetSystemService (Context.LocationService) as LocationManager;
            locationManager.AddTestProvider ("Test", false, false, false, false, false, false, false, Power.Low, Android.Hardware.SensorStatus.AccuracyHigh);
            locationManager.SetTestProviderEnabled("Test", true);
    
            // Set up your test
    
            var location = new Location("Test");
            location.Latitude = 10.0;
            location.Longitude = 20.0;
            locationManager.SetTestProviderLocation("Test", location);
    
            // Check if your event 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" />
    

    Refer :

  • JakobJensenJakobJensen DKMember ✭✭

    What kind of project do i need to be able to use the GMS?

Sign In or Register to comment.