Bug 784086 - Separate images table into favicon and thumbnail tables. r=lucasr,rnewman

This commit is contained in:
Brian Nicholson 2012-11-13 10:56:58 -08:00
parent 437ea18f52
commit 3e3a7db09d
11 changed files with 1042 additions and 658 deletions

View File

@ -5,9 +5,9 @@
package org.mozilla.gecko;
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.db.BrowserContract.Images;
import org.mozilla.gecko.sync.setup.SyncAccounts;
import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity;
import org.mozilla.gecko.util.GeckoAsyncTask;
@ -352,8 +352,8 @@ public class AboutHomeContent extends ScrollView
return thumbnails;
do {
final String url = c.getString(c.getColumnIndexOrThrow(Images.URL));
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Images.THUMBNAIL));
final String url = c.getString(c.getColumnIndexOrThrow(Thumbnails.URL));
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Thumbnails.DATA));
if (b == null)
continue;

View File

@ -45,7 +45,6 @@ public class Favicons {
public static final long NOT_LOADING = 0;
private Context mContext;
private DatabaseHelper mDbHelper;
private Map<Long,LoadFaviconTask> mLoadTasks;
private long mNextFaviconLoadId;
@ -57,89 +56,10 @@ public class Favicons {
public void onFaviconLoaded(String url, Bitmap favicon);
}
private class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "favicon_urls.db";
private static final String TABLE_NAME = "favicon_urls";
private static final int DATABASE_VERSION = 1;
private static final String COLUMN_ID = "_id";
private static final String COLUMN_FAVICON_URL = "favicon_url";
private static final String COLUMN_PAGE_URL = "page_url";
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(LOGTAG, "Creating DatabaseHelper");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(LOGTAG, "Creating database for favicon URLs");
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY," +
COLUMN_FAVICON_URL + " TEXT NOT NULL," +
COLUMN_PAGE_URL + " TEXT UNIQUE NOT NULL" +
");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(LOGTAG, "Upgrading favicon URLs database from version " +
oldVersion + " to " + newVersion + ", which will destroy all old data");
// Drop table completely
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Recreate database
onCreate(db);
}
public String getFaviconUrlForPageUrl(String pageUrl) {
SQLiteDatabase db = mDbHelper.getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(TABLE_NAME);
Cursor c = qb.query(
db,
new String[] { COLUMN_FAVICON_URL },
COLUMN_PAGE_URL + " = ?",
new String[] { pageUrl },
null, null, null
);
if (!c.moveToFirst()) {
c.close();
return null;
}
String url = c.getString(c.getColumnIndexOrThrow(COLUMN_FAVICON_URL));
c.close();
return url;
}
public void setFaviconUrlForPageUrl(String pageUrl, String faviconUrl) {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_FAVICON_URL, faviconUrl);
values.put(COLUMN_PAGE_URL, pageUrl);
db.replace(TABLE_NAME, null, values);
}
public void clearFavicons() {
SQLiteDatabase db = mDbHelper.getWritableDatabase();
db.delete(TABLE_NAME, null, null);
}
}
public Favicons(Context context) {
Log.d(LOGTAG, "Creating Favicons instance");
mContext = context;
mDbHelper = new DatabaseHelper(context);
mLoadTasks = Collections.synchronizedMap(new HashMap<Long,LoadFaviconTask>());
mNextFaviconLoadId = 0;
@ -176,7 +96,7 @@ public class Favicons {
}
public String getFaviconUrlForPageUrl(String pageUrl) {
return mDbHelper.getFaviconUrlForPageUrl(pageUrl);
return BrowserDB.getFaviconUrlForHistoryUrl(mContext.getContentResolver(), pageUrl);
}
public long loadFavicon(String pageUrl, String faviconUrl, boolean persist,
@ -233,13 +153,8 @@ public class Favicons {
return cancelled;
}
public void clearFavicons() {
mDbHelper.clearFavicons();
}
public void close() {
Log.d(LOGTAG, "Closing Favicons database");
mDbHelper.close();
// Cancel any pending tasks
synchronized (mLoadTasks) {
@ -287,11 +202,9 @@ public class Favicons {
// since the Async task can run this on any number of threads in the
// pool, we need to protect against inserting the same url twice
synchronized(mDbHelper) {
synchronized(Favicons.this) {
ContentResolver resolver = mContext.getContentResolver();
BrowserDB.updateFaviconForUrl(resolver, mPageUrl, favicon);
mDbHelper.setFaviconUrlForPageUrl(mPageUrl, mFaviconUrl);
BrowserDB.updateFaviconForUrl(resolver, mPageUrl, favicon, mFaviconUrl);
}
}
@ -361,7 +274,7 @@ public class Favicons {
if (isCancelled())
return null;
String storedFaviconUrl = mDbHelper.getFaviconUrlForPageUrl(mPageUrl);
String storedFaviconUrl = getFaviconUrlForPageUrl(mPageUrl);
if (storedFaviconUrl != null && storedFaviconUrl.equals(mFaviconUrl)) {
image = loadFaviconFromDb();
if (image != null)

View File

@ -812,7 +812,6 @@ abstract public class GeckoApp
void handleClearHistory() {
BrowserDB.clearHistory(getContentResolver());
getFavicons().clearFavicons();
}
/**

View File

@ -7,7 +7,6 @@ package org.mozilla.gecko;
import org.mozilla.gecko.AwesomeBar.ContextMenuSubject;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserContract.Images;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserDB.URLColumns;
import org.mozilla.gecko.util.GeckoAsyncTask;
@ -767,8 +766,8 @@ public class AllPagesTab extends AwesomeBarTab implements GeckoEventListener {
Favicons favicons = GeckoApp.mAppContext.getFavicons();
do {
final String url = c.getString(c.getColumnIndexOrThrow(Images.URL));
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Images.FAVICON));
final String url = c.getString(c.getColumnIndexOrThrow(Combined.URL));
final byte[] b = c.getBlob(c.getColumnIndexOrThrow(Combined.FAVICON));
if (b == null)
continue;

View File

@ -53,10 +53,13 @@ public class BrowserContract {
public static final String _ID = "_id";
}
public interface SyncColumns {
public static final String GUID = "guid";
public interface DateSyncColumns {
public static final String DATE_CREATED = "created";
public static final String DATE_MODIFIED = "modified";
}
public interface SyncColumns extends DateSyncColumns {
public static final String GUID = "guid";
public static final String IS_DELETED = "deleted";
}
@ -65,9 +68,10 @@ public class BrowserContract {
public static final String TITLE = "title";
}
public interface ImageColumns {
public interface FaviconColumns {
public static final String FAVICON = "favicon";
public static final String THUMBNAIL = "thumbnail";
public static final String FAVICON_ID = "favicon_id";
public static final String FAVICON_URL = "favicon_url";
}
public interface HistoryColumns {
@ -81,15 +85,26 @@ public class BrowserContract {
public static final String TIME_DELETED = "timeDeleted";
}
public static final class Images implements CommonColumns, ImageColumns, SyncColumns {
private Images() {}
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "images");
public static final class Favicons implements CommonColumns, DateSyncColumns {
private Favicons() {}
public static final String URL = "url_key";
public static final String FAVICON_URL = "favicon_url";
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "favicons");
public static final String URL = "url";
public static final String DATA = "data";
public static final String PAGE_URL = "page_url";
}
public static final class Bookmarks implements CommonColumns, URLColumns, ImageColumns, SyncColumns {
public static final class Thumbnails implements CommonColumns {
private Thumbnails() {}
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "thumbnails");
public static final String URL = "url";
public static final String DATA = "data";
}
public static final class Bookmarks implements CommonColumns, URLColumns, FaviconColumns, SyncColumns {
private Bookmarks() {}
public static final int FIXED_ROOT_ID = 0;
@ -127,7 +142,7 @@ public class BrowserContract {
public static final String KEYWORD = "keyword";
}
public static final class History implements CommonColumns, URLColumns, HistoryColumns, ImageColumns, SyncColumns {
public static final class History implements CommonColumns, URLColumns, HistoryColumns, FaviconColumns, SyncColumns {
private History() {}
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "history");
public static final Uri CONTENT_OLD_URI = Uri.withAppendedPath(AUTHORITY_URI, "history/old");
@ -136,7 +151,7 @@ public class BrowserContract {
}
// Combined bookmarks and history
public static final class Combined implements CommonColumns, URLColumns, HistoryColumns, ImageColumns {
public static final class Combined implements CommonColumns, URLColumns, HistoryColumns, FaviconColumns {
private Combined() {}
public static final Uri CONTENT_URI = Uri.withAppendedPath(AUTHORITY_URI, "combined");

View File

@ -78,7 +78,9 @@ public class BrowserDB {
public Cursor getFaviconsForUrls(ContentResolver cr, List<String> urls);
public void updateFaviconForUrl(ContentResolver cr, String uri, Bitmap favicon);
public String getFaviconUrlForHistoryUrl(ContentResolver cr, String url);
public void updateFaviconForUrl(ContentResolver cr, String pageUri, Bitmap favicon, String faviconUri);
public void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail);
@ -199,8 +201,12 @@ public class BrowserDB {
return sDb.getFaviconsForUrls(cr, urls);
}
public static void updateFaviconForUrl(ContentResolver cr, String uri, Bitmap favicon) {
sDb.updateFaviconForUrl(cr, uri, favicon);
public static String getFaviconUrlForHistoryUrl(ContentResolver cr, String url) {
return sDb.getFaviconUrlForHistoryUrl(cr, url);
}
public static void updateFaviconForUrl(ContentResolver cr, String pageUri, Bitmap favicon, String faviconUri) {
sDb.updateFaviconForUrl(cr, pageUri, favicon, faviconUri);
}
public static void updateThumbnailForUrl(ContentResolver cr, String uri, BitmapDrawable thumbnail) {

File diff suppressed because it is too large Load Diff

View File

@ -7,12 +7,13 @@ package org.mozilla.gecko.db;
import org.mozilla.gecko.db.BrowserContract.Bookmarks;
import org.mozilla.gecko.db.BrowserContract.Combined;
import org.mozilla.gecko.db.BrowserContract.History;
import org.mozilla.gecko.db.BrowserContract.ImageColumns;
import org.mozilla.gecko.db.BrowserContract.Images;
import org.mozilla.gecko.db.BrowserContract.SyncColumns;
import org.mozilla.gecko.db.BrowserContract.URLColumns;
import org.mozilla.gecko.db.BrowserContract.ExpirePriority;
import org.mozilla.gecko.db.BrowserContract.Favicons;
import org.mozilla.gecko.db.BrowserContract.FaviconColumns;
import org.mozilla.gecko.db.BrowserContract.History;
import org.mozilla.gecko.db.BrowserContract.SyncColumns;
import org.mozilla.gecko.db.BrowserContract.Thumbnails;
import org.mozilla.gecko.db.BrowserContract.URLColumns;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
@ -57,10 +58,11 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
private final Uri mParentsUriWithProfile;
private final Uri mHistoryUriWithProfile;
private final Uri mHistoryExpireUriWithProfile;
private final Uri mImagesUriWithProfile;
private final Uri mCombinedUriWithProfile;
private final Uri mDeletedHistoryUriWithProfile;
private final Uri mUpdateHistoryUriWithProfile;
private final Uri mFaviconsUriWithProfile;
private final Uri mThumbnailsUriWithProfile;
private static final String[] DEFAULT_BOOKMARK_COLUMNS =
new String[] { Bookmarks._ID,
@ -82,8 +84,9 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
mParentsUriWithProfile = appendProfile(Bookmarks.PARENTS_CONTENT_URI);
mHistoryUriWithProfile = appendProfile(History.CONTENT_URI);
mHistoryExpireUriWithProfile = appendProfile(History.CONTENT_OLD_URI);
mImagesUriWithProfile = appendProfile(Images.CONTENT_URI);
mCombinedUriWithProfile = appendProfile(Combined.CONTENT_URI);
mFaviconsUriWithProfile = appendProfile(Favicons.CONTENT_URI);
mThumbnailsUriWithProfile = appendProfile(Thumbnails.CONTENT_URI);
mDeletedHistoryUriWithProfile = mHistoryUriWithProfile.buildUpon().
appendQueryParameter(BrowserContract.PARAM_SHOW_DELETED, "1").build();
@ -130,8 +133,8 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
return uriBuilder.build();
}
private Uri getAllImagesUri() {
Uri.Builder uriBuilder = mImagesUriWithProfile.buildUpon()
private Uri getAllFaviconsUri() {
Uri.Builder uriBuilder = mFaviconsUriWithProfile.buildUpon()
.appendQueryParameter(BrowserContract.PARAM_SHOW_DELETED, "1");
return uriBuilder.build();
}
@ -191,11 +194,9 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
// ignore folders, tags, keywords, separators, etc.
constraint = Bookmarks.TYPE + " = " + Bookmarks.TYPE_BOOKMARK;
} else if ("thumbnails".equals(database)) {
uri = mImagesUriWithProfile;
constraint = Combined.THUMBNAIL + " IS NOT NULL";
uri = mThumbnailsUriWithProfile;
} else if ("favicons".equals(database)) {
uri = mImagesUriWithProfile;
constraint = Combined.FAVICON + " IS NOT NULL";
uri = mFaviconsUriWithProfile;
}
if (uri != null) {
cursor = cr.query(uri, null, constraint, null, null);
@ -628,9 +629,9 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
}
public Bitmap getFaviconForUrl(ContentResolver cr, String uri) {
Cursor c = cr.query(mImagesUriWithProfile,
new String[] { Images.FAVICON },
Images.URL + " = ?",
Cursor c = cr.query(mCombinedUriWithProfile,
new String[] { Combined.FAVICON },
Combined.URL + " = ?",
new String[] { uri },
null);
@ -639,7 +640,7 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
return null;
}
int faviconIndex = c.getColumnIndexOrThrow(Images.FAVICON);
int faviconIndex = c.getColumnIndexOrThrow(Combined.FAVICON);
byte[] b = c.getBlob(faviconIndex);
c.close();
@ -650,6 +651,24 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
public String getFaviconUrlForHistoryUrl(ContentResolver cr, String uri) {
Cursor c = cr.query(mHistoryUriWithProfile,
new String[] { History.FAVICON_URL },
Combined.URL + " = ?",
new String[] { uri },
null);
if (!c.moveToFirst()) {
c.close();
return null;
}
String faviconUrl = c.getString(c.getColumnIndexOrThrow(History.FAVICON_URL));
c.close();
return faviconUrl;
}
public Cursor getFaviconsForUrls(ContentResolver cr, List<String> urls) {
StringBuffer selection = new StringBuffer();
String[] selectionArgs = new String[urls.size()];
@ -660,36 +679,38 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
if (i > 0)
selection.append(" OR ");
selection.append(Images.URL + " = ?");
selection.append(Favicons.URL + " = ?");
selectionArgs[i] = url;
}
return cr.query(mImagesUriWithProfile,
new String[] { Images.URL, Images.FAVICON },
return cr.query(mCombinedUriWithProfile,
new String[] { Combined.URL, Combined.FAVICON },
selection.toString(),
selectionArgs,
null);
}
public void updateFaviconForUrl(ContentResolver cr, String uri,
Bitmap favicon) {
public void updateFaviconForUrl(ContentResolver cr, String pageUri,
Bitmap favicon, String faviconUri) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
favicon.compress(Bitmap.CompressFormat.PNG, 100, stream);
ContentValues values = new ContentValues();
values.put(Images.FAVICON, stream.toByteArray());
values.put(Images.URL, uri);
values.put(Favicons.URL, faviconUri);
values.put(Favicons.DATA, stream.toByteArray());
values.put(Favicons.PAGE_URL, pageUri);
// Restore deleted record if possible
values.put(Images.IS_DELETED, 0);
// Update or insert
Uri faviconsUri = getAllFaviconsUri().buildUpon().
appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build();
int updated = cr.update(mImagesUriWithProfile,
int updated = cr.update(faviconsUri,
values,
Images.URL + " = ?",
new String[] { uri });
Favicons.URL + " = ?",
new String[] { faviconUri });
if (updated == 0)
cr.insert(mImagesUriWithProfile, values);
cr.insert(mFaviconsUriWithProfile, values);
}
public void updateThumbnailForUrl(ContentResolver cr, String uri,
@ -700,25 +721,22 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
ContentValues values = new ContentValues();
values.put(Images.THUMBNAIL, stream.toByteArray());
values.put(Images.URL, uri);
values.put(Thumbnails.DATA, stream.toByteArray());
values.put(Thumbnails.URL, uri);
// Restore deleted record if possible
values.put(Images.IS_DELETED, 0);
int updated = cr.update(mImagesUriWithProfile,
int updated = cr.update(mThumbnailsUriWithProfile,
values,
Images.URL + " = ?",
Thumbnails.URL + " = ?",
new String[] { uri });
if (updated == 0)
cr.insert(mImagesUriWithProfile, values);
cr.insert(mThumbnailsUriWithProfile, values);
}
public byte[] getThumbnailForUrl(ContentResolver cr, String uri) {
Cursor c = cr.query(mImagesUriWithProfile,
new String[] { Images.THUMBNAIL },
Images.URL + " = ?",
Cursor c = cr.query(mThumbnailsUriWithProfile,
new String[] { Thumbnails.DATA },
Thumbnails.URL + " = ?",
new String[] { uri },
null);
@ -727,7 +745,7 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
return null;
}
int thumbnailIndex = c.getColumnIndexOrThrow(Images.THUMBNAIL);
int thumbnailIndex = c.getColumnIndexOrThrow(Thumbnails.DATA);
byte[] b = c.getBlob(thumbnailIndex);
c.close();
@ -745,21 +763,19 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
if (i > 0)
selection.append(" OR ");
selection.append(Images.URL + " = ?");
selection.append(Thumbnails.URL + " = ?");
selectionArgs[i] = url;
}
return cr.query(mImagesUriWithProfile,
new String[] { Images.URL, Images.THUMBNAIL },
return cr.query(mThumbnailsUriWithProfile,
new String[] { Thumbnails.URL, Thumbnails.DATA },
selection.toString(),
selectionArgs,
null);
}
public void removeThumbnails(ContentResolver cr) {
ContentValues values = new ContentValues();
values.putNull(Images.THUMBNAIL);
cr.update(mImagesUriWithProfile, values, null, null);
cr.delete(mThumbnailsUriWithProfile, null, null);
}
// Utility function for updating existing history using batch operations
@ -910,25 +926,20 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
String url, String faviconUrl,
String faviconGuid, byte[] data) {
ContentValues values = new ContentValues();
values.put(Images.FAVICON, data);
values.put(Images.URL, url);
values.put(Favicons.DATA, data);
values.put(Favicons.PAGE_URL, url);
if (faviconUrl != null) {
values.put(Images.FAVICON_URL, faviconUrl);
}
// Restore deleted record if possible
values.put(Images.IS_DELETED, 0);
if (faviconGuid != null) {
values.put(Images.GUID, faviconGuid);
values.put(Favicons.URL, faviconUrl);
}
// Update or insert
Uri imagesUri = getAllImagesUri().buildUpon().
Uri faviconsUri = getAllFaviconsUri().buildUpon().
appendQueryParameter(BrowserContract.PARAM_INSERT_IF_NEEDED, "true").build();
// Update or insert
ContentProviderOperation.Builder builder =
ContentProviderOperation.newUpdate(imagesUri);
ContentProviderOperation.newUpdate(faviconsUri);
builder.withValues(values);
builder.withSelection(Images.URL + " = ?", new String[] { url });
builder.withSelection(Favicons.PAGE_URL + " = ?", new String[] { url });
// Queue the operation
operations.add(builder.build());
}
@ -1035,9 +1046,7 @@ public class LocalBrowserDB implements BrowserDB.BrowserDBIface {
} else if (columnName.equals(BrowserDB.URLColumns.TITLE)) {
columnName = URLColumns.TITLE;
} else if (columnName.equals(BrowserDB.URLColumns.FAVICON)) {
columnName = ImageColumns.FAVICON;
} else if (columnName.equals(BrowserDB.URLColumns.THUMBNAIL)) {
columnName = ImageColumns.THUMBNAIL;
columnName = FaviconColumns.FAVICON;
} else if (columnName.equals(BrowserDB.URLColumns.DATE_LAST_VISITED)) {
columnName = History.DATE_LAST_VISITED;
} else if (columnName.equals(BrowserDB.URLColumns.VISITS)) {

View File

@ -26,7 +26,6 @@ public class BrowserContractHelpers extends BrowserContract {
.build();
}
public static final Uri IMAGES_CONTENT_URI = withSyncAndDeleted(Images.CONTENT_URI);
public static final Uri BOOKMARKS_CONTENT_URI = withSyncAndDeleted(Bookmarks.CONTENT_URI);
public static final Uri BOOKMARKS_PARENTS_CONTENT_URI = withSyncAndDeleted(Bookmarks.PARENTS_CONTENT_URI);
public static final Uri BOOKMARKS_POSITIONS_CONTENT_URI = withSyncAndDeleted(Bookmarks.POSITIONS_CONTENT_URI);

View File

@ -38,14 +38,14 @@ public class testBrowserProvider extends ContentProviderTest {
private Uri mBookmarksPositionUri;
private Uri mHistoryUri;
private Uri mHistoryOldUri;
private Uri mImagesUri;
private Uri mCombinedUri;
private Uri mFaviconsUri;
private Uri mThumbnailsUri;
private String mBookmarksIdCol;
private String mBookmarksTitleCol;
private String mBookmarksUrlCol;
private String mBookmarksFaviconCol;
private String mBookmarksThumbnailCol;
private String mBookmarksParentCol;
private String mBookmarksTypeCol;
private String mBookmarksPositionCol;
@ -65,17 +65,12 @@ public class testBrowserProvider extends ContentProviderTest {
private String mHistoryUrlCol;
private String mHistoryVisitsCol;
private String mHistoryFaviconCol;
private String mHistoryThumbnailCol;
private String mHistoryLastVisitedCol;
private String mHistoryGuidCol;
private String mHistoryIsDeletedCol;
private String mHistoryDateCreatedCol;
private String mHistoryDateModifiedCol;
private String mImagesThumbnailCol;
private String mImagesFaviconCol;
private String mImagesUrlCol;
private String mCombinedIdCol;
private String mCombinedBookmarkIdCol;
private String mCombinedHistoryIdCol;
@ -85,10 +80,16 @@ public class testBrowserProvider extends ContentProviderTest {
private String mCombinedVisitsCol;
private String mCombinedLastVisitedCol;
private String mCombinedFaviconCol;
private String mCombinedThumbnailCol;
private int mCombinedDisplayNormal;
private int mCombinedDisplayReader;
private String mFaviconsUrlCol;
private String mFaviconsPageUrlCol;
private String mFaviconsDataCol;
private String mThumbnailsUrlCol;
private String mThumbnailsDataCol;
@Override
protected int getTestType() {
return TEST_MOCHITEST;
@ -98,8 +99,9 @@ public class testBrowserProvider extends ContentProviderTest {
mBookmarksUri = getContentUri("Bookmarks");
mHistoryUri = getContentUri("History");
mHistoryOldUri = getUriColumn("History", "CONTENT_OLD_URI");
mImagesUri = getContentUri("Images");
mCombinedUri = getContentUri("Combined");
mFaviconsUri = getContentUri("Favicons");
mThumbnailsUri = getContentUri("Thumbnails");
mBookmarksPositionUri = getUriColumn("Bookmarks", "POSITIONS_CONTENT_URI");
@ -115,7 +117,6 @@ public class testBrowserProvider extends ContentProviderTest {
mBookmarksTitleCol = getStringColumn("Bookmarks", "TITLE");
mBookmarksUrlCol = getStringColumn("Bookmarks", "URL");
mBookmarksFaviconCol = getStringColumn("Bookmarks", "FAVICON");
mBookmarksThumbnailCol = getStringColumn("Bookmarks", "THUMBNAIL");
mBookmarksParentCol = getStringColumn("Bookmarks", "PARENT");
mBookmarksTypeCol = getStringColumn("Bookmarks", "TYPE");
mBookmarksPositionCol = getStringColumn("Bookmarks", "POSITION");
@ -136,16 +137,11 @@ public class testBrowserProvider extends ContentProviderTest {
mHistoryVisitsCol = getStringColumn("History", "VISITS");
mHistoryLastVisitedCol = getStringColumn("History", "DATE_LAST_VISITED");
mHistoryFaviconCol = getStringColumn("History", "FAVICON");
mHistoryThumbnailCol = getStringColumn("History", "THUMBNAIL");
mHistoryGuidCol = getStringColumn("History", "GUID");
mHistoryIsDeletedCol = getStringColumn("History", "IS_DELETED");
mHistoryDateCreatedCol = getStringColumn("History", "DATE_CREATED");
mHistoryDateModifiedCol = getStringColumn("History", "DATE_MODIFIED");
mImagesUrlCol = getStringColumn("Images", "URL");
mImagesFaviconCol = getStringColumn("Images", "FAVICON");
mImagesThumbnailCol = getStringColumn("Images", "THUMBNAIL");
mCombinedIdCol = getStringColumn("Combined", "_ID");
mCombinedBookmarkIdCol = getStringColumn("Combined", "BOOKMARK_ID");
mCombinedHistoryIdCol = getStringColumn("Combined", "HISTORY_ID");
@ -155,10 +151,16 @@ public class testBrowserProvider extends ContentProviderTest {
mCombinedVisitsCol = getStringColumn("Combined", "VISITS");
mCombinedLastVisitedCol = getStringColumn("Combined", "DATE_LAST_VISITED");
mCombinedFaviconCol = getStringColumn("Combined", "FAVICON");
mCombinedThumbnailCol = getStringColumn("Combined", "THUMBNAIL");
mCombinedDisplayNormal = getIntColumn("Combined", "DISPLAY_NORMAL");
mCombinedDisplayReader = getIntColumn("Combined", "DISPLAY_READER");
mFaviconsUrlCol = getStringColumn("Favicons", "URL");
mFaviconsPageUrlCol = getStringColumn("Favicons", "PAGE_URL");
mFaviconsDataCol = getStringColumn("Favicons", "DATA");
mThumbnailsUrlCol = getStringColumn("Thumbnails", "URL");
mThumbnailsDataCol = getStringColumn("Thumbnails", "DATA");
}
private void loadMobileFolderId() throws Exception {
@ -196,9 +198,11 @@ public class testBrowserProvider extends ContentProviderTest {
c = mProvider.query(appendUriParam(mHistoryUri, "PARAM_SHOW_DELETED", "1"), null, null, null, null);
mAsserter.is(c.getCount(), 0, "All history entries were deleted");
mProvider.delete(appendUriParam(mImagesUri, "PARAM_IS_SYNC", "1"), null, null);
c = mProvider.query(appendUriParam(mImagesUri, "PARAM_SHOW_DELETED", "1"), null, null, null, null);
mAsserter.is(c.getCount(), 0, "All images were deleted");
mProvider.delete(mFaviconsUri, null, null);
mAsserter.is(c.getCount(), 0, "All favicons were deleted");
mProvider.delete(mThumbnailsUri, null, null);
mAsserter.is(c.getCount(), 0, "All thumbnails were deleted");
}
private ContentValues createBookmark(String title, String url, long parentId,
@ -267,6 +271,25 @@ public class testBrowserProvider extends ContentProviderTest {
return historyEntry;
}
private ContentValues createFaviconEntry(String pageUrl, String data) throws Exception {
ContentValues faviconEntry = new ContentValues();
faviconEntry.put(mFaviconsPageUrlCol, pageUrl);
faviconEntry.put(mFaviconsUrlCol, pageUrl + "/favicon.ico");
faviconEntry.put(mFaviconsDataCol, data.getBytes("UTF8"));
return faviconEntry;
}
private ContentValues createThumbnailEntry(String pageUrl, String data) throws Exception {
ContentValues thumbnailEntry = new ContentValues();
thumbnailEntry.put(mThumbnailsUrlCol, pageUrl);
thumbnailEntry.put(mThumbnailsDataCol, data.getBytes("UTF8"));
return thumbnailEntry;
}
private ContentValues createOneHistoryEntry() throws Exception {
return createHistoryEntry("Example", "http://example.com", 10, System.currentTimeMillis());
}
@ -290,9 +313,16 @@ public class testBrowserProvider extends ContentProviderTest {
null);
}
private Cursor getImagesByUrl(String url) throws Exception {
return mProvider.query(mImagesUri, null,
mImagesUrlCol + " = ?",
private Cursor getFaviconsByUrl(String url) throws Exception {
return mProvider.query(mCombinedUri, null,
mCombinedUrlCol + " = ?",
new String[] { url },
null);
}
private Cursor getThumbnailByUrl(String url) throws Exception {
return mProvider.query(mThumbnailsUri, null,
mThumbnailsUrlCol + " = ?",
new String[] { url },
null);
}
@ -304,20 +334,23 @@ public class testBrowserProvider extends ContentProviderTest {
mTests.add(new TestSpecialFolders());
mTests.add(new TestInsertBookmarks());
mTests.add(new TestInsertBookmarksImages());
mTests.add(new TestInsertBookmarksFavicons());
mTests.add(new TestDeleteBookmarks());
mTests.add(new TestDeleteBookmarksImages());
mTests.add(new TestDeleteBookmarksFavicons());
mTests.add(new TestUpdateBookmarks());
mTests.add(new TestUpdateBookmarksImages());
mTests.add(new TestUpdateBookmarksFavicons());
mTests.add(new TestPositionBookmarks());
mTests.add(new TestInsertHistory());
mTests.add(new TestInsertHistoryImages());
mTests.add(new TestInsertHistoryFavicons());
mTests.add(new TestDeleteHistory());
mTests.add(new TestDeleteHistoryImages());
mTests.add(new TestDeleteHistoryFavicons());
mTests.add(new TestUpdateHistory());
mTests.add(new TestUpdateHistoryImages());
mTests.add(new TestUpdateHistoryFavicons());
mTests.add(new TestUpdateOrInsertHistory());
mTests.add(new TestInsertHistoryThumbnails());
mTests.add(new TestUpdateHistoryThumbnails());
mTests.add(new TestDeleteHistoryThumbnails());
mTests.add(new TestBatchOperations());
@ -435,29 +468,25 @@ public class testBrowserProvider extends ContentProviderTest {
}
// Force a Constraint error, see if later operations still apply correctly
public void testApplyBatchErrors() {
public void testApplyBatchErrors() throws Exception {
ArrayList<ContentProviderOperation> mOperations
= new ArrayList<ContentProviderOperation>();
// Test a bunch of inserts with applyBatch
ContentValues values = new ContentValues();
ContentProviderOperation.Builder builder = null;
values.clear();
values.put(mImagesUrlCol, "http://www.test.org");
values.put(mImagesFaviconCol, "http://www.test.org/favicon.ico");
builder = ContentProviderOperation.newInsert(mImagesUri);
ContentValues values = createFaviconEntry("http://www.test.org", "FAVICON");
builder = ContentProviderOperation.newInsert(mFaviconsUri);
builder.withValues(values);
mOperations.add(builder.build());
// Make a duplicate, this will fail because of a UNIQUE constraint
builder = ContentProviderOperation.newInsert(mImagesUri);
builder = ContentProviderOperation.newInsert(mFaviconsUri);
builder.withValues(values);
mOperations.add(builder.build());
// This is valid and should be in the table afterwards
values.put(mImagesUrlCol, "http://www.test.org/valid");
builder = ContentProviderOperation.newInsert(mImagesUri);
values.put(mFaviconsUrlCol, "http://www.test.org/valid.ico");
builder = ContentProviderOperation.newInsert(mFaviconsUri);
builder.withValues(values);
mOperations.add(builder.build());
@ -471,13 +500,13 @@ public class testBrowserProvider extends ContentProviderTest {
}
// This test may need to go away if Bug 717428 is fixed.
mAsserter.is(seenException, true, "Expected failure in images table");
mAsserter.is(seenException, true, "Expected failure in favicons table");
boolean allFound = true;
Cursor cursor = mProvider.query(mImagesUri,
Cursor cursor = mProvider.query(mFaviconsUri,
null,
mImagesUrlCol + " = ?",
new String[] { "http://www.test.org/valid" },
mFaviconsUrlCol + " = ?",
new String[] { "http://www.test.org/valid.ico" },
null);
if (!cursor.moveToFirst())
@ -618,11 +647,11 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(new Long(id), new Long(-1),
"Should not be able to insert bookmark with null type");
b = createOneBookmark();
b.put(mBookmarksParentCol, -1);
id = -1;
if (Build.VERSION.SDK_INT >= 8) {
b = createOneBookmark();
b.put(mBookmarksParentCol, -1);
id = -1;
try {
id = ContentUris.parseId(mProvider.insert(mBookmarksUri, b));
} catch (Exception e) {}
@ -643,33 +672,30 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestInsertBookmarksImages extends Test {
class TestInsertBookmarksFavicons extends Test {
public void test() throws Exception {
ContentValues b = createOneBookmark();
final String favicon = "FAVICON";
final String thumbnail = "THUMBNAIL";
b.put(mBookmarksFaviconCol, favicon.getBytes("UTF8"));
b.put(mBookmarksThumbnailCol, thumbnail.getBytes("UTF8"));
final String pageUrl = b.getAsString(mBookmarksUrlCol);
long id = ContentUris.parseId(mProvider.insert(mBookmarksUri, b));
Cursor c = getBookmarkById(id, new String[] { mBookmarksFaviconCol, mBookmarksThumbnailCol });
// Insert the favicon into the favicons table
mProvider.insert(mFaviconsUri, createFaviconEntry(pageUrl, favicon));
Cursor c = getBookmarkById(id, new String[] { mBookmarksFaviconCol });
mAsserter.is(c.moveToFirst(), true, "Inserted bookmark found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mBookmarksFaviconCol)), "UTF8"),
favicon, "Inserted bookmark has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mBookmarksThumbnailCol)), "UTF8"),
thumbnail, "Inserted bookmark has corresponding thumbnail image");
c = getImagesByUrl(b.getAsString(mBookmarksUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
favicon, "Inserted image has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesThumbnailCol)), "UTF8"),
thumbnail, "Inserted image has corresponding thumbnail image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
}
}
@ -742,20 +768,23 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestDeleteBookmarksImages extends Test {
class TestDeleteBookmarksFavicons extends Test {
public void test() throws Exception {
ContentValues b = createOneBookmark();
b.put(mBookmarksFaviconCol, new String("FAVICON").getBytes("UTF8"));
final String pageUrl = b.getAsString(mBookmarksUrlCol);
long id = ContentUris.parseId(mProvider.insert(mBookmarksUri, b));
Cursor c = getImagesByUrl(b.getAsString(mBookmarksUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
// Insert the favicon into the favicons table
mProvider.insert(mFaviconsUri, createFaviconEntry(pageUrl, "FAVICON"));
Cursor c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mProvider.delete(ContentUris.withAppendedId(mBookmarksUri, id), null, null);
c = getImagesByUrl(b.getAsString(mBookmarksUrlCol));
mAsserter.is(c.moveToFirst(), false, "Image is deleted with last reference to it");
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
}
}
@ -847,32 +876,34 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestUpdateBookmarksImages extends Test {
class TestUpdateBookmarksFavicons extends Test {
public void test() throws Exception {
ContentValues b = createOneBookmark();
final String favicon = "FAVICON";
b.put(mBookmarksFaviconCol, favicon.getBytes("UTF8"));
long id = ContentUris.parseId(mProvider.insert(mBookmarksUri, b));
Cursor c = getImagesByUrl(b.getAsString(mBookmarksUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
favicon, "Inserted image has corresponding favicon image");
ContentValues u = new ContentValues();
final String newFavicon = "NEW_FAVICON";
u.put(mBookmarksFaviconCol, newFavicon.getBytes("UTF8"));
final String pageUrl = b.getAsString(mBookmarksUrlCol);
mProvider.update(ContentUris.withAppendedId(mBookmarksUri, id), u, null, null);
mProvider.insert(mBookmarksUri, b);
c = getImagesByUrl(b.getAsString(mBookmarksUrlCol));
mAsserter.is(c.moveToFirst(), true, "Updated images found");
// Insert the favicon into the favicons table
ContentValues f = createFaviconEntry(pageUrl, favicon);
long faviconId = ContentUris.parseId(mProvider.insert(mFaviconsUri, f));
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
newFavicon, "Updated image has corresponding favicon image");
Cursor c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
ContentValues u = createFaviconEntry(pageUrl, newFavicon);
mProvider.update(mFaviconsUri, u, null, null);
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
newFavicon, "Updated favicon has corresponding favicon image");
}
}
@ -1003,33 +1034,30 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestInsertHistoryImages extends Test {
class TestInsertHistoryFavicons extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
final String favicon = "FAVICON";
final String thumbnail = "THUMBNAIL";
h.put(mHistoryFaviconCol, favicon.getBytes("UTF8"));
h.put(mHistoryThumbnailCol, thumbnail.getBytes("UTF8"));
final String pageUrl = h.getAsString(mHistoryUrlCol);
long id = ContentUris.parseId(mProvider.insert(mHistoryUri, h));
Cursor c = getHistoryEntryById(id, new String[] { mHistoryFaviconCol, mHistoryThumbnailCol });
// Insert the favicon into the favicons table
mProvider.insert(mFaviconsUri, createFaviconEntry(pageUrl, favicon));
Cursor c = getHistoryEntryById(id, new String[] { mHistoryFaviconCol });
mAsserter.is(c.moveToFirst(), true, "Inserted history entry found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mHistoryFaviconCol)), "UTF8"),
favicon, "Inserted history entry has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mHistoryThumbnailCol)), "UTF8"),
thumbnail, "Inserted history entry has corresponding thumbnail image");
c = getImagesByUrl(h.getAsString(mHistoryUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
favicon, "Inserted image has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesThumbnailCol)), "UTF8"),
thumbnail, "Inserted image has corresponding thumbnail image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
}
}
@ -1077,20 +1105,23 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestDeleteHistoryImages extends Test {
class TestDeleteHistoryFavicons extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
h.put(mHistoryFaviconCol, new String("FAVICON").getBytes("UTF8"));
long id = ContentUris.parseId(mProvider.insert(mHistoryUri, h));
final String pageUrl = h.getAsString(mHistoryUrlCol);
Cursor c = getImagesByUrl(h.getAsString(mHistoryUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
// Insert the favicon into the favicons table
mProvider.insert(mFaviconsUri, createFaviconEntry(pageUrl, "FAVICON"));
Cursor c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mProvider.delete(ContentUris.withAppendedId(mHistoryUri, id), null, null);
c = getImagesByUrl(h.getAsString(mHistoryUrlCol));
mAsserter.is(c.moveToFirst(), false, "Image is deleted with last reference to it");
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Favicon is deleted with last reference to it");
}
}
@ -1173,32 +1204,34 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestUpdateHistoryImages extends Test {
class TestUpdateHistoryFavicons extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
final String favicon = "FAVICON";
h.put(mHistoryFaviconCol, favicon.getBytes("UTF8"));
long id = ContentUris.parseId(mProvider.insert(mHistoryUri, h));
Cursor c = getImagesByUrl(h.getAsString(mHistoryUrlCol));
mAsserter.is(c.moveToFirst(), true, "Inserted images found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
favicon, "Inserted image has corresponding favicon image");
ContentValues u = new ContentValues();
final String newFavicon = "NEW_FAVICON";
u.put(mHistoryFaviconCol, newFavicon.getBytes("UTF8"));
final String pageUrl = h.getAsString(mHistoryUrlCol);
mProvider.update(ContentUris.withAppendedId(mHistoryUri, id), u, null, null);
mProvider.insert(mHistoryUri, h);
c = getImagesByUrl(h.getAsString(mHistoryUrlCol));
mAsserter.is(c.moveToFirst(), true, "Updated images found");
// Insert the favicon into the favicons table
mProvider.insert(mFaviconsUri, createFaviconEntry(pageUrl, favicon));
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mImagesFaviconCol)), "UTF8"),
newFavicon, "Updated image has corresponding favicon image");
Cursor c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
favicon, "Inserted favicon has corresponding favicon image");
ContentValues u = createFaviconEntry(pageUrl, newFavicon);
mProvider.update(mFaviconsUri, u, null, null);
c = getFaviconsByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated favicon found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
newFavicon, "Updated favicon has corresponding favicon image");
}
}
@ -1317,6 +1350,77 @@ public class testBrowserProvider extends ContentProviderTest {
}
}
class TestInsertHistoryThumbnails extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
final String thumbnail = "THUMBNAIL";
final String pageUrl = h.getAsString(mHistoryUrlCol);
long id = ContentUris.parseId(mProvider.insert(mHistoryUri, h));
// Insert the thumbnail into the thumbnails table
mProvider.insert(mThumbnailsUri, createThumbnailEntry(pageUrl, thumbnail));
Cursor c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mThumbnailsDataCol)), "UTF8"),
thumbnail, "Inserted thumbnail has corresponding thumbnail image");
}
}
class TestUpdateHistoryThumbnails extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
final String thumbnail = "THUMBNAIL";
final String newThumbnail = "NEW_THUMBNAIL";
final String pageUrl = h.getAsString(mHistoryUrlCol);
mProvider.insert(mHistoryUri, h);
// Insert the thumbnail into the thumbnails table
mProvider.insert(mThumbnailsUri, createThumbnailEntry(pageUrl, thumbnail));
Cursor c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mThumbnailsDataCol)), "UTF8"),
thumbnail, "Inserted thumbnail has corresponding thumbnail image");
ContentValues u = createThumbnailEntry(pageUrl, newThumbnail);
mProvider.update(mThumbnailsUri, u, null, null);
c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Updated thumbnail found");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mThumbnailsDataCol)), "UTF8"),
newThumbnail, "Updated thumbnail has corresponding thumbnail image");
}
}
class TestDeleteHistoryThumbnails extends Test {
public void test() throws Exception {
ContentValues h = createOneHistoryEntry();
long id = ContentUris.parseId(mProvider.insert(mHistoryUri, h));
final String pageUrl = h.getAsString(mHistoryUrlCol);
// Insert the thumbnail into the thumbnails table
mProvider.insert(mThumbnailsUri, createThumbnailEntry(pageUrl, "THUMBNAIL"));
Cursor c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), true, "Inserted thumbnail found");
mProvider.delete(ContentUris.withAppendedId(mHistoryUri, id), null, null);
c = getThumbnailByUrl(pageUrl);
mAsserter.is(c.moveToFirst(), false, "Thumbnail is deleted with last reference to it");
}
}
class TestCombinedView extends Test {
public void test() throws Exception {
final String TITLE_1 = "Test Page 1";
@ -1329,45 +1433,30 @@ public class testBrowserProvider extends ContentProviderTest {
final String URL_2 = "http://example2.com";
final String URL_3 = "http://example3.com";
final String HISTORY_FAVICON = "HISTORY_FAVICON";
final String HISTORY_THUMBNAIL = "HISTORY_THUMBNAIL";
final String BOOKMARK_FAVICON = "BOOKMARK_FAVICON";
final String BOOKMARK_THUMBNAIL = "BOOKMARK_THUMBNAIL";
final int VISITS = 10;
final long LAST_VISITED = System.currentTimeMillis();
// Create a basic history entry with images
// Create a basic history entry
ContentValues basicHistory = createHistoryEntry(TITLE_1, URL_1, VISITS, LAST_VISITED);
basicHistory.put(mHistoryFaviconCol, HISTORY_FAVICON.getBytes("UTF8"));
basicHistory.put(mHistoryThumbnailCol, HISTORY_THUMBNAIL.getBytes("UTF8"));
long basicHistoryId = ContentUris.parseId(mProvider.insert(mHistoryUri, basicHistory));
// Create a basic bookmark entry with images
// Create a basic bookmark entry
ContentValues basicBookmark = createBookmark(TITLE_2, URL_2, mMobileFolderId,
mBookmarksTypeBookmark, 0, "tags", "description", "keyword");
basicBookmark.put(mBookmarksFaviconCol, BOOKMARK_FAVICON.getBytes("UTF8"));
basicBookmark.put(mBookmarksThumbnailCol, BOOKMARK_THUMBNAIL.getBytes("UTF8"));
long basicBookmarkId = ContentUris.parseId(mProvider.insert(mBookmarksUri, basicBookmark));
// Create a history entry and bookmark entry with the same URL to
// represent a visited bookmark
ContentValues combinedHistory = createHistoryEntry(TITLE_3_HISTORY, URL_3, VISITS, LAST_VISITED);
combinedHistory.put(mHistoryFaviconCol, HISTORY_FAVICON.getBytes("UTF8"));
combinedHistory.put(mHistoryThumbnailCol, HISTORY_THUMBNAIL.getBytes("UTF8"));
long combinedHistoryId = ContentUris.parseId(mProvider.insert(mHistoryUri, combinedHistory));
ContentValues combinedBookmark = createBookmark(TITLE_3_BOOKMARK, URL_3, mMobileFolderId,
mBookmarksTypeBookmark, 0, "tags", "description", "keyword");
combinedBookmark.put(mBookmarksFaviconCol, BOOKMARK_FAVICON.getBytes("UTF8"));
combinedBookmark.put(mBookmarksThumbnailCol, BOOKMARK_THUMBNAIL.getBytes("UTF8"));
long combinedBookmarkId = ContentUris.parseId(mProvider.insert(mBookmarksUri, combinedBookmark));
ContentValues combinedBookmark2 = createBookmark(TITLE_3_BOOKMARK2, URL_3, mMobileFolderId,
mBookmarksTypeBookmark, 0, "tags", "description", "keyword");
combinedBookmark2.put(mBookmarksFaviconCol, BOOKMARK_FAVICON.getBytes("UTF8"));
combinedBookmark2.put(mBookmarksThumbnailCol, BOOKMARK_THUMBNAIL.getBytes("UTF8"));
long combinedBookmarkId2 = ContentUris.parseId(mProvider.insert(mBookmarksUri, combinedBookmark2));
// Create a bookmark folder to make sure it _doesn't_ show up in the results
@ -1397,10 +1486,6 @@ public class testBrowserProvider extends ContentProviderTest {
"Basic history entry has correct number of visits");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(mCombinedLastVisitedCol))), new Long(LAST_VISITED),
"Basic history entry has correct last visit time");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
HISTORY_FAVICON, "Basic history entry has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedThumbnailCol)), "UTF8"),
HISTORY_THUMBNAIL, "Basic history entry has corresponding thumbnail image");
// Second combined entry is basic bookmark entry
mAsserter.is(c.moveToNext(), true, "Found basic bookmark entry");
@ -1417,11 +1502,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getInt(c.getColumnIndex(mCombinedVisitsCol)), -1,
"Visits should be -1 for basic bookmark entry");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(mCombinedLastVisitedCol))), new Long(-1),
"Last visited should be -1 for basic bookmark entry");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
BOOKMARK_FAVICON, "Basic bookmark entry has corresponding favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedThumbnailCol)), "UTF8"),
BOOKMARK_THUMBNAIL, "Basic bookmark entry has corresponding thumbnail image");
"Basic entry has correct last visit time");
// Third combined entry is a combined history/bookmark entry
mAsserter.is(c.moveToNext(), true, "Found third combined entry");
@ -1443,10 +1524,6 @@ public class testBrowserProvider extends ContentProviderTest {
"Combined entry has correct number of visits");
mAsserter.is(new Long(c.getLong(c.getColumnIndex(mCombinedLastVisitedCol))), new Long(LAST_VISITED),
"Combined entry has correct last visit time");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedFaviconCol)), "UTF8"),
BOOKMARK_FAVICON, "Combined entry has bookmark favicon image");
mAsserter.is(new String(c.getBlob(c.getColumnIndex(mCombinedThumbnailCol)), "UTF8"),
BOOKMARK_THUMBNAIL, "Combined entry has bookmark thumbnail image");
}
}
@ -1623,15 +1700,14 @@ public class testBrowserProvider extends ContentProviderTest {
allVals = new ContentValues[count];
for (int i = 0; i < count; i++) {
allVals[i] = new ContentValues();
allVals[i].put(mImagesThumbnailCol, i);
allVals[i].put(mImagesUrlCol, "http://www.test.org/" + i);
allVals[i].put(mImagesFaviconCol, i);
allVals[i].put(mThumbnailsDataCol, i);
allVals[i].put(mThumbnailsUrlCol, "http://www.test.org/" + i);
}
inserts = mProvider.bulkInsert(mImagesUri, allVals);
inserts = mProvider.bulkInsert(mThumbnailsUri, allVals);
mAsserter.is(inserts, count, "Expected number of inserts matches");
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
c = mProvider.query(mThumbnailsUri, null, null, null, null);
mAsserter.is(c.getCount(), count, count + " thumbnails entries found");
}
@ -1649,7 +1725,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getCount(), count, count + " history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
c = mProvider.query(mThumbnailsUri, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
@ -1663,7 +1739,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getCount(), 500, "500 history entries found");
// expiring with a aggressive priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
c = mProvider.query(mThumbnailsUri, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
@ -1679,7 +1755,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getCount(), 2000, "2000 history entries found");
// expiring with a normal priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
c = mProvider.query(mThumbnailsUri, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
ensureEmptyDatabase();
@ -1695,7 +1771,7 @@ public class testBrowserProvider extends ContentProviderTest {
mAsserter.is(c.getCount(), 500, "500 history entries found");
// expiring with a aggressive priority should delete all but 10 thumbnails
c = mProvider.query(mImagesUri, null, mImagesThumbnailCol + " IS NOT NULL", null, null);
c = mProvider.query(mThumbnailsUri, null, null, null, null);
mAsserter.is(c.getCount(), thumbCount, thumbCount + " thumbnails found");
}
}

View File

@ -38,6 +38,7 @@ public class testBrowserProviderPerf extends ContentProviderTest {
private Uri mBookmarksUri;
private Uri mHistoryUri;
private Uri mFaviconsUri;
private String mBookmarksIdCol;
private String mBookmarksTitleCol;
@ -54,10 +55,12 @@ public class testBrowserProviderPerf extends ContentProviderTest {
private String mHistoryTitleCol;
private String mHistoryUrlCol;
private String mHistoryVisitsCol;
private String mHistoryFaviconCol;
private String mHistoryThumbnailCol;
private String mHistoryLastVisitedCol;
private String mFaviconsUrlCol;
private String mFaviconsPageUrlCol;
private String mFaviconsDataCol;
@Override
protected int getTestType() {
return TEST_TALOS;
@ -82,6 +85,7 @@ public class testBrowserProviderPerf extends ContentProviderTest {
private void loadContractInfo() throws Exception {
mBookmarksUri = getContentUri("Bookmarks");
mHistoryUri = getContentUri("History");
mFaviconsUri = getContentUri("Favicons");
mBookmarksIdCol = getStringColumn("Bookmarks", "_ID");
mBookmarksTitleCol = getStringColumn("Bookmarks", "TITLE");
@ -99,7 +103,10 @@ public class testBrowserProviderPerf extends ContentProviderTest {
mHistoryUrlCol = getStringColumn("History", "URL");
mHistoryVisitsCol = getStringColumn("History", "VISITS");
mHistoryLastVisitedCol = getStringColumn("History", "DATE_LAST_VISITED");
mHistoryFaviconCol = getStringColumn("History", "FAVICON");
mFaviconsUrlCol = getStringColumn("Favicons", "URL");
mFaviconsPageUrlCol = getStringColumn("Favicons", "PAGE_URL");
mFaviconsDataCol = getStringColumn("Favicons", "DATA");
}
private void loadMobileFolderId() throws Exception {
@ -144,14 +151,13 @@ public class testBrowserProviderPerf extends ContentProviderTest {
}
private ContentValues createHistoryEntry(String title, String url, int visits,
long lastVisited, byte[] favicon) throws Exception {
long lastVisited) throws Exception {
ContentValues historyEntry = new ContentValues();
historyEntry.put(mHistoryTitleCol, title);
historyEntry.put(mHistoryUrlCol, url);
historyEntry.put(mHistoryVisitsCol, visits);
historyEntry.put(mHistoryLastVisitedCol, lastVisited);
historyEntry.put(mHistoryFaviconCol, favicon);
return historyEntry;
}
@ -159,7 +165,7 @@ public class testBrowserProviderPerf extends ContentProviderTest {
private ContentValues createHistoryEntryWithUrl(String url) throws Exception {
int visits = mGenerator.nextInt(500);
return createHistoryEntry(url, url, visits,
System.currentTimeMillis(), url.getBytes("UTF8"));
System.currentTimeMillis());
}
private ContentValues createRandomHistoryEntry() throws Exception {
@ -171,6 +177,16 @@ public class testBrowserProviderPerf extends ContentProviderTest {
return createHistoryEntryWithUrl(randomStr);
}
private ContentValues createFaviconEntryWithUrl(String url) throws Exception {
ContentValues faviconEntry = new ContentValues();
faviconEntry.put(mFaviconsUrlCol, url + "/favicon.ico");
faviconEntry.put(mFaviconsPageUrlCol, url);
faviconEntry.put(mFaviconsDataCol, url.getBytes("UTF8"));
return faviconEntry;
}
private String createRandomUrl(String knownPrefix) throws Exception {
return knownPrefix + UUID.randomUUID().toString();
}
@ -191,15 +207,19 @@ public class testBrowserProviderPerf extends ContentProviderTest {
// Create some random history entries
ContentValues[] historyEntries = new ContentValues[BATCH_SIZE];
ContentValues[] faviconEntries = new ContentValues[BATCH_SIZE];
for (int i = 0; i < NUMBER_OF_BASIC_HISTORY_URLS / BATCH_SIZE; i++) {
historyEntries = new ContentValues[BATCH_SIZE];
faviconEntries = new ContentValues[BATCH_SIZE];
for (int j = 0; j < BATCH_SIZE; j++) {
historyEntries[j] = createRandomHistoryEntry();
faviconEntries[j] = createFaviconEntryWithUrl(historyEntries[j].getAsString(mHistoryUrlCol));
}
mProvider.bulkInsert(mHistoryUri, historyEntries);
mProvider.bulkInsert(mFaviconsUri, faviconEntries);
}
@ -212,19 +232,24 @@ public class testBrowserProviderPerf extends ContentProviderTest {
String url = createRandomUrl("");
bookmarkEntries[j] = createBookmarkEntryWithUrl(url);
historyEntries[j] = createHistoryEntryWithUrl(url);
faviconEntries[j] = createFaviconEntryWithUrl(url);
}
mProvider.bulkInsert(mBookmarksUri, bookmarkEntries);
mProvider.bulkInsert(mHistoryUri, historyEntries);
mProvider.bulkInsert(mFaviconsUri, faviconEntries);
}
// Create some history entries with a known prefix
historyEntries = new ContentValues[NUMBER_OF_KNOWN_URLS];
faviconEntries = new ContentValues[NUMBER_OF_KNOWN_URLS];
for (int i = 0; i < NUMBER_OF_KNOWN_URLS; i++) {
historyEntries[i] = createRandomHistoryEntry(KNOWN_PREFIX);
faviconEntries[i] = createFaviconEntryWithUrl(historyEntries[i].getAsString(mHistoryUrlCol));
}
mProvider.bulkInsert(mHistoryUri, historyEntries);
mProvider.bulkInsert(mFaviconsUri, faviconEntries);
}
public void setUp() throws Exception {