Skip to content

Commit

Permalink
Merge pull request #107 from KakaoCup/add-dialog-sample
Browse files Browse the repository at this point in the history
feat(test): add alert dialog sample
  • Loading branch information
Vacxe authored Dec 18, 2024
2 parents b1ed8c7 + 84cd8b7 commit fe9db29
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.github.kakaocup.compose.screen

import androidx.compose.ui.test.SemanticsNodeInteractionsProvider
import io.github.kakaocup.compose.node.element.ComposeScreen
import io.github.kakaocup.compose.node.element.KNode
import io.github.kakaocup.compose.sample.DialogActivity

private typealias Tags = DialogActivity.TestTags

class DialogActivityScreen(semanticsProvider: SemanticsNodeInteractionsProvider? = null) :
ComposeScreen<DialogActivityScreen>(
semanticsProvider = semanticsProvider,
viewBuilderAction = { hasTestTag(Tags.Screen) }
) {
val showAlertDialogButton: KNode = child {
hasTestTag(Tags.ShowAlertDialogButton)
}
val alertDialog: KNode = KNode {
hasTestTag(Tags.AlertDialog)
hasAnyAncestor(androidx.compose.ui.test.isDialog())
}

val dismissAlertDialogButton: KNode = KNode {
hasTestTag(Tags.AlertDialogDismissButton)
hasAnyAncestor(androidx.compose.ui.test.isDialog())
}

val status: KNode = child {
hasTestTag(Tags.StatusText)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.kakaocup.compose.test

import androidx.compose.ui.test.junit4.createAndroidComposeRule
import io.github.kakaocup.compose.node.element.ComposeScreen.Companion.onComposeScreen
import io.github.kakaocup.compose.rule.KakaoComposeTestRule
import io.github.kakaocup.compose.sample.DialogActivity
import io.github.kakaocup.compose.screen.DialogActivityScreen
import org.junit.Rule
import org.junit.Test

class DialogTest {

@get:Rule
val composeTestRule = createAndroidComposeRule<DialogActivity>()

@get:Rule
val kakaoComposeTestRule = KakaoComposeTestRule(
semanticsProvider = composeTestRule,
useUnmergedTree = true
)

@Test
fun openAndDismissAlertDialogTest() {
onComposeScreen<DialogActivityScreen> {
status.assertTextEquals("No Status")
alertDialog.assertIsNotDisplayed()

showAlertDialogButton.performClick()
status.assertTextEquals("Dialog Opened")
alertDialog.assertIsDisplayed()

dismissAlertDialogButton.performClick()
status.assertTextEquals("Dialog Dismissed")
alertDialog.assertIsNotDisplayed()
}
}
}
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<activity android:name=".CustomSemanticPropertyKeyActivity" android:exported="true" />
<activity android:name=".ButtonActivity" android:exported="true" />
<activity android:name=".ProgressIndicatorActivity" android:exported="true" />
<activity android:name=".DialogActivity" android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package io.github.kakaocup.compose.sample

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.AlertDialog
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.testTag
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class DialogActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Screen()
}
}

object TestTags {
const val Screen = "Screen"
const val StatusText = "StatusText"
const val ShowAlertDialogButton = "ShowAlertDiaglog"
const val AlertDialog = "AlertDialog"
const val AlertDialogDismissButton = "AlertDialogDismissButton"
}

@Composable
private fun Screen() {

val openAlertDialog = remember { mutableStateOf(false) }
val status = remember { mutableStateOf("No Status") }

when {
openAlertDialog.value -> {
AlertDialogExample(
onDismissRequest = {
status.value = "Dialog Dismissed"
openAlertDialog.value = false
},
onConfirmation = {
status.value = "Dialog Confirmed"
openAlertDialog.value = false
},
dialogTitle = "Alert dialog example",
dialogText = "This is an example of an alert dialog with buttons.",
)
}
}

Column(
modifier = Modifier
.semantics { testTag = TestTags.Screen }
) {
Text(
text = status.value,
modifier = Modifier
.padding(8.dp)
.semantics { testTag = TestTags.StatusText }
)

Button(
content = {
Text(text = "Show Alert Dialog")
},
onClick = {
status.value = "Dialog Opened"
openAlertDialog.value = true
},
modifier = Modifier
.padding(8.dp)
.semantics { testTag = TestTags.ShowAlertDialogButton }
)
}

}

@Composable
@Preview(showSystemUi = true)
private fun TextScreenScreenPreview() {
MaterialTheme {
Screen()
}
}

@Composable
fun AlertDialogExample(
onDismissRequest: () -> Unit,
onConfirmation: () -> Unit,
dialogTitle: String,
dialogText: String,
) {
AlertDialog(
modifier = Modifier.testTag(TestTags.AlertDialog),
title = {
Text(text = dialogTitle)
},
text = {
Text(text = dialogText)
},
onDismissRequest = {
onDismissRequest()
},
confirmButton = {
TextButton(
onClick = {
onConfirmation()
}
) {
Text("Confirm")
}
},
dismissButton = {
TextButton(
modifier = Modifier.testTag(TestTags.AlertDialogDismissButton),
onClick = {
onDismissRequest()
}
) {
Text("Dismiss")
}
}
)
}
}

0 comments on commit fe9db29

Please sign in to comment.