Skip to content

Commit

Permalink
[WIP] Add search screen testing
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoandy107 committed Oct 1, 2022
1 parent 53ee85d commit 9ccd94d
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -293,7 +294,8 @@ fun SearchTextFieldTopAppBar(
title = {
SearchTextField(
modifier = Modifier
.background(color = backgroundColor),
.background(color = backgroundColor)
.testTag("search"),
searchWord = searchWord,
onSearchWordChange = onSearchWordChange,
onSearchTextAreaClicked = onSearchTextAreaClicked
Expand Down Expand Up @@ -354,6 +356,7 @@ private fun SearchTextField(
placeholder = { Text(stringResource(Strings.search_placeholder)) },
trailingIcon = {
IconButton(
modifier = Modifier.testTag("clearInput"),
onClick = { onSearchWordChange("") }
) {
Icon(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
KaigiTheme {
Surface(modifier = Modifier.fillMaxSize()) {
SearchRoot(onItemClick = {})
}
}
}
function(robotTestRule, this@SearchScreenRobot)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}
}

0 comments on commit 9ccd94d

Please sign in to comment.