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
17package com.android.messaging.datamodel;
18
19import android.os.Bundle;
20import android.test.mock.MockCursor;
21
22import java.util.ArrayList;
23
24/**
25 * A simple in memory fake cursor that can be used for UI tests.
26 */
27public 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
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
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
50    public int getCount() {
51        return mData.length;
52    }
53
54    @Override
55    public boolean isFirst() {
56        return mIndex == 0;
57    }
58
59    @Override
60    public boolean isLast() {
61        return mIndex == mData.length - 1;
62    }
63
64    @Override
65    public boolean moveToFirst() {
66        if (mData.length == 0) {
67            return false;
68        }
69        mIndex = 0;
70        return true;
71    }
72
73    @Override
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
83    public int getPosition() {
84        return mIndex;
85    }
86
87    @Override
88    public boolean moveToPrevious() {
89        if (mIndex <= 0) {
90            return false;
91        }
92        mIndex--;
93        return true;
94    }
95
96    @Override
97    public boolean moveToNext() {
98        if (mIndex == mData.length - 1) {
99            return false;
100        }
101
102        mIndex++;
103        return true;
104    }
105
106    @Override
107    public int getColumnCount() {
108        return mColumnNamesOfData.length;
109    }
110
111    @Override
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
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
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
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
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
151    public void close() {
152    }
153
154    @Override
155    public boolean isClosed() {
156        return false;
157    }
158
159    @Override
160    public Bundle getExtras() { return null; }
161}
162