Fix problem with getDefaultAccountId() with no explicit default

* This wasn't working with the new code and the unit test wasn't
  testing this case.
* Updated code in EmailProvider and added a test case

Change-Id: I75c23423c4f43e4201d446886d92a5312fa8a084
This commit is contained in:
Marc Blank 2011-06-22 15:45:17 -07:00
parent f3d07fb3e6
commit 6353774647
2 changed files with 24 additions and 4 deletions

View File

@ -1691,12 +1691,14 @@ public class EmailProvider extends ContentProvider {
long accountId = Account.NO_ACCOUNT;
// Find the account with "isDefault" set
Collection<Cursor> accounts = accountCache.values();
int numAccounts = accounts.size();
for (Cursor accountCursor: accounts) {
if (accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1 ||
numAccounts == 1) {
boolean isDefault =
accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1;
// We'll remember this one if it's the default or the first one we see
if (isDefault || accountId == Account.NO_ACCOUNT) {
accountId = accountCursor.getLong(Account.CONTENT_ID_COLUMN);
break;
// If it's the default, we're done
if (isDefault) break;
}
}
// Return a cursor with an id projection

View File

@ -1444,6 +1444,24 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
}
}
public void testGetDefaultAccountNoneExplicitlySet() {
Account account1 = ProviderTestUtils.setupAccount("account-default-1", false, mMockContext);
account1.mIsDefault = false;
account1.save(mMockContext);
// We should find account1 as default
long defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertEquals(defaultAccountId, account1.mId);
Account account2 = ProviderTestUtils.setupAccount("account-default-1", false, mMockContext);
account2.mIsDefault = false;
account2.save(mMockContext);
// We should find one of the two as default
defaultAccountId = Account.getDefaultAccountId(mMockContext);
assertTrue(defaultAccountId == account1.mId || defaultAccountId == account2.mId);
}
/**
* Tests of default account behavior
*