From dca22e3f1b3ed2b188dcdc5f830687dfdb0be455 Mon Sep 17 00:00:00 2001 From: yaoandy107 Date: Sat, 1 Oct 2022 16:35:02 +0800 Subject: [PATCH 1/2] [WIP] Add search screen testing --- .../confsched2022/feature/sessions/Search.kt | 5 ++- .../feature/sessions/SessionListItem.kt | 4 +- .../feature/sessions/SearchScreenRobot.kt | 44 +++++++++++++++++++ .../feature/sessions/SearchScreenTest.kt | 34 ++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt create mode 100644 feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenTest.kt diff --git a/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/Search.kt b/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/Search.kt index 2bf5962db..9d3e614ce 100644 --- a/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/Search.kt +++ b/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/Search.kt @@ -56,6 +56,7 @@ import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.SolidColor import androidx.compose.ui.platform.LocalSoftwareKeyboardController +import androidx.compose.ui.platform.testTag import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.semantics.CustomAccessibilityAction @@ -293,7 +294,8 @@ fun SearchTextFieldTopAppBar( title = { SearchTextField( modifier = Modifier - .background(color = backgroundColor), + .background(color = backgroundColor) + .testTag("search"), searchWord = searchWord, onSearchWordChange = onSearchWordChange, onSearchTextAreaClicked = onSearchTextAreaClicked @@ -354,6 +356,7 @@ private fun SearchTextField( placeholder = { Text(stringResource(Strings.search_placeholder)) }, trailingIcon = { IconButton( + modifier = Modifier.testTag("clearInput"), onClick = { onSearchWordChange("") } ) { Icon( diff --git a/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/SessionListItem.kt b/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/SessionListItem.kt index 2771b026d..cefc50c7d 100644 --- a/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/SessionListItem.kt +++ b/feature/sessions/src/main/java/io/github/droidkaigi/confsched2022/feature/sessions/SessionListItem.kt @@ -65,7 +65,9 @@ fun SessionListItem( val secondLang = lang.secondLang() Row( - modifier = modifier.fillMaxSize(), + modifier = modifier + .fillMaxSize() + .testTag("session"), horizontalArrangement = Arrangement.SpaceBetween ) { Column(modifier = Modifier.weight(1F)) { diff --git a/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt new file mode 100644 index 000000000..e9ec84823 --- /dev/null +++ b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt @@ -0,0 +1,44 @@ +package io.github.droidkaigi.confsched2022.feature.sessions + +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.Surface +import androidx.compose.ui.Modifier +import androidx.compose.ui.test.assertAny +import androidx.compose.ui.test.hasTestTag +import androidx.compose.ui.test.hasText +import androidx.compose.ui.test.onNodeWithTag +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performTextReplacement +import io.github.droidkaigi.confsched2022.designsystem.theme.KaigiTheme +import io.github.droidkaigi.confsched2022.testing.RobotTestRule +import javax.inject.Inject + +class SearchScreenRobot @Inject constructor() { + + context(RobotTestRule) + fun typeQuery(query: String) { + composeTestRule.onNodeWithTag("search").performTextReplacement(query) + } + + context(RobotTestRule) + fun checkSessionExists(title: String) { + composeTestRule.onAllNodes( + matcher = hasTestTag("session") and hasText(title), + ).assertAny(hasText(title)) + } + + context(RobotTestRule) + fun clearInput() { + composeTestRule.onNodeWithTag("clearInput").performClick() + } + + operator fun invoke( + robotTestRule: RobotTestRule, + function: context(RobotTestRule) SearchScreenRobot.() -> Unit + ) { + robotTestRule.composeTestRule.setContent { + SearchRoot(onItemClick = {}) + } + function(robotTestRule, this@SearchScreenRobot) + } +} diff --git a/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenTest.kt b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenTest.kt new file mode 100644 index 000000000..b358355ea --- /dev/null +++ b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenTest.kt @@ -0,0 +1,34 @@ +package io.github.droidkaigi.confsched2022.feature.sessions + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import dagger.hilt.android.testing.HiltAndroidTest +import io.github.droidkaigi.confsched2022.model.DroidKaigiSchedule +import io.github.droidkaigi.confsched2022.model.TimetableItem.Session +import io.github.droidkaigi.confsched2022.model.fake +import io.github.droidkaigi.confsched2022.testing.RobotTestRule +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import javax.inject.Inject + +@RunWith(AndroidJUnit4::class) +@HiltAndroidTest +class SearchScreenTest { + + @get:Rule val robotTestRule = RobotTestRule(this) + @Inject lateinit var searchScreenRobot: SearchScreenRobot + + @Test + fun searchWithResult() { + searchScreenRobot(robotTestRule) { + DroidKaigiSchedule.fake().dayToTimetable.forEach { (_, timeTable) -> + timeTable.contents.forEach inner@{ + if (it.timetableItem !is Session) return@inner + val query = it.timetableItem.title.currentLangTitle + typeQuery(query) + checkSessionExists(query) + } + } + } + } +} From 6543b416bb451bd45c6d58992a1f830b97ecdff4 Mon Sep 17 00:00:00 2001 From: yaoandy107 Date: Sat, 1 Oct 2022 18:36:10 +0800 Subject: [PATCH 2/2] Remove unused imports --- .../confsched2022/feature/sessions/SearchScreenRobot.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt index e9ec84823..ca28529ed 100644 --- a/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt +++ b/feature/sessions/src/test/java/io/github/droidkaigi/confsched2022/feature/sessions/SearchScreenRobot.kt @@ -1,15 +1,11 @@ package io.github.droidkaigi.confsched2022.feature.sessions -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.material.Surface -import androidx.compose.ui.Modifier import androidx.compose.ui.test.assertAny import androidx.compose.ui.test.hasTestTag import androidx.compose.ui.test.hasText import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTextReplacement -import io.github.droidkaigi.confsched2022.designsystem.theme.KaigiTheme import io.github.droidkaigi.confsched2022.testing.RobotTestRule import javax.inject.Inject