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:
parent
f3d07fb3e6
commit
6353774647
|
@ -1691,12 +1691,14 @@ public class EmailProvider extends ContentProvider {
|
||||||
long accountId = Account.NO_ACCOUNT;
|
long accountId = Account.NO_ACCOUNT;
|
||||||
// Find the account with "isDefault" set
|
// Find the account with "isDefault" set
|
||||||
Collection<Cursor> accounts = accountCache.values();
|
Collection<Cursor> accounts = accountCache.values();
|
||||||
int numAccounts = accounts.size();
|
|
||||||
for (Cursor accountCursor: accounts) {
|
for (Cursor accountCursor: accounts) {
|
||||||
if (accountCursor.getInt(Account.CONTENT_IS_DEFAULT_COLUMN) == 1 ||
|
boolean isDefault =
|
||||||
numAccounts == 1) {
|
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);
|
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
|
// Return a cursor with an id projection
|
||||||
|
|
|
@ -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
|
* Tests of default account behavior
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue