agoda-com / Kakao

This repo is no longer supported. Please visit a https://github.com/KakaoCup/Kakao
Apache License 2.0
1.11k stars 102 forks source link

[Question] how to do action on compound customview child ? #214

Closed letroll closed 4 years ago

letroll commented 4 years ago

Hello,

In my projet I have a custom view with an EditText child. This custom view have a method setEditTextValue(text:String).

How to make a test where we use it to input text ?

I had tried with this code :

class KClearableEditText : KBaseView<KView>, BaseAssertions, CustomEditableActions,TextViewActions {
    constructor(function: ViewBuilder.() -> Unit) : super(function)
    constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function)
    constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function)
}

interface CustomEditableActions: EditableActions{
    override fun typeText(text: String) {
        view.perform(CustomTypeTextAction(text))
    }
}

class CustomTypeTextAction(private val text: String?,val tapToFocus:Boolean=true) : ViewAction{
    override fun getDescription() = "typing text: $text"

    override fun getConstraints() = null

    override fun perform(uiController: UiController?, view: View?) {
        if (view is ClearableEditText) {
            view.setEditTextValue(text)
        }
    }
}
letroll commented 4 years ago

resolved by using same principle than KTextInputLayout.kt :

''' class KClearableEditText : KBaseView, BaseAssertions,TextViewActions,EditableActions { val edit: KEditText

constructor(function: ViewBuilder.() -> Unit) : super(function) {
    edit = getkEditText(function)
}

constructor(parent: Matcher<View>, function: ViewBuilder.() -> Unit) : super(parent, function) {
    edit = getkEditText(function)
}

constructor(parent: DataInteraction, function: ViewBuilder.() -> Unit) : super(parent, function) {
    edit = getkEditText(function)
}

private fun getkEditText(function: ViewBuilder.() -> Unit): KEditText {
    return KEditText {
        isDescendantOfA(function)
        isInstanceOf(EditText::class.java)
    }
}

val nameEditTextView = KClearableEditText { withId(R.id.nameClearableEditText) }

nameEditTextView.edit.typeText("toto")

'''