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