• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 import static org.junit.Assert.assertNull;
5 import static org.robolectric.Shadows.shadowOf;
6 
7 import android.app.Activity;
8 import android.app.Application;
9 import android.app.TabActivity;
10 import android.view.View;
11 import android.widget.TabHost;
12 import android.widget.TabWidget;
13 import android.widget.TextView;
14 import androidx.test.core.app.ApplicationProvider;
15 import androidx.test.ext.junit.runners.AndroidJUnit4;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.robolectric.R;
20 import org.robolectric.Robolectric;
21 
22 @RunWith(AndroidJUnit4.class)
23 public class ShadowTabHostTest {
24 
25   private Application context;
26 
27   @Before
setUp()28   public void setUp() throws Exception {
29     context = ApplicationProvider.getApplicationContext();
30   }
31 
32   @Test
newTabSpec_shouldMakeATabSpec()33   public void newTabSpec_shouldMakeATabSpec() {
34     TabHost tabHost = new TabHost(context);
35     TabHost.TabSpec tabSpec = tabHost.newTabSpec("Foo");
36     assertThat(tabSpec.getTag()).isEqualTo("Foo");
37   }
38 
39   @Test
shouldAddTabsToLayoutWhenAddedToHost()40   public void shouldAddTabsToLayoutWhenAddedToHost() {
41     TabHost tabHost = new TabHost(context);
42 
43     View fooView = new View(context);
44     TabHost.TabSpec foo = tabHost.newTabSpec("Foo").setIndicator(fooView);
45 
46     View barView = new View(context);
47     TabHost.TabSpec bar = tabHost.newTabSpec("Bar").setIndicator(barView);
48 
49     tabHost.addTab(foo);
50     tabHost.addTab(bar);
51 
52     assertThat(tabHost.getChildAt(0)).isSameInstanceAs(fooView);
53     assertThat(tabHost.getChildAt(1)).isSameInstanceAs(barView);
54   }
55 
56   @Test
shouldReturnTabSpecsByTag()57   public void shouldReturnTabSpecsByTag() {
58     TabHost tabHost = new TabHost(context);
59     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
60     TabHost.TabSpec bar = tabHost.newTabSpec("Bar");
61     TabHost.TabSpec baz = tabHost.newTabSpec("Baz");
62 
63     tabHost.addTab(foo);
64     tabHost.addTab(bar);
65     tabHost.addTab(baz);
66 
67     assertThat(shadowOf(tabHost).getSpecByTag("Bar")).isSameInstanceAs(bar);
68     assertThat(shadowOf(tabHost).getSpecByTag("Baz")).isSameInstanceAs(baz);
69     assertNull(shadowOf(tabHost).getSpecByTag("Whammie"));
70   }
71 
72   @Test
shouldFireTheTabChangeListenerWhenCurrentTabIsSet()73   public void shouldFireTheTabChangeListenerWhenCurrentTabIsSet() {
74     TabHost tabHost = new TabHost(context);
75 
76     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
77     TabHost.TabSpec bar = tabHost.newTabSpec("Bar");
78     TabHost.TabSpec baz = tabHost.newTabSpec("Baz");
79 
80     tabHost.addTab(foo);
81     tabHost.addTab(bar);
82     tabHost.addTab(baz);
83 
84     TestOnTabChangeListener listener = new TestOnTabChangeListener();
85     tabHost.setOnTabChangedListener(listener);
86 
87     tabHost.setCurrentTab(2);
88 
89     assertThat(listener.tag).isEqualTo("Baz");
90   }
91 
92   @Test
shouldFireTheTabChangeListenerWhenTheCurrentTabIsSetByTag()93   public void shouldFireTheTabChangeListenerWhenTheCurrentTabIsSetByTag() {
94     TabHost tabHost = new TabHost(context);
95 
96     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
97     TabHost.TabSpec bar = tabHost.newTabSpec("Bar");
98     TabHost.TabSpec baz = tabHost.newTabSpec("Baz");
99 
100     tabHost.addTab(foo);
101     tabHost.addTab(bar);
102     tabHost.addTab(baz);
103 
104     TestOnTabChangeListener listener = new TestOnTabChangeListener();
105     tabHost.setOnTabChangedListener(listener);
106 
107     tabHost.setCurrentTabByTag("Bar");
108 
109     assertThat(listener.tag).isEqualTo("Bar");
110   }
111 
112   @Test
shouldRetrieveTheCurrentViewFromTabContentFactory()113   public void shouldRetrieveTheCurrentViewFromTabContentFactory() {
114     TabHost tabHost = new TabHost(context);
115 
116     TabHost.TabSpec foo =
117         tabHost
118             .newTabSpec("Foo")
119             .setContent(
120                 tag -> {
121                   TextView tv = new TextView(context);
122                   tv.setText("The Text of " + tag);
123                   return tv;
124                 });
125 
126     tabHost.addTab(foo);
127     tabHost.setCurrentTabByTag("Foo");
128     TextView textView = (TextView) tabHost.getCurrentView();
129 
130     assertThat(textView.getText().toString()).isEqualTo("The Text of Foo");
131   }
132 
133   @Test
shouldRetrieveTheCurrentViewFromViewId()134   public void shouldRetrieveTheCurrentViewFromViewId() {
135     Activity activity = Robolectric.buildActivity(Activity.class).create().get();
136     activity.setContentView(org.robolectric.R.layout.main);
137     TabHost tabHost = new TabHost(activity);
138     TabHost.TabSpec foo = tabHost.newTabSpec("Foo").setContent(org.robolectric.R.id.title);
139 
140     tabHost.addTab(foo);
141     tabHost.setCurrentTabByTag("Foo");
142     TextView textView = (TextView) tabHost.getCurrentView();
143 
144     assertThat(textView.getText().toString()).isEqualTo("Main Layout");
145   }
146 
147   private static class TestOnTabChangeListener implements TabHost.OnTabChangeListener {
148     private String tag;
149 
150     @Override
onTabChanged(String tag)151     public void onTabChanged(String tag) {
152       this.tag = tag;
153     }
154   }
155 
156   @Test
canGetCurrentTabTag()157   public void canGetCurrentTabTag() {
158     TabHost tabHost = new TabHost(context);
159 
160     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
161     TabHost.TabSpec bar = tabHost.newTabSpec("Bar");
162     TabHost.TabSpec baz = tabHost.newTabSpec("Baz");
163 
164     tabHost.addTab(foo);
165     tabHost.addTab(bar);
166     tabHost.addTab(baz);
167 
168     tabHost.setCurrentTabByTag("Bar");
169 
170     assertThat(tabHost.getCurrentTabTag()).isEqualTo("Bar");
171   }
172 
173   @Test
canGetCurrentTab()174   public void canGetCurrentTab() {
175     TabHost tabHost = new TabHost(context);
176 
177     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
178     TabHost.TabSpec bar = tabHost.newTabSpec("Bar");
179     TabHost.TabSpec baz = tabHost.newTabSpec("Baz");
180 
181     tabHost.addTab(foo);
182     tabHost.addTab(bar);
183     tabHost.addTab(baz);
184     assertThat(shadowOf(tabHost).getCurrentTabSpec()).isEqualTo(foo);
185     assertThat(tabHost.getCurrentTab()).isEqualTo(0);
186     tabHost.setCurrentTabByTag("Bar");
187     assertThat(tabHost.getCurrentTab()).isEqualTo(1);
188     assertThat(shadowOf(tabHost).getCurrentTabSpec()).isEqualTo(bar);
189     tabHost.setCurrentTabByTag("Foo");
190     assertThat(tabHost.getCurrentTab()).isEqualTo(0);
191     assertThat(shadowOf(tabHost).getCurrentTabSpec()).isEqualTo(foo);
192     tabHost.setCurrentTabByTag("Baz");
193     assertThat(tabHost.getCurrentTab()).isEqualTo(2);
194     assertThat(shadowOf(tabHost).getCurrentTabSpec()).isEqualTo(baz);
195   }
196 
197   @Test
setCurrentTabByTagShouldAcceptNullAsParameter()198   public void setCurrentTabByTagShouldAcceptNullAsParameter() {
199     TabHost tabHost = new TabHost(context);
200     TabHost.TabSpec foo = tabHost.newTabSpec("Foo");
201     tabHost.addTab(foo);
202 
203     tabHost.setCurrentTabByTag(null);
204     assertThat(tabHost.getCurrentTabTag()).isEqualTo("Foo");
205   }
206 
207   @Test
shouldGetTabWidget()208   public void shouldGetTabWidget() {
209     TabActivity activity = Robolectric.buildActivity(TabActivity.class).create().get();
210     activity.setContentView(R.layout.tab_activity);
211     TabHost host = new TabHost(activity);
212     assertThat(host.getTabWidget()).isInstanceOf(TabWidget.class);
213   }
214 }
215