|
| 1 | +package com.mapbox.navigation.base.internal.extensions |
| 2 | + |
| 3 | +import com.mapbox.navigation.base.internal.route.Waypoint |
| 4 | +import io.mockk.every |
| 5 | +import io.mockk.mockk |
| 6 | +import junit.framework.Assert.assertEquals |
| 7 | +import junit.framework.Assert.assertTrue |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import org.junit.runners.Parameterized |
| 11 | + |
| 12 | +class WaypointExTest { |
| 13 | + |
| 14 | + @RunWith(Parameterized::class) |
| 15 | + class FilterTest( |
| 16 | + private val waypoints: List<Waypoint>, |
| 17 | + private val requestedWaypointsExpected: List<@Waypoint.Type Int>, |
| 18 | + private val legsWaypointsExpected: List<@Waypoint.Type Int> |
| 19 | + ) { |
| 20 | + companion object { |
| 21 | + @JvmStatic |
| 22 | + @Parameterized.Parameters |
| 23 | + fun data() = listOf( |
| 24 | + arrayOf( |
| 25 | + provideWaypoints(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR), |
| 26 | + listOf(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR), |
| 27 | + listOf(Waypoint.REGULAR, Waypoint.REGULAR), |
| 28 | + ), |
| 29 | + arrayOf( |
| 30 | + provideWaypoints( |
| 31 | + Waypoint.REGULAR, |
| 32 | + Waypoint.SILENT, |
| 33 | + Waypoint.EV_CHARGING, |
| 34 | + Waypoint.SILENT, |
| 35 | + Waypoint.REGULAR |
| 36 | + ), |
| 37 | + listOf(Waypoint.REGULAR, Waypoint.SILENT, Waypoint.SILENT, Waypoint.REGULAR), |
| 38 | + listOf(Waypoint.REGULAR, Waypoint.EV_CHARGING, Waypoint.REGULAR), |
| 39 | + ), |
| 40 | + arrayOf( |
| 41 | + provideWaypoints( |
| 42 | + Waypoint.REGULAR, |
| 43 | + Waypoint.EV_CHARGING, |
| 44 | + Waypoint.EV_CHARGING, |
| 45 | + Waypoint.EV_CHARGING, |
| 46 | + Waypoint.REGULAR |
| 47 | + ), |
| 48 | + listOf(Waypoint.REGULAR, Waypoint.REGULAR), |
| 49 | + listOf( |
| 50 | + Waypoint.REGULAR, |
| 51 | + Waypoint.EV_CHARGING, |
| 52 | + Waypoint.EV_CHARGING, |
| 53 | + Waypoint.EV_CHARGING, |
| 54 | + Waypoint.REGULAR |
| 55 | + ), |
| 56 | + ), |
| 57 | + arrayOf( |
| 58 | + provideWaypoints( |
| 59 | + Waypoint.REGULAR, |
| 60 | + Waypoint.SILENT, |
| 61 | + Waypoint.REGULAR, |
| 62 | + Waypoint.REGULAR, |
| 63 | + Waypoint.EV_CHARGING, |
| 64 | + Waypoint.REGULAR |
| 65 | + ), |
| 66 | + listOf( |
| 67 | + Waypoint.REGULAR, |
| 68 | + Waypoint.SILENT, |
| 69 | + Waypoint.REGULAR, |
| 70 | + Waypoint.REGULAR, |
| 71 | + Waypoint.REGULAR |
| 72 | + ), |
| 73 | + listOf( |
| 74 | + Waypoint.REGULAR, |
| 75 | + Waypoint.REGULAR, |
| 76 | + Waypoint.REGULAR, |
| 77 | + Waypoint.EV_CHARGING, |
| 78 | + Waypoint.REGULAR |
| 79 | + ), |
| 80 | + ), |
| 81 | + ) |
| 82 | + |
| 83 | + fun checkWaypoints( |
| 84 | + expectedWaypoints: List<@Waypoint.Type Int>, |
| 85 | + modified: List<Waypoint>, |
| 86 | + original: List<Waypoint>, |
| 87 | + ) { |
| 88 | + |
| 89 | + assertEquals(expectedWaypoints.size, modified.size) |
| 90 | + var bufferIndex = -1 |
| 91 | + modified.forEachIndexed { index, waypoint -> |
| 92 | + assertEquals(expectedWaypoints[index], waypoint.type) |
| 93 | + assertTrue(original.contains(waypoint)) |
| 94 | + val idx = original.indexOf(waypoint) |
| 95 | + assertTrue(idx > bufferIndex) |
| 96 | + bufferIndex = idx |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun testCases() { |
| 103 | + checkWaypoints(requestedWaypointsExpected, waypoints.requestedWaypoints(), waypoints) |
| 104 | + checkWaypoints(legsWaypointsExpected, waypoints.legsWaypoints(), waypoints) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @RunWith(Parameterized::class) |
| 109 | + class IndexOfNextCoordinateTest( |
| 110 | + private val testDescription: String, |
| 111 | + private val waypoints: List<Waypoint>, |
| 112 | + private val remainingWaypoints: Int, |
| 113 | + private val expectedIndex: Int? |
| 114 | + ) { |
| 115 | + |
| 116 | + companion object { |
| 117 | + @JvmStatic |
| 118 | + @Parameterized.Parameters(name = "{0}") |
| 119 | + fun data() = listOf( |
| 120 | + arrayOf( |
| 121 | + "Next index: 1 for 2 relevant waypoints and remaining waypoint 1", |
| 122 | + provideWaypoints( |
| 123 | + Waypoint.REGULAR, Waypoint.REGULAR |
| 124 | + ), |
| 125 | + 1, |
| 126 | + 1, |
| 127 | + ), |
| 128 | + arrayOf( |
| 129 | + "Next index: 1 for 3 relevant waypoints and remaining waypoint 2", |
| 130 | + provideWaypoints( |
| 131 | + Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR |
| 132 | + ), |
| 133 | + 2, |
| 134 | + 1, |
| 135 | + ), |
| 136 | + arrayOf( |
| 137 | + "Next index: 2 for 3 relevant waypoints and remaining waypoint 1", |
| 138 | + provideWaypoints( |
| 139 | + Waypoint.REGULAR, Waypoint.SILENT, Waypoint.REGULAR |
| 140 | + ), |
| 141 | + 1, |
| 142 | + 2, |
| 143 | + ), |
| 144 | + arrayOf( |
| 145 | + "Next index: 3 for 4 relevant waypoints and remaining waypoint 1", |
| 146 | + provideWaypoints( |
| 147 | + Waypoint.REGULAR, Waypoint.SILENT, Waypoint.SILENT, Waypoint.REGULAR |
| 148 | + ), |
| 149 | + 1, |
| 150 | + 3, |
| 151 | + ), |
| 152 | + arrayOf( |
| 153 | + "Next index: 1 for 2 relevant waypoints (1 is EV) and remaining waypoint 2", |
| 154 | + provideWaypoints( |
| 155 | + Waypoint.REGULAR, Waypoint.EV_CHARGING, Waypoint.REGULAR |
| 156 | + ), |
| 157 | + 2, |
| 158 | + 1, |
| 159 | + ), |
| 160 | + arrayOf( |
| 161 | + "Next index: 1 for 3 relevant waypoints (2 is EV) and remaining waypoint 4", |
| 162 | + provideWaypoints( |
| 163 | + Waypoint.REGULAR, |
| 164 | + Waypoint.EV_CHARGING, |
| 165 | + Waypoint.SILENT, |
| 166 | + Waypoint.EV_CHARGING, |
| 167 | + Waypoint.REGULAR |
| 168 | + ), |
| 169 | + 4, |
| 170 | + 1, |
| 171 | + ), |
| 172 | + arrayOf( |
| 173 | + "Next index: 1 for 3 relevant waypoints (2 is EV) and remaining waypoint 2", |
| 174 | + provideWaypoints( |
| 175 | + Waypoint.REGULAR, |
| 176 | + Waypoint.EV_CHARGING, |
| 177 | + Waypoint.SILENT, |
| 178 | + Waypoint.EV_CHARGING, |
| 179 | + Waypoint.REGULAR |
| 180 | + ), |
| 181 | + 2, |
| 182 | + 2, |
| 183 | + ), |
| 184 | + arrayOf( |
| 185 | + "Next index: null for non-valid case - 3 relevant waypoints (2 is EV) and " + |
| 186 | + "remaining waypoint 7", |
| 187 | + provideWaypoints( |
| 188 | + Waypoint.REGULAR, |
| 189 | + Waypoint.EV_CHARGING, |
| 190 | + Waypoint.SILENT, |
| 191 | + Waypoint.EV_CHARGING, |
| 192 | + Waypoint.REGULAR |
| 193 | + ), |
| 194 | + 7, |
| 195 | + null, |
| 196 | + ), |
| 197 | + arrayOf( |
| 198 | + "Next index: 0 for 3 relevant waypoints (2 is EV) and remaining waypoint 5", |
| 199 | + provideWaypoints( |
| 200 | + Waypoint.REGULAR, |
| 201 | + Waypoint.EV_CHARGING, |
| 202 | + Waypoint.SILENT, |
| 203 | + Waypoint.EV_CHARGING, |
| 204 | + Waypoint.REGULAR |
| 205 | + ), |
| 206 | + 5, |
| 207 | + 0, |
| 208 | + ), |
| 209 | + ) |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + fun testCases() { |
| 214 | + assertEquals( |
| 215 | + testDescription, |
| 216 | + expectedIndex, |
| 217 | + indexOfNextCoordinate(waypoints, remainingWaypoints) |
| 218 | + ) |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +private fun provideWaypoints(@Waypoint.Type vararg waypointType: Int): List<Waypoint> = |
| 224 | + waypointType.map { mapToType -> |
| 225 | + mockk { every { type } returns mapToType } |
| 226 | + } |
0 commit comments