[go: nahoru, domu]

blob: 5dd938f7e9a364747234966c5daa17db101dde7e [file] [log] [blame]
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.compose.foundation.textfield
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.runtime.Providers
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.AmbientTextInputService
import androidx.compose.ui.test.hasSetTextAction
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.performClick
import androidx.compose.ui.text.SoftwareKeyboardController
import androidx.compose.ui.text.input.TextInputService
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.MediumTest
import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.times
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@MediumTest
@RunWith(AndroidJUnit4::class)
@OptIn(ExperimentalFoundationApi::class)
class SoftwareKeyboardTest {
@get:Rule
val rule = createComposeRule()
@Test
fun textField_onTextLayoutCallback() {
val textInputService = mock<TextInputService>()
val inputSessionToken = 10 // any positive number is fine.
whenever(textInputService.startInput(any(), any(), any(), any()))
.thenReturn(inputSessionToken)
val onTextInputStarted: (SoftwareKeyboardController) -> Unit = mock()
rule.setContent {
Providers(
AmbientTextInputService provides textInputService
) {
val state = remember { mutableStateOf("") }
BasicTextField(
value = state.value,
modifier = Modifier.fillMaxSize(),
onValueChange = {
state.value = it
},
onTextInputStarted = onTextInputStarted
)
}
}
// Perform click to focus in.
rule.onNode(hasSetTextAction())
.performClick()
rule.runOnIdle {
verify(onTextInputStarted, times(1)).invoke(any())
}
}
}