Skip to content

Commit c9f8003

Browse files
Add more tests
1 parent dd99226 commit c9f8003

File tree

3 files changed

+220
-0
lines changed

3 files changed

+220
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream License;
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.getstream.android.core.api.components
17+
18+
import android.content.Context
19+
import android.net.ConnectivityManager
20+
import android.net.wifi.WifiManager
21+
import android.os.Build
22+
import android.telephony.TelephonyManager
23+
import androidx.test.core.app.ApplicationProvider
24+
import kotlin.test.BeforeTest
25+
import kotlin.test.Test
26+
import kotlin.test.assertEquals
27+
import kotlin.test.assertTrue
28+
import org.junit.runner.RunWith
29+
import org.robolectric.RobolectricTestRunner
30+
import org.robolectric.annotation.Config
31+
32+
@RunWith(RobolectricTestRunner::class)
33+
@Config(sdk = [Build.VERSION_CODES.VANILLA_ICE_CREAM])
34+
internal class StreamAndroidComponentsProviderLatestApiTest {
35+
36+
private lateinit var context: Context
37+
private lateinit var provider: StreamAndroidComponentsProvider
38+
39+
@BeforeTest
40+
fun setUp() {
41+
context = ApplicationProvider.getApplicationContext()
42+
provider = StreamAndroidComponentsProvider(context)
43+
}
44+
45+
@Test
46+
fun `connectivity manager is returned from application context`() {
47+
val expected = context.applicationContext.getSystemService(ConnectivityManager::class.java)
48+
val result = provider.connectivityManager()
49+
50+
assertTrue(result.isSuccess)
51+
assertEquals(expected, result.getOrNull())
52+
}
53+
54+
@Test
55+
fun `wifi manager is returned from application context`() {
56+
val expected = context.applicationContext.getSystemService(WifiManager::class.java)
57+
val result = provider.wifiManager()
58+
59+
assertTrue(result.isSuccess)
60+
assertEquals(expected, result.getOrNull())
61+
}
62+
63+
@Test
64+
fun `telephony manager is returned from application context`() {
65+
val expected = context.applicationContext.getSystemService(TelephonyManager::class.java)
66+
val result = provider.telephonyManager()
67+
68+
assertTrue(result.isSuccess)
69+
assertEquals(expected, result.getOrNull())
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream License;
5+
* You may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.getstream.android.core.api.components
17+
18+
import android.content.Context
19+
import android.net.ConnectivityManager
20+
import android.net.wifi.WifiManager
21+
import android.os.Build
22+
import android.telephony.TelephonyManager
23+
import androidx.test.core.app.ApplicationProvider
24+
import kotlin.test.BeforeTest
25+
import kotlin.test.Test
26+
import kotlin.test.assertEquals
27+
import kotlin.test.assertTrue
28+
import org.junit.runner.RunWith
29+
import org.robolectric.RobolectricTestRunner
30+
import org.robolectric.annotation.Config
31+
32+
@RunWith(RobolectricTestRunner::class)
33+
@Config(sdk = [Build.VERSION_CODES.LOLLIPOP])
34+
internal class StreamAndroidComponentsProviderLegacyApiTest {
35+
36+
private lateinit var context: Context
37+
private lateinit var provider: StreamAndroidComponentsProvider
38+
39+
@BeforeTest
40+
fun setUp() {
41+
context = ApplicationProvider.getApplicationContext()
42+
provider = StreamAndroidComponentsProvider(context)
43+
}
44+
45+
@Test
46+
fun `connectivity manager is obtained via legacy service lookup`() {
47+
val expected =
48+
context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE)
49+
as ConnectivityManager
50+
val result = provider.connectivityManager()
51+
52+
assertTrue(result.isSuccess)
53+
assertEquals(expected, result.getOrNull())
54+
}
55+
56+
@Test
57+
fun `wifi manager is obtained via legacy service lookup`() {
58+
val expected =
59+
context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
60+
val result = provider.wifiManager()
61+
62+
assertTrue(result.isSuccess)
63+
assertEquals(expected, result.getOrNull())
64+
}
65+
66+
@Test
67+
fun `telephony manager is obtained via legacy service lookup`() {
68+
val expected =
69+
context.applicationContext.getSystemService(Context.TELEPHONY_SERVICE)
70+
as TelephonyManager
71+
val result = provider.telephonyManager()
72+
73+
assertTrue(result.isSuccess)
74+
assertEquals(expected, result.getOrNull())
75+
}
76+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+
*
4+
* Licensed under the Stream License;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://github.com/GetStream/stream-core-android/blob/main/LICENSE
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.getstream.android.core.api.observers.network
17+
18+
import io.getstream.android.core.api.model.connection.network.StreamNetworkInfo
19+
import kotlin.test.Test
20+
import kotlin.test.assertEquals
21+
import kotlin.test.assertTrue
22+
import kotlinx.coroutines.ExperimentalCoroutinesApi
23+
import kotlinx.coroutines.test.runTest
24+
import kotlin.test.assertContentEquals
25+
import kotlin.time.ExperimentalTime
26+
27+
@OptIn(ExperimentalCoroutinesApi::class, ExperimentalTime::class)
28+
internal class StreamNetworkMonitorListenerTest {
29+
30+
@Test
31+
fun `default listener implementations no-op`() = runTest {
32+
val listener = object : StreamNetworkMonitorListener {}
33+
34+
listener.onNetworkConnected(null)
35+
listener.onNetworkLost()
36+
listener.onNetworkPropertiesChanged(
37+
StreamNetworkInfo.Snapshot(transports = emptySet()),
38+
)
39+
}
40+
41+
@Test
42+
fun `overrides receive the expected payloads`() = runTest {
43+
val snapshots = mutableListOf<StreamNetworkInfo.Snapshot?>()
44+
var lostFlag: Boolean? = null
45+
46+
val listener =
47+
object : StreamNetworkMonitorListener {
48+
override suspend fun onNetworkConnected(snapshot: StreamNetworkInfo.Snapshot?) {
49+
snapshots += snapshot
50+
}
51+
52+
override suspend fun onNetworkLost(permanent: Boolean) {
53+
lostFlag = permanent
54+
}
55+
56+
override suspend fun onNetworkPropertiesChanged(
57+
snapshot: StreamNetworkInfo.Snapshot
58+
) {
59+
snapshots += snapshot
60+
}
61+
}
62+
63+
val connected = StreamNetworkInfo.Snapshot(transports = emptySet())
64+
val updated = connected.copy(priority = StreamNetworkInfo.PriorityHint.LATENCY)
65+
66+
listener.onNetworkConnected(connected)
67+
listener.onNetworkPropertiesChanged(updated)
68+
listener.onNetworkLost(permanent = true)
69+
70+
assertContentEquals(listOf(connected, updated), snapshots)
71+
assertTrue(lostFlag == true)
72+
}
73+
}

0 commit comments

Comments
 (0)