Skip to content

Commit

Permalink
test: modifier keys equality
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO authored and mikehardy committed Feb 10, 2025
1 parent f7ee6b9 commit 4fd3bdb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/reviewer/Binding.kt
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ sealed interface Binding {

override fun equals(other: Any?): Boolean = other is ModifierKeys && semiStructuralEquals(other)

override fun hashCode(): Int = Objects.hash(ctrl, alt, shift, shiftMatches(true), shiftMatches(false))
override fun hashCode(): Int = Objects.hash(ctrl, alt, shiftMatches(true))

companion object {
fun none(): ModifierKeys = ModifierKeys(shift = false, ctrl = false, alt = false)
Expand All @@ -213,7 +213,7 @@ sealed interface Binding {
}

/** Modifier keys which cannot be defined by a binding */
private class AppDefinedModifierKeys private constructor() : ModifierKeys(false, false, false) {
class AppDefinedModifierKeys private constructor() : ModifierKeys(false, false, false) {
override fun shiftMatches(shiftPressed: Boolean): Boolean = true

companion object {
Expand Down
17 changes: 17 additions & 0 deletions AnkiDroid/src/test/java/com/ichi2/anki/reviewer/BindingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.mockito.ArgumentMatchers.anyInt
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import kotlin.reflect.KFunction1
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class BindingTest {
@Test
Expand Down Expand Up @@ -68,6 +70,21 @@ class BindingTest {
assertThat(Binding.unknown().toString(), equalTo(""))
}

@Test
fun testModifierKeysEquality() {
val one = Binding.AppDefinedModifierKeys.allowShift()
val two = Binding.ModifierKeys(shift = true, ctrl = false, alt = false)

assertTrue(one.shiftMatches(true))
assertTrue(one.shiftMatches(false))

assertTrue(two.shiftMatches(true))
assertFalse(two.shiftMatches(false))

assertEquals(one, two)
assertEquals(one.hashCode(), two.hashCode())
}

private fun testModifierKeys(
name: String,
event: KFunction1<KeyEvent, Boolean>,
Expand Down

0 comments on commit 4fd3bdb

Please sign in to comment.