ShadowAbstractCursorTest.java revision ceb8863e0cd0706c72351f063bc7af3c7d76d120
1package org.robolectric.shadows;
2
3import static org.assertj.core.api.Assertions.assertThat;
4
5import android.database.AbstractCursor;
6import android.net.Uri;
7import java.util.ArrayList;
8import java.util.List;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.robolectric.RuntimeEnvironment;
13import org.robolectric.Shadows;
14import org.robolectric.RobolectricTestRunner;
15
16@RunWith(RobolectricTestRunner.class)
17public class ShadowAbstractCursorTest {
18
19  private TestCursor cursor;
20
21  @Before
22  public void setUp() throws Exception {
23    cursor = new TestCursor();
24  }
25
26  @Test
27  public void testMoveToFirst() {
28    cursor.theTable.add("Foobar");
29    assertThat(cursor.moveToFirst()).isTrue();
30    assertThat(cursor.getCount()).isEqualTo(1);
31  }
32
33  @Test
34  public void testMoveToFirstEmptyList() {
35    assertThat(cursor.moveToFirst()).isFalse();
36    assertThat(cursor.getCount()).isEqualTo(0);
37  }
38
39  @Test
40  public void testMoveToLast() {
41    cursor.theTable.add("Foobar");
42    cursor.theTable.add("Bletch");
43
44    assertThat(cursor.moveToLast()).isTrue();
45    assertThat(cursor.getCount()).isEqualTo(2);
46  }
47
48  @Test
49  public void testMoveToLastEmptyList() {
50    assertThat(cursor.moveToLast()).isFalse();
51    assertThat(cursor.getCount()).isEqualTo(0);
52  }
53
54  @Test
55  public void testGetPosition() {
56    cursor.theTable.add("Foobar");
57    cursor.theTable.add("Bletch");
58
59    assertThat(cursor.moveToFirst()).isTrue();
60    assertThat(cursor.getCount()).isEqualTo(2);
61    assertThat(cursor.getPosition()).isEqualTo(0);
62  }
63
64  @Test
65  public void testGetPositionSingleEntry() {
66    cursor.theTable.add("Foobar");
67
68    assertThat(cursor.moveToFirst()).isTrue();
69    assertThat(cursor.getCount()).isEqualTo(1);
70    assertThat(cursor.getPosition()).isEqualTo(0);
71  }
72
73  @Test
74  public void testGetPositionEmptyList() {
75    assertThat(cursor.moveToFirst()).isFalse();
76    assertThat(cursor.getCount()).isEqualTo(0);
77    assertThat(cursor.getPosition()).isEqualTo(0);
78  }
79
80  @Test
81  public void testMoveToNext() {
82    cursor.theTable.add("Foobar");
83    cursor.theTable.add("Bletch");
84
85    assertThat(cursor.moveToFirst()).isTrue();
86    assertThat(cursor.getCount()).isEqualTo(2);
87    assertThat(cursor.moveToNext()).isTrue();
88    assertThat(cursor.getPosition()).isEqualTo(1);
89  }
90
91  @Test
92  public void testAttemptToMovePastEnd() {
93    cursor.theTable.add("Foobar");
94    cursor.theTable.add("Bletch");
95
96    assertThat(cursor.moveToFirst()).isTrue();
97    assertThat(cursor.getCount()).isEqualTo(2);
98    assertThat(cursor.moveToNext()).isTrue();
99    assertThat(cursor.getPosition()).isEqualTo(1);
100    assertThat(cursor.isLast()).isTrue();
101    assertThat(cursor.moveToNext()).isFalse();
102    assertThat(cursor.isAfterLast()).isTrue();
103    assertThat(cursor.getPosition()).isEqualTo(2);
104  }
105
106  @Test
107  public void testAttemptToMovePastSingleEntry() {
108    cursor.theTable.add("Foobar");
109
110    assertThat(cursor.moveToFirst()).isTrue();
111    assertThat(cursor.getCount()).isEqualTo(1);
112    assertThat(cursor.moveToNext()).isFalse();
113    assertThat(cursor.getPosition()).isEqualTo(1);
114  }
115
116  @Test
117  public void testAttemptToMovePastEmptyList() {
118    assertThat(cursor.moveToFirst()).isFalse();
119    assertThat(cursor.getCount()).isEqualTo(0);
120    assertThat(cursor.moveToNext()).isFalse();
121    assertThat(cursor.getPosition()).isEqualTo(0);
122  }
123
124  @Test
125  public void testMoveToPrevious() {
126    cursor.theTable.add("Foobar");
127    cursor.theTable.add("Bletch");
128    assertThat(cursor.moveToFirst()).isTrue();
129    assertThat(cursor.moveToNext()).isTrue();
130    assertThat(cursor.getPosition()).isEqualTo(1);
131    assertThat(cursor.moveToPrevious()).isTrue();
132    assertThat(cursor.getPosition()).isEqualTo(0);
133  }
134
135  @Test
136  public void testAttemptToMovePastStart() {
137    cursor.theTable.add("Foobar");
138    cursor.theTable.add("Bletch");
139    assertThat(cursor.moveToFirst()).isTrue();
140    assertThat(cursor.moveToPrevious()).isFalse();
141    assertThat(cursor.getPosition()).isEqualTo(-1);
142  }
143
144  @Test
145  public void testIsFirst() {
146    cursor.theTable.add("Foobar");
147    cursor.theTable.add("Bletch");
148    assertThat(cursor.moveToFirst()).isTrue();
149    assertThat(cursor.isFirst()).isTrue();
150    cursor.moveToNext();
151    assertThat(cursor.isFirst()).isFalse();
152    cursor.moveToFirst();
153    cursor.moveToPrevious();
154    assertThat(cursor.isFirst()).isFalse();
155  }
156
157  @Test
158  public void testIsLast() {
159    cursor.theTable.add("Foobar");
160    cursor.theTable.add("Bletch");
161    assertThat(cursor.moveToFirst()).isTrue();
162    cursor.moveToNext();
163    assertThat(cursor.isLast()).isTrue();
164    cursor.moveToPrevious();
165    assertThat(cursor.isLast()).isFalse();
166    cursor.moveToFirst();
167    cursor.moveToNext();
168    assertThat(cursor.isLast()).isTrue();
169  }
170
171  @Test
172  public void testIsBeforeFirst() {
173    cursor.theTable.add("Foobar");
174    cursor.theTable.add("Bletch");
175    assertThat(cursor.moveToFirst()).isTrue();
176    cursor.moveToNext();
177    assertThat(cursor.isLast()).isTrue();
178    cursor.moveToPrevious();
179    assertThat(cursor.isLast()).isFalse();
180    cursor.moveToPrevious();
181    assertThat(cursor.isFirst()).isFalse();
182    cursor.moveToPrevious();
183    assertThat(cursor.isBeforeFirst()).isTrue();
184  }
185
186  @Test
187  public void testIsAfterLast() {
188    cursor.theTable.add("Foobar");
189    cursor.theTable.add("Bletch");
190    assertThat(cursor.moveToFirst()).isTrue();
191    cursor.moveToNext();
192    assertThat(cursor.isLast()).isTrue();
193    cursor.moveToNext();
194    assertThat(cursor.isAfterLast()).isTrue();
195    cursor.moveToPrevious();
196    assertThat(cursor.isLast()).isTrue();
197    cursor.moveToPrevious();
198    assertThat(cursor.isLast()).isFalse();
199    cursor.moveToFirst();
200    cursor.moveToNext();
201    assertThat(cursor.isAfterLast()).isFalse();
202    cursor.moveToNext();
203    assertThat(cursor.isAfterLast()).isTrue();
204  }
205
206  @Test
207  public void testGetNotificationUri() {
208    Uri uri = Uri.parse("content://foo.com");
209    ShadowAbstractCursor shadow = Shadows.shadowOf(cursor);
210    assertThat(shadow.getNotificationUri_Compatibility()).isNull();
211    cursor.setNotificationUri(RuntimeEnvironment.application.getContentResolver(), uri);
212    assertThat(shadow.getNotificationUri_Compatibility()).isEqualTo(uri);
213  }
214
215  @Test
216  public void testIsClosedWhenAfterCallingClose() {
217    assertThat(cursor.isClosed()).isFalse();
218    cursor.close();
219    assertThat(cursor.isClosed()).isTrue();
220  }
221
222  private class TestCursor extends AbstractCursor {
223
224    public List<Object> theTable = new ArrayList<>();
225
226    @Override
227    public int getCount() {
228      return theTable.size();
229    }
230
231    @Override
232    public String[] getColumnNames() {
233      throw new UnsupportedOperationException();
234    }
235
236    @Override
237    public double getDouble(int columnIndex) {
238      throw new UnsupportedOperationException();
239    }
240
241    @Override
242    public float getFloat(int columnIndex) {
243      throw new UnsupportedOperationException();
244    }
245
246    @Override
247    public int getInt(int columnIndex) {
248      throw new UnsupportedOperationException();
249    }
250
251    @Override
252    public long getLong(int columnIndex) {
253      throw new UnsupportedOperationException();
254    }
255
256    @Override
257    public short getShort(int columnIndex) {
258      throw new UnsupportedOperationException();
259    }
260
261    @Override
262    public String getString(int columnIndex) {
263      throw new UnsupportedOperationException();
264    }
265
266    @Override
267    public boolean isNull(int columnIndex) {
268      throw new UnsupportedOperationException();
269    }
270  }
271}