diff --git a/medusalib/src/androidTest/java/com/trendyol/medusalib/TestChildFragment.kt b/medusalib/src/androidTest/java/com/trendyol/medusalib/TestChildFragment.kt index 535844b..45b9fb2 100644 --- a/medusalib/src/androidTest/java/com/trendyol/medusalib/TestChildFragment.kt +++ b/medusalib/src/androidTest/java/com/trendyol/medusalib/TestChildFragment.kt @@ -26,6 +26,8 @@ class TestChildFragment : Fragment() { } } + fun requireArgumentTitle() = requireArguments().getString(KEY_TITLE)!! + companion object { fun newInstance(title: String): TestChildFragment { return TestChildFragment().apply { diff --git a/medusalib/src/androidTest/java/com/trendyol/medusalib/TestParentWithNavigatorFragment.kt b/medusalib/src/androidTest/java/com/trendyol/medusalib/TestParentWithNavigatorFragment.kt new file mode 100644 index 0000000..6f9af47 --- /dev/null +++ b/medusalib/src/androidTest/java/com/trendyol/medusalib/TestParentWithNavigatorFragment.kt @@ -0,0 +1,50 @@ +package com.trendyol.medusalib + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import androidx.fragment.app.Fragment +import com.trendyol.medusalib.navigator.MultipleStackNavigator +import com.trendyol.medusalib.navigator.Navigator +import com.trendyol.medusalib.navigator.NavigatorConfiguration +import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction +import com.trendyol.medusalib.navigator.transaction.TransactionType + +class TestParentWithNavigatorFragment : Fragment() { + + lateinit var navigator: Navigator + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + navigator = createNavigator( + { TestChildFragment.newInstance("Root1") }, + { TestChildFragment.newInstance("Root2") }, + { TestChildFragment.newInstance("Root3") } + ) + navigator.initialize(savedInstanceState) + return FrameLayout(requireContext()).apply { id = CONTAINER_ID } + } + + fun createNavigator( + vararg rootFragments: () -> TestChildFragment + ): MultipleStackNavigator { + return MultipleStackNavigator( + fragmentManager = this.childFragmentManager, + containerId = TestParentFragment.CONTAINER_ID, + rootFragmentProvider = rootFragments.toList(), + navigatorConfiguration = NavigatorConfiguration( + defaultNavigatorTransaction = NavigatorTransaction(TransactionType.SHOW_HIDE) + ) + ) + } + + override fun onSaveInstanceState(outState: Bundle) { + navigator.onSaveInstanceState(outState) + super.onSaveInstanceState(outState) + } + + companion object { + const val CONTAINER_ID = 1_000 + } +} \ No newline at end of file diff --git a/medusalib/src/androidTest/java/com/trendyol/medusalib/navigator/ActivityRecreationResetTabTest.kt b/medusalib/src/androidTest/java/com/trendyol/medusalib/navigator/ActivityRecreationResetTabTest.kt new file mode 100644 index 0000000..ca86364 --- /dev/null +++ b/medusalib/src/androidTest/java/com/trendyol/medusalib/navigator/ActivityRecreationResetTabTest.kt @@ -0,0 +1,48 @@ +package com.trendyol.medusalib.navigator + +import androidx.fragment.app.testing.launchFragmentInContainer +import androidx.lifecycle.Lifecycle +import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.google.common.truth.Truth +import com.trendyol.medusalib.TestChildFragment +import com.trendyol.medusalib.TestParentWithNavigatorFragment +import org.junit.Test +import org.junit.runner.RunWith + + +@RunWith(AndroidJUnit4::class) +class ActivityRecreationResetTabTest { + + @Test + fun givenWithMultipleStackNavigatorWhenresetCurrentTabIsCalledAfterActionRecreationThenRootFragmentMustBevVisible() { + // Given + launchFragmentInContainer( + initialState = Lifecycle.State.INITIALIZED + ) + .moveToState(Lifecycle.State.RESUMED) + .onFragment { + it.navigator.switchTab(2) + it.childFragmentManager.executePendingTransactions() + + it.navigator.start(TestChildFragment.newInstance("Root3-1")) + it.childFragmentManager.executePendingTransactions() + + it.navigator.start(TestChildFragment.newInstance("Root3-2")) + it.childFragmentManager.executePendingTransactions() + } + // When + .recreate() + // Then + .onFragment { + it.navigator.resetCurrentTab(resetRootFragment = false) + it.childFragmentManager.executePendingTransactions() + Truth.assertThat( + it + .childFragmentManager + .fragments.first { (it as TestChildFragment).requireArgumentTitle() == "Root3" } + .isVisible + ).isTrue() + } + + } +} \ No newline at end of file diff --git a/medusalib/src/main/java/com/trendyol/medusalib/navigator/MultipleStackNavigator.kt b/medusalib/src/main/java/com/trendyol/medusalib/navigator/MultipleStackNavigator.kt index 62df4cd..a6a7842 100755 --- a/medusalib/src/main/java/com/trendyol/medusalib/navigator/MultipleStackNavigator.kt +++ b/medusalib/src/main/java/com/trendyol/medusalib/navigator/MultipleStackNavigator.kt @@ -164,7 +164,7 @@ open class MultipleStackNavigator( val upperFragment: Fragment? = fragmentManagerController.getFragment(upperFragmentTag) val newDestination: Fragment = upperFragment ?: getRootFragment(currentTabIndex) - val newDestinationTag: String = tagCreator.create(newDestination) + val newDestinationTag: String = newDestination.tag ?: tagCreator.create(newDestination) newDestination.observeFragmentLifecycle( onViewCreated = ::onFragmentViewCreated,