Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ui test #345

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions user/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,5 @@ dependencies {

//Permission
implementation("com.google.accompanist:accompanist-permissions:0.28.0")
androidTestImplementation ("com.google.truth:truth:1.1.4")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.the_chance.honeymart.ui.feature.home.search

import androidx.compose.ui.res.painterResource
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.common.truth.Truth
import org.junit.Rule
import org.junit.Test
import org.the_chance.design_system.R
import org.the_chance.honeymart.ui.feature.home.composables.SearchBar

class SearchBarTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun checkTextIsDisplayed() {
composeTestRule.setContent {
SearchBar(
icon = painterResource(id = R.drawable.ic_search),
text = "search",
onClick = { /*TODO*/ })
}

composeTestRule.onNodeWithText("search").assertIsDisplayed()
}

@Test
fun checkIconIsDisplayed() {
composeTestRule.setContent {
SearchBar(
icon = painterResource(id = R.drawable.ic_search),
onClick = { /*TODO*/ })
}

composeTestRule.onNodeWithContentDescription("SearchIcon").assertIsDisplayed()
}

@Test
fun checkSearchBarClick() {
var isClicked = false

composeTestRule.setContent {
SearchBar(
icon = painterResource(id = R.drawable.ic_search),
onClick = { isClicked = true })
}
composeTestRule.onNodeWithContentDescription("SearchWidget").performClick()
Truth.assertThat(isClicked).isTrue()
}

@Test
fun searchBar_DisplaysText_Search() {
testDisplayText("search")
}

@Test
fun searchBar_DisplaysText_Find() {
testDisplayText("find")
}

@Test
fun searchBar_DisplaysText_Lookup() {
testDisplayText("lookup")
}

private fun testDisplayText(text: String) {
composeTestRule.setContent {
SearchBar(
icon = painterResource(id = R.drawable.ic_search),
text = text,
onClick = { /*TODO*/ }
)
}

composeTestRule.waitForIdle()

composeTestRule.onNodeWithText(text).assertIsDisplayed()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.tooling.preview.Preview
import org.the_chance.design_system.R
import org.the_chance.honymart.ui.theme.HoneyMartTheme
Expand All @@ -28,12 +30,16 @@ import org.the_chance.honymart.ui.theme.dimens
fun SearchBar(
icon: Painter,
onClick: () -> Unit,
text : String = stringResource(R.string.search),
modifier: Modifier = Modifier,
) {
Surface(
modifier = modifier
.clip(shape = MaterialTheme.shapes.medium)
.clickable { onClick() },
.clickable { onClick() }
.semantics {
contentDescription = "SearchWidget"
},
color = MaterialTheme.colorScheme.tertiaryContainer,
shape = MaterialTheme.shapes.medium,
border = BorderStroke(
Expand All @@ -55,12 +61,18 @@ fun SearchBar(
painter = icon,
contentDescription = null,
modifier = Modifier
.size(MaterialTheme.dimens.icon24),
.size(MaterialTheme.dimens.icon24)
.semantics {
contentDescription = "SearchIcon"
},
tint = MaterialTheme.colorScheme.onBackground

)
Text(
text = stringResource(R.string.search),
modifier = Modifier.semantics {
contentDescription = "TextFiled"
},
text = text,
style = Typography.displaySmall.copy(MaterialTheme.colorScheme.onSecondaryContainer),
)
}
Expand Down