Follow-up to Id18fb940. Tests, fix transaction.

Change-Id: If425db7d729e260102a9db82b98e14ff6f6be143
This commit is contained in:
Makoto Onuki 2010-12-02 16:33:49 -08:00
parent a2593be72e
commit 9d5aaeacd6
2 changed files with 98 additions and 20 deletions

View File

@ -1402,30 +1402,33 @@ public class EmailProvider extends ContentProvider {
if (field == null || add == null) {
throw new IllegalArgumentException("No field/add specified " + uri);
}
ContentValues actualValues = new ContentValues();
if (cache != null) {
cache.lock(id);
}
db.beginTransaction();
ContentValues actualValues = new ContentValues();
try {
Cursor c = db.query(tableName,
new String[] {EmailContent.RECORD_ID, field},
whereWithId(id, selection),
selectionArgs, null, null, null);
db.beginTransaction();
try {
result = 0;
String[] bind = new String[1];
if (c.moveToNext()) {
bind[0] = c.getString(0); // _id
long value = c.getLong(1) + add;
actualValues.put(field, value);
result = db.update(tableName, actualValues, ID_EQUALS, bind);
Cursor c = db.query(tableName,
new String[] {EmailContent.RECORD_ID, field},
whereWithId(id, selection),
selectionArgs, null, null, null);
try {
result = 0;
String[] bind = new String[1];
if (c.moveToNext()) {
bind[0] = c.getString(0); // _id
long value = c.getLong(1) + add;
actualValues.put(field, value);
result = db.update(tableName, actualValues, ID_EQUALS, bind);
}
db.setTransactionSuccessful();
} finally {
c.close();
}
} finally {
c.close();
db.endTransaction();
}
db.setTransactionSuccessful();
db.endTransaction();
} finally {
if (cache != null) {
cache.unlock(id, actualValues);
@ -1513,8 +1516,6 @@ public class EmailProvider extends ContentProvider {
result = db.update(tableName, CONTENT_VALUES_RESET_NEW_MESSAGE_COUNT,
selection, selectionArgs);
// Affects all accounts. Just invalidate all account cache.
// This operation shouldn't be used anyway (at least not on the XL UI),
// because we don't do this for ALL accounts at once.
cache.invalidate("Reset all new counts", null, null);
notificationUri = Account.CONTENT_URI; // Only notify account cursors.
break;

View File

@ -62,8 +62,8 @@ import java.util.ArrayList;
*/
public class ProviderTests extends ProviderTestCase2<EmailProvider> {
EmailProvider mProvider;
Context mMockContext;
private EmailProvider mProvider;
private Context mMockContext;
public ProviderTests() {
super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
@ -73,6 +73,7 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
public void setUp() throws Exception {
super.setUp();
mMockContext = getMockContext();
mProvider = getProvider();
// Invalidate all caches, since we reset the database for each test
ContentCache.invalidateAllCachesForTest();
}
@ -2261,4 +2262,80 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
// No such account
assertEquals(null, Message.getLatestIncomingMessage(c, 9999999L));
}
/**
* Check if update on ACCOUNT_ID_ADD_TO_FIELD updates the cache properly.
*/
public void testUpdateCacheAccountIdAddToField() {
final Context c = mMockContext;
Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
int start = Account.restoreAccountWithId(c, a1.mId).mNewMessageCount;
// +1 to NEW_MESSAGE_COUNT
ContentValues cv = new ContentValues();
cv.put(EmailContent.FIELD_COLUMN_NAME, AccountColumns.NEW_MESSAGE_COUNT);
cv.put(EmailContent.ADD_COLUMN_NAME, 1);
mProvider.update(ContentUris.withAppendedId(Account.ADD_TO_FIELD_URI, a1.mId), cv,
null, null);
// Check
assertEquals(start + 1, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
}
/**
* Check if update on ACCOUNT_RESET_NEW_COUNT updates the cache properly.
*/
public void testUpdateCacheAccountResetNewCount() {
final Context c = mMockContext;
Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
// precondition
assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0);
// Reset
mProvider.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, null, null);
// Check
assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
}
/**
* Check if update on ACCOUNT_RESET_NEW_COUNT_ID updates the cache properly.
*/
public void testUpdateCacheAccountResetNewCountId() {
final Context c = mMockContext;
Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
// precondition
assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0);
// Reset
mProvider.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a1.mId),
null, null, null);
// Check
assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount);
}
/**
* Check if update on MAILBOX_ID_ADD_TO_FIELD updates the cache properly.
*/
public void testUpdateCacheMailboxIdAddToField() {
final Context c = mMockContext;
Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_INBOX);
int start = Mailbox.restoreMailboxWithId(c, b1.mId).mSyncInterval;
// +1 to SYNC_INTERVAL
ContentValues cv = new ContentValues();
cv.put(EmailContent.FIELD_COLUMN_NAME, MailboxColumns.SYNC_INTERVAL);
cv.put(EmailContent.ADD_COLUMN_NAME, 1);
mProvider.update(ContentUris.withAppendedId(Mailbox.ADD_TO_FIELD_URI, a1.mId), cv,
null, null);
// Check
assertEquals(start + 1, Mailbox.restoreMailboxWithId(c, b1.mId).mSyncInterval);
}
}