Skip to content

Commit be0ae90

Browse files
committed
fix(android): fix copy-paste bug in LocationStateHolderTest, improve ServiceEventBusTest
1 parent c90d171 commit be0ae90

2 files changed

Lines changed: 104 additions & 3 deletions

File tree

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
package org.onebusaway.vehiclepositions.util
22

3+
import android.location.Location
4+
import org.junit.Assert.assertFalse
35
import org.junit.Assert.assertNull
6+
import org.junit.Assert.assertTrue
7+
import org.junit.Before
48
import org.junit.Test
9+
import org.mockito.Mockito.mock
10+
import org.mockito.Mockito.`when`
511

612
class LocationStateHolderTest {
713

8-
private val holder = LocationStateHolder()
14+
private lateinit var holder: LocationStateHolder
15+
16+
@Before
17+
fun setup() {
18+
holder = LocationStateHolder()
19+
}
920

1021
@Test
1122
fun `initial location is null`() {
@@ -14,6 +25,26 @@ class LocationStateHolderTest {
1425

1526
@Test
1627
fun `hasLocation returns false when no location set`() {
17-
assertNull(holder.lastLocation.value)
28+
assertFalse(holder.hasLocation())
29+
}
30+
31+
@Test
32+
fun `hasLocation returns true after location updated`() {
33+
val mockLocation = mock(Location::class.java)
34+
35+
holder.updateLocation(mockLocation)
36+
assertTrue(holder.hasLocation())
37+
}
38+
39+
@Test
40+
fun `lastLocation value updated after updateLocation`() {
41+
val mockLocation = mock(Location::class.java)
42+
`when`(mockLocation.latitude).thenReturn(1.2921)
43+
`when`(mockLocation.longitude).thenReturn(36.8219)
44+
45+
holder.updateLocation(mockLocation)
46+
47+
assertTrue(holder.lastLocation.value?.latitude == 1.2921)
48+
assertTrue(holder.lastLocation.value?.longitude == 36.8219)
1849
}
1950
}
Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,83 @@
11
package org.onebusaway.vehiclepositions.util
22

3+
import kotlinx.coroutines.flow.toList
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
6+
import kotlinx.coroutines.test.runTest
7+
import org.junit.Assert.assertEquals
38
import org.junit.Assert.assertNotNull
9+
import org.junit.Before
410
import org.junit.Test
511

612
class ServiceEventBusTest {
713

14+
private lateinit var bus: ServiceEventBus
15+
16+
@Before
17+
fun setup() {
18+
bus = ServiceEventBus()
19+
}
20+
821
@Test
922
fun `ServiceEventBus initializes without error`() {
10-
val bus = ServiceEventBus()
1123
assertNotNull(bus)
1224
}
25+
26+
@Test
27+
fun `emitting StopShift event is received by collector`() = runTest {
28+
val events = mutableListOf<ServiceEvent>()
29+
30+
// starts listening in the background before emitting
31+
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
32+
bus.events.toList(events)
33+
}
34+
35+
bus.emitStopShift()
36+
37+
assertEquals(ServiceEvent.StopShift, events.first())
38+
}
39+
40+
@Test
41+
fun `emitting NavigateToLogin event is received by collector`() = runTest {
42+
val events = mutableListOf<ServiceEvent>()
43+
44+
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
45+
bus.events.toList(events)
46+
}
47+
48+
bus.emitNavigateToLogin()
49+
50+
assertEquals(ServiceEvent.NavigateToLogin, events.first())
51+
}
52+
53+
@Test
54+
fun `emitting LocationPermissionRevoked event is received by collector`() = runTest {
55+
val events = mutableListOf<ServiceEvent>()
56+
57+
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
58+
bus.events.toList(events)
59+
}
60+
61+
bus.emitLocationPermissionRevoked()
62+
63+
assertEquals(ServiceEvent.LocationPermissionRevoked, events.first())
64+
}
65+
66+
@Test
67+
fun `multiple events are received in order`() = runTest {
68+
val events = mutableListOf<ServiceEvent>()
69+
70+
backgroundScope.launch(UnconfinedTestDispatcher(testScheduler)) {
71+
bus.events.toList(events)
72+
}
73+
74+
bus.emitStopShift()
75+
bus.emitNavigateToLogin()
76+
bus.emitLocationPermissionRevoked()
77+
78+
assertEquals(3, events.size)
79+
assertEquals(ServiceEvent.StopShift, events[0])
80+
assertEquals(ServiceEvent.NavigateToLogin, events[1])
81+
assertEquals(ServiceEvent.LocationPermissionRevoked, events[2])
82+
}
1383
}

0 commit comments

Comments
 (0)