1package com.xtremelabs.robolectric.shadows;
2
3
4import android.app.Activity;
5import android.content.Context;
6import android.view.View;
7import android.view.ViewGroup;
8import android.widget.AbsSpinner;
9import android.widget.AdapterView;
10import android.widget.ArrayAdapter;
11import android.widget.Gallery;
12import android.widget.Spinner;
13import android.widget.TextView;
14
15import com.xtremelabs.robolectric.Robolectric;
16import com.xtremelabs.robolectric.WithTestDefaultsRunner;
17
18import org.hamcrest.core.IsNull;
19import org.junit.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22
23import static org.hamcrest.CoreMatchers.*;
24import static org.junit.Assert.assertThat;
25
26@RunWith(WithTestDefaultsRunner.class)
27public class AbsSpinnerTest {
28    private Context context;
29    private AdapterView adapterView;
30	private Spinner spinner;
31	private ShadowAbsSpinner shadowSpinner;
32	private ArrayAdapter<String> arrayAdapter;
33
34    @Before
35    public void setUp() throws Exception {
36        context = new Activity();
37        adapterView = new Gallery(context);
38		spinner = new Spinner(context);
39		shadowSpinner = (ShadowAbsSpinner) Robolectric.shadowOf(spinner);
40		String [] testItems = {"foo", "bar"};
41		arrayAdapter = new MyArrayAdapter(this.context, testItems);
42    }
43
44    @Test
45    public void shouldHaveAdapterViewCommonBehavior() throws Exception {
46        AdapterViewBehavior.shouldActAsAdapterView(adapterView);
47    }
48
49	@Test
50	public void checkSetAdapter() {
51		spinner.setAdapter(arrayAdapter);
52	}
53
54	@Test
55	public void getSelectedItemShouldReturnCorrectValue(){
56		spinner.setAdapter(arrayAdapter);
57		spinner.setSelection(0);
58		assertThat((String) spinner.getSelectedItem(), equalTo("foo"));
59		assertThat((String) spinner.getSelectedItem(), not(equalTo("bar")));
60
61		spinner.setSelection(1);
62		assertThat((String) spinner.getSelectedItem(), equalTo("bar"));
63		assertThat((String) spinner.getSelectedItem(), not(equalTo("foo")));
64	}
65
66	@Test
67	public void getSelectedItemShouldReturnNull_NoAdapterSet(){
68		assertThat(spinner.getSelectedItem(), nullValue());
69	}
70
71	@Test (expected = IndexOutOfBoundsException.class)
72	public void getSelectedItemShouldThrowException_EmptyArray(){
73		spinner.setAdapter(new MyArrayAdapter(context, new String[]{}));
74		spinner.getSelectedItem();
75	}
76
77	@Test
78	public void setSelectionWithAnimatedTransition() {
79		spinner.setAdapter(arrayAdapter);
80		spinner.setSelection(0, true);
81
82		assertThat((String) spinner.getSelectedItem(), equalTo("foo"));
83		assertThat((String) spinner.getSelectedItem(), not(equalTo("bar")));
84
85		assertThat(shadowSpinner.isAnimatedTransition(), equalTo(true));
86	}
87
88    @Test
89    public void testGetSelectedView_noAdapter() {
90        assertThat(spinner.getSelectedView(), nullValue());
91    }
92
93    @Test
94    public void testGetSelectedView_noSelection() {
95        spinner.setAdapter(arrayAdapter);
96        spinner.setSelection(AdapterView.INVALID_POSITION);
97
98        assertThat(spinner.getSelectedView(), nullValue());
99    }
100
101    @Test
102    public void testGetSelectedView_default() {
103        spinner.setAdapter(arrayAdapter);
104
105        assertThat(((TextView) spinner.getSelectedView()).getText().toString(),
106                equalTo("foo"));
107    }
108
109    @Test
110    public void testGetSelectedView_changed() {
111        spinner.setAdapter(arrayAdapter);
112        spinner.setSelection(1);
113
114        assertThat(((TextView) spinner.getSelectedView()).getText().toString(),
115                equalTo("bar"));
116    }
117
118    private static class MyArrayAdapter extends ArrayAdapter<String> {
119        public MyArrayAdapter(Context context, String[] testItems) {
120            super(context, android.R.layout.simple_spinner_item, testItems);
121        }
122
123        @Override public View getView(int position, View convertView, ViewGroup parent) {
124            TextView textView = new TextView(getContext());
125            textView.setText(getItem(position));
126            return textView;
127        }
128    }
129}
130