Skip to content

Commit

Permalink
update scene tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-sts committed Apr 9, 2024
1 parent 695edc2 commit 0204fb1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
21 changes: 18 additions & 3 deletions src/main/kotlin/solve/scene/model/Scene.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ class Scene(
val layers: List<Layer>
get() = frames.firstOrNull()?.layers ?: emptyList()

private val layersStorage = layerSettings.toMutableList()
private val planesLayersStorage = layerSettings.filter { it is LayerSettings.PlaneLayerSettings }.toMutableList()
private val nonPlanesLayersStorage = layerSettings.filterNot {
it is LayerSettings.PlaneLayerSettings
}.toMutableList()

private val changedCallbacks = mutableListOf<() -> Unit>()

Expand All @@ -53,15 +56,27 @@ class Scene(
}

override fun indexOf(element: LayerSettings): Int {
return layersStorage.indexOf(element)
if (element is LayerSettings.PlaneLayerSettings) {
return planesLayersStorage.indexOf(element)
}

if (nonPlanesLayersStorage.contains(element)) {
return planesLayersStorage.size + nonPlanesLayersStorage.indexOf(element)
}

return -1
}

/**
* Changes index of layer in the storage.
* If index is invalid IndexOutOfBoundsException is thrown.
*/
fun changeLayerIndex(element: LayerSettings, index: Int) {
layersStorage.move(element, index)
if (element is LayerSettings.PlaneLayerSettings)
planesLayersStorage.move(element, index)
else
nonPlanesLayersStorage.move(element, index)

changedCallbacks.forEach { it() }
}
}
15 changes: 3 additions & 12 deletions src/test/kotlin/solve/unit/scene/model/SceneTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ internal class SceneTests {
@Test
fun `Changes layer settings if index is not correct`() {
val orderManager = Scene(listOf(), listOf(pointLayerSettings1, pointLayerSettings2))
assertThrows<IndexOutOfBoundsException> { orderManager.changeLayerIndex(pointLayerSettings1, 10) }
assertThrows<IndexOutOfBoundsException> { orderManager.changeLayerIndex(pointLayerSettings1, -1) }
assertThrows<IllegalStateException> { orderManager.changeLayerIndex(pointLayerSettings1, 10) }
assertThrows<IllegalStateException> { orderManager.changeLayerIndex(pointLayerSettings1, -1) }
}

@Test
Expand Down Expand Up @@ -87,15 +87,6 @@ internal class SceneTests {
orderManager.changeLayerIndex(pointLayerSettings1, 1)
assertEquals(3, orderManager.indexOf(pointLayerSettings1))
assertEquals(2, orderManager.indexOf(pointLayerSettings2))
assertThrows<IndexOutOfBoundsException> { orderManager.changeLayerIndex(planeLayerSettings1, 2) }
}

@Test
fun `Canvas layers count`() {
val scene = Scene(
listOf(),
listOf(pointLayerSettings1, pointLayerSettings2, planeLayerSettings1, planeLayerSettings2)
)
assertEquals(2, scene.canvasLayersCount)
assertThrows<IllegalStateException> { orderManager.changeLayerIndex(planeLayerSettings1, 2) }
}
}

0 comments on commit 0204fb1

Please sign in to comment.