-
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.
Merge pull request #56 from Trendyol/FixIllegalStateExceptionConcurre…
…ntTransaction FixIllegalStateExceptionConcurrentTransaction
- Loading branch information
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
Oops, something went wrong.