Skip to content

Commit

Permalink
#1274 fix: do not allow multiple trigger keys when switching to a par…
Browse files Browse the repository at this point in the history
…allel trigger
  • Loading branch information
sds100 committed Nov 7, 2024
1 parent 628dfd3 commit 2db6560
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ class ConfigKeyMapUseCaseImpl(
// remove duplicates of keys that have the same keycode and device id
.distinctBy { key ->
when (key) {
is AssistantTriggerKey -> key.type
// You can't mix assistant trigger types in a parallel trigger because there is no notion of a "down" key event, which means they can't be pressed at the same time
is AssistantTriggerKey -> 0
is KeyCodeTriggerKey -> Pair(key.keyCode, key.device)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.github.sds100.keymapper.mappings.keymaps.KeyMap
import io.github.sds100.keymapper.mappings.keymaps.KeyMapAction
import io.github.sds100.keymapper.mappings.keymaps.trigger.AssistantTriggerKey
import io.github.sds100.keymapper.mappings.keymaps.trigger.AssistantTriggerType
import io.github.sds100.keymapper.mappings.keymaps.trigger.KeyCodeTriggerKey
import io.github.sds100.keymapper.mappings.keymaps.trigger.Trigger
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerKeyDevice
import io.github.sds100.keymapper.mappings.keymaps.trigger.TriggerMode
Expand All @@ -22,6 +23,8 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
import org.hamcrest.Matchers.hasSize
import org.hamcrest.Matchers.instanceOf
import org.hamcrest.Matchers.`is`
import org.junit.Before
import org.junit.Test
Expand All @@ -47,8 +50,43 @@ class ConfigKeyMapUseCaseTest {
)
}

/**
* This ensures that it isn't possible to have two or more assistant triggers when the mode is parallel.
*/
@Test
fun `Remove device assistant trigger if setting mode to parallel and voice assistant already exists`() =
runTest(testDispatcher) {
useCase.mapping.value = State.Data(KeyMap())

useCase.addKeyCodeTriggerKey(KeyEvent.KEYCODE_VOLUME_DOWN, TriggerKeyDevice.Any)
useCase.addAssistantTriggerKey(AssistantTriggerType.VOICE)
useCase.addAssistantTriggerKey(AssistantTriggerType.DEVICE)
useCase.setParallelTriggerMode()

val trigger = useCase.mapping.value.dataOrNull()!!.trigger
assertThat(trigger.keys, hasSize(2))
assertThat(trigger.keys[0], instanceOf(KeyCodeTriggerKey::class.java))
assertThat(trigger.keys[1], instanceOf(AssistantTriggerKey::class.java))
}

@Test
fun `Remove voice assistant trigger if setting mode to parallel and device assistant already exists`() =
runTest(testDispatcher) {
useCase.mapping.value = State.Data(KeyMap())

useCase.addKeyCodeTriggerKey(KeyEvent.KEYCODE_VOLUME_DOWN, TriggerKeyDevice.Any)
useCase.addAssistantTriggerKey(AssistantTriggerType.DEVICE)
useCase.addAssistantTriggerKey(AssistantTriggerType.VOICE)
useCase.setParallelTriggerMode()

val trigger = useCase.mapping.value.dataOrNull()!!.trigger
assertThat(trigger.keys, hasSize(2))
assertThat(trigger.keys[0], instanceOf(KeyCodeTriggerKey::class.java))
assertThat(trigger.keys[1], instanceOf(AssistantTriggerKey::class.java))
}

@Test
fun `Set trigger mode to short press when adding assistant key to multiple long press trigger keys`() =
fun `Set click type to short press when adding assistant key to multiple long press trigger keys`() =
runTest(testDispatcher) {
useCase.mapping.value = State.Data(KeyMap())

Expand All @@ -63,7 +101,7 @@ class ConfigKeyMapUseCaseTest {
}

@Test
fun `Set trigger mode to short press when adding assistant key to double press trigger key`() =
fun `Set click type to short press when adding assistant key to double press trigger key`() =
runTest(testDispatcher) {
useCase.mapping.value = State.Data(KeyMap())

Expand All @@ -76,7 +114,7 @@ class ConfigKeyMapUseCaseTest {
}

@Test
fun `Set trigger mode to short press when adding assistant key to long press trigger key`() =
fun `Set click type to short press when adding assistant key to long press trigger key`() =
runTest(testDispatcher) {
useCase.mapping.value = State.Data(KeyMap())

Expand Down

0 comments on commit 2db6560

Please sign in to comment.