1package com.xtremelabs.robolectric.shadows; 2 3import android.content.Intent; 4import android.graphics.Canvas; 5import android.graphics.ColorFilter; 6import android.graphics.drawable.Drawable; 7import android.view.View; 8import android.widget.TabHost; 9import android.widget.TabHost.TabContentFactory; 10import android.widget.TextView; 11import com.xtremelabs.robolectric.R; 12import com.xtremelabs.robolectric.WithTestDefaultsRunner; 13import org.junit.Before; 14import org.junit.Test; 15import org.junit.runner.RunWith; 16 17import static com.xtremelabs.robolectric.Robolectric.shadowOf; 18import static org.hamcrest.core.Is.is; 19import static org.hamcrest.core.IsEqual.equalTo; 20import static org.junit.Assert.assertThat; 21 22@RunWith(WithTestDefaultsRunner.class) 23public class TabSpecTest { 24 Drawable icon1; 25 26 @Before 27 public void init() { 28 icon1 = new TestIcon(); 29 } 30 31 @Test 32 public void shouldGetAndSetTheIndicator() throws Exception { 33 TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo"); 34 View view = new View(null); 35 TabHost.TabSpec self = spec.setIndicator(view); 36 assertThat(self, is(spec)); 37 assertThat(shadowOf(spec).getIndicatorAsView(), is(view)); 38 } 39 40 @Test 41 public void shouldGetAndSetTheIntentContent() throws Exception { 42 TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo"); 43 Intent intent = new Intent(); 44 TabHost.TabSpec self = spec.setContent(intent); 45 assertThat(self, is(spec)); 46 assertThat(shadowOf(spec).getContentAsIntent(), is(intent)); 47 } 48 49 50 51 @Test 52 public void shouldGetAndSetTheIndicatorLabel() throws Exception { 53 TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo") 54 .setContent(R.layout.main).setIndicator("labelText"); 55 56 assertThat(shadowOf(spec).getIndicatorLabel(), is("labelText")); 57 assertThat(shadowOf(spec).getText(), is("labelText")); 58 } 59 @Test 60 public void shouldGetAndSetTheIndicatorLabelAndIcon() throws Exception { 61 TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo") 62 .setContent(R.layout.main).setIndicator("labelText",icon1); 63 64 assertThat(shadowOf(spec).getIndicatorLabel(), is("labelText")); 65 assertThat(shadowOf(spec).getText(), is("labelText")); 66 assertThat(shadowOf(spec).getIndicatorIcon(), is(icon1)); 67 } 68 69 @Test 70 public void shouldSetTheContentView() throws Exception { 71 TabHost.TabSpec foo = new TabHost(null).newTabSpec("Foo").setContent( 72 new TabContentFactory() { 73 public View createTabContent(String tag) { 74 TextView tv = new TextView(null); 75 tv.setText("The Text of " + tag); 76 return tv; 77 } 78 }); 79 80 ShadowTabSpec shadowFoo = shadowOf(foo); 81 TextView textView = (TextView) shadowFoo.getContentView(); 82 83 84 assertThat(textView.getText().toString(), equalTo("The Text of Foo")); 85 } 86 87 @Test 88 public void shouldSetTheContentViewId() throws Exception { 89 TabHost.TabSpec foo = new TabHost(null).newTabSpec("Foo") 90 .setContent(R.id.title); 91 92 ShadowTabSpec shadowFoo = shadowOf(foo); 93 int viewId = shadowFoo.getContentViewId(); 94 95 assertThat(viewId, equalTo(R.id.title)); 96} 97 98 private class TestIcon extends Drawable { 99 100 @Override 101 public void draw(Canvas canvas) { 102 } 103 104 @Override 105 public void setAlpha(int alpha) { 106 } 107 108 @Override 109 public void setColorFilter(ColorFilter cf) { 110 } 111 112 @Override 113 public int getOpacity() { 114 return 0; 115 } 116 117 } 118 119} 120