From 606e607ebbfc34aceee1175cb1874c79de673032 Mon Sep 17 00:00:00 2001 From: Stanislav Mishchenko Date: Sat, 8 Jun 2024 18:12:16 +0300 Subject: [PATCH] add association manager tests --- .../utils/RenderingTestsOpenGLUtils.kt | 22 ---- .../engine/core/batch/RenderBatchTests.kt | 36 ----- .../association/AssociationManagerTests.kt | 123 ++++++++++++++++++ 3 files changed, 123 insertions(+), 58 deletions(-) delete mode 100644 src/test/kotlin/solve/rendering/utils/RenderingTestsOpenGLUtils.kt delete mode 100644 src/test/kotlin/solve/unit/rendering/engine/core/batch/RenderBatchTests.kt create mode 100644 src/test/kotlin/solve/unit/scene/view/association/AssociationManagerTests.kt diff --git a/src/test/kotlin/solve/rendering/utils/RenderingTestsOpenGLUtils.kt b/src/test/kotlin/solve/rendering/utils/RenderingTestsOpenGLUtils.kt deleted file mode 100644 index 916c664a..00000000 --- a/src/test/kotlin/solve/rendering/utils/RenderingTestsOpenGLUtils.kt +++ /dev/null @@ -1,22 +0,0 @@ -package solve.rendering.utils - -import com.huskerdev.openglfx.canvas.GLCanvas -import com.huskerdev.openglfx.canvas.GLCanvasAnimator -import com.huskerdev.openglfx.lwjgl.LWJGLExecutor -import org.lwjgl.opengl.GL.createCapabilities - -internal fun runInOpenGLContext(action: () -> Unit) { - val openGLCanvas = GLCanvas(LWJGLExecutor.LWJGL_MODULE) - openGLCanvas.animator = GLCanvasAnimator(60.0) - openGLCanvas.addOnInitEvent { - createCapabilities() - } - var isActionRan = false - openGLCanvas.addOnRenderEvent { - if (isActionRan) { - return@addOnRenderEvent - } - - action() - } -} diff --git a/src/test/kotlin/solve/unit/rendering/engine/core/batch/RenderBatchTests.kt b/src/test/kotlin/solve/unit/rendering/engine/core/batch/RenderBatchTests.kt deleted file mode 100644 index 0f021801..00000000 --- a/src/test/kotlin/solve/unit/rendering/engine/core/batch/RenderBatchTests.kt +++ /dev/null @@ -1,36 +0,0 @@ -package solve.unit.rendering.engine.core.batch - -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.ExtendWith -import org.testfx.framework.junit5.ApplicationExtension -import solve.constants.IconsCatalogueApplyPath -import solve.interactive.InteractiveTestClass -import solve.rendering.engine.core.batch.PrimitiveType -import solve.rendering.engine.core.batch.RenderBatch -import solve.rendering.engine.core.texture.Texture2D -import solve.rendering.utils.runInOpenGLContext -import solve.utils.getResourceAbsolutePath - -@ExtendWith(ApplicationExtension::class) -internal class RenderBatchTests : InteractiveTestClass() { - @Test - fun `Fully fills a render batch with textures`() { - runInOpenGLContext { - val renderBatch = RenderBatch(1000, 0, PrimitiveType.Quad, emptyList()) - assertEquals(false, renderBatch.isTexturesFull) - repeat(RenderBatch.MaxTexturesNumber - 1) { - renderBatch.addTexture(createTestTexture()) - assertEquals(false, renderBatch.isTexturesFull) - } - renderBatch.addTexture(createTestTexture()) - assertEquals(true, renderBatch.isTexturesFull) - } - } - - companion object { - fun createTestTexture(): Texture2D { - return Texture2D(getResourceAbsolutePath(IconsCatalogueApplyPath).toString()) - } - } -} diff --git a/src/test/kotlin/solve/unit/scene/view/association/AssociationManagerTests.kt b/src/test/kotlin/solve/unit/scene/view/association/AssociationManagerTests.kt new file mode 100644 index 00000000..b029477f --- /dev/null +++ b/src/test/kotlin/solve/unit/scene/view/association/AssociationManagerTests.kt @@ -0,0 +1,123 @@ +package solve.unit.scene.view.association + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import solve.scene.model.ColorManager +import solve.scene.model.Landmark +import solve.scene.model.Layer +import solve.scene.model.LayerSettings +import solve.scene.model.VisualizationFrame +import solve.scene.view.association.AssociationManager +import kotlin.io.path.Path + +internal class AssociationManagerTests { + @Test + fun `Associates two frames`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(0, 1) + assert(associationManager.associationConnections.any { + it.firstFrameIndex == 0 && it.secondFrameIndex == 1 + }) + } + + @Test + fun `Associate three frames transitively`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(0, 1) + associationManager.associate(1, 2) + associationManager.associate(2, 0) + assert(associationManager.associationConnections.any { + it.firstFrameIndex == 0 && it.secondFrameIndex == 1 + } && associationManager.associationConnections.any { + it.firstFrameIndex == 1 && it.secondFrameIndex == 2 + } && associationManager.associationConnections.any { + it.firstFrameIndex == 2 && it.secondFrameIndex == 0 + }) + } + + @Test + fun `Associates an existing frame with a non-existing frame`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(1, 10) + assert(associationManager.associationConnections.isEmpty()) + } + + @Test + fun `Associates frames a few times`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(0, 1) + associationManager.associate(0, 1) + assertEquals(1, associationManager.associationConnections.count()) + associationManager.associate(1, 0) + assertEquals(1, associationManager.associationConnections.count()) + } + + @Test + fun `Associates frames and clears association for the first frame`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(0, 1) + assertEquals(1, associationManager.associationConnections.count()) + associationManager.clearAssociation(0) + assertEquals(0, associationManager.associationConnections.count()) + } + + @Test + fun `Associates frames and clears association for the second frame`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(0, 1) + assertEquals(1, associationManager.associationConnections.count()) + associationManager.clearAssociation(1) + assertEquals(0, associationManager.associationConnections.count()) + } + + @Test + fun `Associates many-to-one frames and clears associations`() { + val associationManager = AssociationManager() + associationManager.setFramesSelection(testFrames) + associationManager.associate(1, 0) + associationManager.associate(2, 0) + associationManager.associate(3, 0) + associationManager.associate(4, 0) + assertEquals(4, associationManager.associationConnections.count()) + associationManager.clearAssociation(0) + assertEquals(0, associationManager.associationConnections.count()) + } + + companion object { + val testPointLayers = listOf( + Layer.PointLayer( + "0", + LayerSettings.PointLayerSettings("0", "0", ColorManager()) + ) { emptyList() }, + Layer.PointLayer( + "1", + LayerSettings.PointLayerSettings("1", "1", ColorManager()) + ) { emptyList() }, + Layer.PointLayer( + "2", + LayerSettings.PointLayerSettings("2", "2", ColorManager()) + ) { emptyList() }, + Layer.PointLayer( + "3", + LayerSettings.PointLayerSettings("3", "3", ColorManager()) + ) { emptyList() }, + Layer.PointLayer( + "4", + LayerSettings.PointLayerSettings("4", "4", ColorManager()) + ) { emptyList() } + ) + val testFrames = listOf( + VisualizationFrame(0L, Path("0.png"), listOf(testPointLayers[0])), + VisualizationFrame(1L, Path("1.png"), listOf(testPointLayers[1])), + VisualizationFrame(2L, Path("1.png"), listOf(testPointLayers[2])), + VisualizationFrame(3L, Path("1.png"), listOf(testPointLayers[3])), + VisualizationFrame(4L, Path("1.png"), listOf(testPointLayers[4])) + ) + } +} \ No newline at end of file