|
| 1 | +package com.trendyol.medusalib.navigator |
| 2 | + |
| 3 | +import androidx.fragment.app.Fragment |
| 4 | +import androidx.fragment.app.testing.launchFragmentInContainer |
| 5 | +import com.google.common.truth.Truth.assertThat |
| 6 | +import com.trendyol.medusalib.TestChildFragment |
| 7 | +import com.trendyol.medusalib.TestParentFragment |
| 8 | +import org.junit.Test |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import org.robolectric.RobolectricTestRunner |
| 11 | + |
| 12 | +@RunWith(RobolectricTestRunner::class) |
| 13 | +class MultipleStackNavigatorBackstackOrderTest { |
| 14 | + |
| 15 | + @Test |
| 16 | + fun `given MultipleStackNavigator with empty stack and null as tag, when calling getFragmentIndexInStackBySameType, should return -1`() { |
| 17 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 18 | + // Given |
| 19 | + val sut = MultipleStackNavigator( |
| 20 | + fragmentManager = fragment.childFragmentManager, |
| 21 | + containerId = TestParentFragment.CONTAINER_ID, |
| 22 | + rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), |
| 23 | + ) |
| 24 | + sut.initialize(null) |
| 25 | + |
| 26 | + // When |
| 27 | + val actual = sut.getFragmentIndexInStackBySameType(null) |
| 28 | + |
| 29 | + // Then |
| 30 | + assertThat(actual).isEqualTo(-1) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `given MultipleStackNavigator with empty stack and nonnull tag, when calling getFragmentIndexInStackBySameType, should return -1`() { |
| 36 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 37 | + // Given |
| 38 | + val sut = MultipleStackNavigator( |
| 39 | + fragmentManager = fragment.childFragmentManager, |
| 40 | + containerId = TestParentFragment.CONTAINER_ID, |
| 41 | + rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), |
| 42 | + ) |
| 43 | + sut.initialize(null) |
| 44 | + |
| 45 | + // When |
| 46 | + val actual = sut.getFragmentIndexInStackBySameType("random-tag") |
| 47 | + |
| 48 | + // Then |
| 49 | + assertThat(actual).isEqualTo(-1) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `given MultipleStackNavigator with stack with single fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for current fragment, should return 0`() { |
| 55 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 56 | + // Given |
| 57 | + val sut = MultipleStackNavigator( |
| 58 | + fragmentManager = fragment.childFragmentManager, |
| 59 | + containerId = TestParentFragment.CONTAINER_ID, |
| 60 | + rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), |
| 61 | + ) |
| 62 | + sut.initialize(null) |
| 63 | + |
| 64 | + sut.start(TestChildFragment.newInstance("child fragment")) |
| 65 | + |
| 66 | + fragment.childFragmentManager.executePendingTransactions() |
| 67 | + |
| 68 | + // When |
| 69 | + val actual = sut.getFragmentIndexInStackBySameType(sut.getCurrentFragment()?.tag) |
| 70 | + |
| 71 | + // Then |
| 72 | + assertThat(actual).isEqualTo(0) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `given MultipleStackNavigator with stack with multiple fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for first child fragment, should return 2`() { |
| 78 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 79 | + // Given |
| 80 | + val sut = MultipleStackNavigator( |
| 81 | + fragmentManager = fragment.childFragmentManager, |
| 82 | + containerId = TestParentFragment.CONTAINER_ID, |
| 83 | + rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), |
| 84 | + ) |
| 85 | + sut.initialize(null) |
| 86 | + |
| 87 | + val fragments = mutableListOf<Fragment>() |
| 88 | + sut.observeDestinationChanges(fragment.viewLifecycleOwner) { |
| 89 | + fragments.add(it) |
| 90 | + } |
| 91 | + |
| 92 | + sut.start(TestChildFragment.newInstance("child fragment 1")) |
| 93 | + sut.start(TestChildFragment.newInstance("child fragment 2")) |
| 94 | + sut.start(TestChildFragment.newInstance("child fragment 3")) |
| 95 | + fragment.childFragmentManager.executePendingTransactions() |
| 96 | + |
| 97 | + // When |
| 98 | + val actual = sut.getFragmentIndexInStackBySameType(fragments[1].tag) |
| 99 | + |
| 100 | + // Then |
| 101 | + assertThat(actual).isEqualTo(2) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `given MultipleStackNavigator with stack with multiple fragment and nonnull tag, when calling getFragmentIndexInStackBySameType for last child fragment, should return 0`() { |
| 107 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 108 | + // Given |
| 109 | + val sut = MultipleStackNavigator( |
| 110 | + fragmentManager = fragment.childFragmentManager, |
| 111 | + containerId = TestParentFragment.CONTAINER_ID, |
| 112 | + rootFragmentProvider = listOf({ TestChildFragment.newInstance("root 1") }), |
| 113 | + ) |
| 114 | + sut.initialize(null) |
| 115 | + |
| 116 | + val fragments = mutableListOf<Fragment>() |
| 117 | + sut.observeDestinationChanges(fragment.viewLifecycleOwner) { |
| 118 | + fragments.add(it) |
| 119 | + } |
| 120 | + |
| 121 | + sut.start(TestChildFragment.newInstance("child fragment 1")) |
| 122 | + sut.start(TestChildFragment.newInstance("child fragment 2")) |
| 123 | + fragment.childFragmentManager.executePendingTransactions() |
| 124 | + |
| 125 | + // When |
| 126 | + val actual = sut.getFragmentIndexInStackBySameType(fragments[2].tag) |
| 127 | + |
| 128 | + // Then |
| 129 | + assertThat(actual).isEqualTo(0) |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + 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`() { |
| 135 | + launchFragmentInContainer<TestParentFragment>().onFragment { fragment -> |
| 136 | + // Given |
| 137 | + val sut = MultipleStackNavigator( |
| 138 | + fragmentManager = fragment.childFragmentManager, |
| 139 | + containerId = TestParentFragment.CONTAINER_ID, |
| 140 | + rootFragmentProvider = listOf( |
| 141 | + { TestChildFragment.newInstance("root 1") }, |
| 142 | + { TestChildFragment.newInstance("root 2") }, |
| 143 | + ), |
| 144 | + ) |
| 145 | + sut.initialize(null) |
| 146 | + |
| 147 | + val fragments = mutableListOf<Fragment>() |
| 148 | + sut.observeDestinationChanges(fragment.viewLifecycleOwner) { |
| 149 | + fragments.add(it) |
| 150 | + } |
| 151 | + |
| 152 | + sut.start(TestChildFragment.newInstance("child fragment 1")) |
| 153 | + sut.start(TestChildFragment.newInstance("child fragment 2")) |
| 154 | + sut.switchTab(1) |
| 155 | + sut.start(TestChildFragment.newInstance("child fragment 1")) |
| 156 | + sut.start(TestChildFragment.newInstance("child fragment 1")) |
| 157 | + |
| 158 | + fragment.childFragmentManager.executePendingTransactions() |
| 159 | + |
| 160 | + // When |
| 161 | + val actual = sut.getFragmentIndexInStackBySameType(fragments[4].tag) |
| 162 | + |
| 163 | + // Then |
| 164 | + assertThat(actual).isEqualTo(1) |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments