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