All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.robolectric.android.controller.FragmentController Maven / Gradle / Ivy

package org.robolectric.android.controller;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import androidx.test.runner.lifecycle.ActivityLifecycleCallback;
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import androidx.test.runner.lifecycle.Stage;
import org.robolectric.util.ReflectionHelpers;

/**
 * FragmentController provides low-level APIs to control fragment's lifecycle.
 *
 * @param  a class of the fragment which is under control by this class.
 * @deprecated Native Fragments have been deprecated in Android P. Android encourages developers to
 *     use androidx fragments, to test these use FragmentScenario.
 */
@Deprecated
public class FragmentController
    extends ComponentController, F> {
  private F fragment;
  private final ActivityController activityController;

  public static  FragmentController of(F fragment) {
    return of(fragment, FragmentControllerActivity.class, null, null);
  }

  public static  FragmentController of(F fragment, Class activityClass) {
    return of(fragment, activityClass, null, null);
  }

  public static  FragmentController of(F fragment, Intent intent) {
    return new FragmentController<>(fragment, FragmentControllerActivity.class, intent);
  }

  public static  FragmentController of(F fragment, Bundle arguments) {
    return new FragmentController<>(fragment, FragmentControllerActivity.class, arguments);
  }

  public static  FragmentController of(F fragment, Intent intent, Bundle arguments) {
    return new FragmentController<>(fragment, FragmentControllerActivity.class, intent,
            arguments);
  }

  public static  FragmentController of(F fragment, Class activityClass, Intent intent) {
    return new FragmentController<>(fragment, activityClass, intent);
  }

  public static  FragmentController of(F fragment, Class activityClass, Bundle arguments) {
    return new FragmentController<>(fragment, activityClass, arguments);
  }

  public static  FragmentController of(F fragment, Class activityClass,
                                                              Intent intent, Bundle arguments) {
    return new FragmentController<>(fragment, activityClass, intent, arguments);
  }

  private FragmentController(F fragment, Class activityClass, Intent intent) {
    this(fragment, activityClass, intent, null);
  }

  private FragmentController(F fragment, Class activityClass, Bundle arguments) {
    this(fragment, activityClass, null, arguments);
  }

  private FragmentController(F fragment, Class activityClass,
                             Intent intent, Bundle arguments) {
    super(fragment, intent);
    this.fragment = fragment;
    if (arguments != null) {
      this.fragment.setArguments(arguments);
    }
    this.activityController = ActivityController.of(ReflectionHelpers.callConstructor(activityClass), intent);
  }

  /**
   * Creates the activity with {@link Bundle} and adds the fragment to the view with ID {@code contentViewId}.
   */
  public FragmentController create(final int contentViewId, final Bundle bundle) {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.create(bundle).get().getFragmentManager().beginTransaction().add(contentViewId, fragment).commit();
      }
    });
    return this;
  }

  /**
   * Creates the activity with {@link Bundle} and adds the fragment to it. Note that the fragment will be added to the view with ID 1.
   */
  public FragmentController create(Bundle bundle) {
    return create(1, bundle);
  }

  @Override
  public FragmentController create() {
    return create(null);
  }

  @Override
  public FragmentController destroy() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.destroy();
      }
    });
    return this;
  }

  public FragmentController start() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.start();
      }
    });
    return this;
  }

  public FragmentController resume() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.resume();
      }
    });
    return this;
  }

  public FragmentController pause() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.pause();
      }
    });
    return this;
  }

  public FragmentController visible() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.visible();
      }
    });
    return this;
  }

  public FragmentController stop() {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.stop();
      }
    });
    return this;
  }

  public FragmentController saveInstanceState(final Bundle outState) {
    shadowMainLooper.runPaused(new Runnable() {
      @Override
      public void run() {
        activityController.saveInstanceState(outState);
      }
    });
    return this;
  }

  public FragmentController recreate() {
    return recreate((F) ReflectionHelpers.callConstructor(fragment.getClass()), 1);
  }

  public FragmentController recreate(final F recreatedFragment, final int contentViewId) {
    ActivityLifecycleCallback fragmentCreateCallback =
        new ActivityLifecycleCallback() {
          @Override
          public void onActivityLifecycleChanged(Activity activity, Stage stage) {
            if (Stage.CREATED.equals(stage)) {
              activity
                  .getFragmentManager()
                  .beginTransaction()
                  .add(contentViewId, recreatedFragment)
                  .commit();
              FragmentController.this.fragment = recreatedFragment;
              FragmentController.this.component = recreatedFragment;
            }
          }
        };
    ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(fragmentCreateCallback);

    shadowMainLooper.runPaused(
        new Runnable() {
          @Override
          public void run() {
            activityController.recreate();
          }
        });

    ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(fragmentCreateCallback);
    return this;
  }

  private static class FragmentControllerActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout view = new LinearLayout(this);
      view.setId(1);

      setContentView(view);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy