Skip to content

Commit

Permalink
Temp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Laimiux committed Sep 9, 2024
1 parent b568c21 commit 7378154
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ abstract class ComposeViewFactory<RenderModel : Any> : ViewFactory<RenderModel>
return FeatureView(
view = view,
setOutput = outputRelay::accept,
lifecycleCallbacks = null,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.fragment.app.Fragment
import com.instacart.formula.FormulaAndroid
import com.instacart.formula.android.internal.FormulaFragmentDelegate
import com.instacart.formula.android.internal.getFormulaFragmentId
import com.instacart.formula.android.internal.getOrSetArguments
import java.lang.Exception

class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
Expand All @@ -18,7 +19,7 @@ class FormulaFragment : Fragment(), BaseFormulaFragment<Any> {
@JvmStatic
fun newInstance(key: FragmentKey): FormulaFragment {
val fragment = FormulaFragment()
fragment.arguments = Bundle().apply {
fragment.getOrSetArguments().apply {
putParcelable(ARG_CONTRACT, key)
}
FormulaAndroid.fragmentEnvironment().fragmentDelegate.onNewInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import com.instacart.formula.android.ViewFactory
internal object FormulaFragmentDelegate {
fun viewFactory(fragment: FormulaFragment): ViewFactory<Any>? {
val appManager = FormulaAndroid.appManagerOrThrow()

val activity = fragment.activity ?: run {
fragmentEnvironment().logger("FormulaFragment has no activity attached: ${fragment.getFragmentKey()}")
return null
}

val activity = fragment.requireActivity()
val viewFactory = appManager.findStore(activity)?.viewFactory(fragment) ?: run {
// Log view factory is missing
if (activity.isDestroyed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,7 @@ private fun Fragment.getFragmentKey(): FragmentKey {
*/
private fun Fragment.getFragmentInstanceId(): String {
return if (this is BaseFormulaFragment<*>) {
val arguments = arguments ?: run {
Bundle().apply {
arguments = this
}
}
val arguments = getOrSetArguments()
val id = arguments.getString(FormulaFragment.ARG_FORMULA_ID, "")
if (id.isNullOrBlank()) {
val initializedId = UUID.randomUUID().toString()
Expand All @@ -68,4 +64,12 @@ internal fun Fragment.getFormulaFragmentId(): FragmentId {
instanceId = getFragmentInstanceId(),
key = getFragmentKey()
)
}

internal fun Fragment.getOrSetArguments(): Bundle {
return arguments ?: run {
Bundle().apply {
arguments = this
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.instacart.formula.android

import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import com.instacart.formula.android.events.ActivityResult
import com.instacart.formula.android.events.FragmentLifecycleEvent
import com.instacart.formula.android.fakes.MainKey
import org.junit.Test

Expand All @@ -13,8 +16,8 @@ class FragmentDataClassTest {
key = fragmentKey
)

Truth.assertThat(fragmentId.instanceId).isEqualTo("instanceId")
Truth.assertThat(fragmentId.key).isEqualTo(fragmentKey)
assertThat(fragmentId.instanceId).isEqualTo("instanceId")
assertThat(fragmentId.key).isEqualTo(fragmentKey)
}

@Test fun fragmentOutput() {
Expand All @@ -23,7 +26,33 @@ class FragmentDataClassTest {
key = key,
renderModel = Unit
)
Truth.assertThat(output.key).isEqualTo(key)
Truth.assertThat(output.renderModel).isEqualTo(Unit)
assertThat(output.key).isEqualTo(key)
assertThat(output.renderModel).isEqualTo(Unit)
}

@Test fun activityResult() {
val result = ActivityResult(
requestCode = 0,
resultCode = 1,
data = null
)
assertThat(result.requestCode).isEqualTo(0)
assertThat(result.resultCode).isEqualTo(1)
assertThat(result.data).isNull()
}

@Test fun fragmentLifecycleEventRemoved() {
val fragmentKey = MainKey(id = 1)
val fragmentId = FragmentId(
instanceId = "instanceId",
key = fragmentKey
)

val event = FragmentLifecycleEvent.Removed(
fragmentId = fragmentId,
lastState = "last-state"
)
assertThat(event.fragmentId).isEqualTo(fragmentId)
assertThat(event.lastState).isEqualTo("last-state")
}
}

0 comments on commit 7378154

Please sign in to comment.