androidTest: Add tests for reusable composable
* Add semantics wherever required to make composable easy to test Signed-off-by: Aayush Gupta <aayushgupta219@gmail.com>
This commit is contained in:
29
app/src/androidTest/java/com/aurora/store/IsolatedTest.kt
Normal file
29
app/src/androidTest/java/com/aurora/store/IsolatedTest.kt
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.test.junit4.createComposeRule
|
||||
import com.aurora.store.compose.theme.AuroraTheme
|
||||
import org.junit.Rule
|
||||
|
||||
/**
|
||||
* Class that provides helper methods to test isolated composable
|
||||
*/
|
||||
abstract class IsolatedTest {
|
||||
|
||||
@get:Rule
|
||||
val composeTestRule = createComposeRule()
|
||||
|
||||
/**
|
||||
* Sets given composable as content with default theme
|
||||
*/
|
||||
fun setContent(content: @Composable () -> Unit) {
|
||||
composeTestRule.setContent {
|
||||
AuroraTheme(content = content)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.composable
|
||||
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.test.assertHasClickAction
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotEnabled
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import com.aurora.store.IsolatedTest
|
||||
import com.aurora.store.R
|
||||
import org.junit.Test
|
||||
|
||||
class ErrorTest : IsolatedTest() {
|
||||
|
||||
@Test
|
||||
fun testErrorWithoutActionHandling() {
|
||||
setContent {
|
||||
Error(
|
||||
painter = painterResource(R.drawable.ic_apps_outage),
|
||||
message = "An error occurred!",
|
||||
actionMessage = "Retry"
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("Retry")
|
||||
.assertIsDisplayed()
|
||||
.assertHasClickAction()
|
||||
.assertIsNotEnabled()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.composable
|
||||
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.test.assertHasClickAction
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotEnabled
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import androidx.compose.ui.text.AnnotatedString
|
||||
import com.aurora.store.IsolatedTest
|
||||
import com.aurora.store.R
|
||||
import org.junit.Test
|
||||
|
||||
class InfoTest: IsolatedTest() {
|
||||
|
||||
@Test
|
||||
fun testInfoWithoutClickHandling() {
|
||||
setContent {
|
||||
Info(title = AnnotatedString(text = stringResource(R.string.app_name)))
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("Aurora Store")
|
||||
.assertIsDisplayed()
|
||||
.assertHasClickAction()
|
||||
.assertIsNotEnabled()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.composable
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsEnabled
|
||||
import androidx.compose.ui.test.assertIsNotEnabled
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import com.aurora.store.IsolatedTest
|
||||
import com.aurora.store.R
|
||||
import com.aurora.store.data.model.Permission
|
||||
import com.aurora.store.data.model.PermissionType
|
||||
import org.junit.Test
|
||||
|
||||
class PermissionListItemTest : IsolatedTest() {
|
||||
|
||||
private val permission: Permission
|
||||
@Composable
|
||||
get() = Permission(
|
||||
PermissionType.STORAGE_MANAGER,
|
||||
stringResource(R.string.onboarding_permission_esm),
|
||||
stringResource(R.string.onboarding_permission_esa_desc),
|
||||
)
|
||||
|
||||
@Test
|
||||
fun testPermissionNotGranted() {
|
||||
setContent {
|
||||
PermissionListItem(permission = permission.copy(isGranted = false))
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("Grant")
|
||||
.assertIsDisplayed()
|
||||
.assertIsEnabled()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testPermissionGranted() {
|
||||
setContent {
|
||||
PermissionListItem(permission = permission.copy(isGranted = true))
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("Granted")
|
||||
.assertIsDisplayed()
|
||||
.assertIsNotEnabled()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.composable
|
||||
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.onNodeWithContentDescription
|
||||
import androidx.compose.ui.test.onNodeWithText
|
||||
import com.aurora.store.IsolatedTest
|
||||
import org.junit.Test
|
||||
|
||||
class TopAppBarTest : IsolatedTest() {
|
||||
|
||||
@Test
|
||||
fun testTitleWithNoNavigationAction() {
|
||||
setContent {
|
||||
TopAppBar(title = "About", onNavigateUp = null)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithText("About")
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule.onNodeWithContentDescription("Back")
|
||||
.assertDoesNotExist()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 The Calyx Institute
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
package com.aurora.store.compose.composable.app
|
||||
|
||||
import androidx.compose.ui.semantics.ProgressBarRangeInfo
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotDisplayed
|
||||
import androidx.compose.ui.test.assertRangeInfoEquals
|
||||
import androidx.compose.ui.test.onNodeWithTag
|
||||
import com.aurora.store.IsolatedTest
|
||||
import org.junit.Test
|
||||
|
||||
class AnimatedAppIconTest: IsolatedTest() {
|
||||
|
||||
@Test
|
||||
fun testAnimatedAppIconNoProgress() {
|
||||
setContent {
|
||||
AnimatedAppIcon(
|
||||
iconUrl = "https://example.com/icon.png",
|
||||
inProgress = false
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithTag("progressIndicator")
|
||||
.assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAnimatedAppIconProgressAt0() {
|
||||
setContent {
|
||||
AnimatedAppIcon(
|
||||
iconUrl = "https://example.com/icon.png",
|
||||
progress = 0F,
|
||||
inProgress = true
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithTag("progressIndicator")
|
||||
.assertIsDisplayed()
|
||||
.assertRangeInfoEquals(ProgressBarRangeInfo.Indeterminate)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAnimatedAppIconProgressAt50() {
|
||||
setContent {
|
||||
AnimatedAppIcon(
|
||||
iconUrl = "https://example.com/icon.png",
|
||||
progress = 50F,
|
||||
inProgress = true
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule.onNodeWithTag("progressIndicator")
|
||||
.assertIsDisplayed()
|
||||
.assertRangeInfoEquals(ProgressBarRangeInfo(0.5F, 0.00F..1.00F))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user