Add tests

This commit is contained in:
2026-01-12 06:00:57 +01:00
parent b05099da46
commit a8a07d9c8a
4 changed files with 270 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
package ch.dissem.yaep.ui.common.focus
import androidx.compose.ui.input.key.Key
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
class CluesSelectionManagerTest : SelectionManagerTest<CluesSelectionManager>() {
var primaryActionCalled: Int? = null
var secondaryActionCalled: Int? = null
lateinit var focusables: MutableList<CluesFocusable>
override fun setUp() {
manager = CluesSelectionManager()
primaryActionCalled = null
secondaryActionCalled = null
focusables = mutableListOf()
for (i in 0..10) {
focusables.add(
manager.add(
primaryAction = {
primaryActionCalled = i
},
secondaryAction = {
secondaryActionCalled = i
}
)
)
}
manager.columns = 3
}
@Test
fun ensure_navigation_right_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[2])
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_navigation_left_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[2])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[0])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[2])
}
@Test
fun ensure_navigation_down_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[3])
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[6])
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[9])
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_navigation_up_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[9])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[6])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[3])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[0])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[9])
}
}

View File

@@ -0,0 +1,80 @@
package ch.dissem.yaep.ui.common.focus
import androidx.compose.ui.input.key.Key
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
class GridSelectionManagerTest : SelectionManagerTest<GridSelectionManager>() {
lateinit var focusables: MutableList<GridFocusable>
override fun setUp() {
manager = GridSelectionManager()
focusables = mutableListOf()
repeat(3) {
manager.addRow()
repeat(3) {
focusables.add(
manager.add()
)
}
}
}
@Test
fun ensure_navigation_right_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[2])
manager.onKeyEvent(keyEvent(Key.DirectionRight))
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_navigation_left_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[2])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[0])
manager.onKeyEvent(keyEvent(Key.DirectionLeft))
expect(manager.focused).toEqual(focusables[2])
}
@Test
fun ensure_navigation_down_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[3])
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[6])
manager.onKeyEvent(keyEvent(Key.DirectionDown))
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_navigation_up_cycles() {
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[6])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[3])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[0])
manager.onKeyEvent(keyEvent(Key.DirectionUp))
expect(manager.focused).toEqual(focusables[6])
}
}

View File

@@ -0,0 +1,73 @@
package ch.dissem.yaep.ui.common.focus
import androidx.compose.ui.input.key.Key
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import kotlin.test.Test
class LinearSelectionManagerTest : SelectionManagerTest<LinearSelectionManager>() {
override fun setUp() {
manager = LinearSelectionManager(
keyNext = Key.A,
keyPrevious = Key.B
)
}
@Test
fun ensure_manager_with_focusables_has_focus_on_first_item() {
val focusables = arrayOf(
manager.add(),
manager.add()
)
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_keyEvent_A_selects_next() {
val focusables = arrayOf(
manager.add(),
manager.add(),
manager.add()
)
manager.onKeyEvent(keyEvent(Key.A))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.A))
expect(manager.focused).toEqual(focusables[2])
}
@Test
fun ensure_keyEvent_B_selects_previous() {
val focusables = arrayOf(
manager.add(),
manager.add(),
manager.add()
)
manager.focused = focusables[2]
manager.onKeyEvent(keyEvent(Key.B))
expect(manager.focused).toEqual(focusables[1])
manager.onKeyEvent(keyEvent(Key.B))
expect(manager.focused).toEqual(focusables[0])
}
@Test
fun ensure_ring_behaviour() {
val focusables = arrayOf(
manager.add(),
manager.add(),
manager.add()
)
manager.onKeyEvent(keyEvent(Key.B))
expect(manager.focused).toEqual(focusables[2])
manager.onKeyEvent(keyEvent(Key.A))
expect(manager.focused).toEqual(focusables[0])
}
}

View File

@@ -0,0 +1,21 @@
package ch.dissem.yaep.ui.common.focus
import androidx.compose.ui.InternalComposeUiApi
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import kotlin.test.BeforeTest
abstract class SelectionManagerTest<M : SelectionManager<*>> {
lateinit var manager: M
@BeforeTest
abstract fun setUp()
@OptIn(InternalComposeUiApi::class)
fun keyEvent(key: Key, type: KeyEventType = KeyEventType.KeyUp) = KeyEvent(
key = key,
type = type
)
}