FragmentScenario
class FragmentScenario<F : Fragment> : Closeable
FragmentScenario provides API to start and drive a Fragment's lifecycle state for testing. It works with arbitrary fragments and works consistently across different versions of the Android framework.
FragmentScenario only supports androidx.fragment.app.Fragment. If you are using a deprecated fragment class such as android.support.v4.app.Fragment or android.app.Fragment, please update your code to androidx.fragment.app.Fragment.
If your testing Fragment has a dependency to specific theme such as Theme.AppCompat, use the theme ID parameter in launch method.
| Parameters | |
|---|---|
<F : Fragment> |
The Fragment class being tested |
| See also | |
|---|---|
ActivityScenario |
a scenario API for Activity |
Summary
Nested types |
|---|
fun interface FragmentScenario.FragmentAction<F : Fragment>FragmentAction interface should be implemented by any class whose instances are intended to be executed by the main thread. |
Public companion functions |
|
|---|---|
FragmentScenario<F> |
<F : Fragment> launch(Launches a Fragment with given arguments hosted by an empty |
FragmentScenario<F> |
<F : Fragment> launch(Launches a Fragment with given arguments hosted by an empty |
FragmentScenario<F> |
<F : Fragment> launch(Launches a Fragment with given arguments hosted by an empty |
FragmentScenario<F> |
<F : Fragment> launchInContainer(Launches a Fragment in the Activity's root view container |
FragmentScenario<F> |
<F : Fragment> launchInContainer(Launches a Fragment in the Activity's root view container |
FragmentScenario<F> |
<F : Fragment> launchInContainer(Launches a Fragment in the Activity's root view container |
Public functions |
|
|---|---|
open Unit |
close()Finishes the managed fragments and cleans up device's state. |
FragmentScenario<F> |
moveToState(newState: Lifecycle.State)Moves Fragment state to a new state. |
FragmentScenario<F> |
onFragment(action: FragmentScenario.FragmentAction<F>)Runs a given |
FragmentScenario<F> |
recreate()Recreates the host Activity. |
Extension functions |
|
|---|---|
inline T |
<F : Fragment, T : Any> FragmentScenario<F>.withFragment(Run |
Public companion functions
launch
fun <F : Fragment> launch(
fragmentClass: Class<F>,
fragmentArgs: Bundle?,
factory: FragmentFactory?
): FragmentScenario<F>
Launches a Fragment with given arguments hosted by an empty FragmentActivity using the given FragmentFactory and waits for it to reach the resumed state.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? |
a bundle to passed into fragment |
factory: FragmentFactory? |
a fragment factory to use or null to use default factory |
launch
fun <F : Fragment> launch(
fragmentClass: Class<F>,
fragmentArgs: Bundle?,
themeResId: @StyleRes Int,
factory: FragmentFactory?
): FragmentScenario<F>
Launches a Fragment with given arguments hosted by an empty FragmentActivity themed by themeResId, using the given FragmentFactory and waits for it to reach the resumed state.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? |
a bundle to passed into fragment |
themeResId: @StyleRes Int |
a style resource id to be set to the host activity's theme |
factory: FragmentFactory? |
a fragment factory to use or null to use default factory |
launch
fun <F : Fragment> launch(
fragmentClass: Class<F>,
fragmentArgs: Bundle? = null,
themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
initialState: Lifecycle.State = Lifecycle.State.RESUMED,
factory: FragmentFactory? = null
): FragmentScenario<F>
Launches a Fragment with given arguments hosted by an empty FragmentActivity themed by themeResId, using the given FragmentFactory and waits for it to reach initialState.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? = null |
a bundle to passed into fragment |
themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme |
a style resource id to be set to the host activity's theme |
initialState: Lifecycle.State = Lifecycle.State.RESUMED |
The initial |
factory: FragmentFactory? = null |
a fragment factory to use or null to use default factory |
launchInContainer
fun <F : Fragment> launchInContainer(
fragmentClass: Class<F>,
fragmentArgs: Bundle?,
factory: FragmentFactory?
): FragmentScenario<F>
Launches a Fragment in the Activity's root view container android.R.id.content, with given arguments hosted by an empty FragmentActivity using the given FragmentFactory and waits for it to reach the resumed state.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? |
a bundle to passed into fragment |
factory: FragmentFactory? |
a fragment factory to use or null to use default factory |
launchInContainer
fun <F : Fragment> launchInContainer(
fragmentClass: Class<F>,
fragmentArgs: Bundle?,
themeResId: @StyleRes Int,
factory: FragmentFactory?
): FragmentScenario<F>
Launches a Fragment in the Activity's root view container android.R.id.content, with given arguments hosted by an empty FragmentActivity themed by themeResId, using the given FragmentFactory and waits for it to reach the resumed state.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? |
a bundle to passed into fragment |
themeResId: @StyleRes Int |
a style resource id to be set to the host activity's theme |
factory: FragmentFactory? |
a fragment factory to use or null to use default factory |
launchInContainer
fun <F : Fragment> launchInContainer(
fragmentClass: Class<F>,
fragmentArgs: Bundle? = null,
themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
initialState: Lifecycle.State = Lifecycle.State.RESUMED,
factory: FragmentFactory? = null
): FragmentScenario<F>
Launches a Fragment in the Activity's root view container android.R.id.content, with given arguments hosted by an empty FragmentActivity themed by themeResId, using the given FragmentFactory and waits for it to reach initialState.
This method cannot be called from the main thread.
| Parameters | |
|---|---|
fragmentClass: Class<F> |
a fragment class to instantiate |
fragmentArgs: Bundle? = null |
a bundle to passed into fragment |
themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme |
a style resource id to be set to the host activity's theme |
initialState: Lifecycle.State = Lifecycle.State.RESUMED |
The initial |
factory: FragmentFactory? = null |
a fragment factory to use or null to use default factory |
Public functions
close
open fun close(): Unit
Finishes the managed fragments and cleans up device's state. This method blocks execution until the host activity becomes Lifecycle.State.DESTROYED.
moveToState
fun moveToState(newState: Lifecycle.State): FragmentScenario<F>
Moves Fragment state to a new state.
If a new state and current state are the same, this method does nothing. It accepts all Lifecycle.States. DESTROYED is a terminal state. You cannot move to any other state after the Fragment reaches that state.
This method cannot be called from the main thread.
onFragment
fun onFragment(action: FragmentScenario.FragmentAction<F>): FragmentScenario<F>
Runs a given action on the current Activity's main thread.
Note that you should never keep Fragment reference passed into your action because it can be recreated at anytime during state transitions.
Throwing an exception from action makes the host Activity crash. You can inspect the exception in logcat outputs.
This method cannot be called from the main thread.
recreate
fun recreate(): FragmentScenario<F>
Recreates the host Activity.
After this method call, it is ensured that the Fragment state goes back to the same state as its previous state.
This method cannot be called from the main thread.
Extension functions
FragmentScenario.withFragment
inline fun <F : Fragment, T : Any> FragmentScenario<F>.withFragment(
crossinline block: F.() -> T
): T
Run block using FragmentScenario.onFragment, returning the result of the block.
If any exceptions are raised while running block, they are rethrown.