aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/AdapterViewBehavior.java
blob: 851c0bae25ccc7dfc024c3008636266fe6ecd301 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.xtremelabs.robolectric.shadows;

import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.util.Transcript;

import static com.xtremelabs.robolectric.Robolectric.*;
import static com.xtremelabs.robolectric.matchers.TextViewHasTextMatcher.hasText;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.core.CombinableMatcher.both;
import static org.junit.Assert.assertThat;

public class AdapterViewBehavior {
    public static void shouldActAsAdapterView(AdapterView adapterView) throws Exception {
        Robolectric.shadowOf(Looper.getMainLooper()).pause();
        
        testSetAdapter_ShouldCauseViewsToBeRenderedAsynchronously(adapterView);
        testSetAdapter_ShouldSelectFirstItemAsynchronously(adapterView);
        testSetAdapter_ShouldFireOnNothingSelectedWhenAdapterCountIsReducedToZero(adapterView);
        
        shouldIgnoreSetSelectionCallsWithInvalidPosition(adapterView);
        shouldOnlyUpdateOnceIfInvalidatedMultipleTimes(adapterView);
        
        testSetEmptyView_ShouldHideAdapterViewIfAdapterIsNull(adapterView);
        testSetEmptyView_ShouldHideAdapterViewIfAdapterViewIsEmpty(adapterView);
        testSetEmptyView_ShouldHideEmptyViewIfAdapterViewIsNotEmpty(adapterView);
        testSetEmptyView_ShouldHideEmptyViewWhenAdapterGetsNewItem(adapterView);
    }

    private static void shouldIgnoreSetSelectionCallsWithInvalidPosition(AdapterView adapterView) {
        final Transcript transcript = new Transcript();

        adapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                transcript.add("onItemSelected fired");
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        
        ShadowHandler.idleMainLooper();
        transcript.assertNoEventsSoFar();
        adapterView.setSelection(AdapterView.INVALID_POSITION);
        ShadowHandler.idleMainLooper();
        transcript.assertNoEventsSoFar();
    }
    
    private static void testSetAdapter_ShouldCauseViewsToBeRenderedAsynchronously(AdapterView adapterView) throws Exception {
        adapterView.setAdapter(new CountingAdapter(2));

        assertThat(adapterView.getCount(), equalTo(2));
        assertThat(adapterView.getChildCount(), equalTo(0));

        ShadowHandler.idleMainLooper();
        assertThat(adapterView.getChildCount(), equalTo(2));
        assertThat((TextView) adapterView.getChildAt(0), hasText("Item 0"));
        assertThat((TextView) adapterView.getChildAt(1), hasText("Item 1"));
    }

    private static void testSetAdapter_ShouldSelectFirstItemAsynchronously(final AdapterView adapterView) throws Exception {
        final Transcript transcript = new Transcript();

        adapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                assertThat(parent, sameInstance(adapterView));
                assertThat(view, both(sameInstance(adapterView.getChildAt(position))).and(not(nullValue())));
                assertThat(id, equalTo(adapterView.getAdapter().getItemId(position)));
                transcript.add("selected item " + position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        adapterView.setAdapter(new CountingAdapter(2));
        transcript.assertNoEventsSoFar();
        ShadowHandler.idleMainLooper();
        transcript.assertEventsSoFar("selected item 0");
    }
    
    private static void testSetAdapter_ShouldFireOnNothingSelectedWhenAdapterCountIsReducedToZero(final AdapterView adapterView) throws Exception {
        final Transcript transcript = new Transcript();

        adapterView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                transcript.add("onNothingSelected fired");
            }
        });
        CountingAdapter adapter = new CountingAdapter(2);
        adapterView.setAdapter(adapter);
        ShadowHandler.idleMainLooper();
        transcript.assertNoEventsSoFar();
        adapter.setCount(0);
        ShadowHandler.idleMainLooper();
        transcript.assertEventsSoFar("onNothingSelected fired");
    }
    
    private static void testSetEmptyView_ShouldHideAdapterViewIfAdapterIsNull(final AdapterView adapterView) throws Exception {
    	adapterView.setAdapter(null);
    	
    	View emptyView = new View(adapterView.getContext());
		adapterView.setEmptyView(emptyView);
		
		assertThat(adapterView.getVisibility(), is(View.GONE));
		assertThat(emptyView.getVisibility(), is(View.VISIBLE));
    }
    
    private static void testSetEmptyView_ShouldHideAdapterViewIfAdapterViewIsEmpty(final AdapterView adapterView) throws Exception {
    	adapterView.setAdapter(new CountingAdapter(0));
    	
    	View emptyView = new View(adapterView.getContext());
		adapterView.setEmptyView(emptyView);
		
		assertThat(adapterView.getVisibility(), is(View.GONE));
		assertThat(emptyView.getVisibility(), is(View.VISIBLE));
    }

    private static void testSetEmptyView_ShouldHideEmptyViewIfAdapterViewIsNotEmpty(final AdapterView adapterView) throws Exception {
    	adapterView.setAdapter(new CountingAdapter(1));
    	
    	View emptyView = new View(adapterView.getContext());
		adapterView.setEmptyView(emptyView);
		
		assertThat(adapterView.getVisibility(), is(View.VISIBLE));
		assertThat(emptyView.getVisibility(), is(View.GONE));
    }
    
    private static void testSetEmptyView_ShouldHideEmptyViewWhenAdapterGetsNewItem(final AdapterView adapterView) throws Exception {
    	CountingAdapter adapter = new CountingAdapter(0);
		adapterView.setAdapter(adapter);
    	
    	View emptyView = new View(adapterView.getContext());
		adapterView.setEmptyView(emptyView);
		
		assertThat(adapterView.getVisibility(), is(View.GONE));
		assertThat(emptyView.getVisibility(), is(View.VISIBLE));
		
		adapter.setCount(1);
		
		ShadowHandler.idleMainLooper();
		
		assertThat(adapterView.getVisibility(), is(View.VISIBLE));
		assertThat(emptyView.getVisibility(), is(View.GONE));
    }
    
    private static void testSetEmptyView_ShouldHideAdapterViewWhenAdapterBecomesEmpty(final AdapterView adapterView) throws Exception {
    	CountingAdapter adapter = new CountingAdapter(1);
		adapterView.setAdapter(adapter);
    	
    	View emptyView = new View(adapterView.getContext());
		adapterView.setEmptyView(emptyView);
		
		assertThat(adapterView.getVisibility(), is(View.GONE));
		assertThat(emptyView.getVisibility(), is(View.VISIBLE));
		
		adapter.setCount(0);
		
		ShadowHandler.idleMainLooper();
		
		assertThat(adapterView.getVisibility(), is(View.VISIBLE));
		assertThat(emptyView.getVisibility(), is(View.GONE));
    }

    private static void shouldOnlyUpdateOnceIfInvalidatedMultipleTimes(final AdapterView adapterView) {
        final Transcript transcript = new Transcript();
        CountingAdapter adapter = new CountingAdapter(2) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                transcript.add("getView for " + position);
                return super.getView(position, convertView, parent);
            }
        };
        adapterView.setAdapter(adapter);

        transcript.assertNoEventsSoFar();

        adapter.notifyDataSetChanged();
        adapter.notifyDataSetChanged();

        ShadowHandler.idleMainLooper();

        transcript.assertEventsSoFar("getView for 0", "getView for 1");
    }
}