-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for
BackHandler
composable
- Loading branch information
Showing
4 changed files
with
208 additions
and
8 deletions.
There are no files selected for viewing
187 changes: 187 additions & 0 deletions
187
redwood-compose/src/commonTest/kotlin/app/cash/redwood/compose/BackHandlerTest.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,187 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package app.cash.redwood.compose | ||
|
||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import app.cash.redwood.layout.compose.Box | ||
import app.cash.redwood.testing.TestRedwoodComposition | ||
import app.cash.redwood.testing.WidgetValue | ||
import app.cash.redwood.ui.Cancellable | ||
import app.cash.redwood.ui.OnBackPressedCallback | ||
import app.cash.redwood.ui.OnBackPressedDispatcher | ||
import assertk.assertFailure | ||
import assertk.assertThat | ||
import assertk.assertions.contains | ||
import assertk.assertions.isEqualTo | ||
import assertk.assertions.isInstanceOf | ||
import assertk.assertions.single | ||
import com.example.redwood.testing.compose.Text | ||
import com.example.redwood.testing.widget.TestSchemaTester | ||
import com.example.redwood.testing.widget.TextValue | ||
import kotlin.test.Test | ||
import kotlinx.coroutines.TimeoutCancellationException | ||
import kotlinx.coroutines.test.runTest | ||
|
||
private val throwingOnBack = { error("Only the innermost enabled back handler should be invoked.") } | ||
|
||
class BackHandlerTest { | ||
@Test | ||
fun enabledBackHandler() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
var backCounter by remember { mutableStateOf(0) } | ||
BackHandler { | ||
backCounter++ | ||
} | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).single().isEqualTo(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertThat(awaitSnapshot()).single().isEqualTo(TextValue(text = "1")) | ||
} | ||
} | ||
|
||
@Test | ||
fun disabledBackHandler() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
val backCounter by remember { mutableStateOf(0) } | ||
BackHandler(enabled = false, throwingOnBack) | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).single().isEqualTo(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertNoSnapshot() | ||
} | ||
} | ||
|
||
@Test | ||
fun outermostEnabledAndInnermostEnabledBackHandlers() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
var backCounter by remember { mutableStateOf(0) } | ||
BackHandler(enabled = false, throwingOnBack) | ||
Box { | ||
BackHandler { | ||
backCounter += 1 | ||
} | ||
} | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).contains(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertThat(awaitSnapshot()).contains(TextValue(text = "1")) | ||
} | ||
} | ||
|
||
@Test | ||
fun outermostEnabledAndInnermostDisabledBackHandlers() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
var backCounter by remember { mutableStateOf(0) } | ||
BackHandler { | ||
backCounter += 1 | ||
} | ||
Box { | ||
BackHandler(enabled = false, throwingOnBack) | ||
} | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).contains(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertThat(awaitSnapshot()).contains(TextValue(text = "1")) | ||
} | ||
} | ||
|
||
@Test | ||
fun outermostDisabledAndInnermostEnabledBackHandlers() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
var backCounter by remember { mutableStateOf(0) } | ||
BackHandler(enabled = false, throwingOnBack) | ||
Box { | ||
BackHandler { | ||
backCounter += 1 | ||
} | ||
} | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).contains(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertThat(awaitSnapshot()).contains(TextValue(text = "1")) | ||
} | ||
} | ||
|
||
@Test | ||
fun outermostDisabledAndInnermostDisabledBackHandlers() = runTest { | ||
val onBackPressedDispatcher = FakeOnBackPressedDispatcher() | ||
TestSchemaTester(onBackPressedDispatcher) { | ||
setContent { | ||
val backCounter by remember { mutableStateOf(0) } | ||
BackHandler(enabled = false, throwingOnBack) | ||
Box { | ||
BackHandler(enabled = false, throwingOnBack) | ||
} | ||
Text(backCounter.toString()) | ||
} | ||
|
||
assertThat(awaitSnapshot()).contains(TextValue(text = "0")) | ||
onBackPressedDispatcher.onBackPressed() | ||
assertNoSnapshot() | ||
} | ||
} | ||
} | ||
|
||
private suspend fun TestRedwoodComposition<List<WidgetValue>>.assertNoSnapshot() { | ||
assertFailure { awaitSnapshot() }.isInstanceOf<TimeoutCancellationException>() | ||
} | ||
|
||
private class FakeOnBackPressedDispatcher : OnBackPressedDispatcher { | ||
private val callbacks = mutableListOf<OnBackPressedCallback>() | ||
|
||
override fun addCallback(onBackPressedCallback: OnBackPressedCallback): Cancellable { | ||
callbacks += onBackPressedCallback | ||
|
||
return object : Cancellable { | ||
var canceled = false | ||
|
||
override fun cancel() { | ||
if (canceled) return | ||
canceled = true | ||
|
||
callbacks -= onBackPressedCallback | ||
} | ||
} | ||
} | ||
|
||
fun onBackPressed() { | ||
val callbackToNotify = callbacks.lastOrNull { it.isEnabled } | ||
callbackToNotify?.handleOnBackPressed() | ||
} | ||
} |
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
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
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