单元测试位置服务 [英] Unit Testing Location Service

查看:93
本文介绍了单元测试位置服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我想要的单元测试位置跟踪服务。我试图用locationManager.addTestProvider和setTestProviderLocation的方法来实现这一目标。我似乎无法得到任何的位置。但是通过提供者和打我LocationListener的。这是我的code:

I've got a location tracking service that I'm trying to unit test. I'm trying to use the locationManager.addTestProvider and setTestProviderLocation methods to achieve this. I can't seem to get any of the locations to pass through the provider and hit my LocationListener, however. 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());
    }
};

断言失败,因为locations.size()返回0,而在我LocationListener的日志报表永远不会被打印出来。

The assert fails because locations.size() returns 0, and the log statement in my locationlistener never gets printed.

有没有人有使用这些方法进行单元测试位置服务的任何成功?

Has anyone had any success using these methods to unit test Location services?

推荐答案

我是能够得到@ ingsaurabh的例子进行以下修改后的单元测试工作:

I was able to get @ingsaurabh's example working in a unit test after making the following modifications:

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);
        }
    }
}

最重要的细节是IP地址。模拟器不承认本地主机作为一个有效的主机名。

The important detail is the IP address. The emulator does not recognize 'localhost' as a valid hostname.

这篇关于单元测试位置服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