• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import android.R;
4 import android.app.Activity;
5 import android.content.Context;
6 import android.content.Intent;
7 import android.graphics.drawable.Drawable;
8 import android.view.View;
9 import android.widget.TabHost;
10 import android.widget.TabHost.TabSpec;
11 import android.widget.TabWidget;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.robolectric.annotation.Implementation;
15 import org.robolectric.annotation.Implements;
16 import org.robolectric.annotation.RealObject;
17 import org.robolectric.shadow.api.Shadow;
18 
19 @SuppressWarnings({"UnusedDeclaration"})
20 @Implements(TabHost.class)
21 public class ShadowTabHost extends ShadowViewGroup {
22   private List<TabHost.TabSpec> tabSpecs = new ArrayList<>();
23   private TabHost.OnTabChangeListener listener;
24   private int currentTab = -1;
25 
26   @RealObject private TabHost realObject;
27 
28   @Implementation
newTabSpec(java.lang.String tag)29   protected android.widget.TabHost.TabSpec newTabSpec(java.lang.String tag) {
30     TabSpec realTabSpec = Shadow.newInstanceOf(TabHost.TabSpec.class);
31     ShadowTabSpec shadowTabSpec = Shadow.extract(realTabSpec);
32     shadowTabSpec.setTag(tag);
33     return realTabSpec;
34   }
35 
36   @Implementation
addTab(android.widget.TabHost.TabSpec tabSpec)37   protected void addTab(android.widget.TabHost.TabSpec tabSpec) {
38     tabSpecs.add(tabSpec);
39     ShadowTabSpec shadowTabSpec = Shadow.extract(tabSpec);
40     View indicatorAsView = shadowTabSpec.getIndicatorAsView();
41     if (indicatorAsView != null) {
42       realObject.addView(indicatorAsView);
43     }
44   }
45 
46   @Implementation
setCurrentTab(int index)47   protected void setCurrentTab(int index) {
48     currentTab = index;
49     if (listener != null) {
50       listener.onTabChanged(getCurrentTabTag());
51     }
52   }
53 
54   @Implementation
setCurrentTabByTag(String tag)55   protected void setCurrentTabByTag(String tag) {
56     for (int x = 0; x < tabSpecs.size(); x++) {
57       TabSpec tabSpec = tabSpecs.get(x);
58       if (tabSpec.getTag().equals(tag)) {
59         currentTab = x;
60       }
61     }
62     if (listener != null) {
63       listener.onTabChanged(getCurrentTabTag());
64     }
65   }
66 
67   @Implementation
getCurrentTab()68   protected int getCurrentTab() {
69     if (currentTab == -1 && tabSpecs.size() > 0) currentTab = 0;
70     return currentTab;
71   }
72 
getCurrentTabSpec()73   public TabSpec getCurrentTabSpec() {
74     return tabSpecs.get(getCurrentTab());
75   }
76 
77   @Implementation
getCurrentTabTag()78   protected String getCurrentTabTag() {
79     int i = getCurrentTab();
80     if (i >= 0 && i < tabSpecs.size()) {
81       return tabSpecs.get(i).getTag();
82     }
83     return null;
84   }
85 
86   @Implementation
setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener)87   protected void setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener) {
88     this.listener = listener;
89   }
90 
91   @Implementation
getCurrentView()92   protected View getCurrentView() {
93     ShadowTabSpec ts = Shadow.extract(getCurrentTabSpec());
94     View v = ts.getContentView();
95     if (v == null) {
96       int viewId = ts.getContentViewId();
97       if (realView.getContext() instanceof Activity) {
98         v = ((Activity) realView.getContext()).findViewById(viewId);
99       } else {
100         return null;
101       }
102     }
103     return v;
104   }
105 
106   @Implementation
getTabWidget()107   protected TabWidget getTabWidget() {
108     Context context = realView.getContext();
109     if (context instanceof Activity) {
110       return (TabWidget) ((Activity) context).findViewById(R.id.tabs);
111     } else {
112       return null;
113     }
114   }
115 
getSpecByTag(String tag)116   public TabHost.TabSpec getSpecByTag(String tag) {
117     for (TabHost.TabSpec tabSpec : tabSpecs) {
118       if (tag.equals(tabSpec.getTag())) {
119         return tabSpec;
120       }
121     }
122     return null;
123   }
124 
125   @SuppressWarnings({"UnusedDeclaration"})
126   @Implements(TabSpec.class)
127   public static class ShadowTabSpec {
128 
129     @RealObject TabSpec realObject;
130     private String tag;
131     private View indicatorView;
132     private Intent intent;
133     private int viewId;
134     private View contentView;
135     private CharSequence label;
136     private Drawable icon;
137 
138     /**
139      * Sets the tag on the TabSpec.
140      *
141      * @param tag The tag.
142      */
setTag(String tag)143     public void setTag(String tag) {
144       this.tag = tag;
145     }
146 
147     @Implementation
getTag()148     protected String getTag() {
149       return tag;
150     }
151 
152     /**
153      * @return the view object set in a call to {@code TabSpec#setIndicator(View)}
154      */
getIndicatorAsView()155     public View getIndicatorAsView() {
156       return this.indicatorView;
157     }
158 
getIndicatorLabel()159     public String getIndicatorLabel() {
160       return this.label.toString();
161     }
162 
getIndicatorIcon()163     public Drawable getIndicatorIcon() {
164       return this.icon;
165     }
166 
167     /**
168      * Same as GetIndicatorLabel()
169      *
170      * @return Tab text.
171      */
getText()172     public String getText() {
173       return label.toString();
174     }
175 
176     @Implementation
setIndicator(View view)177     protected TabSpec setIndicator(View view) {
178       this.indicatorView = view;
179       return realObject;
180     }
181 
182     @Implementation
setIndicator(CharSequence label)183     protected TabSpec setIndicator(CharSequence label) {
184       this.label = label;
185       return realObject;
186     }
187 
188     @Implementation
setIndicator(CharSequence label, Drawable icon)189     protected TabSpec setIndicator(CharSequence label, Drawable icon) {
190       this.label = label;
191       this.icon = icon;
192       return realObject;
193     }
194 
195     /**
196      * @return the intent object set in a call to {@code TabSpec#setContent(Intent)}
197      */
getContentAsIntent()198     public Intent getContentAsIntent() {
199       return intent;
200     }
201 
202     @Implementation
setContent(Intent intent)203     protected TabSpec setContent(Intent intent) {
204       this.intent = intent;
205       return realObject;
206     }
207 
208     @Implementation
setContent(TabHost.TabContentFactory factory)209     protected TabSpec setContent(TabHost.TabContentFactory factory) {
210       contentView = factory.createTabContent(this.tag);
211       return realObject;
212     }
213 
214     @Implementation
setContent(int viewId)215     protected TabSpec setContent(int viewId) {
216       this.viewId = viewId;
217       return realObject;
218     }
219 
getContentViewId()220     public int getContentViewId() {
221       return viewId;
222     }
223 
getContentView()224     public View getContentView() {
225       return contentView;
226     }
227   }
228 }
229