Cotton code
73 lines · 3.0 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.compose.foundation.clickableimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.text.BasicTextFieldimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.ui.focus.FocusRequesterimport androidx.compose.ui.focus.focusRequesterimport androidx.compose.ui.platform.LocalFocusManagerimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.assertIsFocusedimport androidx.compose.ui.test.assertIsNotFocusedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** A note opened from the list's search: the search lets go of the keyboard, so the note does not stand under it. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], application = Application::class)class NotesOpenFromSearchTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    @Test fun opening_a_row_lets_the_search_go() {        var opened by mutableStateOf(false)        val search = FocusRequester()        compose.setContent {            val focus = LocalFocusManager.current            var words by remember { mutableStateOf("Everything") }            Column {                BasicTextField(words, { words = it }, Modifier.testTag("search").focusRequester(search))                Text("Everything a note can hold", Modifier.testTag("row").clickable { openFrom(focus) { opened = true } })            }        }        compose.runOnIdle { search.requestFocus() }        compose.onNodeWithTag("search").assertIsFocused()        compose.onNodeWithTag("row").performClick()        compose.waitForIdle()        assertTrue(opened)        compose.onNodeWithTag("search").assertIsNotFocused()    }    /** The side page opened while the search holds the keyboard: the search lets go of it. */    @Test fun the_side_page_lets_the_search_go() {        var open by mutableStateOf(false)        val search = FocusRequester()        compose.setContent {            var words by remember { mutableStateOf("travel") }            Column {                BasicTextField(words, { words = it }, Modifier.testTag("search").focusRequester(search))                LetGoOfTheKeyboard(open)            }        }        compose.runOnIdle { search.requestFocus() }        compose.onNodeWithTag("search").assertIsFocused()        open = true        compose.waitForIdle()        compose.onNodeWithTag("search").assertIsNotFocused()    }}