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

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

package org.robolectric.android.controller;

import static org.robolectric.util.ReflectionHelpers.ClassParameter.from;

import android.app.ActivityThread;
import android.app.Application;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.IBinder;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;

public class ServiceController extends ComponentController, T> {

  public static  ServiceController of(T service, Intent intent) {
    ServiceController controller = new ServiceController<>(service, intent);
    controller.attach();
    return controller;
  }

  private ServiceController(T service, Intent intent) {
    super(service, intent);
  }

  private ServiceController attach() {
    if (attached) {
      return this;
    }
    // make sure the component is enabled
    Context context = RuntimeEnvironment.getApplication().getBaseContext();
    ComponentName name =
        new ComponentName(context.getPackageName(), component.getClass().getName());
    context
        .getPackageManager()
        .setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);

    ReflectionHelpers.callInstanceMethod(
        Service.class,
        component,
        "attach",
        from(Context.class, RuntimeEnvironment.getApplication().getBaseContext()),
        from(ActivityThread.class, null),
        from(String.class, component.getClass().getSimpleName()),
        from(IBinder.class, null),
        from(Application.class, RuntimeEnvironment.getApplication()),
        from(Object.class, null));

    attached = true;
    return this;
  }

  public ServiceController bind() {
    invokeWhilePaused("onBind", from(Intent.class, getIntent()));
    shadowMainLooper.idleIfPaused();
    return this;
  }

  @Override public ServiceController create() {
    invokeWhilePaused("onCreate");
    shadowMainLooper.idleIfPaused();
    return this;
  }

  @Override public ServiceController destroy() {
    invokeWhilePaused("onDestroy");
    shadowMainLooper.idleIfPaused();
    return this;
  }

  public ServiceController rebind() {
    invokeWhilePaused("onRebind", from(Intent.class, getIntent()));
    shadowMainLooper.idleIfPaused();
    return this;
  }

  public ServiceController startCommand(int flags, int startId) {
    invokeWhilePaused(
        "onStartCommand",
        from(Intent.class, getIntent()),
        from(int.class, flags),
        from(int.class, startId));
    shadowMainLooper.idleIfPaused();
    return this;
  }

  public ServiceController unbind() {
    invokeWhilePaused("onUnbind", from(Intent.class, getIntent()));
    shadowMainLooper.idleIfPaused();
    return this;
  }

  /**
   * @deprecated Use the appropriate builder in {@link org.robolectric.Robolectric} instead.
   *
   * This method will be removed in Robolectric 3.6.
   */
  @Deprecated
  public ServiceController withIntent(Intent intent) {
    this.intent = intent;
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy