Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / appstore / src / test / java / com / vayunmathur / appstore / RouteSerializerTest.kt
RouteSerializerTest.kt85 lines · 3.1 KiBRaw
| 1 | package com.vayunmathur.appstore |
| 2 | import com.vayunmathur.appstore.ui.tv.TvStoreRoute |
| 3 | |
| 4 | import kotlin.test.Test |
| 5 | import kotlin.test.assertEquals |
| 6 | import kotlin.test.assertIs |
| 7 | import kotlinx.serialization.json.Json |
| 8 | |
| 9 | /** |
| 10 | * Navigation keys survive serialization: the back stack is saved and |
| 11 | * restored through these serializers, so every destination — objects |
| 12 | * and the argument-carrying classes, on both phone and TV — must |
| 13 | * round-trip to an equal key. |
| 14 | */ |
| 15 | class RouteSerializerTest { |
| 16 | |
| 17 | private val json = Json |
| 18 | |
| 19 | @Test |
| 20 | fun `phone routes round trip through their serializer`() { |
| 21 | val browse: Route = Route.Browse("editors-choice") |
| 22 | val restoredBrowse = json.decodeFromString( |
| 23 | Route.serializer(), |
| 24 | json.encodeToString(Route.serializer(), browse), |
| 25 | ) |
| 26 | assertEquals(browse, restoredBrowse) |
| 27 | assertEquals("editors-choice", assertIs<Route.Browse>(restoredBrowse).sectionId) |
| 28 | |
| 29 | val shots: Route = Route.Screenshots("Example", listOf("https://a/1.png", "https://a/2.png"), 1) |
| 30 | val restoredShots = json.decodeFromString( |
| 31 | Route.serializer(), |
| 32 | json.encodeToString(Route.serializer(), shots), |
| 33 | ) |
| 34 | assertEquals(shots, restoredShots) |
| 35 | assertEquals(1, assertIs<Route.Screenshots>(restoredShots).index) |
| 36 | |
| 37 | val detail: Route = Route.Detail("com.example.app") |
| 38 | val restoredDetail = json.decodeFromString( |
| 39 | Route.serializer(), |
| 40 | json.encodeToString(Route.serializer(), detail), |
| 41 | ) |
| 42 | assertEquals(detail, restoredDetail) |
| 43 | assertEquals("com.example.app", assertIs<Route.Detail>(restoredDetail).packageName) |
| 44 | |
| 45 | for (route in listOf<Route>( |
| 46 | Route.Home, Route.Search, Route.Updates, Route.Library, Route.Sources, Route.Trust, |
| 47 | )) { |
| 48 | val restored = json.decodeFromString( |
| 49 | Route.serializer(), |
| 50 | json.encodeToString(Route.serializer(), route), |
| 51 | ) |
| 52 | assertEquals(route, restored) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | @Test |
| 57 | fun `tv routes round trip through their serializer`() { |
| 58 | val detail: TvStoreRoute = TvStoreRoute.Detail("com.example.app") |
| 59 | val restoredDetail = json.decodeFromString( |
| 60 | TvStoreRoute.serializer(), |
| 61 | json.encodeToString(TvStoreRoute.serializer(), detail), |
| 62 | ) |
| 63 | assertEquals(detail, restoredDetail) |
| 64 | assertEquals("com.example.app", assertIs<TvStoreRoute.Detail>(restoredDetail).packageName) |
| 65 | |
| 66 | val browse: TvStoreRoute = TvStoreRoute.Browse("recent") |
| 67 | assertEquals( |
| 68 | browse, |
| 69 | json.decodeFromString( |
| 70 | TvStoreRoute.serializer(), |
| 71 | json.encodeToString(TvStoreRoute.serializer(), browse), |
| 72 | ), |
| 73 | ) |
| 74 | for (route in listOf<TvStoreRoute>( |
| 75 | TvStoreRoute.Home, TvStoreRoute.Search, TvStoreRoute.Updates, |
| 76 | TvStoreRoute.Library, TvStoreRoute.Sources, |
| 77 | )) { |
| 78 | val restored = json.decodeFromString( |
| 79 | TvStoreRoute.serializer(), |
| 80 | json.encodeToString(TvStoreRoute.serializer(), route), |
| 81 | ) |
| 82 | assertEquals(route, restored) |
| 83 | } |
| 84 | } |
| 85 | } |