Hello.
I wrote small service listening for GPS device messages.
I wrote also ServiceTestCase planning to check that my service returns
correct longitude and latitude when location is changed.
Unfortunately, onLocationChanged is not called not in the service
under the test nor in the test case.
Should it work?
If yes, what's wrong with my test case?
Thanks in advance.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

package com.gkatz.android.mtg.test;

import java.util.List;

import com.gkatz.android.mtg.LocationService;
import com.gkatz.android.mtg.LocationService.LocationBinder;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.test.ServiceTestCase;

public class LocationServiceTest extends
ServiceTestCase<LocationService> implements LocationListener{

        public LocationServiceTest() {
                super(LocationService.class);
                // TODO Auto-generated constructor stub
        }

        @Override
        protected void setUp() throws Exception {
                // TODO Auto-generated method stub
                super.setUp();
        }

        public void testBinding(){
        IBinder locationBinder;

        locationBinder = getServiceBinder();
                assertNotNull(locationBinder);
        }

        public void testStart(){
        Intent locationIntent = new Intent(getContext(),
LocationService.class);

        startService(locationIntent);
        }

        public void testLocationUpdate() throws InterruptedException{

                LocationBinder locationBinder;
                LocationService locationService;
                Context context = getContext();

                LocationManager lm = getLocationManager();

                context.registerReceiver(locationReceiver,
                        new
IntentFilter("android.mtg.custom.intent.action.GPS_LOCATION"));

        locationBinder = (LocationBinder)getServiceBinder();
                assertNotNull(locationBinder);

                locationService = getService();
                assertNotNull(locationService);

                Location loc = new Location(LocationManager.GPS_PROVIDER);
                loc.setLongitude(4.890935);
                loc.setLatitude(52.373801);
                loc.setTime(System.currentTimeMillis());

                lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, loc);

                SystemClock.sleep(3000);

                loc.setLongitude(35.2276757);
                loc.setLatitude(31.7765488);
                loc.setTime(System.currentTimeMillis());

                lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, loc);

                synchronized (this) {
                        this.wait(2000);
                }

                Location lastLoc =
lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                System.out.println("Last known longitude: " +
Double.toString(lastLoc.getLongitude()) +
                                                   "Last known latitude: " +
Double.toString(lastLoc.getLatitude()));
                assertEquals(35.2276757, locationService.getLongitude());
                assertEquals(31.7765488, locationService.getLatitude());

                context.unregisterReceiver(locationReceiver);
                lm.removeTestProvider(LocationManager.GPS_PROVIDER);
        }
    private BroadcastReceiver locationReceiver=new BroadcastReceiver()
{
                public void onReceive(Context context, Intent intent) {
        
assertTrue(intent.getAction().equals("android.mtg.custom.intent.action.GPS_LOCATION"));
                        System.out.println("Action received: " + 
intent.getAction());
                        this.notify();
                }
    };

    private IBinder getServiceBinder(){
        Intent locationIntent = new Intent(getContext(),
LocationService.class);
        return bindService(locationIntent);
    }

    private LocationManager getLocationManager(){
                LocationManager lm = (LocationManager)
                getContext().getSystemService(Context.LOCATION_SERVICE);

                lm.addTestProvider(LocationManager.GPS_PROVIDER,
                                                   true, true, true, true, 
true, true, true,
                                                   Criteria.POWER_LOW, 
Criteria.ACCURACY_FINE);

                lm.setTestProviderStatus(LocationManager.GPS_PROVIDER,
                LocationProvider.AVAILABLE, null, System.currentTimeMillis());
                lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
                
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
                return lm;
    }

        @Override
        public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                System.out.println("LocationServiceTest, onLocationChanged, 
lon:" +
                                Double.toString(location.getLongitude()) + ", 
lat:" +
                                Double.toString(location.getLatitude()));
        }

        @Override
        public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle
extras) {
                // TODO Auto-generated method stub

        }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to