|
| 1 | +// Copyright 2000-2025 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 2 | +package org.jetbrains.jewel.ui.component.search |
| 3 | + |
| 4 | +import androidx.compose.foundation.layout.widthIn |
| 5 | +import androidx.compose.runtime.LaunchedEffect |
| 6 | +import androidx.compose.runtime.getValue |
| 7 | +import androidx.compose.runtime.mutableIntStateOf |
| 8 | +import androidx.compose.runtime.remember |
| 9 | +import androidx.compose.runtime.setValue |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.focus.FocusRequester |
| 12 | +import androidx.compose.ui.focus.focusRequester |
| 13 | +import androidx.compose.ui.input.key.Key |
| 14 | +import androidx.compose.ui.platform.testTag |
| 15 | +import androidx.compose.ui.test.SemanticsNodeInteraction |
| 16 | +import androidx.compose.ui.test.assertIsDisplayed |
| 17 | +import androidx.compose.ui.test.assertIsSelected |
| 18 | +import androidx.compose.ui.test.hasAnyAncestor |
| 19 | +import androidx.compose.ui.test.hasTestTag |
| 20 | +import androidx.compose.ui.test.hasText |
| 21 | +import androidx.compose.ui.test.junit4.ComposeContentTestRule |
| 22 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 23 | +import androidx.compose.ui.test.onNodeWithTag |
| 24 | +import androidx.compose.ui.test.performClick |
| 25 | +import androidx.compose.ui.unit.dp |
| 26 | +import kotlin.test.Test |
| 27 | +import org.jetbrains.jewel.intui.standalone.theme.IntUiTheme |
| 28 | +import org.jetbrains.jewel.ui.component.SpeedSearchArea |
| 29 | +import org.jetbrains.jewel.ui.component.assertCursorAtPosition |
| 30 | +import org.jetbrains.jewel.ui.component.interactions.performKeyPress |
| 31 | +import org.junit.Rule |
| 32 | + |
| 33 | +@Suppress("ImplicitUnitReturnType") |
| 34 | +class SpeedSearchableComboBoxTest { |
| 35 | + @get:Rule val rule = createComposeRule() |
| 36 | + |
| 37 | + private val comboBox: SemanticsNodeInteraction |
| 38 | + get() = rule.onNodeWithTag("ComboBox") |
| 39 | + |
| 40 | + private val ComposeContentTestRule.onSpeedSearchAreaInput |
| 41 | + get() = onNodeWithTag("SpeedSearchArea.Input") |
| 42 | + |
| 43 | + private fun ComposeContentTestRule.onComboBoxItem(text: String) = |
| 44 | + onNode(hasAnyAncestor(hasTestTag("Jewel.ComboBox.List")) and hasText(text)) |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `should show on type text`() = runComposeTest { |
| 48 | + comboBox.performClick() |
| 49 | + comboBox.performKeyPress("Item", rule = this) |
| 50 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `should not show on type text before opening the popup`() = runComposeTest { |
| 55 | + comboBox.performKeyPress("Item", rule = this) |
| 56 | + onSpeedSearchAreaInput.assertDoesNotExist() |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `should hide on esc press`() = runComposeTest { |
| 61 | + comboBox.performClick() |
| 62 | + |
| 63 | + comboBox.performKeyPress("Item", rule = this) |
| 64 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 65 | + |
| 66 | + comboBox.performKeyPress(Key.Escape, rule = this) |
| 67 | + onSpeedSearchAreaInput.assertDoesNotExist() |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `on option navigation, move input cursor`() = runComposeTest { |
| 72 | + comboBox.performClick() |
| 73 | + |
| 74 | + comboBox.performKeyPress("Item 42", rule = this) |
| 75 | + |
| 76 | + comboBox.performKeyPress(Key.DirectionLeft, alt = true, rule = this) |
| 77 | + onSpeedSearchAreaInput.assertCursorAtPosition(0) |
| 78 | + |
| 79 | + comboBox.performKeyPress(Key.DirectionRight, alt = true, rule = this) |
| 80 | + onSpeedSearchAreaInput.assertCursorAtPosition(7) |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + fun `on type, select first occurrence`() = runComposeTest { |
| 85 | + comboBox.performClick() |
| 86 | + comboBox.performKeyPress("Item 2", rule = this) |
| 87 | + onComboBoxItem("Item 2").assertIsDisplayed().assertIsSelected() |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `on type continue typing, continue selecting first occurrence`() = runComposeTest { |
| 92 | + comboBox.performClick() |
| 93 | + |
| 94 | + comboBox.performKeyPress("Item 2", rule = this) |
| 95 | + onComboBoxItem("Item 2").assertIsDisplayed().assertIsSelected() |
| 96 | + |
| 97 | + onSpeedSearchAreaInput.performKeyPress("4", rule = this) |
| 98 | + onComboBoxItem("Item 24").assertIsDisplayed().assertIsSelected() |
| 99 | + |
| 100 | + onSpeedSearchAreaInput.performKeyPress("5", rule = this) |
| 101 | + onComboBoxItem("Item 245").assertIsDisplayed().assertIsSelected() |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `select closest match if after the current item`() = runComposeTest { |
| 106 | + comboBox.performClick() |
| 107 | + comboBox.performKeyPress("Item 245", rule = this) |
| 108 | + onComboBoxItem("Item 245").assertIsDisplayed().assertIsSelected() |
| 109 | + |
| 110 | + // Delete the number |
| 111 | + onSpeedSearchAreaInput.performKeyPress(Key.Backspace, rule = this) |
| 112 | + onSpeedSearchAreaInput.performKeyPress(Key.Backspace, rule = this) |
| 113 | + onSpeedSearchAreaInput.performKeyPress(Key.Backspace, rule = this) |
| 114 | + |
| 115 | + // Add `99` and jumps to next reference matching "99" |
| 116 | + onSpeedSearchAreaInput.performKeyPress("99", rule = this) |
| 117 | + onComboBoxItem("Item 299").assertIsDisplayed().assertIsSelected() |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `on arrow up or down, navigate to the next and previous occurrences`() = runComposeTest { |
| 122 | + comboBox.performClick() |
| 123 | + comboBox.performKeyPress("Item 9", rule = this) |
| 124 | + onComboBoxItem("Item 9").assertIsDisplayed().assertIsSelected() |
| 125 | + |
| 126 | + comboBox.performKeyPress(Key.DirectionDown, rule = this) |
| 127 | + onComboBoxItem("Item 19").assertIsDisplayed().assertIsSelected() |
| 128 | + |
| 129 | + comboBox.performKeyPress(Key.DirectionDown, rule = this) |
| 130 | + onComboBoxItem("Item 29").assertIsDisplayed().assertIsSelected() |
| 131 | + |
| 132 | + comboBox.performKeyPress(Key.DirectionUp, rule = this) |
| 133 | + onComboBoxItem("Item 19").assertIsDisplayed().assertIsSelected() |
| 134 | + |
| 135 | + comboBox.performKeyPress(Key.DirectionUp, rule = this) |
| 136 | + onComboBoxItem("Item 9").assertIsDisplayed().assertIsSelected() |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + fun `deleting last char should keep current state`() = runComposeTest { |
| 141 | + comboBox.performClick() |
| 142 | + comboBox.performKeyPress("Item 42", rule = this) |
| 143 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 144 | + onComboBoxItem("Item 42").assertIsDisplayed().assertIsSelected() |
| 145 | + |
| 146 | + // Remove "2" from "Item 42" to make "Item 4", but keep 42 selected as it matches the search query |
| 147 | + onSpeedSearchAreaInput.performKeyPress(Key.Backspace, rule = this) |
| 148 | + onComboBoxItem("Item 42").assertIsDisplayed().assertIsSelected() |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `should handle partial text matching`() = runComposeTest { |
| 153 | + comboBox.performClick() |
| 154 | + |
| 155 | + comboBox.performKeyPress("em 1", rule = this) |
| 156 | + onComboBoxItem("Item 1").assertIsDisplayed().assertIsSelected() |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + fun `should keep text when navigating through matches`() = runComposeTest { |
| 161 | + comboBox.performClick() |
| 162 | + comboBox.performKeyPress("Item 9", rule = this) |
| 163 | + onComboBoxItem("Item 9").assertIsDisplayed().assertIsSelected() |
| 164 | + |
| 165 | + comboBox.performKeyPress(Key.DirectionDown, rule = this) |
| 166 | + onComboBoxItem("Item 19").assertIsDisplayed().assertIsSelected() |
| 167 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 168 | + |
| 169 | + comboBox.performKeyPress(Key.DirectionDown, rule = this) |
| 170 | + onComboBoxItem("Item 29").assertIsDisplayed().assertIsSelected() |
| 171 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 172 | + |
| 173 | + comboBox.performKeyPress(Key.DirectionUp, rule = this) |
| 174 | + onComboBoxItem("Item 19").assertIsDisplayed().assertIsSelected() |
| 175 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 176 | + |
| 177 | + comboBox.performKeyPress(Key.DirectionUp, rule = this) |
| 178 | + onComboBoxItem("Item 9").assertIsDisplayed().assertIsSelected() |
| 179 | + onSpeedSearchAreaInput.assertExists().assertIsDisplayed() |
| 180 | + } |
| 181 | + |
| 182 | + private fun runComposeTest( |
| 183 | + entries: List<String> = List(500) { "Item ${it + 1}" }, |
| 184 | + block: ComposeContentTestRule.() -> Unit, |
| 185 | + ) { |
| 186 | + rule.setContent { |
| 187 | + val focusRequester = remember { FocusRequester() } |
| 188 | + |
| 189 | + IntUiTheme { |
| 190 | + var selectedIndex by remember { mutableIntStateOf(0) } |
| 191 | + SpeedSearchArea( |
| 192 | + Modifier.widthIn(max = 200.dp).focusRequester(focusRequester).testTag("SpeedSearchArea") |
| 193 | + ) { |
| 194 | + SpeedSearchableComboBox( |
| 195 | + items = entries, |
| 196 | + selectedIndex = selectedIndex, |
| 197 | + onSelectedItemChange = { index -> selectedIndex = index }, |
| 198 | + modifier = Modifier.testTag("ComboBox"), |
| 199 | + ) |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + LaunchedEffect(Unit) { focusRequester.requestFocus() } |
| 204 | + } |
| 205 | + |
| 206 | + rule.block() |
| 207 | + } |
| 208 | +} |
0 commit comments