Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FragmentScenario closeable #121

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion fragment/fragment-testing/api/current.txt
Original file line number Diff line number Diff line change
@@ -1,7 1,8 @@
// Signature format: 4.0
package androidx.fragment.app.testing {

public final class FragmentScenario<F extends androidx.fragment.app.Fragment> {
public final class FragmentScenario<F extends androidx.fragment.app.Fragment> implements java.io.Closeable {
method public void close();
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, @StyleRes int themeResId, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, optional android.os.Bundle? fragmentArgs, optional @StyleRes int themeResId, optional androidx.lifecycle.Lifecycle.State initialState, optional androidx.fragment.app.FragmentFactory? factory);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 1,8 @@
// Signature format: 4.0
package androidx.fragment.app.testing {

public final class FragmentScenario<F extends androidx.fragment.app.Fragment> {
public final class FragmentScenario<F extends androidx.fragment.app.Fragment> implements java.io.Closeable {
method public void close();
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, @StyleRes int themeResId, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, optional android.os.Bundle? fragmentArgs, optional @StyleRes int themeResId, optional androidx.lifecycle.Lifecycle.State initialState, optional androidx.fragment.app.FragmentFactory? factory);
Expand Down
3 changes: 2 additions & 1 deletion fragment/fragment-testing/api/restricted_current.txt
Original file line number Diff line number Diff line change
@@ -1,7 1,8 @@
// Signature format: 4.0
package androidx.fragment.app.testing {

public final class FragmentScenario<F extends androidx.fragment.app.Fragment> {
public final class FragmentScenario<F extends androidx.fragment.app.Fragment> implements java.io.Closeable {
method public void close();
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, android.os.Bundle? fragmentArgs, @StyleRes int themeResId, androidx.fragment.app.FragmentFactory? factory);
method public static <F extends androidx.fragment.app.Fragment> androidx.fragment.app.testing.FragmentScenario<F> launch(Class<F> fragmentClass, optional android.os.Bundle? fragmentArgs, optional @StyleRes int themeResId, optional androidx.lifecycle.Lifecycle.State initialState, optional androidx.fragment.app.FragmentFactory? factory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 26,8 @@ import androidx.fragment.testing.test.R.style.ThemedFragmentTheme
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.Lifecycle.State
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import androidx.test.core.app.ApplicationProvider.getApplicationContext
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
Expand Down Expand Up @@ -483,4 485,20 @@ class FragmentScenarioTest {
openActionBarOverflowOrOptionsMenu(getApplicationContext())
onView(withText("Item1")).check(matches(isDisplayed()))
}

@Test
fun autoCloseFragment() {
var destroyed = false
launchFragment<StateRecordingFragment>().use {
it.onFragment { fragment ->
fragment.lifecycle.addObserver(object : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
destroyed = true
}
})
}
}
assertThat(destroyed).isTrue()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import java.io.Closeable

@Deprecated(
"Superseded by launchFragment that takes an initialState",
Expand Down Expand Up @@ -236,7 237,7 @@ public class FragmentScenario<F : Fragment> private constructor(
@Suppress("MemberVisibilityCanBePrivate") /* synthetic access */
internal val fragmentClass: Class<F>,
private val activityScenario: ActivityScenario<EmptyFragmentActivity>
) {
) : Closeable {

/**
* An empty activity inheriting FragmentActivity. This Activity is used to host Fragment in
Expand Down Expand Up @@ -398,6 399,14 @@ public class FragmentScenario<F : Fragment> private constructor(
return this
}

/**
* Finishes the managed fragments and cleans up device's state. This method blocks execution
* until the host activity becomes [Lifecycle.State.DESTROYED].
*/
public override fun close() {
activityScenario.close()
}

public companion object {
private const val FRAGMENT_TAG = "FragmentScenario_Fragment_Tag"

Expand Down