Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / locationprovider / src / test / java / com / android / location / provider / LocationStubsTest.kt

LocationStubsTest.kt95 lines · 3.8 KiBRaw
1package com.android.location.provider
2
3import android.content.Context
4import android.location.Address
5import android.location.GeocoderParams
6import android.location.Location
7import android.os.WorkSource
8import androidx.test.core.app.ApplicationProvider
9import kotlin.test.Test
10import kotlin.test.assertEquals
11import kotlin.test.assertFailsWith
12import kotlin.test.assertNull
13import kotlin.test.assertTrue
14import org.junit.runner.RunWith
15import org.robolectric.RobolectricTestRunner
16
17/**
18 * Pins the compile-only stub contract: stub bodies throw, no-op bodies do
19 * nothing, and the abstract provider bases can be subclassed exactly the way
20 * the real platform classes require. These classes are never packaged; at
21 * runtime the framework's own implementations replace them.
22 */
23@RunWith(RobolectricTestRunner::class)
24class LocationStubsTest {
25
26 private val context = ApplicationProvider.getApplicationContext<Context>()
27
28 private class TestGeocodeProvider : GeocodeProvider() {
29 override fun onGetFromLocation(
30 latitude: Double, longitude: Double, maxResults: Int,
31 params: GeocoderParams?, addrs: List<Address>,
32 ): String? = null
33
34 override fun onGetFromLocationName(
35 locationName: String,
36 lowerLeftLatitude: Double, lowerLeftLongitude: Double,
37 upperRightLatitude: Double, upperRightLongitude: Double,
38 maxResults: Int, params: GeocoderParams?, addrs: List<Address>,
39 ): String = "no results"
40 }
41
42 private class TestLocationProvider(context: Context) :
43 LocationProviderBase(context, "test", null) {
44 var lastRequest: ProviderRequestUnbundled? = null
45 override fun onSetRequest(request: ProviderRequestUnbundled, source: WorkSource) {
46 lastRequest = request
47 }
48 }
49
50 @Test
51 fun geocodeProviderSubclassContractAndStubBinder() {
52 val provider = TestGeocodeProvider()
53 assertNull(provider.onGetFromLocation(1.0, 2.0, 3, null, mutableListOf()))
54 assertEquals(
55 "no results",
56 provider.onGetFromLocationName("x", 0.0, 0.0, 1.0, 1.0, 1, null, mutableListOf()),
57 )
58 val ex = assertFailsWith<RuntimeException> { provider.binder }
59 assertEquals("stub", ex.message)
60 }
61
62 @Test
63 fun locationProviderBaseNoOpsAndSubclassHook() {
64 val provider = TestLocationProvider(context)
65 // No-op surface: none of these throw or change observable state.
66 provider.setEnabled(true)
67 provider.setEnabled(false)
68 provider.setProperties(null)
69 provider.reportLocation(Location("test"))
70 provider.reportLocations(listOf(Location("test")))
71 val ex = assertFailsWith<RuntimeException> { provider.binder }
72 assertEquals("stub", ex.message)
73 // The two-arg constructor form also exists for platform parity.
74 val bare = object : LocationProviderBase("tag", null) {
75 override fun onSetRequest(request: ProviderRequestUnbundled, source: WorkSource) = Unit
76 }
77 bare.setEnabled(true)
78 assertTrue(true)
79 }
80
81 @Test
82 fun propertiesAndRequestFactoriesAreStubs() {
83 val ex = assertFailsWith<RuntimeException> {
84 ProviderPropertiesUnbundled.create(
85 true, false, false, false, true, true, true, 1, 2,
86 )
87 }
88 assertEquals("stub", ex.message)
89 // ProviderRequestUnbundled has no accessible constructor (private, like
90 // the real class): its stub accessors are exercised only by the platform
91 // at runtime, so the type's presence is the contract pinned here.
92 assertEquals("com.android.location.provider.ProviderRequestUnbundled", ProviderRequestUnbundled::class.java.name)
93 assertEquals("android.location.GeocoderParams", GeocoderParams::class.java.name)
94 }
95}