aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/com/xtremelabs/robolectric/shadows/TabSpecTest.java
blob: 74959bca2bdcc41581590a6bd2837757b983bf57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.xtremelabs.robolectric.shadows;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
import android.widget.TextView;
import com.xtremelabs.robolectric.R;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

@RunWith(WithTestDefaultsRunner.class)
public class TabSpecTest {
	Drawable icon1;
		
	@Before
	public void init() {
		 icon1 = new TestIcon();		 
	}
	
    @Test
    public void shouldGetAndSetTheIndicator() throws Exception {
        TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo");
        View view = new View(null);
        TabHost.TabSpec self = spec.setIndicator(view);
        assertThat(self, is(spec));
        assertThat(shadowOf(spec).getIndicatorAsView(), is(view));
    }

    @Test
    public void shouldGetAndSetTheIntentContent() throws Exception {
        TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo");
        Intent intent = new Intent();
        TabHost.TabSpec self = spec.setContent(intent);
        assertThat(self, is(spec));
        assertThat(shadowOf(spec).getContentAsIntent(), is(intent));
    }
    
   
    
    @Test
    public void shouldGetAndSetTheIndicatorLabel() throws Exception {
        TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo")
        .setContent(R.layout.main).setIndicator("labelText");

        assertThat(shadowOf(spec).getIndicatorLabel(), is("labelText"));
        assertThat(shadowOf(spec).getText(), is("labelText"));
    }
    @Test
    public void shouldGetAndSetTheIndicatorLabelAndIcon() throws Exception {
        TabHost.TabSpec spec = new TabHost(null).newTabSpec("foo")
        .setContent(R.layout.main).setIndicator("labelText",icon1);

        assertThat(shadowOf(spec).getIndicatorLabel(), is("labelText"));
        assertThat(shadowOf(spec).getText(), is("labelText"));
        assertThat(shadowOf(spec).getIndicatorIcon(), is(icon1));
    }
    
    @Test
    public void shouldSetTheContentView() throws Exception {
    	TabHost.TabSpec foo = new TabHost(null).newTabSpec("Foo").setContent(
			new TabContentFactory() {
				public View createTabContent(String tag) {
					TextView tv = new TextView(null);
					tv.setText("The Text of " + tag);
					return tv;
				}
			});
	        
		ShadowTabSpec shadowFoo = shadowOf(foo);
        TextView textView = (TextView) shadowFoo.getContentView();


        assertThat(textView.getText().toString(), equalTo("The Text of Foo"));
    }
    
    @Test
    public void shouldSetTheContentViewId() throws Exception {
    	TabHost.TabSpec foo = new TabHost(null).newTabSpec("Foo")
    	.setContent(R.id.title);
    				
		ShadowTabSpec shadowFoo = shadowOf(foo);
        int viewId = shadowFoo.getContentViewId();

        assertThat(viewId, equalTo(R.id.title));
}
    
    private class TestIcon extends Drawable {

		@Override
		public void draw(Canvas canvas) {
		}

		@Override
		public void setAlpha(int alpha) {
		}

		@Override
		public void setColorFilter(ColorFilter cf) {
		}

		@Override
		public int getOpacity() {
			return 0;
		}
		
	}

}