Bug 760956 - Post: Cleanup TopSitesCursorWrapper remainders r=rnewman

MozReview-Commit-ID: ExIkvmfJO5O
This commit is contained in:
Andrzej Hunt 2016-02-17 09:47:06 -08:00
parent 6e38747a3a
commit a9d728f2a1
5 changed files with 0 additions and 979 deletions

View File

@ -1,588 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.db;
import java.util.HashMap;
import java.util.Map;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserContract.TopSites;
import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
/**
* {@TopSitesCursorWrapper} is a cursor wrapper that merges
* the top and pinned sites cursors into one. It ensures the
* cursor will contain at least a given minimum number of
* entries.
*/
public class TopSitesCursorWrapper implements Cursor {
private enum RowType {
UNKNOWN,
BLANK,
TOP,
PINNED,
SUGGESTED
}
private static final String[] columnNames = new String[] {
TopSites._ID,
TopSites.URL,
TopSites.TITLE,
TopSites.BOOKMARK_ID,
TopSites.HISTORY_ID,
TopSites.TYPE,
};
private static final Map<String, Integer> columnIndexes =
new HashMap<String, Integer>(columnNames.length);
static {
for (int i = 0; i < columnNames.length; i++) {
columnIndexes.put(columnNames[i], i);
}
}
// Maps column indexes from the wrapper's to the cursor's.
private SparseIntArray topIndexes;
private SparseIntArray pinnedIndexes;
private SparseIntArray suggestedIndexes;
// Type of content in the current position.
private RowType currentRowType;
// Currently active cursor.
private Cursor currentCursor;
// The cursor for the top sites query. Never null.
private final Cursor topCursor;
// The cursor for the pinned sites query. Never null.
private final Cursor pinnedCursor;
// The cursor for the suggested sites query. Can be null.
private final Cursor suggestedCursor;
// Associates pinned sites and their respective positions.
private final SparseBooleanArray pinnedPositions = new SparseBooleanArray();
// Current position of the cursor.
private int currentPosition = -1;
// Number of pinned sites before the current position.
private int pinnedBefore;
// The size of the cursor wrapper.
private int count;
// The minimum size of the cursor wrapper.
private final int minSize;
public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor topCursor, int minSize) {
this(pinnedCursor, topCursor, null, minSize);
}
public TopSitesCursorWrapper(Cursor pinnedCursor, Cursor topCursor, Cursor suggestedCursor, int minSize) {
currentRowType = RowType.UNKNOWN;
this.minSize = minSize;
// These must not be null.
if (topCursor == null) {
throw new IllegalArgumentException("topCursor is null.");
}
if (pinnedCursor == null) {
throw new IllegalArgumentException("pinnedCursor is null.");
}
this.topCursor = topCursor;
this.pinnedCursor = pinnedCursor;
// Can be null.
this.suggestedCursor = suggestedCursor;
updateIndexMaps();
updatePinnedPositions();
updateCount();
}
private void updateIndexMaps() {
topIndexes = new SparseIntArray(topCursor.getColumnCount());
updateIndexMapFromCursor(topIndexes, topCursor);
pinnedIndexes = new SparseIntArray(pinnedCursor.getColumnCount());
updateIndexMapFromCursor(pinnedIndexes, pinnedCursor);
if (suggestedCursor != null) {
suggestedIndexes = new SparseIntArray(suggestedCursor.getColumnCount());
updateIndexMapFromCursor(suggestedIndexes, suggestedCursor);
}
}
private static void updateIndexMapFromCursor(SparseIntArray indexMap, Cursor c) {
final int columnCount = c.getColumnCount();
for (int i = 0; i < columnCount; i++) {
final Integer index = columnIndexes.get(c.getColumnName(i));
if (index != null) {
indexMap.put(index, i);
}
}
}
private void updatePinnedPositions() {
pinnedPositions.clear();
pinnedCursor.moveToPosition(-1);
while (pinnedCursor.moveToNext()) {
int pos = pinnedCursor.getInt(pinnedCursor.getColumnIndex(Bookmarks.POSITION));
pinnedPositions.put(pos, true);
};
}
private void updateCount() {
int sum = pinnedCursor.getCount() + topCursor.getCount();
if (suggestedCursor != null) {
sum += suggestedCursor.getCount();
}
count = Math.max(minSize, sum);
}
private static boolean cursorHasValidPosition(Cursor c) {
return (c != null && !c.isBeforeFirst() && !c.isAfterLast());
}
private void updatePinnedBefore(int position) {
pinnedBefore = 0;
for (int i = 0; i < position; i++) {
if (pinnedPositions.get(i)) {
pinnedBefore++;
}
}
}
private void setCurrentCursor(Cursor cursor, int position, RowType rowType) {
cursor.moveToPosition(position);
currentRowType = rowType;
currentCursor = cursor;
}
private boolean updateTopCursorPosition(int position) {
final int index = position - pinnedBefore;
if (index >= 0 && index < topCursor.getCount()) {
setCurrentCursor(topCursor, index, RowType.TOP);
return true;
}
return false;
}
private boolean updatePinnedCursorPosition(int position) {
if (position >= minSize) {
return false;
}
if (pinnedPositions.get(position)) {
setCurrentCursor(pinnedCursor, pinnedPositions.indexOfKey(position), RowType.PINNED);
return true;
}
return false;
}
private boolean updateSuggestedCursorPosition(int position) {
if (suggestedCursor == null) {
return false;
}
if (position >= minSize) {
return false;
}
final int index = position - pinnedBefore - topCursor.getCount();
if (index >= 0 && index < suggestedCursor.getCount()) {
setCurrentCursor(suggestedCursor, index, RowType.SUGGESTED);
return true;
}
return false;
}
private void assertValidColumnIndex(int columnIndex) {
if (columnIndex < 0 || columnIndex > columnNames.length - 1) {
throw new IllegalArgumentException("Column index is out of bounds: " + columnIndex);
}
}
private void assertValidRowType() {
if (currentRowType == RowType.UNKNOWN) {
throw new IllegalStateException("No provided cursor holds data at this position");
}
}
private int getColumnIndexForCurrentRowType(int columnIndex) {
assertValidRowType();
assertValidColumnIndex(columnIndex);
SparseIntArray map = null;
switch (currentRowType) {
case TOP:
map = topIndexes;
break;
case PINNED:
map = pinnedIndexes;
break;
case SUGGESTED:
map = suggestedIndexes;
break;
default:
return -1;
}
if (map != null) {
return map.get(columnIndex, -1);
}
return -1;
}
@Override
public int getPosition() {
return currentPosition;
}
@Override
public int getCount() {
return count;
}
@Override
public boolean isAfterLast() {
return (currentPosition >= count);
}
@Override
public boolean isBeforeFirst() {
return (currentPosition < 0);
}
@Override
public boolean isFirst() {
return (currentPosition == 0);
}
@Override
public boolean isLast() {
return (currentPosition == count - 1);
}
@Override
public boolean moveToNext() {
return moveToPosition(currentPosition + 1);
}
@Override
public boolean moveToPrevious() {
return moveToPosition(currentPosition - 1);
}
@Override
public boolean moveToFirst() {
return moveToPosition(0);
}
@Override
public boolean moveToLast() {
return moveToPosition(count - 1);
}
@Override
public boolean move(int offset) {
return moveToPosition(currentPosition + offset);
}
@Override
public boolean moveToPosition(int position) {
currentPosition = position;
updatePinnedBefore(position);
if (!updatePinnedCursorPosition(position) &&
!updateTopCursorPosition(position) &&
!updateSuggestedCursorPosition(position)) {
if (position >= 0 && position < minSize) {
currentRowType = RowType.BLANK;
} else {
currentRowType = RowType.UNKNOWN;
}
currentCursor = null;
}
return cursorHasValidPosition(this);
}
@Override
public long getLong(int columnIndex) {
final int index = getColumnIndexForCurrentRowType(columnIndex);
if (index >= 0) {
return currentCursor.getLong(index);
}
return 0;
}
@Override
public int getInt(int columnIndex) {
assertValidRowType();
assertValidColumnIndex(columnIndex);
if (columnNames[columnIndex].equals(TopSites.TYPE)) {
switch (currentRowType) {
case BLANK:
return TopSites.TYPE_BLANK;
case TOP:
return TopSites.TYPE_TOP;
case PINNED:
return TopSites.TYPE_PINNED;
case SUGGESTED:
return TopSites.TYPE_SUGGESTED;
default:
return -1;
}
}
final int index = getColumnIndexForCurrentRowType(columnIndex);
if (index >= 0) {
return currentCursor.getInt(index);
}
return 0;
}
@Override
public String getString(int columnIndex) {
final int index = getColumnIndexForCurrentRowType(columnIndex);
if (index >= 0) {
return currentCursor.getString(index);
}
return "";
}
@Override
public float getFloat(int columnIndex) {
throw new UnsupportedOperationException();
}
@Override
public double getDouble(int columnIndex) {
throw new UnsupportedOperationException();
}
@Override
public short getShort(int columnIndex) {
throw new UnsupportedOperationException();
}
@Override
public byte[] getBlob(int columnIndex) {
throw new UnsupportedOperationException();
}
@Override
public void copyStringToBuffer(int columnIndex, CharArrayBuffer buffer) {
throw new UnsupportedOperationException();
}
@Override
public boolean isNull(int columnIndex) {
final int index = getColumnIndexForCurrentRowType(columnIndex);
if (index >= 0) {
return currentCursor.isNull(index);
}
return true;
}
@Override
public int getType(int columnIndex) {
throw new UnsupportedOperationException();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public int getColumnIndex(String columnName) {
final Integer index = columnIndexes.get(columnName);
if (index == null) {
return -1;
}
return index;
}
@Override
public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException {
final int index = getColumnIndex(columnName);
if (index < 0) {
throw new IllegalArgumentException("Column index not found: " + columnName);
}
return index;
}
@Override
public String getColumnName(int columnIndex) {
return columnNames[columnIndex];
}
@Override
public String[] getColumnNames() {
return columnNames;
}
@SuppressWarnings("deprecation")
@Override
public boolean requery() {
boolean result = topCursor.requery() && pinnedCursor.requery();
if (suggestedCursor != null) {
result &= suggestedCursor.requery();
}
updatePinnedPositions();
updateCount();
return result;
}
@Override
public Bundle respond(Bundle extras) {
throw new UnsupportedOperationException();
}
@Override
public Bundle getExtras() {
throw new UnsupportedOperationException();
}
@Override
public void setExtras(Bundle extras) {
throw new UnsupportedOperationException();
}
@Override
public boolean getWantsAllOnMoveCalls() {
return false;
}
@Override
public Uri getNotificationUri() {
// There's no single notification URI for the wrapper
return null;
}
@Override
public void setNotificationUri(ContentResolver cr, Uri uri) {
// Keep the original notification URI for the
// wrapped cursors so that we get proper change
// notifications from the ContentResolver.
}
@Override
public void registerContentObserver(ContentObserver observer) {
topCursor.registerContentObserver(observer);
pinnedCursor.registerContentObserver(observer);
if (suggestedCursor != null) {
suggestedCursor.registerContentObserver(observer);
}
}
@Override
public void unregisterContentObserver(ContentObserver observer) {
topCursor.unregisterContentObserver(observer);
pinnedCursor.unregisterContentObserver(observer);
if (suggestedCursor != null) {
suggestedCursor.unregisterContentObserver(observer);
}
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
topCursor.registerDataSetObserver(observer);
pinnedCursor.registerDataSetObserver(observer);
if (suggestedCursor != null) {
suggestedCursor.registerDataSetObserver(observer);
}
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
topCursor.unregisterDataSetObserver(observer);
pinnedCursor.unregisterDataSetObserver(observer);
if (suggestedCursor != null) {
suggestedCursor.unregisterDataSetObserver(observer);
}
}
@SuppressWarnings("deprecation")
@Override
public void deactivate() {
topCursor.deactivate();
pinnedCursor.deactivate();
if (suggestedCursor != null) {
suggestedCursor.deactivate();
}
}
@Override
public boolean isClosed() {
boolean result = topCursor.isClosed() && pinnedCursor.isClosed();
if (suggestedCursor != null) {
result &= suggestedCursor.isClosed();
}
return result;
}
@Override
public void close() {
topCursor.close();
topIndexes = null;
pinnedCursor.close();
pinnedIndexes = null;
pinnedPositions.clear();
if (suggestedCursor != null) {
suggestedCursor.close();
suggestedIndexes = null;
}
}
}

