1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.robolectric.Shadows.shadowOf; 5 6 import android.os.Looper; 7 import android.view.View; 8 import android.widget.AdapterView; 9 import android.widget.TextView; 10 import androidx.test.ext.junit.runners.AndroidJUnit4; 11 import java.util.ArrayList; 12 import java.util.List; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.robolectric.Shadows; 17 18 @RunWith(AndroidJUnit4.class) 19 abstract public class AdapterViewBehavior { 20 private AdapterView adapterView; 21 22 @Before setUp()23 public void setUp() throws Exception { 24 Shadows.shadowOf(Looper.getMainLooper()).pause(); 25 adapterView = createAdapterView(); 26 } 27 createAdapterView()28 abstract public AdapterView createAdapterView(); 29 shouldIgnoreSetSelectionCallsWithInvalidPosition()30 @Test public void shouldIgnoreSetSelectionCallsWithInvalidPosition() { 31 final List<String> transcript = new ArrayList<>(); 32 33 adapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 34 @Override 35 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 36 transcript.add("onItemSelected fired"); 37 } 38 39 @Override 40 public void onNothingSelected(AdapterView<?> parent) { 41 } 42 }); 43 44 ShadowLooper.idleMainLooper(); 45 assertThat(transcript).isEmpty(); 46 adapterView.setSelection(AdapterView.INVALID_POSITION); 47 ShadowLooper.idleMainLooper(); 48 assertThat(transcript).isEmpty(); 49 } 50 testSetAdapter_ShouldCauseViewsToBeRenderedAsynchronously()51 @Test public void testSetAdapter_ShouldCauseViewsToBeRenderedAsynchronously() throws Exception { 52 adapterView.setAdapter(new ShadowCountingAdapter(2)); 53 54 assertThat(adapterView.getCount()).isEqualTo(2); 55 assertThat(adapterView.getChildCount()).isEqualTo(0); 56 57 shadowOf(adapterView).populateItems(); 58 assertThat(adapterView.getChildCount()).isEqualTo(2); 59 assertThat(((TextView) adapterView.getChildAt(0)).getText()).isEqualTo("Item 0"); 60 assertThat(((TextView) adapterView.getChildAt(1)).getText()).isEqualTo("Item 1"); 61 } 62 testSetEmptyView_ShouldHideAdapterViewIfAdapterIsNull()63 @Test public void testSetEmptyView_ShouldHideAdapterViewIfAdapterIsNull() throws Exception { 64 adapterView.setAdapter(null); 65 66 View emptyView = new View(adapterView.getContext()); 67 adapterView.setEmptyView(emptyView); 68 69 assertThat(adapterView.getVisibility()).isEqualTo(View.GONE); 70 assertThat(emptyView.getVisibility()).isEqualTo(View.VISIBLE); 71 } 72 testSetEmptyView_ShouldHideAdapterViewIfAdapterViewIsEmpty()73 @Test public void testSetEmptyView_ShouldHideAdapterViewIfAdapterViewIsEmpty() throws Exception { 74 adapterView.setAdapter(new ShadowCountingAdapter(0)); 75 76 View emptyView = new View(adapterView.getContext()); 77 adapterView.setEmptyView(emptyView); 78 79 assertThat(adapterView.getVisibility()).isEqualTo(View.GONE); 80 assertThat(emptyView.getVisibility()).isEqualTo(View.VISIBLE); 81 } 82 testSetEmptyView_ShouldHideEmptyViewIfAdapterViewIsNotEmpty()83 @Test public void testSetEmptyView_ShouldHideEmptyViewIfAdapterViewIsNotEmpty() throws Exception { 84 adapterView.setAdapter(new ShadowCountingAdapter(1)); 85 86 View emptyView = new View(adapterView.getContext()); 87 adapterView.setEmptyView(emptyView); 88 89 assertThat(adapterView.getVisibility()).isEqualTo(View.VISIBLE); 90 assertThat(emptyView.getVisibility()).isEqualTo(View.GONE); 91 } 92 testSetEmptyView_ShouldHideEmptyViewWhenAdapterGetsNewItem()93 @Test public void testSetEmptyView_ShouldHideEmptyViewWhenAdapterGetsNewItem() throws Exception { 94 ShadowCountingAdapter adapter = new ShadowCountingAdapter(0); 95 adapterView.setAdapter(adapter); 96 97 View emptyView = new View(adapterView.getContext()); 98 adapterView.setEmptyView(emptyView); 99 100 assertThat(adapterView.getVisibility()).isEqualTo(View.GONE); 101 assertThat(emptyView.getVisibility()).isEqualTo(View.VISIBLE); 102 103 adapter.setCount(1); 104 105 ShadowLooper.idleMainLooper(); 106 107 assertThat(adapterView.getVisibility()).isEqualTo(View.VISIBLE); 108 assertThat(emptyView.getVisibility()).isEqualTo(View.GONE); 109 } 110 testSetEmptyView_ShouldHideAdapterViewWhenAdapterBecomesEmpty()111 @Test public void testSetEmptyView_ShouldHideAdapterViewWhenAdapterBecomesEmpty() throws Exception { 112 ShadowCountingAdapter adapter = new ShadowCountingAdapter(1); 113 adapterView.setAdapter(adapter); 114 115 View emptyView = new View(adapterView.getContext()); 116 adapterView.setEmptyView(emptyView); 117 118 assertThat(adapterView.getVisibility()).isEqualTo(View.VISIBLE); 119 assertThat(emptyView.getVisibility()).isEqualTo(View.GONE); 120 121 adapter.setCount(0); 122 123 ShadowLooper.idleMainLooper(); 124 125 assertThat(adapterView.getVisibility()).isEqualTo(View.GONE); 126 assertThat(emptyView.getVisibility()).isEqualTo(View.VISIBLE); 127 } 128 } 129