1package com.xtremelabs.robolectric.shadows; 2 3import android.content.ContentResolver; 4import android.database.*; 5import android.net.Uri; 6import android.os.Bundle; 7import com.xtremelabs.robolectric.Robolectric; 8import com.xtremelabs.robolectric.WithTestDefaultsRunner; 9import org.junit.Test; 10import org.junit.runner.RunWith; 11import org.mockito.Mockito; 12 13import java.lang.reflect.Method; 14import java.util.HashMap; 15 16import static org.hamcrest.CoreMatchers.*; 17import static org.junit.Assert.assertThat; 18import static org.mockito.Mockito.*; 19 20@RunWith(WithTestDefaultsRunner.class) 21public class CursorWrapperTest { 22 23 private class ForwardVerifier { 24 25 final Cursor mockCursor; 26 final CursorWrapper cursorWrapper; 27 final HashMap<String, Method> cursorMethod; 28 29 public ForwardVerifier() { 30 mockCursor = mock(Cursor.class); 31 cursorWrapper = new CursorWrapper(mockCursor); 32 cursorMethod = new HashMap<String, Method>(); 33 34 // This works because no two methods in the Cursor interface have the same name 35 for (Method m : Cursor.class.getMethods()) { 36 cursorMethod.put(m.getName(), m); 37 } 38 } 39 40 public void verifyForward(String methodName, Object... params) throws Exception { 41 assertThat(cursorMethod.keySet(), hasItem(methodName)); 42 43 Method method = cursorMethod.get(methodName); 44 method.invoke(cursorWrapper, params); 45 method.invoke(verify(mockCursor, times(1)), params); 46 Mockito.verifyNoMoreInteractions(mockCursor); 47 } 48 49 } 50 51 @Test 52 public void testCursorMethodsAreForwarded() throws Exception { 53 ForwardVerifier v = new ForwardVerifier(); 54 55 v.verifyForward("close"); 56 v.verifyForward("copyStringToBuffer", 1, mock(CharArrayBuffer.class)); 57 v.verifyForward("deactivate"); 58 v.verifyForward("getBlob", 2); 59 v.verifyForward("getColumnCount"); 60 v.verifyForward("getColumnIndex", "foo"); 61 v.verifyForward("getColumnIndexOrThrow", "foo"); 62 v.verifyForward("getColumnName", 3); 63 v.verifyForward("getColumnNames"); 64 v.verifyForward("getCount"); 65 v.verifyForward("getDouble", 12); 66 v.verifyForward("getExtras"); 67 v.verifyForward("getFloat", 4); 68 v.verifyForward("getInt", 5); 69 v.verifyForward("getLong", 6); 70 v.verifyForward("getPosition"); 71 v.verifyForward("getShort", 7); 72 v.verifyForward("getString", 8); 73 v.verifyForward("getWantsAllOnMoveCalls"); 74 v.verifyForward("isAfterLast"); 75 v.verifyForward("isBeforeFirst"); 76 v.verifyForward("isClosed"); 77 v.verifyForward("isFirst"); 78 v.verifyForward("isLast"); 79 v.verifyForward("isNull", 10); 80 v.verifyForward("move", 11); 81 v.verifyForward("moveToFirst"); 82 v.verifyForward("moveToLast"); 83 v.verifyForward("moveToNext"); 84 v.verifyForward("moveToPosition", 13); 85 v.verifyForward("moveToPrevious"); 86 v.verifyForward("registerContentObserver", mock(ContentObserver.class)); 87 v.verifyForward("registerDataSetObserver", mock(DataSetObserver.class)); 88 v.verifyForward("requery"); 89 v.verifyForward("respond", mock(Bundle.class)); 90 v.verifyForward("setNotificationUri", mock(ContentResolver.class), mock(Uri.class)); 91 v.verifyForward("unregisterContentObserver", mock(ContentObserver.class)); 92 v.verifyForward("unregisterDataSetObserver", mock(DataSetObserver.class)); 93 94 } 95 96 @Test 97 public void getWrappedCursor() { 98 Cursor mockCursor = mock(Cursor.class); 99 CursorWrapper cursorWrapper = new CursorWrapper(mockCursor); 100 ShadowCursorWrapper shadow = Robolectric.shadowOf(cursorWrapper); 101 102 assertThat(shadow.getWrappedCursor(), is(sameInstance(mockCursor))); 103 } 104 105} 106