-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(android.NearbyTransitPage): leave / rejoin vehicles channel afte…
…r backgrounding (#579) * feat(android.NearbyTransitPage): leave / rejoin vehicles channel after backgrounding * feat(android.ContentView): Stop / start socket after backgrounding * WIP * refactor(StopDetailsFilter): RouteDirection interface * test(subscribeToVehicles): add test for connecting / disconnecting * test: backgrounding / restoring * cleanup: remove debug logs * fix(subscribeToVehiclesTest): waitUntil so not flaky * fix(ContentViewTests): set contentVM
- Loading branch information
1 parent
f8aaf69
commit a264d30
Showing
13 changed files
with
350 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...oidTest/java/com/mbta/tid/mbta_app/android/nearbyTransit/NearbyTransitTabViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.mbta.tid.mbta_app.android.nearbyTransit | ||
|
||
import com.mbta.tid.mbta_app.model.ObjectCollectionBuilder | ||
import com.mbta.tid.mbta_app.model.PatternsByStop | ||
import com.mbta.tid.mbta_app.model.StopDetailsDepartures | ||
import com.mbta.tid.mbta_app.model.StopDetailsFilter | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertNull | ||
import org.junit.Test | ||
|
||
class NearbyTransitTabViewModelTest { | ||
|
||
@Test | ||
fun testSetStopDetailsFilter() { | ||
val vm = NearbyTransitTabViewModel() | ||
val newFilter = StopDetailsFilter("route_1", 1) | ||
|
||
assertNull(vm.stopDetailsFilter.value) | ||
|
||
vm.setStopDetailsFilter(newFilter) | ||
assertEquals(newFilter, vm.stopDetailsFilter.value) | ||
} | ||
|
||
@Test | ||
fun testSetStopDetailsDepartures() { | ||
val vm = NearbyTransitTabViewModel() | ||
val objectCollectionBuilder = ObjectCollectionBuilder() | ||
val route = objectCollectionBuilder.route {} | ||
val stop = objectCollectionBuilder.stop {} | ||
|
||
val departures = StopDetailsDepartures(listOf(PatternsByStop(route, stop, emptyList()))) | ||
|
||
assertNull(vm.stopDetailsDepartures.value) | ||
|
||
vm.setStopDetailsDepartures(departures) | ||
assertEquals(departures, vm.stopDetailsDepartures.value) | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...idApp/src/androidTest/java/com/mbta/tid/mbta_app/android/state/SubscribeToVehiclesTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package com.mbta.tid.mbta_app.android.state | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.compose.ui.test.runComposeUiTest | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.compose.LocalLifecycleOwner | ||
import androidx.lifecycle.testing.TestLifecycleOwner | ||
import com.mbta.tid.mbta_app.model.ObjectCollectionBuilder | ||
import com.mbta.tid.mbta_app.model.StopDetailsFilter | ||
import com.mbta.tid.mbta_app.model.Vehicle | ||
import com.mbta.tid.mbta_app.model.response.VehiclesStreamDataResponse | ||
import com.mbta.tid.mbta_app.repositories.MockVehiclesRepository | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
@OptIn(ExperimentalTestApi::class) | ||
class SubscribeToVehiclesTest { | ||
@get:Rule val composeTestRule = createAndroidComposeRule<ComponentActivity>() | ||
|
||
// Test structure based on lifecycle tests: | ||
// https://github.com/androidx/androidx/blob/0b709f31b71110fe671ed8b4c03f96ad30a0cd37/lifecycle/lifecycle-runtime-compose/src/androidInstrumentedTest/kotlin/androidx/lifecycle/compose/LifecycleEffectTest.kt#L247 | ||
@Test | ||
fun testSubscribeToVehicles() = runComposeUiTest { | ||
val vehicle = | ||
ObjectCollectionBuilder().vehicle { currentStatus = Vehicle.CurrentStatus.StoppedAt } | ||
var connectProps: Pair<String, Int>? = null | ||
|
||
val vehiclesRepo = | ||
MockVehiclesRepository( | ||
VehiclesStreamDataResponse(mapOf(vehicle.id to vehicle)), | ||
onConnect = { routeId, directionId -> connectProps = Pair(routeId, directionId) } | ||
) | ||
|
||
var vehicles: List<Vehicle> = emptyList() | ||
|
||
var stateFilter = mutableStateOf(StopDetailsFilter("route_1", 1)) | ||
|
||
setContent { | ||
var filter by remember { stateFilter } | ||
vehicles = subscribeToVehicles(filter, vehiclesRepo) | ||
} | ||
|
||
waitUntil { connectProps == Pair("route_1", 1) } | ||
waitUntil { listOf(vehicle) == vehicles } | ||
|
||
runOnUiThread { stateFilter.value = StopDetailsFilter("route_2", 1) } | ||
waitUntil { connectProps == Pair("route_2", 1) } | ||
} | ||
|
||
@Test | ||
fun testDisconnectsOnPause() = runComposeUiTest { | ||
val lifecycleOwner = TestLifecycleOwner(Lifecycle.State.RESUMED) | ||
|
||
val vehicle = | ||
ObjectCollectionBuilder().vehicle { currentStatus = Vehicle.CurrentStatus.StoppedAt } | ||
var connectCount = 0 | ||
var disconnectCount = 0 | ||
|
||
val vehiclesRepo = | ||
MockVehiclesRepository( | ||
VehiclesStreamDataResponse(mapOf(vehicle.id to vehicle)), | ||
onConnect = { _, _ -> connectCount += 1 }, | ||
onDisconnect = { disconnectCount += 1 } | ||
) | ||
|
||
var vehicles: List<Vehicle> = emptyList() | ||
|
||
var stateFilter = mutableStateOf(StopDetailsFilter("route_1", 1)) | ||
|
||
setContent { | ||
CompositionLocalProvider(LocalLifecycleOwner provides lifecycleOwner) { | ||
var filter by remember { stateFilter } | ||
vehicles = subscribeToVehicles(filter, vehiclesRepo) | ||
} | ||
} | ||
|
||
waitUntil { connectCount == 1 } | ||
// Disconnect called before connecting | ||
assertEquals(1, disconnectCount) | ||
|
||
runOnIdle { lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) } | ||
|
||
waitUntil { disconnectCount == 2 } | ||
assertEquals(2, disconnectCount) | ||
assertEquals(1, connectCount) | ||
|
||
runOnIdle { lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME) } | ||
|
||
waitUntil { disconnectCount == 3 } | ||
assertEquals(2, connectCount) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...pp/src/main/java/com/mbta/tid/mbta_app/android/nearbyTransit/NearbyTransitTabViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.mbta.tid.mbta_app.android.nearbyTransit | ||
|
||
import androidx.lifecycle.ViewModel | ||
import com.mbta.tid.mbta_app.model.StopDetailsDepartures | ||
import com.mbta.tid.mbta_app.model.StopDetailsFilter | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
class NearbyTransitTabViewModel : ViewModel() { | ||
|
||
private val _stopDetailsFilter = MutableStateFlow<StopDetailsFilter?>(null) | ||
val stopDetailsFilter: StateFlow<StopDetailsFilter?> = _stopDetailsFilter | ||
|
||
private val _stopDetailsDepartures = MutableStateFlow<StopDetailsDepartures?>(null) | ||
val stopDetailsDepartures: StateFlow<StopDetailsDepartures?> = _stopDetailsDepartures | ||
|
||
fun setStopDetailsFilter(filter: StopDetailsFilter?) { | ||
_stopDetailsFilter.value = filter | ||
} | ||
|
||
fun setStopDetailsDepartures(departures: StopDetailsDepartures?) { | ||
_stopDetailsDepartures.value = departures | ||
} | ||
} |
Oops, something went wrong.