1 package org.robolectric.android.controller; 2 3 import android.content.Intent; 4 import android.os.Looper; 5 import org.robolectric.RuntimeEnvironment; 6 import org.robolectric.shadow.api.Shadow; 7 import org.robolectric.shadows.ShadowLooper; 8 import org.robolectric.util.ReflectionHelpers; 9 import org.robolectric.util.ReflectionHelpers.ClassParameter; 10 11 public abstract class ComponentController<C extends ComponentController<C, T>, T> { 12 protected final C myself; 13 protected T component; 14 protected final ShadowLooper shadowMainLooper; 15 16 protected Intent intent; 17 18 protected boolean attached; 19 20 @SuppressWarnings("unchecked") ComponentController(T component, Intent intent)21 public ComponentController(T component, Intent intent) { 22 this(component); 23 this.intent = intent; 24 } 25 26 @SuppressWarnings("unchecked") ComponentController(T component)27 public ComponentController(T component) { 28 myself = (C) this; 29 this.component = component; 30 shadowMainLooper = Shadow.extract(Looper.getMainLooper()); 31 } 32 get()33 public T get() { 34 return component; 35 } 36 create()37 public abstract C create(); 38 destroy()39 public abstract C destroy(); 40 getIntent()41 public Intent getIntent() { 42 Intent intent = this.intent == null ? new Intent(RuntimeEnvironment.application, component.getClass()) : this.intent; 43 if (intent.getComponent() == null) { 44 intent.setClass(RuntimeEnvironment.application, component.getClass()); 45 } 46 return intent; 47 } 48 invokeWhilePaused(final String methodName, final ClassParameter<?>... classParameters)49 protected C invokeWhilePaused(final String methodName, final ClassParameter<?>... classParameters) { 50 shadowMainLooper.runPaused(new Runnable() { 51 @Override 52 public void run() { 53 ReflectionHelpers.callInstanceMethod(component, methodName, classParameters); 54 } 55 }); 56 return myself; 57 } 58 } 59