2 Stimmen

setTestProviderLocation löst onLocationChanged im Service-Testfall nicht aus

Ich habe kleinen Dienst implementieren LocationListener. Ich habe es manuell mit meiner Anwendung getestet und es funktioniert einwandfrei. Ich habe auch einen Testfall für den Dienst geschrieben. Ich habe setTestProviderLocation in der Erwartung verwendet, dass der Dienst eine Standortaktualisierung erhält. Dies ist jedoch nicht der Fall. Weiß jemand, wo das Problem liegt? Ich möchte betonen, dass der gleiche Dienst in der realen Anwendung funktioniert. Der Testfall ist unten hinzugefügt

 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 testNoStart(){
    LocationService locationService = getService();
    assertNull(locationService);
}

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

}

}

CodeJaeger.com

CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.

Powered by:

X