模拟位置在Android Pie及更高版本上不起作用

问题描述 投票:0回答:1

我编写了使用模拟位置提供程序的单元测试。收到位置更新后,测试将通过;如果超时,则测试将失败。

[该测试通过了在模拟Pixel 3,Android M到Android O上的正常运行。它在Android P和Q上均超时。我还在具有Q的物理Pixel 3上进行了测试,仍然失败。

我一直对此感到头疼,无法弄清楚发生了什么。

测试:

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.location.Location
import android.location.LocationManager
import androidx.test.rule.GrantPermissionRule
import design.inhale.androidtestutils.InstrumentedTest
import design.inhale.datasource.observer.NullDataSourceListener
import design.inhale.testutils.Latch
import design.inhale.utils.locationManager
import design.inhale.utils.mock
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.core.Is
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class DeviceLocationSourceTest: InstrumentedTest() {
    private val mockProviderName = "MockProvider"
    private val locationManager: LocationManager
    get() = appContext.locationManager

    @get:Rule
    val permissionRule: GrantPermissionRule = grant(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION)

    @Before
    fun init() {
        addMockLocationProvider()
    }

    @After
    fun deinit() {
        instrumentation.waitForIdleSync()
        removeMockLocationProvider()
    }

    @Test(timeout = 10_000)
    fun receiveLocationUpdate() {
        val latch = Latch()

        val listener = object: LocationSourceListener {

            override fun onDataUpdated(data: Location) {
                with(data) {
                    assertThat(latitude, Is(equalTo(0.0)))
                    assertThat(longitude, Is(equalTo(0.0)))
                }

                latch.release()
            }
        }

        mockLocationSource(listener).start()
        instrumentation.waitForIdleSync() // in case we're hitting race conditions?

        updateMockLocation(0.0, 0.0)

        latch.await()
    }

    @Suppress("SameParameterValue")
    private fun updateMockLocation(latitude: Double, longitude: Double) {
        val location = Location(mockProviderName).mock(latitude, longitude)
        locationManager.setTestProviderLocation(mockProviderName, location)
    }

    private fun mockLocationSource(listener: LocationSourceListener = NullDataSourceListener()) =
            DeviceLocationSource(appContext, mockProviderName, listener)

    private fun addMockLocationProvider() {
        with(locationManager) {
            try {
                addTestProvider(
                        mockProviderName,
                        false,
                        false,
                        false,
                        false,
                        true,
                        true,
                        true,
                        0,
                        5)
            } catch (e: IllegalArgumentException) {
                // If this is caught, the mock provider already exists
            }

            setTestProviderEnabled(mockProviderName, true)
        }
    }

    private fun removeMockLocationProvider() = locationManager.removeTestProvider(mockProviderName)
}

清单:

<manifest package="design.inhale.locationapi"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
</manifest>

androidTest / Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    package="design.inhale.locationapi"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>

</manifest>

Location.mock():

@SuppressLint("ObsoleteSdkInt")
fun Location.mock(latitude: Double = 0.0, longitude: Double = 0.0, accuracy: Float = 0f): Location {

    this.latitude = latitude
    this.longitude = longitude
    this.accuracy = accuracy
    this.time = currentTimeMillis()

    if (SDK_INT > JELLY_BEAN) this.elapsedRealtimeNanos = elapsedRealtimeNanos()

    return this
}
android unit-testing testing mocking location
1个回答
0
投票

结果是我需要添加后台位置访问权限(我认为测试算作是在后台运行?)。>

向清单添加了权限。

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>`

并更改了测试以授予它:

val permissionRule: GrantPermissionRule = grant(ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, ACCESS_BACKGROUND_LOCATION)
© www.soinside.com 2019 - 2024. All rights reserved.