Skip to content

Commit

Permalink
Add dismiss Popup by Esc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan committed Jul 27, 2023
1 parent 8aa4d09 commit cc776f2
Showing 1 changed file with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,17 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.keyEvent
import androidx.compose.ui.layout.Layout
import androidx.compose.ui.test.isPopup
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performKeyPress
import androidx.compose.ui.test.runSkikoComposeUiTest
import androidx.compose.ui.unit.dp
import com.google.common.truth.Truth.assertThat
import org.junit.Rule
Expand Down Expand Up @@ -147,4 +156,100 @@ class DesktopPopupTest {

rule.waitForIdle()
}

@Test
fun dismissPopupByEscWithBackPressProperty() {
var onDismissRequestCallCount = 0
rule.setContent {
Popup(
onDismissRequest = { onDismissRequestCallCount++ },
properties = PopupProperties(
focusable = true,
dismissOnBackPress = true
)
) {
Box(Modifier)
}
}

rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyDown))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(1)
rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyUp))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(1)
}

@Test
fun doNotDismissPopupByEscWithoutBackPressProperty() {
var onDismissRequestCallCount = 0
rule.setContent {
Popup(
onDismissRequest = { onDismissRequestCallCount++ },
properties = PopupProperties(
focusable = true,
dismissOnBackPress = false
)
) {
Box(Modifier)
}
}

rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyDown))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(0)
rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyUp))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(0)
}

@Test
fun dismissPopupByEscOnNotConsumedKeyEvent() {
var onDismissRequestCallCount = 0
rule.setContent {
Popup(
focusable = true,
onDismissRequest = { onDismissRequestCallCount++ },
onKeyEvent = { false }
) {
Box(Modifier)
}
}

rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyDown))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(1)
rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyUp))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(1)
}

@Test
fun doNotDismissPopupByEscOnConsumedKeyEvent() {
var onDismissRequestCallCount = 0
rule.setContent {
Popup(
focusable = true,
onDismissRequest = { onDismissRequestCallCount++ },
onKeyEvent = { true }
) {
Box(Modifier)
}
}

rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyDown))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(0)
rule.onNode(isPopup())
.performKeyPress(keyEvent(Key.Escape, KeyEventType.KeyUp))
rule.waitForIdle()
assertThat(onDismissRequestCallCount).isEqualTo(0)
}
}

0 comments on commit cc776f2

Please sign in to comment.