From 8ac794e958d635c37b795a74c25fb230bb17b381 Mon Sep 17 00:00:00 2001 From: Laimonas Turauskas Date: Mon, 9 Sep 2024 16:44:27 -0400 Subject: [PATCH] Temp. --- .../android/compose/ComposeViewFactory.kt | 1 + .../test/TestBackCallbackRenderModel.kt | 13 ++++++++ .../instacart/formula/FormulaFragmentTest.kt | 32 +++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 formula-android-tests/src/main/java/com/instacart/formula/test/TestBackCallbackRenderModel.kt diff --git a/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt b/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt index 9e994616..2f6cf898 100644 --- a/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt +++ b/formula-android-compose/src/main/java/com/instacart/formula/android/compose/ComposeViewFactory.kt @@ -24,6 +24,7 @@ abstract class ComposeViewFactory : ViewFactory return FeatureView( view = view, setOutput = outputRelay::accept, + lifecycleCallbacks = null, ) } diff --git a/formula-android-tests/src/main/java/com/instacart/formula/test/TestBackCallbackRenderModel.kt b/formula-android-tests/src/main/java/com/instacart/formula/test/TestBackCallbackRenderModel.kt new file mode 100644 index 00000000..6065ffdb --- /dev/null +++ b/formula-android-tests/src/main/java/com/instacart/formula/test/TestBackCallbackRenderModel.kt @@ -0,0 +1,13 @@ +package com.instacart.formula.test + +import com.instacart.formula.android.BackCallback + +data class TestBackCallbackRenderModel( + private val onBackPressed: () -> Unit, + val blockBackCallback: Boolean = false, +) : BackCallback { + override fun onBackPressed(): Boolean { + this.onBackPressed.invoke() + return blockBackCallback + } +} \ No newline at end of file diff --git a/formula-android-tests/src/test/java/com/instacart/formula/FormulaFragmentTest.kt b/formula-android-tests/src/test/java/com/instacart/formula/FormulaFragmentTest.kt index acd23c2f..e7e1152a 100644 --- a/formula-android-tests/src/test/java/com/instacart/formula/FormulaFragmentTest.kt +++ b/formula-android-tests/src/test/java/com/instacart/formula/FormulaFragmentTest.kt @@ -15,6 +15,7 @@ import com.instacart.formula.android.BackCallback import com.instacart.formula.android.FormulaFragment import com.instacart.formula.android.FragmentEnvironment import com.instacart.formula.android.FragmentStore +import com.instacart.formula.test.TestBackCallbackRenderModel import com.instacart.formula.test.TestKey import com.instacart.formula.test.TestKeyWithId import com.instacart.formula.test.TestFragmentActivity @@ -275,6 +276,37 @@ class FormulaFragmentTest { assertThat(updateThreads).containsExactly(Thread.currentThread()) } + @Test fun `back callback blocks navigation`() { + val key = TestKeyWithId(1) + navigateToTaskDetail(id = key.id) + + Shadows.shadowOf(Looper.getMainLooper()).idle() + + var onBackPressed = 0 + sendStateUpdate(key, TestBackCallbackRenderModel( + onBackPressed = { + onBackPressed += 1 + }, + blockBackCallback = true + )) + + navigateBack() + + // We blocked navigation so visible fragment should still be details + assertThat(onBackPressed).isEqualTo(1) + assertVisibleContract(key) + + sendStateUpdate(key, TestBackCallbackRenderModel( + onBackPressed = { onBackPressed += 1 }, + blockBackCallback = false + )) + + navigateBack() + + assertThat(onBackPressed).isEqualTo(2) + assertVisibleContract(TestKey()) + } + @Test fun `notify fragment environment if setOutput throws an error`() { val key = TestKeyWithId(1) navigateToTaskDetail(id = key.id)