View File

@ -243,7 +243,6 @@ gbjar.sources += ['java/org/mozilla/gecko/' + x for x in [
'db/Table.java',
'db/TabsAccessor.java',
'db/TabsProvider.java',
'db/TopSitesCursorWrapper.java',
'db/UrlAnnotations.java',
'db/URLMetadata.java',
'db/URLMetadataTable.java',

View File

@ -7,4 +7,3 @@ subsuite = browser
[src/org/mozilla/tests/browser/junit3/TestJarReader.java]
[src/org/mozilla/tests/browser/junit3/TestRawResource.java]
[src/org/mozilla/tests/browser/junit3/TestSuggestedSites.java]
[src/org/mozilla/tests/browser/junit3/TestTopSitesCursorWrapper.java]

View File

@ -23,7 +23,6 @@ jar.sources += [
'src/org/mozilla/tests/browser/junit3/TestRawResource.java',
'src/org/mozilla/tests/browser/junit3/TestRemoteTabs.java',
'src/org/mozilla/tests/browser/junit3/TestSuggestedSites.java',
'src/org/mozilla/tests/browser/junit3/TestTopSitesCursorWrapper.java',
]
jar.generated_sources = [] # None yet -- try to keep it this way.
jar.javac_flags += ['-Xlint:all']

View File

@ -1,388 +0,0 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
package org.mozilla.tests.browser.junit3;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.database.MatrixCursor.RowBuilder;
import android.test.InstrumentationTestCase;
import android.text.TextUtils;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserContract.SuggestedSites;
import org.mozilla.gecko.db.BrowserContract.TopSites;
import org.mozilla.gecko.db.TopSitesCursorWrapper;
import java.util.Arrays;
import java.util.List;
public class TestTopSitesCursorWrapper extends InstrumentationTestCase {
private String[] TOP_SITES_COLUMNS = new String[] { Combined._ID,
Combined.URL,
Combined.TITLE,
Combined.BOOKMARK_ID,
Combined.HISTORY_ID };
private String[] PINNED_SITES_COLUMNS = new String[] { Bookmarks._ID,
Bookmarks.URL,
Bookmarks.TITLE,
Bookmarks.POSITION };
private String[] SUGGESTED_SITES_COLUMNS = new String[] { SuggestedSites._ID,
SuggestedSites.URL,
SuggestedSites.TITLE };
private final int MIN_COUNT = 6;
private final String TOP_PREFIX = "top-";
private final String PINNED_PREFIX = "pinned-";
private final String SUGGESTED_PREFIX = "suggested-";
private Cursor createTopSitesCursor(int count) {
MatrixCursor c = new MatrixCursor(TOP_SITES_COLUMNS);
for (int i = 0; i < count; i++) {
RowBuilder row = c.newRow();
row.add(-1);
row.add(TOP_PREFIX + "url" + i);
row.add(TOP_PREFIX + "title" + i);
row.add(i);
row.add(i);
}
return c;
}
private Cursor createPinnedSitesCursor(Integer[] positions) {
MatrixCursor c = new MatrixCursor(PINNED_SITES_COLUMNS);
if (positions == null) {
return c;
}
for (int i = 0; i < positions.length; i++) {
int position = positions[i];
RowBuilder row = c.newRow();
row.add(-1);
row.add(PINNED_PREFIX + "url" + i);
row.add(PINNED_PREFIX + "title" + i);
row.add(position);
}
return c;
}
private Cursor createSuggestedSitesCursor(int count) {
MatrixCursor c = new MatrixCursor(SUGGESTED_SITES_COLUMNS);
for (int i = 0; i < count; i++) {
RowBuilder row = c.newRow();
row.add(-1);
row.add(SUGGESTED_PREFIX + "url" + i);
row.add(SUGGESTED_PREFIX + "title" + i);
}
return c;
}
private TopSitesCursorWrapper createTopSitesCursorWrapper(int topSitesCount, Integer[] pinnedPositions,
int suggestedSitesCount) {
Cursor pinnedSites = createPinnedSitesCursor(pinnedPositions);
Cursor topSites = createTopSitesCursor(topSitesCount);
Cursor suggestedSites = createSuggestedSitesCursor(suggestedSitesCount);
return new TopSitesCursorWrapper(pinnedSites, topSites, suggestedSites, MIN_COUNT);
}
private void assertUrlAndTitle(Cursor c, String prefix, int index) {
String url = c.getString(c.getColumnIndex(TopSites.URL));
String title = c.getString(c.getColumnIndex(TopSites.TITLE));
assertEquals(prefix + "url" + index, url);
assertEquals(prefix + "title" + index, title);
}
private void assertBlank(Cursor c) {
String url = c.getString(c.getColumnIndex(TopSites.URL));
String title = c.getString(c.getColumnIndex(TopSites.TITLE));
assertTrue(TextUtils.isEmpty(url));
assertTrue(TextUtils.isEmpty(title));
}
private void assertRowType(Cursor c, int position, int rowType) {
assertTrue(c.moveToPosition(position));
assertEquals(rowType, getRowType(c));
}
private int getRowType(Cursor c) {
return c.getInt(c.getColumnIndex(TopSites.TYPE));
}
public void testCount() {
// The sum of all sites is smaller than MIN_COUNT
Cursor c = createTopSitesCursorWrapper(2, new Integer[] { 0, 1 }, 1);
assertEquals(MIN_COUNT, c.getCount());
c.close();
// No top or suggested sites, some pinned sites, still smaller than MIN_COUNT
c = createTopSitesCursorWrapper(0, new Integer[] { 0, 1, 2 }, 0);
assertEquals(MIN_COUNT, c.getCount());
c.close();
// Some top sites, no pinned or suggested sites, still smaller than MIN_COUNT
c = createTopSitesCursorWrapper(3, null, 0);
assertEquals(MIN_COUNT, c.getCount());
c.close();
// Some suggested sites, no pinned or top sites, still smaller than MIN_COUNT
c = createTopSitesCursorWrapper(0, null, 3);
assertEquals(MIN_COUNT, c.getCount());
c.close();
// The sum of top and pinned sites is greater than MIN_COUNT
c = createTopSitesCursorWrapper(10, new Integer[] { 0, 1, 2 }, 0);
assertEquals(13, c.getCount());
c.close();
// The sum of suggested and pinned sites is greater than MIN_COUNT
c = createTopSitesCursorWrapper(0, new Integer[] { 0, 1 }, 8);
assertEquals(10, c.getCount());
c.close();
// The sum of top, pinned, and suggested sites is greater than MIN_COUNT
c = createTopSitesCursorWrapper(2, new Integer[] { 0, 1, 2 }, 2);
assertEquals(7, c.getCount());
c.close();
// The sum of top and suggested sites is smaller than MIN_COUNT
c = createTopSitesCursorWrapper(2, null, 2);
assertEquals(MIN_COUNT, c.getCount());
c.close();
}
public void testCloseWrappedCursors() {
Cursor pinnedSites = createPinnedSitesCursor(new Integer[] { 0, 1 });
Cursor topSites = createTopSitesCursor(2);
Cursor suggestedSites = createSuggestedSitesCursor(2);
Cursor wrapper = new TopSitesCursorWrapper(pinnedSites, topSites, suggestedSites, MIN_COUNT);
assertFalse(pinnedSites.isClosed());
assertFalse(topSites.isClosed());
assertFalse(suggestedSites.isClosed());
wrapper.close();
// Closing wrapper closes wrapped cursors
assertTrue(pinnedSites.isClosed());
assertTrue(topSites.isClosed());
assertTrue(suggestedSites.isClosed());
}
public void testIsPinned() {
Integer[] pinnedPositions = new Integer[] { 0, 1 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 2);
List<Integer> pinnedList = Arrays.asList(pinnedPositions);
c.moveToPosition(-1);
while (c.moveToNext()) {
boolean isPinnedPosition = pinnedList.contains(c.getPosition());
assertEquals(isPinnedPosition, getRowType(c) == TopSites.TYPE_PINNED);
}
c.close();
}
public void testBlankPositions() {
Integer[] pinnedPositions = new Integer[] { 0, 1, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(0, pinnedPositions, 1);
c.moveToPosition(-1);
while (c.moveToNext()) {
if (getRowType(c) == TopSites.TYPE_BLANK) {
assertBlank(c);
}
}
c.close();
}
public void testIsNull() {
Integer[] pinnedPositions = new Integer[] { 0, 1, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 1);
c.moveToPosition(-1);
while (c.moveToNext()) {
int rowType = getRowType(c);
if (rowType == TopSites.TYPE_BLANK) {
assertTrue(c.isNull(c.getColumnIndex(TopSites.URL)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.TITLE)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.BOOKMARK_ID)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.HISTORY_ID)));
} else if (rowType == TopSites.TYPE_PINNED) {
assertFalse(c.isNull(c.getColumnIndex(TopSites.URL)));
assertFalse(c.isNull(c.getColumnIndex(TopSites.TITLE)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.BOOKMARK_ID)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.HISTORY_ID)));
} else if (rowType == TopSites.TYPE_TOP) {
assertFalse(c.isNull(c.getColumnIndex(TopSites.URL)));
assertFalse(c.isNull(c.getColumnIndex(TopSites.TITLE)));
assertFalse(c.isNull(c.getColumnIndex(TopSites.BOOKMARK_ID)));
assertFalse(c.isNull(c.getColumnIndex(TopSites.HISTORY_ID)));
} else if (rowType == TopSites.TYPE_SUGGESTED) {
assertFalse(c.isNull(c.getColumnIndex(TopSites.URL)));
assertFalse(c.isNull(c.getColumnIndex(TopSites.TITLE)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.BOOKMARK_ID)));
assertTrue(c.isNull(c.getColumnIndex(TopSites.HISTORY_ID)));
} else {
fail("Invalid row type found in the cursor");
}
}
c.close();
}
public void testColumnValues() {
Integer[] pinnedPositions = new Integer[] { 0, 1 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 1);
int topIndex = 0;
int pinnedIndex = 0;
int suggestedIndex = 0;
c.moveToPosition(-1);
while (c.moveToNext()) {
int rowType = getRowType(c);
if (rowType == TopSites.TYPE_BLANK) {
assertBlank(c);
} else if (rowType == TopSites.TYPE_PINNED) {
assertUrlAndTitle(c, PINNED_PREFIX, pinnedIndex++);
} else if (rowType == TopSites.TYPE_TOP) {
assertUrlAndTitle(c, TOP_PREFIX, topIndex++);
} else if (rowType == TopSites.TYPE_SUGGESTED) {
assertUrlAndTitle(c, SUGGESTED_PREFIX, suggestedIndex++);
} else {
fail("Invalid row type found in the cursor");
}
}
c.close();
}
public void testColumns() {
Integer[] pinnedPositions = new Integer[] { 0, 1, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 3);
assertEquals(6, c.getColumnCount());
String[] columnNames = c.getColumnNames();
assertEquals(columnNames.length, c.getColumnCount());
boolean allRowsHaveAllCols = true;
c.moveToPosition(-1);
while (c.moveToNext()) {
for (int i = 0; i < columnNames.length; i++) {
try {
c.getColumnIndexOrThrow(columnNames[i]);
} catch (Exception e) {
allRowsHaveAllCols = false;
}
}
}
assertTrue(allRowsHaveAllCols);
c.close();
}
public void testRowTypes() {
Integer[] pinnedPositions = new Integer[] { 0, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 1);
// Check pinned sites
for (int i = 0; i < pinnedPositions.length; i++) {
assertRowType(c, pinnedPositions[0], TopSites.TYPE_PINNED);
}
// Check top sites
assertRowType(c, 1, TopSites.TYPE_TOP);
assertRowType(c, 2, TopSites.TYPE_TOP);
// Suggested site
assertRowType(c, 3, TopSites.TYPE_SUGGESTED);
// Blank position
assertRowType(c, 5, TopSites.TYPE_BLANK);
c.close();
}
public void testPositionOutOfBounds() {
Integer[] pinnedPositions = new Integer[] { 0, 1, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 1);
boolean failed = false;
try {
assertFalse(c.moveToPosition(-1));
c.getInt(0);
} catch (Exception e) {
failed = true;
}
assertTrue(failed);
failed = false;
try {
assertFalse(c.moveToPosition(c.getCount()));
c.getInt(0);
} catch (Exception e) {
failed = true;
}
assertTrue(failed);
c.close();
}
public void testColumnIndexOutOfBounds() {
Integer[] pinnedPositions = new Integer[] { 0, 1, 4 };
TopSitesCursorWrapper c = createTopSitesCursorWrapper(2, pinnedPositions, 1);
boolean failed = false;
try {
assertTrue(c.moveToPosition(0));
c.getInt(-1);
} catch (Exception e) {
failed = true;
}
assertTrue(failed);
failed = false;
try {
assertTrue(c.moveToPosition(0));
c.getString(c.getColumnCount());
} catch (Exception e) {
failed = true;
}
assertTrue(failed);
c.close();
}
public void testNullSuggestedCursor() {
Cursor pinnedSites = createPinnedSitesCursor(new Integer[] { 0, 1, 4 });
Cursor topSites = createTopSitesCursor(3);
Cursor c = new TopSitesCursorWrapper(pinnedSites, topSites, null, MIN_COUNT);
// Simply go through all rows and make sure nothing breaks.
c.moveToPosition(-1);
while (c.moveToNext()) {
assertNotSame(TopSites.TYPE_SUGGESTED, getRowType(c));
}
c.close();
}
}