如何将模拟位置从junit测试传递到Android中的Activity

时间:2012-11-21 16:20:39

标签: android junit

我正在进行一项活动,在textView中打印任何位置变化。我想用JUnit测试它。这是我的代码,这是我的输出:

public void testGPS() {
        {
            LocationManager lm = (LocationManager) myActivity
                    .getSystemService(Context.LOCATION_SERVICE);
            String testProvider = "Test";

            if (null == lm.getProvider(testProvider)) {
                lm.addTestProvider(testProvider, false, false, false, false,
                        false, false, false, Criteria.POWER_LOW,
                        Criteria.ACCURACY_FINE);
            }

            lm.setTestProviderEnabled(testProvider, true);
            lm.requestLocationUpdates(testProvider, 0, 0,
                    myActivity.mLocListener);
            lm.setTestProviderStatus(testProvider, LocationProvider.AVAILABLE,
                    null, System.currentTimeMillis());

            // getService().setUpProvider(testProvider,lm)

            Location location = new Location(testProvider);
            location.setLatitude(1.0);
            location.setLongitude(2.0);
            location.setTime(System.currentTimeMillis());
            lm.setTestProviderLocation(testProvider, location);

            String test_rs = "lon=" + location.getLongitude() + "\nlat="
                    + location.getLatitude();
            String msg = "Location Succeeded";
            // Assert.assertFalse("Received Location is null", received == null
            // );
            assertEquals(msg, myActivity.mTextLocation.getText().toString(),
                    test_rs);

            lm.removeTestProvider(testProvider);
        }

,跟踪是:

junit.framework.ComparisonFailure: Location Succeeded expected:<Unknown> but was:<lon=2.0
lat=1.0>
at mypackage.TestActivity.testGPS(TestActivity.java:73)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

所以你可以在这里看到:

junit.framework.ComparisonFailure: Location Succeeded expected:<Unknown> but was:<lon=2.0 lat=1.0>.

所以就像myMain Activity没有从我的测试中获得位置。

任何帮助?

编辑:myActivity是我想要测试的活动。第73行是断言。我只是想从测试中将位置传递给活动。怎么做?

1 个答案:

答案 0 :(得分:1)

我设法让这个工作,至少对我而言。

线索在这里:https://developer.android.com/training/location/location-testing.html

  

...您不必在发布之前禁用或删除测试代码。

......和......

  

始终将提供者值设置为flp,这是位置服务放入其发出的位置对象的代码。

我还发现有必要提供更新时间。

所以,这就是我最终的结果:

public void testGPS() {
    LocationManager lm = (LocationManager) mActivity
        .getSystemService(Context.LOCATION_SERVICE);

    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
    lm.setTestProviderStatus(LocationManager.GPS_PROVIDER, 
                             LocationProvider.AVAILABLE,
                             null, System.currentTimeMillis());

    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(1.0);
    location.setLongitude(2.0);
    location.setTime(System.currentTimeMillis());
    lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);

    try {
        Thread.sleep(2000);
    } catch(InterruptedException e) {
    }
}