• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private boolean added;
29 
setView(View view)30     public void setView(View view) {
31         this.view = view;
32     }
33 
setActivity(FragmentActivity activity)34     public void setActivity(FragmentActivity activity) {
35         this.activity = activity;
36     }
37 
38     @Implementation
getView()39     public View getView() {
40         return view;
41     }
42 
43     @Implementation
getActivity()44     public FragmentActivity getActivity() {
45         return activity;
46     }
47 
48     @Implementation
startActivity(Intent intent)49     public void startActivity(Intent intent) {
50         new FragmentActivity().startActivity(intent);
51     }
52 
53     @Implementation
startActivityForResult(Intent intent, int requestCode)54     public void startActivityForResult(Intent intent, int requestCode) {
55         activity.startActivityForResult(intent, requestCode);
56     }
57 
58     @Implementation
getFragmentManager()59     public FragmentManager getFragmentManager() {
60         return activity.getSupportFragmentManager();
61     }
62 
63     @Implementation
getChildFragmentManager()64     public FragmentManager getChildFragmentManager() {
65         // Doesn't follow support library behavior, but is correct enough
66         // for robolectric v1.
67         return getFragmentManager();
68     }
69 
70     @Implementation
getTag()71     public String getTag() {
72         return tag;
73     }
74 
75     @Implementation
getResources()76     public Resources getResources() {
77         if (activity == null) {
78             throw new IllegalStateException("Fragment " + this + " not attached to Activity");
79         }
80         return activity.getResources();
81     }
82 
setTag(String tag)83     public void setTag(String tag) {
84         this.tag = tag;
85     }
86 
setSavedInstanceState(Bundle savedInstanceState)87     public void setSavedInstanceState(Bundle savedInstanceState) {
88         this.savedInstanceState = savedInstanceState;
89     }
90 
getSavedInstanceState()91     public Bundle getSavedInstanceState() {
92         return savedInstanceState;
93     }
94 
setContainerViewId(int containerViewId)95     public void setContainerViewId(int containerViewId) {
96         this.containerViewId = containerViewId;
97     }
98 
getContainerViewId()99     public int getContainerViewId() {
100         return containerViewId;
101     }
102 
setShouldReplace(boolean shouldReplace)103     public void setShouldReplace(boolean shouldReplace) {
104         this.shouldReplace = shouldReplace;
105     }
106 
getShouldReplace()107     public boolean getShouldReplace() {
108         return shouldReplace;
109     }
110 
111     @Implementation
getArguments()112     public Bundle getArguments() {
113         return arguments;
114     }
115 
116     @Implementation
setArguments(Bundle arguments)117     public void setArguments(Bundle arguments) {
118         this.arguments = arguments;
119     }
120 
121     @Implementation
getString(int resId)122     public final String getString(int resId) {
123         if (activity == null) {
124           throw new IllegalStateException("Fragment " + this + " not attached to Activity");
125         }
126         return activity.getString(resId);
127     }
128 
129     @Implementation
getString(int resId, Object... formatArgs)130     public final String getString(int resId, Object... formatArgs) {
131         if (activity == null) {
132           throw new IllegalStateException("Fragment " + this + " not attached to Activity");
133         }
134         return activity.getString(resId, formatArgs);
135     }
136 
setAttached(boolean isAttached)137     public void setAttached(boolean isAttached) {
138         attached = isAttached;
139     }
140 
isAttached()141     public boolean isAttached() {
142         return attached;
143     }
144 
setHidden(boolean isHidden)145     public void setHidden(boolean isHidden) {
146         hidden = isHidden;
147     }
148 
setAdded(boolean isAdded)149     public void setAdded(boolean isAdded) {
150         added = isAdded;
151     }
152 
153     @Implementation
isAdded()154     public boolean isAdded() {
155         return getActivity() != null && added;
156     }
157 
158     @Implementation
isHidden()159     public boolean isHidden() {
160         return hidden;
161     }
162 
163     @Implementation
isVisible()164     public boolean isVisible() {
165         return isAdded() && !isHidden() && getView() != null
166                 && getView().getVisibility() == View.VISIBLE;
167     }
168 }
169