1 package org.robolectric.fakes; 2 3 import android.database.DatabaseUtils; 4 import android.net.Uri; 5 import android.os.Bundle; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 /** 10 * Robolectric implementation of {@link android.database.Cursor}. 11 */ 12 public class RoboCursor extends BaseCursor { 13 public Uri uri; 14 public String[] projection; 15 public String selection; 16 public String[] selectionArgs; 17 public String sortOrder; 18 protected Object[][] results = new Object[0][0]; 19 protected List<String> columnNames = new ArrayList<>(); 20 private int resultsIndex = -1; 21 private boolean closeWasCalled; 22 private Bundle extras; 23 24 @Override 25 public void setQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { 26 this.uri = uri; 27 this.projection = projection; 28 this.selection = selection; 29 this.selectionArgs = selectionArgs; 30 this.sortOrder = sortOrder; 31 } 32 33 @Override 34 public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { 35 int col = getColumnIndex(columnName); 36 if (col == -1) { 37 throw new IllegalArgumentException("No column with name: " + columnName); 38 } 39 return col; 40 } 41 42 @Override 43 public int getColumnIndex(String columnName) { 44 return columnNames.indexOf(columnName); 45 } 46 47 @Override 48 public String getString(int columnIndex) { 49 Object value = results[resultsIndex][columnIndex]; 50 return value == null ? null : value.toString(); 51 } 52 53 @Override 54 public short getShort(int columnIndex) { 55 Object value = results[resultsIndex][columnIndex]; 56 return value == null ? 0 : (value instanceof Number ? ((Number) value).shortValue() : Short.parseShort(value.toString())); 57 } 58 59 @Override 60 public int getInt(int columnIndex) { 61 Object value = results[resultsIndex][columnIndex]; 62 return value == null ? 0 : (value instanceof Number ? ((Number) value).intValue() : Integer.parseInt(value.toString())); 63 } 64 65 @Override 66 public long getLong(int columnIndex) { 67 Object value = results[resultsIndex][columnIndex]; 68 return value == null 69 ? 0 70 : (value instanceof Number 71 ? ((Number) value).longValue() 72 : Long.parseLong(value.toString())); 73 } 74 75 @Override 76 public float getFloat(int columnIndex) { 77 Object value = results[resultsIndex][columnIndex]; 78 return value == null ? 0 : (value instanceof Number ? ((Number) value).floatValue() : Float.parseFloat(value.toString())); 79 } 80 81 @Override 82 public double getDouble(int columnIndex) { 83 Object value = results[resultsIndex][columnIndex]; 84 return value == null ? 0 : (value instanceof Number ? ((Number) value).doubleValue() : Double.parseDouble(value.toString())); 85 } 86 87 @Override 88 public byte[] getBlob(int columnIndex) { 89 return (byte[]) results[resultsIndex][columnIndex]; 90 } 91 92 @Override 93 public int getType(int columnIndex) { 94 return DatabaseUtils.getTypeOfObject(results[0][columnIndex]); 95 } 96 97 @Override 98 public boolean isNull(int columnIndex) { 99 return results[resultsIndex][columnIndex] == null; 100 } 101 102 @Override 103 public int getCount() { 104 return results.length; 105 } 106 107 @Override 108 public boolean moveToNext() { 109 return doMoveToPosition(resultsIndex + 1); 110 } 111 112 @Override 113 public boolean moveToFirst() { 114 return doMoveToPosition(0); 115 } 116 117 @Override 118 public boolean moveToPosition(int position) { 119 return doMoveToPosition(position); 120 } 121 122 private boolean doMoveToPosition(int position) { 123 resultsIndex = position; 124 return resultsIndex >= 0 && resultsIndex < results.length; 125 } 126 127 @Override 128 public void close() { 129 closeWasCalled = true; 130 } 131 132 @Override 133 public int getColumnCount() { 134 if (columnNames.isEmpty()) { 135 return results[0].length; 136 } else { 137 return columnNames.size(); 138 } 139 } 140 141 @Override 142 public String getColumnName(int index) { 143 return columnNames.get(index); 144 } 145 146 @Override 147 public boolean isBeforeFirst() { 148 return resultsIndex < 0; 149 } 150 151 @Override 152 public boolean isAfterLast() { 153 return resultsIndex > results.length - 1; 154 } 155 156 @Override 157 public boolean isFirst() { 158 return resultsIndex == 0; 159 } 160 161 @Override 162 public boolean isLast() { 163 return resultsIndex == results.length - 1; 164 } 165 166 @Override public int getPosition() { 167 return resultsIndex; 168 } 169 170 @Override public boolean move(int offset) { 171 return doMoveToPosition(resultsIndex + offset); 172 } 173 174 @Override public boolean moveToLast() { 175 return doMoveToPosition(results.length - 1); 176 } 177 178 @Override public boolean moveToPrevious() { 179 return doMoveToPosition(resultsIndex - 1); 180 } 181 182 @Override public String[] getColumnNames() { 183 return columnNames.toArray(new String[columnNames.size()]); 184 } 185 186 @Override public boolean isClosed() { 187 return closeWasCalled; 188 } 189 190 @Override public Bundle getExtras() { 191 return extras; 192 } 193 194 @Override 195 public void setExtras(Bundle extras) { 196 this.extras = extras; 197 } 198 199 public void setColumnNames(List<String> columnNames) { 200 this.columnNames = columnNames; 201 } 202 203 public void setResults(Object[][] results) { 204 this.results = results; 205 } 206 207 public boolean getCloseWasCalled() { 208 return closeWasCalled; 209 } 210 } 211