-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds
getFragmentIndexInStackBySameType
function to Navigator
- Create tests for `getFragmentIndexInStackBySameType` in `MultipleStackNavigatorBackstackOrderTest`. - Use "kotlin-parcelize" plugin for `StackItem`. - Fix comments on `Navigator`. - Update library version to `0.11.0`
- Loading branch information
1 parent
dd46a06
commit 289a75e
Showing
5 changed files
with
219 additions
and
54 deletions.
There are no files selected for viewing
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
30 changes: 3 additions & 27 deletions
30
medusalib/src/main/java/com/trendyol/medusalib/navigator/data/StackItem.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 |
---|---|---|
@@ -1,31 +1,7 @@ | ||
package com.trendyol.medusalib.navigator.data | ||
|
||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
data class StackItem(val fragmentTag: String, val groupName: String = "") : Parcelable { | ||
constructor(parcel: Parcel) : this( | ||
requireNotNull(parcel.readString()), | ||
requireNotNull(parcel.readString()) | ||
) | ||
|
||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeString(fragmentTag) | ||
parcel.writeString(groupName) | ||
} | ||
|
||
override fun describeContents(): Int { | ||
return 0 | ||
} | ||
|
||
companion object CREATOR : Parcelable.Creator<StackItem> { | ||
override fun createFromParcel(parcel: Parcel): StackItem { | ||
return StackItem(parcel) | ||
} | ||
|
||
override fun newArray(size: Int): Array<StackItem?> { | ||
return arrayOfNulls(size) | ||
} | ||
} | ||
|
||
} | ||
@Parcelize | ||
data class StackItem(val fragmentTag: String, val groupName: String = "") : Parcelable |
167 changes: 167 additions & 0 deletions
167
...rc/test/java/com/trendyol/medusalib/navigator/MultipleStackNavigatorBackstackOrderTest.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,167 @@ | ||
package com.trendyol.medusalib.navigator | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import com.google.common.truth.Truth.assertThat | ||
import com.trendyol.medusalib.TestChildFragment | ||
import com.trendyol.medusalib.TestParentFragment | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class MultipleStackNavigatorBackstackOrderTest { | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with empty stack and null as tag, when calling getFragmentIndexInStackBySameType, should return -1`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), | ||
) | ||
sut.initialize(null) | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType(null) | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(-1) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with empty stack and nonnull tag, when calling getFragmentIndexInStackBySameType, should return -1`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), | ||
) | ||
sut.initialize(null) | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType("random-tag") | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(-1) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with stack with single fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for current fragment, should return 0`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), | ||
) | ||
sut.initialize(null) | ||
|
||
sut.start(TestChildFragment.newInstance("child fragment")) | ||
|
||
fragment.childFragmentManager.executePendingTransactions() | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType(sut.getCurrentFragment()?.tag) | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(0) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with stack with multiple fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for first child fragment, should return 2`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), | ||
) | ||
sut.initialize(null) | ||
|
||
val fragments = mutableListOf<Fragment>() | ||
sut.observeDestinationChanges(fragment.viewLifecycleOwner) { | ||
fragments.add(it) | ||
} | ||
|
||
sut.start(TestChildFragment.newInstance("child fragment 1")) | ||
sut.start(TestChildFragment.newInstance("child fragment 2")) | ||
sut.start(TestChildFragment.newInstance("child fragment 3")) | ||
fragment.childFragmentManager.executePendingTransactions() | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType(fragments[1].tag) | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(2) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with stack with multiple fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for last child fragment, should return 0`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), | ||
) | ||
sut.initialize(null) | ||
|
||
val fragments = mutableListOf<Fragment>() | ||
sut.observeDestinationChanges(fragment.viewLifecycleOwner) { | ||
fragments.add(it) | ||
} | ||
|
||
sut.start(TestChildFragment.newInstance("child fragment 1")) | ||
sut.start(TestChildFragment.newInstance("child fragment 2")) | ||
fragment.childFragmentManager.executePendingTransactions() | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType(fragments[2].tag) | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(0) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given MultipleStackNavigator with stack with multiple root fragments and nonnull tag and switch tab, when calling getFragmentIndexInStackBySameType for first child in switched tab, should return 1`() { | ||
launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> | ||
// Given | ||
val sut = MultipleStackNavigator( | ||
fragmentManager = fragment.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf( | ||
{ TestChildFragment.newInstance("root 1") }, | ||
{ TestChildFragment.newInstance("root 2") }, | ||
), | ||
) | ||
sut.initialize(null) | ||
|
||
val fragments = mutableListOf<Fragment>() | ||
sut.observeDestinationChanges(fragment.viewLifecycleOwner) { | ||
fragments.add(it) | ||
} | ||
|
||
sut.start(TestChildFragment.newInstance("child fragment 1")) | ||
sut.start(TestChildFragment.newInstance("child fragment 2")) | ||
sut.switchTab(1) | ||
sut.start(TestChildFragment.newInstance("child fragment 1")) | ||
sut.start(TestChildFragment.newInstance("child fragment 1")) | ||
|
||
fragment.childFragmentManager.executePendingTransactions() | ||
|
||
// When | ||
val actual = sut.getFragmentIndexInStackBySameType(fragments[4].tag) | ||
|
||
// Then | ||
assertThat(actual).isEqualTo(1) | ||
} | ||
} | ||
} |