-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from KakaoCup/add-dialog-sample
feat(test): add alert dialog sample
- Loading branch information
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
sample/src/androidTest/java/io/github/kakaocup/compose/screen/DialogActivityScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
sample/src/androidTest/java/io/github/kakaocup/compose/test/DialogTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
sample/src/main/java/io/github/kakaocup/compose/sample/DialogActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
) | ||
} | ||
} |