-
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.
Loading status checks…
Merge remote-tracking branch 'origin/master'
Showing
10 changed files
with
332 additions
and
31 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
36 changes: 36 additions & 0 deletions
36
medusalib/src/androidTest/java/com/trendyol/medusalib/TestChildFragment.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,36 @@ | ||
package com.trendyol.medusalib | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.fragment.app.Fragment | ||
|
||
private const val KEY_TITLE = "title" | ||
|
||
class TestChildFragment : Fragment() { | ||
|
||
var onFragmentVisibleAgain: (() -> Unit)? = null | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
return TextView(requireContext()).apply { | ||
text = requireArguments().getString(KEY_TITLE) | ||
} | ||
} | ||
|
||
override fun onHiddenChanged(hidden: Boolean) { | ||
super.onHiddenChanged(hidden) | ||
if (hidden.not() && view != null) { | ||
onFragmentVisibleAgain?.invoke() | ||
} | ||
} | ||
|
||
companion object { | ||
fun newInstance(title: String): TestChildFragment { | ||
return TestChildFragment().apply { | ||
arguments = Bundle().apply { putString(KEY_TITLE, title) } | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
medusalib/src/androidTest/java/com/trendyol/medusalib/TestParentFragment.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,19 @@ | ||
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 | ||
|
||
class TestParentFragment : Fragment() { | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||
return FrameLayout(requireContext()).apply { id = CONTAINER_ID } | ||
} | ||
|
||
companion object { | ||
const val CONTAINER_ID = 1_000 | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
medusalib/src/androidTest/java/com/trendyol/medusalib/navigator/ConcurrentTransactionTest.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,83 @@ | ||
package com.trendyol.medusalib.navigator | ||
|
||
import androidx.fragment.app.testing.FragmentScenario | ||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import androidx.fragment.app.testing.withFragment | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.test.espresso.Espresso.onView | ||
import androidx.test.espresso.assertion.ViewAssertions.matches | ||
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed | ||
import androidx.test.espresso.matcher.ViewMatchers.withText | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.trendyol.medusalib.TestChildFragment | ||
import com.trendyol.medusalib.TestParentFragment | ||
import com.trendyol.medusalib.navigator.transaction.NavigatorTransaction | ||
import com.trendyol.medusalib.navigator.transaction.TransactionType | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import java.util.concurrent.CountDownLatch | ||
|
||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ConcurrentTransactionTest { | ||
|
||
@Test | ||
fun givenNavigatorWithShowAndHideWhenFragmentResetsTheCurrentTabAndStartsAnotherFragmentThenItMustNotThrowAnyExceptions() { | ||
// Given | ||
val transaction = CountDownLatch(1) | ||
var caughtException: Throwable? = null | ||
var navigator: Navigator? = null | ||
val scenario = launchFragmentInContainer<TestParentFragment>( | ||
initialState = Lifecycle.State.INITIALIZED | ||
) | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
val rootFragment = TestChildFragment.newInstance("Root") | ||
val expectedFragment = TestChildFragment.newInstance("ExpectedFragment") | ||
|
||
scenario.withFragment { navigator = createNavigator(rootFragment = rootFragment) } | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
|
||
// When | ||
rootFragment.onFragmentVisibleAgain = { | ||
resetTabAndStartFragment(navigator!!, expectedFragment) | ||
.onSuccess { transaction.countDown() } | ||
.onFailure { | ||
caughtException = it | ||
transaction.countDown() | ||
} | ||
} | ||
scenario.startAndDismissAFragment(navigator!!) | ||
|
||
transaction.await() | ||
caughtException?.let { throw it } | ||
scenario.moveToState(Lifecycle.State.RESUMED) | ||
onView(withText("ExpectedFragment")).check(matches(isDisplayed())) | ||
} | ||
|
||
private fun FragmentScenario<TestParentFragment>.startAndDismissAFragment(navigator: Navigator) { | ||
onFragment { navigator.start(TestChildFragment.newInstance("SecondFragment")) } | ||
moveToState(Lifecycle.State.RESUMED) | ||
onFragment { navigator.goBack() } | ||
} | ||
|
||
private fun resetTabAndStartFragment( | ||
navigator: Navigator, | ||
expectedFragment: TestChildFragment | ||
): Result<Unit> { | ||
return runCatching { | ||
navigator.resetCurrentTab(true) | ||
navigator.start(expectedFragment) | ||
} | ||
} | ||
|
||
private fun TestParentFragment.createNavigator(rootFragment: TestChildFragment): MultipleStackNavigator { | ||
return MultipleStackNavigator( | ||
fragmentManager = this.childFragmentManager, | ||
containerId = TestParentFragment.CONTAINER_ID, | ||
rootFragmentProvider = listOf({ rootFragment }), | ||
navigatorConfiguration = NavigatorConfiguration( | ||
defaultNavigatorTransaction = NavigatorTransaction(TransactionType.SHOW_HIDE) | ||
) | ||
).apply { this.initialize(null) } | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
medusalib/src/main/java/com/trendyol/medusalib/navigator/controller/StagedFragmentHolder.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,26 @@ | ||
package com.trendyol.medusalib.navigator.controller | ||
|
||
import androidx.fragment.app.Fragment | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleOwner | ||
|
||
internal class StagedFragmentHolder constructor( | ||
private val fragmentsByTags: MutableMap<String, Fragment> | ||
) { | ||
|
||
fun stageFragmentForCommit(tag: String, fragment: Fragment) { | ||
fragmentsByTags.put(tag, fragment) | ||
fragment.lifecycle.addObserver(object : DefaultLifecycleObserver { | ||
|
||
override fun onDestroy(owner: LifecycleOwner) { | ||
fragmentsByTags.remove(tag) | ||
fragment.lifecycle.removeObserver(this) | ||
super.onDestroy(owner) | ||
} | ||
}) | ||
} | ||
|
||
fun getStagedFragment(tag: String): Fragment? { | ||
return fragmentsByTags.get(tag) | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...lib/src/test/java/com/trendyol/medusalib/navigator/controller/StagedFragmentHolderTest.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,69 @@ | ||
package com.trendyol.medusalib.navigator.controller | ||
|
||
import androidx.fragment.app.testing.launchFragmentInContainer | ||
import androidx.fragment.app.testing.withFragment | ||
import com.google.common.truth.Truth | ||
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 StagedFragmentHolderTest { | ||
|
||
@Test | ||
fun `given a fragment for a tag doesnt exist, when getStagedFragment is called, then it must return null`() { | ||
val sut = StagedFragmentHolder(mutableMapOf()) | ||
|
||
val actualFragment = sut.getStagedFragment("missing-fragment") | ||
|
||
Truth.assertThat(actualFragment).isNull() | ||
} | ||
|
||
@Test | ||
fun `given a fragment for a tag exists, when getStagedFragment is called, then it must return that fragment`() { | ||
val sut = StagedFragmentHolder(mutableMapOf()) | ||
|
||
|
||
launchFragmentInContainer { TestParentFragment() }.withFragment { | ||
val fragmentToBeStaged = TestChildFragment.newInstance("title") | ||
childFragmentManager | ||
.beginTransaction() | ||
.add(fragmentToBeStaged, "staged-child") | ||
.commitNow() | ||
|
||
sut.stageFragmentForCommit("staged-child", fragmentToBeStaged) | ||
|
||
val actualFragment = sut.getStagedFragment("staged-child") | ||
|
||
Truth.assertThat(actualFragment).isEqualTo(fragmentToBeStaged) | ||
} | ||
} | ||
|
||
@Test | ||
fun `given a fragment for a tag exists, when it is removed, then it must return null for that tag`() { | ||
val sut = StagedFragmentHolder(mutableMapOf()) | ||
|
||
|
||
launchFragmentInContainer { TestParentFragment() }.withFragment { | ||
val fragmentToBeStaged = TestChildFragment.newInstance("title") | ||
childFragmentManager | ||
.beginTransaction() | ||
.add(fragmentToBeStaged, "staged-child") | ||
.commitNow() | ||
sut.stageFragmentForCommit("staged-child", fragmentToBeStaged) | ||
|
||
childFragmentManager | ||
.beginTransaction() | ||
.remove(fragmentToBeStaged) | ||
.commitNow() | ||
|
||
|
||
val fragment = sut.getStagedFragment("staged-child") | ||
|
||
Truth.assertThat(fragment).isNull() | ||
} | ||
} | ||
|
||
} |