androidx.fragment.app.testing


Interfaces

FragmentScenario.FragmentAction

FragmentAction interface should be implemented by any class whose instances are intended to be executed by the main thread.

Classes

FragmentScenario

FragmentScenario provides API to start and drive a Fragment's lifecycle state for testing.

Top-level functions summary

inline FragmentScenario<F>
<F : Fragment> launchFragment(
    fragmentArgs: Bundle?,
    themeResId: @StyleRes Int,
    initialState: Lifecycle.State,
    factory: FragmentFactory?
)

Launches a Fragment with given arguments hosted by an empty FragmentActivity using given FragmentFactory and waits for it to reach initialState.

inline FragmentScenario<F>
<F : Fragment> launchFragment(
    fragmentArgs: Bundle?,
    themeResId: @StyleRes Int,
    initialState: Lifecycle.State,
    crossinline instantiate: () -> F
)

Launches a Fragment with given arguments hosted by an empty FragmentActivity using instantiate to create the Fragment and waits for it to reach initialState.

inline FragmentScenario<F>
<F : Fragment> launchFragmentInContainer(
    fragmentArgs: Bundle?,
    themeResId: @StyleRes Int,
    initialState: Lifecycle.State,
    factory: FragmentFactory?
)

Launches a Fragment in the Activity's root view container android.R.id.content, with given arguments hosted by an empty FragmentActivity and waits for it to reach initialState.

inline FragmentScenario<F>
<F : Fragment> launchFragmentInContainer(
    fragmentArgs: Bundle?,
    themeResId: @StyleRes Int,
    initialState: Lifecycle.State,
    crossinline instantiate: () -> F
)

Launches a Fragment in the Activity's root view container android.R.id.content, with given arguments hosted by an empty FragmentActivity using instantiate to create the Fragment and waits for it to reach initialState.

Extension functions summary

inline T
<F : Fragment, T : Any> FragmentScenario<F>.withFragment(
    crossinline block: F.() -> T
)

Run block using FragmentScenario.onFragment, returning the result of the block.

Top-level functions

inline fun <F : Fragment> launchFragment(
    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 using given FragmentFactory and waits for it to reach initialState.

This method cannot be called from the main thread.

Parameters
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 Lifecycle.State. Passing in DESTROYED will result in an IllegalArgumentException.

factory: FragmentFactory? = null

a fragment factory to use or null to use default factory

inline fun <F : Fragment> launchFragment(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline instantiate: () -> F
): FragmentScenario<F>

Launches a Fragment with given arguments hosted by an empty FragmentActivity using instantiate to create the Fragment and waits for it to reach initialState.

This method cannot be called from the main thread.

Parameters
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 Lifecycle.State. Passing in DESTROYED will result in an IllegalArgumentException.

crossinline instantiate: () -> F

method which will be used to instantiate the Fragment.

launchFragmentInContainer

inline fun <F : Fragment> launchFragmentInContainer(
    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 and waits for it to reach initialState.

This method cannot be called from the main thread.

Parameters
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 Lifecycle.State. Passing in DESTROYED will result in an IllegalArgumentException.

factory: FragmentFactory? = null

a fragment factory to use or null to use default factory

launchFragmentInContainer

inline fun <F : Fragment> launchFragmentInContainer(
    fragmentArgs: Bundle? = null,
    themeResId: @StyleRes Int = R.style.FragmentScenarioEmptyFragmentActivityTheme,
    initialState: Lifecycle.State = Lifecycle.State.RESUMED,
    crossinline instantiate: () -> F
): 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 instantiate to create the Fragment and waits for it to reach initialState.

This method cannot be called from the main thread.

Parameters
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 Lifecycle.State. Passing in DESTROYED will result in an IllegalArgumentException.

crossinline instantiate: () -> F

method which will be used to instantiate the Fragment. This is a simplification of the FragmentFactory interface for cases where only a single class needs a custom constructor called.

Extension functions

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.