如何编写依赖于位置更新的 Android 单元测试?

发布于 2024-11-09 18:38:30 字数 2609 浏览 3 评论 0原文

我有一个应用程序显示与该位置对应的梅登黑德网格方块。我想为此功能编写一个单元测试。

我创建了一个模拟位置提供程序。当我将模拟提供程序插入我的应用程序时,我在显示屏上看到预期的梅登黑德网格方块。当我将模拟提供程序插入我的测试项目并检查视图时,它永远不会更新,即使我调用 Thread.sleep() 或 waitOnIdleSync() 也是如此。

我会直接测试计算实际网格平方的方法,但它是私有的,并且无法测试私有方法。我在网上看到的用于检查视图的单元测试的所有示例代码都适用于计算器等应用程序,其中活动是通过假按钮按下触发的。

这是测试的代码:

    public void testMaidenhead() {
        // this is a single test which doesn't really validate the algorithm
        // identifying a bunch of edge cases would do that
        publishMockLocation();
        final String expectedMH = "CM87wk";
        // TODO: checking the textview does not work
        TextView mhValueView = (TextView) mActivity.findViewById(org.twilley.android.hfbeacon.R.id.maidenheadValue);
        String actualMH = mhValueView.getText().toString();
        // final String actualMH = mActivity.gridSquare(mLocation);
        assertEquals(expectedMH, actualMH);
    }

这是发布模拟位置的代码:

    protected void publishMockLocation() {
        final double TEST_LONGITUDE = -122.084095;
        final double TEST_LATITUDE = 37.422006;
        final String TEST_PROVIDER = "test";
        final Location mLocation;
        final LocationManager mLocationManager;

        mLocationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.getProvider(TEST_PROVIDER) != null) {
            mLocationManager.removeTestProvider(TEST_PROVIDER);
        }
        if (mLocationManager.getProvider(TEST_PROVIDER) == null) {
            mLocationManager.addTestProvider(TEST_PROVIDER, 
                false, //requiresNetwork,
                false, // requiresSatellite,
                false, // requiresCell,
                false, // hasMonetaryCost,
                false, // supportsAltitude,
                false, // supportsSpeed,
                false, // supportsBearing,
                android.location.Criteria.POWER_MEDIUM, // powerRequirement
                android.location.Criteria.ACCURACY_FINE); // accuracy
        }
        mLocation = new Location(TEST_PROVIDER);
        mLocation.setLatitude(TEST_LATITUDE);
        mLocation.setLongitude(TEST_LONGITUDE);
        mLocation.setTime(System.currentTimeMillis());
        mLocation.setAccuracy(25);
        mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true);
        mLocationManager.setTestProviderStatus(TEST_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
        mLocationManager.setTestProviderLocation(TEST_PROVIDER, mLocation);
    }

任何帮助将不胜感激。先感谢您!

杰克。

I have an app that displays the Maidenhead grid square corresponding to that location. I would like to write a unit test for this feature.

I have created a mock location provider. When I stick the mock provider into my app, I see the expected Maidenhead grid square on the display. When I stick the mock provider into my test project and check the view it never updates, even when I call Thread.sleep() or waitOnIdleSync().

I would directly test the method that computes the actual grid square but it is private, and there's no way to test private methods. All the example code I have seen online for unit tests that check views is for apps like calculators, where activity is triggered by fake button presses.

Here is the code for the test:

    public void testMaidenhead() {
        // this is a single test which doesn't really validate the algorithm
        // identifying a bunch of edge cases would do that
        publishMockLocation();
        final String expectedMH = "CM87wk";
        // TODO: checking the textview does not work
        TextView mhValueView = (TextView) mActivity.findViewById(org.twilley.android.hfbeacon.R.id.maidenheadValue);
        String actualMH = mhValueView.getText().toString();
        // final String actualMH = mActivity.gridSquare(mLocation);
        assertEquals(expectedMH, actualMH);
    }

And here is the code for publishing the mock location:

    protected void publishMockLocation() {
        final double TEST_LONGITUDE = -122.084095;
        final double TEST_LATITUDE = 37.422006;
        final String TEST_PROVIDER = "test";
        final Location mLocation;
        final LocationManager mLocationManager;

        mLocationManager = (LocationManager) mActivity.getSystemService(Context.LOCATION_SERVICE);
        if (mLocationManager.getProvider(TEST_PROVIDER) != null) {
            mLocationManager.removeTestProvider(TEST_PROVIDER);
        }
        if (mLocationManager.getProvider(TEST_PROVIDER) == null) {
            mLocationManager.addTestProvider(TEST_PROVIDER, 
                false, //requiresNetwork,
                false, // requiresSatellite,
                false, // requiresCell,
                false, // hasMonetaryCost,
                false, // supportsAltitude,
                false, // supportsSpeed,
                false, // supportsBearing,
                android.location.Criteria.POWER_MEDIUM, // powerRequirement
                android.location.Criteria.ACCURACY_FINE); // accuracy
        }
        mLocation = new Location(TEST_PROVIDER);
        mLocation.setLatitude(TEST_LATITUDE);
        mLocation.setLongitude(TEST_LONGITUDE);
        mLocation.setTime(System.currentTimeMillis());
        mLocation.setAccuracy(25);
        mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true);
        mLocationManager.setTestProviderStatus(TEST_PROVIDER, LocationProvider.AVAILABLE, null, System.currentTimeMillis());
        mLocationManager.setTestProviderLocation(TEST_PROVIDER, mLocation);
    }

Any help would be dearly appreciated. Thank you in advance!

Jack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2024-11-16 18:38:46

单元测试不会让您的手机伪造其 GPS 位置,因此它会向您显示要测试的位置的梅登黑德。单元测试将是:编写一个采用 WGS84 GPS 坐标并输出 Maidenhead 的函数,并为一系列输入位置和输出编写一些测试,以确保您的函数按您的需要工作。

测试实际的 Android 活动将是集成或验收测试,但与 Maidenhead 功能的实际协调应该在您对其进行单元测试时起作用。

Unit testing would not be to make your phone fake its GPS location so it shows you the Maidenhead for the location you want to test. Unit testing would be: write a function that takes WGS84 GPS coordinates and outputs Maidenhead, and write a couple of tests for a series of input locations and outputs to make sure your function works as you need.

Testing the actual Android activity would be integration or acceptance testing, but the actual coordinate to Maidenhead functionality should work as you unit tested it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文