Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.content.Intent;
      4 import android.content.res.Resources;
      5 import android.os.Bundle;
      6 import android.support.v4.app.Fragment;
      7 import android.support.v4.app.FragmentActivity;
      8 import android.support.v4.app.FragmentManager;
      9 import android.view.View;
     10 import com.xtremelabs.robolectric.internal.Implementation;
     11 import com.xtremelabs.robolectric.internal.Implements;
     12 import com.xtremelabs.robolectric.internal.RealObject;
     13 
     14 @Implements(Fragment.class)
     15 public class ShadowFragment {
     16     @RealObject
     17     protected Fragment realFragment;
     18 
     19     protected View view;
     20     protected FragmentActivity activity;
     21     private String tag;
     22     private Bundle savedInstanceState;
     23     private int containerViewId;
     24     private boolean shouldReplace;
     25     private Bundle arguments;
     26     private boolean attached;
     27     private boolean hidden;
     28 
     29     public void setView(View view) {
     30         this.view = view;
     31     }
     32 
     33     public void setActivity(FragmentActivity activity) {
     34         this.activity = activity;
     35     }
     36 
     37     @Implementation
     38     public View getView() {
     39         return view;
     40     }
     41 
     42     @Implementation
     43     public FragmentActivity getActivity() {
     44         return activity;
     45     }
     46 
     47     @Implementation
     48     public void startActivity(Intent intent) {
     49         new FragmentActivity().startActivity(intent);
     50     }
     51 
     52     @Implementation
     53     public void startActivityForResult(Intent intent, int requestCode) {
     54         activity.startActivityForResult(intent, requestCode);
     55     }
     56 
     57     @Implementation
     58     final public FragmentManager getFragmentManager() {
     59         return activity.getSupportFragmentManager();
     60     }
     61 
     62     @Implementation
     63     public String getTag() {
     64         return tag;
     65     }
     66 
     67     @Implementation
     68     public Resources getResources() {
     69         if (activity == null) {
     70             throw new IllegalStateException("Fragment " + this + " not attached to Activity");
     71         }
     72         return activity.getResources();
     73     }
     74 
     75     public void setTag(String tag) {
     76         this.tag = tag;
     77     }
     78 
     79     public void setSavedInstanceState(Bundle savedInstanceState) {
     80         this.savedInstanceState = savedInstanceState;
     81     }
     82 
     83     public Bundle getSavedInstanceState() {
     84         return savedInstanceState;
     85     }
     86 
     87     public void setContainerViewId(int containerViewId) {
     88         this.containerViewId = containerViewId;
     89     }
     90 
     91     public int getContainerViewId() {
     92         return containerViewId;
     93     }
     94 
     95     public void setShouldReplace(boolean shouldReplace) {
     96         this.shouldReplace = shouldReplace;
     97     }
     98 
     99     public boolean getShouldReplace() {
    100         return shouldReplace;
    101     }
    102 
    103     @Implementation
    104     public Bundle getArguments() {
    105         return arguments;
    106     }
    107 
    108     @Implementation
    109     public void setArguments(Bundle arguments) {
    110         this.arguments = arguments;
    111     }
    112 
    113     @Implementation
    114     public final String getString(int resId) {
    115         if (activity == null) {
    116           throw new IllegalStateException("Fragment " + this + " not attached to Activity");
    117         }
    118         return activity.getString(resId);
    119     }
    120 
    121     @Implementation
    122     public final String getString(int resId, Object... formatArgs) {
    123         if (activity == null) {
    124           throw new IllegalStateException("Fragment " + this + " not attached to Activity");
    125         }
    126         return activity.getString(resId, formatArgs);
    127     }
    128 
    129     public void setAttached(boolean isAttached) {
    130         attached = isAttached;
    131     }
    132 
    133     public boolean isAttached() {
    134         return attached;
    135     }
    136 
    137     public void setHidden(boolean isHidden) {
    138         hidden = isHidden;
    139     }
    140 
    141     @Implementation
    142     public boolean isHidden() {
    143         return hidden;
    144     }
    145 }
    146