Adds getSelectedView support to Robolectric

Change-Id: Ib8e731155fe94eb07ce7137b3a71731f9f6ae886
diff --git a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsSpinner.java b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsSpinner.java
index dcdb93b..80c73fe 100644
--- a/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsSpinner.java
+++ b/src/main/java/com/xtremelabs/robolectric/shadows/ShadowAbsSpinner.java
@@ -1,10 +1,12 @@
 package com.xtremelabs.robolectric.shadows;
 
-import android.widget.AbsSpinner;
-import android.widget.SpinnerAdapter;
 import com.xtremelabs.robolectric.internal.Implementation;
 import com.xtremelabs.robolectric.internal.Implements;
 
+import android.view.View;
+import android.widget.AbsSpinner;
+import android.widget.SpinnerAdapter;
+
 @SuppressWarnings({"UnusedDeclaration"})
 @Implements(AbsSpinner.class)
 public class ShadowAbsSpinner extends ShadowAdapterView {
@@ -27,6 +29,16 @@
     	animatedTransition = animate;
     }
 
+    @Implementation
+    public View getSelectedView() {
+        int selectedItemPosition = getSelectedItemPosition();
+        if (getCount() == 0 || selectedItemPosition < 0) {
+            return null;
+        } else {
+            return getChildAt(selectedItemPosition);
+        }
+    }
+
     // Non-implementation helper method
     public boolean isAnimatedTransition() {
     	return animatedTransition;
diff --git a/src/test/java/com/xtremelabs/robolectric/shadows/AbsSpinnerTest.java b/src/test/java/com/xtremelabs/robolectric/shadows/AbsSpinnerTest.java
index dcac28a..9dbecdb 100644
--- a/src/test/java/com/xtremelabs/robolectric/shadows/AbsSpinnerTest.java
+++ b/src/test/java/com/xtremelabs/robolectric/shadows/AbsSpinnerTest.java
@@ -5,12 +5,17 @@
 import android.content.Context;
 import android.view.View;
 import android.view.ViewGroup;
+import android.widget.AbsSpinner;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Gallery;
 import android.widget.Spinner;
+import android.widget.TextView;
+
 import com.xtremelabs.robolectric.Robolectric;
 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+
+import org.hamcrest.core.IsNull;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -52,41 +57,73 @@
 		spinner.setSelection(0);
 		assertThat((String) spinner.getSelectedItem(), equalTo("foo"));
 		assertThat((String) spinner.getSelectedItem(), not(equalTo("bar")));
-		
+
 		spinner.setSelection(1);
 		assertThat((String) spinner.getSelectedItem(), equalTo("bar"));
 		assertThat((String) spinner.getSelectedItem(), not(equalTo("foo")));
 	}
-	
+
 	@Test
 	public void getSelectedItemShouldReturnNull_NoAdapterSet(){
 		assertThat(spinner.getSelectedItem(), nullValue());
 	}
-	
-	@Test (expected = IndexOutOfBoundsException.class)	
+
+	@Test (expected = IndexOutOfBoundsException.class)
 	public void getSelectedItemShouldThrowException_EmptyArray(){
 		spinner.setAdapter(new MyArrayAdapter(context, new String[]{}));
-		spinner.getSelectedItem();		
+		spinner.getSelectedItem();
 	}
-	
+
 	@Test
-	public void setSelectionWithAnimatedTransition() {		
+	public void setSelectionWithAnimatedTransition() {
 		spinner.setAdapter(arrayAdapter);
 		spinner.setSelection(0, true);
-		
+
 		assertThat((String) spinner.getSelectedItem(), equalTo("foo"));
 		assertThat((String) spinner.getSelectedItem(), not(equalTo("bar")));
-		
+
 		assertThat(shadowSpinner.isAnimatedTransition(), equalTo(true));
 	}
 
+    @Test
+    public void testGetSelectedView_noAdapter() {
+        assertThat(spinner.getSelectedView(), nullValue());
+    }
+
+    @Test
+    public void testGetSelectedView_noSelection() {
+        spinner.setAdapter(arrayAdapter);
+        spinner.setSelection(AdapterView.INVALID_POSITION);
+
+        assertThat(spinner.getSelectedView(), nullValue());
+    }
+
+    @Test
+    public void testGetSelectedView_default() {
+        spinner.setAdapter(arrayAdapter);
+
+        assertThat(((TextView) spinner.getSelectedView()).getText().toString(),
+                equalTo("foo"));
+    }
+
+    @Test
+    public void testGetSelectedView_changed() {
+        spinner.setAdapter(arrayAdapter);
+        spinner.setSelection(1);
+
+        assertThat(((TextView) spinner.getSelectedView()).getText().toString(),
+                equalTo("bar"));
+    }
+
     private static class MyArrayAdapter extends ArrayAdapter<String> {
         public MyArrayAdapter(Context context, String[] testItems) {
             super(context, android.R.layout.simple_spinner_item, testItems);
         }
 
         @Override public View getView(int position, View convertView, ViewGroup parent) {
-            return new View(getContext());
+            TextView textView = new TextView(getContext());
+            textView.setText(getItem(position));
+            return textView;
         }
     }
 }