Vahid
Vahid

Reputation: 1949

Jetpack Compose UI test breaks at setup

I'm trying a fairly simple UI test on a Jetpack Compose app.

The SUT is a login screen with two fields for email and password and a link for password reset.

When reset link is hit, the password fields hides and the email field stays.

Here's the entire test class:

@HiltAndroidTest
class LoginScreenTests {
    @get:Rule(order = 0)
    val hiltRule = HiltAndroidRule(this)

    @get:Rule(order = 1)
    val composeRule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setup() {
        hiltRule.inject()
        composeRule.setContent {
            MyTheme {
                LoginScreen()
            }
        }
    }

    @Test
    fun onStart_shouldDisplayEmailAndPass() {
        composeRule.onNodeWithTag("email").assertIsDisplayed()
        composeRule.onNodeWithTag("password").assertIsDisplayed()
    }

    @Test
    fun resetMode_shouldHidePass() {
        composeRule.onNodeWithTag("resetPassword").performClick()

        composeRule.onNodeWithTag("email").assertIsDisplayed()
        composeRule.onNodeWithTag("password").assertDoesNotExist()
    }
}

When I run them individually, both tests pass. When I run the class, the second one fails.

And as I noticed, it is not even in the test function that the failure occurs. It breaks in the setup function but I couldn't pinpoint the cause of the break. I can set breakpoints on hiltRule.inject and composeRule.setContent, the breakpoints do hit but the process gets killed even when stopped by the breakpoint.

And there's also a log for the failed test that says something about unregistering an input channel.

02-13 20:39:39.475 26380 26412 I TestRunner: started: onStart_shouldDisplayEmailAndPass(com.sample.ui.screens.LoginScreenTests)
02-13 20:39:39.488 26380 26412 W Settings: Setting always_finish_activities has moved from android.provider.Settings.System to android.provider.Settings.Global, returning read-only value.
02-13 20:39:39.518 10516 10534 D EventSequenceValidator: inc AccIntentStartedEvents to 26
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.534 10516 15024 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.535 10516 15024 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.584   304  8900 D goldfish-address-space: claimShared: Ask to claim region [0x3f6ed4000 0x3f76bd000]
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.657 10516 11864 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.658 10516 11864 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.659 10516 11864 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.660 10516 11864 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.676   304  8900 D goldfish-address-space: claimShared: Ask to claim region [0x3f28f2000 0x3f30db000]
02-13 20:39:39.687 11465 26026 I PBSessionCacheImpl: Deleted sessionId[50425247988668063] from persistence.
02-13 20:39:39.696 11465 12035 W SearchServiceCore: Abort, client detached.
02-13 20:39:39.702 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: PRE_ON_CREATE
02-13 20:39:39.702 26380 26380 V ActivityScenario: Activity lifecycle changed event received but ignored because the reported transition was not ON_CREATE while the last known transition was PRE_ON_CREATE
02-13 20:39:39.704 26380 26410 V FA      : onActivityCreated
02-13 20:39:39.710 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: CREATED
02-13 20:39:39.710 26380 26380 V ActivityScenario: Update currentActivityStage to CREATED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.716 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: STARTED
02-13 20:39:39.716 26380 26380 V ActivityScenario: Update currentActivityStage to STARTED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.719 26380 26414 V FA      : Activity resumed, time: 59230451
02-13 20:39:39.720 26380 26380 D LifecycleMonitor: Lifecycle status change: com.sample.MainActivity@6689b60 in: RESUMED
02-13 20:39:39.721 26380 26380 V ActivityScenario: Update currentActivityStage to RESUMED, currentActivity=com.sample.MainActivity@6689b60
02-13 20:39:39.739 26380 26380 D LandingScreen.kt: is tablet: false
02-13 20:39:39.802 26380 26460 I Process : Sending signal. PID: 26380 SIG: 9
02-13 20:39:39.847 10516 12816 W InputDispatcher: Attempted to unregister already unregistered input channel 'bd87344 com.sample/com.sample.MainActivity (server)'
02-13 20:39:39.855 11520 11520 D BoundBrokerSvc: onUnbind: Intent { act=com.google.android.gms.measurement.START pkg=com.google.android.gms }
02-13 20:39:39.865 26366 26366 D AndroidRuntime: Shutting down VM
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_9 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_7 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.868 10516 14101 W InputReader: Device virtio_input_multi_touch_10 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.869 10516 14101 W InputReader: Device virtio_input_multi_touch_4 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_6 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_11 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_3 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_5 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_2 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.870 10516 14101 W InputReader: Device virtio_input_multi_touch_8 is associated with display ADISPLAY_ID_NONE.
02-13 20:39:39.882 10477 10477 I Zygote  : Process 26380 exited due to signal 9 (Killed)

Upvotes: 1

Views: 4472

Answers (1)

Immanuel Diaz
Immanuel Diaz

Reputation: 402

You need to add this line in your class

@RunWith(AndroidJUnit4::class)

and these lines are not necessary for the code

@HiltAndroidTest
@get:Rule(order = 0)
val hiltRule = HiltAndroidRule(this)
hiltRule.inject()

Your code should looks like

@RunWith(AndroidJUnit4::class)
class LoginScreenTests {
    
    @get:Rule
    val composeRule = createAndroidComposeRule<MainActivity>()

    @Before
    fun setup() {
        composeRule.setContent {
            MyTheme {
                LoginScreen()
            }
        }
    }

    @Test
    fun onStart_shouldDisplayEmailAndPass() {
        composeRule.onNodeWithTag("email").assertIsDisplayed()
        composeRule.onNodeWithTag("password").assertIsDisplayed()
    }

    @Test
    fun resetMode_shouldHidePass() {
        composeRule.onNodeWithTag("resetPassword").performClick()

        composeRule.onNodeWithTag("email").assertIsDisplayed()
        composeRule.onNodeWithTag("password").assertDoesNotExist()
    }
}

Upvotes: 1

Related Questions