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