1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.messaging.datamodel;
18 
19 import android.os.Bundle;
20 import android.test.mock.MockCursor;
21 
22 import java.util.ArrayList;
23 
24 /**
25  * A simple in memory fake cursor that can be used for UI tests.
26  */
27 public class FakeCursor extends MockCursor {
28     private final ArrayList<Integer> mProjection;
29     private final String[] mColumnNamesOfData;
30     private final Object[][] mData;
31     private int mIndex;
32 
FakeCursor(final String[] projection, final String[] columnNames, final Object[][] data)33     public FakeCursor(final String[] projection, final String[] columnNames,
34             final Object[][] data) {
35         mColumnNamesOfData = columnNames;
36         mData = data;
37         mIndex = -1;
38         mProjection = new ArrayList<Integer>(projection.length);
39         for (final String column : projection) {
40             mProjection.add(getColumnIndex(column));
41         }
42     }
43 
getAt(final String columnName, final int row)44     public Object getAt(final String columnName, final int row) {
45         final int dataIdx = getColumnIndex(columnName);
46         return (dataIdx < 0 || row < 0 || row >= mData.length) ? 0 : mData[row][dataIdx];
47     }
48 
49     @Override
getCount()50     public int getCount() {
51         return mData.length;
52     }
53 
54     @Override
isFirst()55     public boolean isFirst() {
56         return mIndex == 0;
57     }
58 
59     @Override
isLast()60     public boolean isLast() {
61         return mIndex == mData.length - 1;
62     }
63 
64     @Override
moveToFirst()65     public boolean moveToFirst() {
66         if (mData.length == 0) {
67             return false;
68         }
69         mIndex = 0;
70         return true;
71     }
72 
73     @Override
moveToPosition(final int position)74     public boolean moveToPosition(final int position) {
75         if (position < 0 || position >= mData.length) {
76             return false;
77         }
78         mIndex = position;
79         return true;
80     }
81 
82     @Override
getPosition()83     public int getPosition() {
84         return mIndex;
85     }
86 
87     @Override
moveToPrevious()88     public boolean moveToPrevious() {
89         if (mIndex <= 0) {
90             return false;
91         }
92         mIndex--;
93         return true;
94     }
95 
96     @Override
moveToNext()97     public boolean moveToNext() {
98         if (mIndex == mData.length - 1) {
99             return false;
100         }
101 
102         mIndex++;
103         return true;
104     }
105 
106     @Override
getColumnCount()107     public int getColumnCount() {
108         return mColumnNamesOfData.length;
109     }
110 
111     @Override
getColumnIndex(final String columnName)112     public int getColumnIndex(final String columnName) {
113         for (int i = 0 ; i < mColumnNamesOfData.length ; i++) {
114             if (mColumnNamesOfData[i].equals(columnName)) {
115                 return i;
116             }
117         }
118         return -1;
119     }
120 
121     @Override
getColumnIndexOrThrow(final String columnName)122     public int getColumnIndexOrThrow(final String columnName) {
123         final int result = getColumnIndex(columnName);
124         if (result == -1) {
125             throw new IllegalArgumentException();
126         }
127 
128         return result;
129     }
130 
131     @Override
getString(final int columnIndex)132     public String getString(final int columnIndex) {
133         final int dataIdx = mProjection.get(columnIndex);
134         final Object obj = (dataIdx < 0 ? null : mData[mIndex][dataIdx]);
135         return (obj == null ? null : obj.toString());
136     }
137 
138     @Override
getInt(final int columnIndex)139     public int getInt(final int columnIndex) {
140         final int dataIdx = mProjection.get(columnIndex);
141         return (dataIdx < 0 ? 0 : (Integer) mData[mIndex][dataIdx]);
142     }
143 
144     @Override
getLong(final int columnIndex)145     public long getLong(final int columnIndex) {
146         final int dataIdx = mProjection.get(columnIndex);
147         return (dataIdx < 0 ? 0 : (Long) mData[mIndex][dataIdx]);
148     }
149 
150     @Override
close()151     public void close() {
152     }
153 
154     @Override
isClosed()155     public boolean isClosed() {
156         return false;
157     }
158 
159     @Override
getExtras()160     public Bundle getExtras() { return null; }
161 }
162