• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.xtremelabs.robolectric.shadows;
2 
3 import android.R;
4 import android.app.Activity;
5 import android.view.View;
6 import android.widget.TabHost;
7 import android.widget.TabHost.TabSpec;
8 import android.widget.TabWidget;
9 import com.xtremelabs.robolectric.Robolectric;
10 import com.xtremelabs.robolectric.internal.Implementation;
11 import com.xtremelabs.robolectric.internal.Implements;
12 import com.xtremelabs.robolectric.internal.RealObject;
13 
14 import java.util.ArrayList;
15 import java.util.List;
16 
17 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
18 
19 @SuppressWarnings({"UnusedDeclaration"})
20 @Implements(TabHost.class)
21 public class ShadowTabHost extends ShadowFrameLayout {
22     private List<TabHost.TabSpec> tabSpecs = new ArrayList<TabHost.TabSpec>();
23     private TabHost.OnTabChangeListener listener;
24     private int currentTab = -1;
25 
26     @RealObject
27     TabHost realObject;
28 
29     @Implementation
newTabSpec(java.lang.String tag)30     public android.widget.TabHost.TabSpec newTabSpec(java.lang.String tag) {
31         TabSpec realTabSpec = Robolectric.newInstanceOf(TabHost.TabSpec.class);
32         shadowOf(realTabSpec).setTag(tag);
33         return realTabSpec;
34     }
35 
36     @Implementation
addTab(android.widget.TabHost.TabSpec tabSpec)37     public void addTab(android.widget.TabHost.TabSpec tabSpec) {
38         tabSpecs.add(tabSpec);
39         View indicatorAsView = shadowOf(tabSpec).getIndicatorAsView();
40         if (indicatorAsView != null) {
41             realObject.addView(indicatorAsView);
42         }
43     }
44 
45     @Implementation
setCurrentTab(int index)46     public void setCurrentTab(int index) {
47         currentTab = index;
48         if (listener != null) {
49             listener.onTabChanged(getCurrentTabTag());
50         }
51     }
52 
53     @Implementation
setCurrentTabByTag(String tag)54     public void setCurrentTabByTag(String tag) {
55         for (int x = 0; x < tabSpecs.size(); x++) {
56             TabSpec tabSpec = tabSpecs.get(x);
57             if (tabSpec.getTag().equals(tag)) {
58                 currentTab = x;
59             }
60         }
61         if (listener != null) {
62             listener.onTabChanged(getCurrentTabTag());
63         }
64     }
65 
66     @Implementation
getCurrentTab()67     public int getCurrentTab() {
68         if (currentTab == -1 && tabSpecs.size() > 0) currentTab = 0;
69         return currentTab;
70     }
71 
getCurrentTabSpec()72     public TabSpec getCurrentTabSpec() {
73         return tabSpecs.get(getCurrentTab());
74     }
75 
76     @Implementation
getCurrentTabTag()77     public String getCurrentTabTag() {
78         int i = getCurrentTab();
79         if (i >= 0 && i < tabSpecs.size()) {
80             return tabSpecs.get(i).getTag();
81         }
82         return null;
83     }
84 
85     @Implementation
setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener)86     public void setOnTabChangedListener(android.widget.TabHost.OnTabChangeListener listener) {
87         this.listener = listener;
88     }
89 
90     @Implementation
getCurrentView()91     public View getCurrentView() {
92         ShadowTabSpec ts = Robolectric.shadowOf(getCurrentTabSpec());
93         View v = ts.getContentView();
94         if (v == null) {
95             int viewId = ts.getContentViewId();
96             if (getContext() instanceof Activity) {
97                 v = ((Activity) getContext()).findViewById(viewId);
98             } else {
99                 return null;
100             }
101         }
102         return v;
103     }
104 
105     @Implementation
getTabWidget()106     public TabWidget getTabWidget() {
107         if (context instanceof Activity) {
108             return (TabWidget) ((Activity)context).findViewById(R.id.tabs);
109         } else {
110             return null;
111         }
112     }
113 
getSpecByTag(String tag)114     public TabHost.TabSpec getSpecByTag(String tag) {
115         for (TabHost.TabSpec tabSpec : tabSpecs) {
116             if (tag.equals(tabSpec.getTag())) {
117                 return tabSpec;
118             }
119         }
120         return null;
121     }
122 }
123