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