Migrate to v2 testing APIs

This commit is contained in:
Benoit Marty 2026-04-30 15:54:33 +02:00
parent 21795a86c1
commit 723b9b8d61
83 changed files with 2197 additions and 2320 deletions

View file

@ -6,60 +6,58 @@
* Please see LICENSE files in the repository root for full details.
*/
@file:OptIn(ExperimentalTestApi::class)
package io.element.android.libraries.troubleshoot.impl
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.AndroidComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.v2.runAndroidComposeUiTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.element.android.tests.testutils.EnsureNeverCalled
import io.element.android.tests.testutils.EventsRecorder
import io.element.android.tests.testutils.ensureCalledOnce
import io.element.android.tests.testutils.pressBack
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.robolectric.annotation.Config
@RunWith(AndroidJUnit4::class)
class TroubleshootNotificationsViewTest {
@get:Rule
val rule = createAndroidComposeRule<ComponentActivity>()
@Test
fun `press menu back invokes the expected callback`() {
fun `press menu back invokes the expected callback`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>(expectEvents = false)
ensureCalledOnce {
rule.setTroubleshootNotificationsView(
setTroubleshootNotificationsView(
state = aTroubleshootNotificationsState(
eventSink = eventsRecorder
),
onBackClick = it,
)
rule.pressBack()
pressBack()
}
}
@Test
fun `clicking on run test emits the expected Event`() {
fun `clicking on run test emits the expected Event`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Run tests").performClick()
onNodeWithText("Run tests").performClick()
eventsRecorder.assertSingle(TroubleshootNotificationsEvents.StartTests)
}
@Config(qualifiers = "h1024dp")
@Test
fun `clicking on run test again emits the expected Event`() {
fun `clicking on run test again emits the expected Event`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
tests = listOf(
aTroubleshootTestStateFailure(
@ -69,7 +67,7 @@ class TroubleshootNotificationsViewTest {
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Run tests again").performClick()
onNodeWithText("Run tests again").performClick()
eventsRecorder.assertList(
listOf(
TroubleshootNotificationsEvents.RetryFailedTests,
@ -80,9 +78,9 @@ class TroubleshootNotificationsViewTest {
@Config(qualifiers = "h1024dp")
@Test
fun `clicking on quick fix emits the expected Event`() {
fun `clicking on quick fix emits the expected Event`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<TroubleshootNotificationsEvents>()
rule.setTroubleshootNotificationsView(
setTroubleshootNotificationsView(
aTroubleshootNotificationsState(
tests = listOf(
aTroubleshootTestStateFailure(
@ -92,7 +90,7 @@ class TroubleshootNotificationsViewTest {
eventSink = eventsRecorder
),
)
rule.onNodeWithText("Attempt to fix").performClick()
onNodeWithText("Attempt to fix").performClick()
eventsRecorder.assertList(
listOf(
TroubleshootNotificationsEvents.RetryFailedTests,
@ -102,7 +100,7 @@ class TroubleshootNotificationsViewTest {
}
}
private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setTroubleshootNotificationsView(
private fun AndroidComposeUiTest<ComponentActivity>.setTroubleshootNotificationsView(
state: TroubleshootNotificationsState,
onBackClick: () -> Unit = EnsureNeverCalled(),
) {

View file

@ -6,14 +6,17 @@
* Please see LICENSE files in the repository root for full details.
*/
@file:OptIn(ExperimentalTestApi::class)
package io.element.android.libraries.troubleshoot.impl.history
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.AndroidComposeUiTest
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.v2.runAndroidComposeUiTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.libraries.matrix.test.A_FORMATTED_DATE
@ -23,67 +26,62 @@ import io.element.android.libraries.ui.strings.CommonStrings
import io.element.android.tests.testutils.EnsureNeverCalled
import io.element.android.tests.testutils.EventsRecorder
import io.element.android.tests.testutils.clickOn
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class PushHistoryViewTest {
@get:Rule
val rule = createAndroidComposeRule<ComponentActivity>()
@Test
fun `clicking on Reset sends a PushHistoryEvents`() {
fun `clicking on Reset sends a PushHistoryEvents`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
rule.setPushHistoryView(
setPushHistoryView(
aPushHistoryState(
pushCounter = 123,
eventSink = eventsRecorder,
),
)
val menuContentDescription = rule.activity.getString(CommonStrings.a11y_user_menu)
rule.onNodeWithContentDescription(menuContentDescription).performClick()
rule.clickOn(CommonStrings.action_reset)
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
clickOn(CommonStrings.action_reset)
eventsRecorder.assertSingle(PushHistoryEvents.Reset(requiresConfirmation = true))
// Also check that the push counter is rendered
rule.onNodeWithText("123").assertExists()
onNodeWithText("123").assertExists()
}
@Test
fun `clicking on show only errors sends a PushHistoryEvents(true)`() {
fun `clicking on show only errors sends a PushHistoryEvents(true)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
rule.setPushHistoryView(
setPushHistoryView(
aPushHistoryState(
showOnlyErrors = false,
eventSink = eventsRecorder,
),
)
val menuContentDescription = rule.activity.getString(CommonStrings.a11y_user_menu)
rule.onNodeWithContentDescription(menuContentDescription).performClick()
rule.onNodeWithText("Show only errors").performClick()
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
onNodeWithText("Show only errors").performClick()
eventsRecorder.assertSingle(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = true))
}
@Test
fun `clicking on show only errors sends a PushHistoryEvents(false)`() {
fun `clicking on show only errors sends a PushHistoryEvents(false)`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
rule.setPushHistoryView(
setPushHistoryView(
aPushHistoryState(
showOnlyErrors = true,
eventSink = eventsRecorder,
),
)
val menuContentDescription = rule.activity.getString(CommonStrings.a11y_user_menu)
rule.onNodeWithContentDescription(menuContentDescription).performClick()
rule.onNodeWithText("Show only errors").performClick()
val menuContentDescription = activity!!.getString(CommonStrings.a11y_user_menu)
onNodeWithContentDescription(menuContentDescription).performClick()
onNodeWithText("Show only errors").performClick()
eventsRecorder.assertSingle(PushHistoryEvents.SetShowOnlyErrors(showOnlyErrors = false))
}
@Test
fun `clicking on an invalid event has no effect`() {
fun `clicking on an invalid event has no effect`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>(expectEvents = false)
rule.setPushHistoryView(
setPushHistoryView(
aPushHistoryState(
pushHistoryItems = listOf(
aPushHistoryItem(
@ -93,14 +91,14 @@ class PushHistoryViewTest {
eventSink = eventsRecorder,
),
)
rule.onNodeWithText(A_FORMATTED_DATE).performClick()
onNodeWithText(A_FORMATTED_DATE).performClick()
// No callback invoked
}
@Test
fun `clicking on a valid event emits the expected Event`() {
fun `clicking on a valid event emits the expected Event`() = runAndroidComposeUiTest {
val eventsRecorder = EventsRecorder<PushHistoryEvents>()
rule.setPushHistoryView(
setPushHistoryView(
aPushHistoryState(
pushHistoryItems = listOf(
aPushHistoryItem(
@ -113,7 +111,7 @@ class PushHistoryViewTest {
eventSink = eventsRecorder,
),
)
rule.onNodeWithText(A_FORMATTED_DATE).performClick()
onNodeWithText(A_FORMATTED_DATE).performClick()
eventsRecorder.assertSingle(
PushHistoryEvents.NavigateTo(
sessionId = A_SESSION_ID,
@ -124,7 +122,7 @@ class PushHistoryViewTest {
}
}
private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setPushHistoryView(
private fun AndroidComposeUiTest<ComponentActivity>.setPushHistoryView(
state: PushHistoryState,
onBackClick: () -> Unit = EnsureNeverCalled(),
) {