본문 바로가기

Android/Function

[Android Test] Espresso 로 View의 Visibility 설정 하기

728x90
반응형

 

  • 종속성 추가
dependencies {

    ...

    // test dependency
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test:core:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.2.0'

}

 

  • DisplayTest.kt

import android.view.View
import android.widget.FrameLayout
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import org.hamcrest.Matcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith


@RunWith(AndroidJUnit4::class)
class DisplayTest {
    @Rule
    @JvmField
    var activityRule = ActivityTestRule(MainActivity::class.java)

    @Test
    fun display() {
        onView(withId(R.id.frame_home)).perform(setVisibility(false))
        onView(withId(R.id.frame_AI)).perform(setVisibility(true))
        while(true){}
    }
    private fun setVisibility(value: Boolean): ViewAction? {
        return object : ViewAction {
            override fun getConstraints(): Matcher<View> {
                return isAssignableFrom(FrameLayout::class.java)
            }

            override fun perform(uiController: UiController?, view: View) {
                view.visibility = if (value) View.VISIBLE else View.GONE
            }

            override fun getDescription(): String {
                return "Show / Hide View"
            }
        }
    }
}

 

 

728x90
반응형