package com.cottondb.notes import android.app.Application import androidx.activity.ComponentActivity import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Column import androidx.compose.foundation.text.BasicTextField import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.assertIsFocused import androidx.compose.ui.test.assertIsNotFocused import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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() @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() } }