1package com.xtremelabs.robolectric.shadows;
2
3import android.content.Intent;
4import android.content.res.Resources;
5import android.os.Bundle;
6import android.support.v4.app.Fragment;
7import android.support.v4.app.FragmentActivity;
8import android.support.v4.app.FragmentManager;
9import android.view.View;
10import com.xtremelabs.robolectric.internal.Implementation;
11import com.xtremelabs.robolectric.internal.Implements;
12import com.xtremelabs.robolectric.internal.RealObject;
13
14@Implements(Fragment.class)
15public 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
30    public void setView(View view) {
31        this.view = view;
32    }
33
34    public void setActivity(FragmentActivity activity) {
35        this.activity = activity;
36    }
37
38    @Implementation
39    public View getView() {
40        return view;
41    }
42
43    @Implementation
44    public FragmentActivity getActivity() {
45        return activity;
46    }
47
48    @Implementation
49    public void startActivity(Intent intent) {
50        new FragmentActivity().startActivity(intent);
51    }
52
53    @Implementation
54    public void startActivityForResult(Intent intent, int requestCode) {
55        activity.startActivityForResult(intent, requestCode);
56    }
57
58    @Implementation
59    public FragmentManager getFragmentManager() {
60        return activity.getSupportFragmentManager();
61    }
62
63    @Implementation
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
71    public String getTag() {
72        return tag;
73    }
74
75    @Implementation
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
83    public void setTag(String tag) {
84        this.tag = tag;
85    }
86
87    public void setSavedInstanceState(Bundle savedInstanceState) {
88        this.savedInstanceState = savedInstanceState;
89    }
90
91    public Bundle getSavedInstanceState() {
92        return savedInstanceState;
93    }
94
95    public void setContainerViewId(int containerViewId) {
96        this.containerViewId = containerViewId;
97    }
98
99    public int getContainerViewId() {
100        return containerViewId;
101    }
102
103    public void setShouldReplace(boolean shouldReplace) {
104        this.shouldReplace = shouldReplace;
105    }
106
107    public boolean getShouldReplace() {
108        return shouldReplace;
109    }
110
111    @Implementation
112    public Bundle getArguments() {
113        return arguments;
114    }
115
116    @Implementation
117    public void setArguments(Bundle arguments) {
118        this.arguments = arguments;
119    }
120
121    @Implementation
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
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
137    public void setAttached(boolean isAttached) {
138        attached = isAttached;
139    }
140
141    public boolean isAttached() {
142        return attached;
143    }
144
145    public void setHidden(boolean isHidden) {
146        hidden = isHidden;
147    }
148
149    public void setAdded(boolean isAdded) {
150        added = isAdded;
151    }
152
153    @Implementation
154    public boolean isAdded() {
155        return getActivity() != null && added;
156    }
157
158    @Implementation
159    public boolean isHidden() {
160        return hidden;
161    }
162
163    @Implementation
164    public boolean isVisible() {
165        return isAdded() && !isHidden() && getView() != null
166                && getView().getVisibility() == View.VISIBLE;
167    }
168}
169