Fix failing account backup/restore unit tests

* We can't run the AccountManager functionality of account backup
  and restore in the unit tests, because IsolatedContext doesn't
  mock the AccountManager; this leads to various NPE's when the
  test is run
* These problems started, by the way, when we added POP/IMAP
  account integration with AccountManager
* Since the AccountManager side of account backup/restore isn't
  tested, we'll skip that part of the process when running unit
  tests

Bug: 2873546

Change-Id: I94673913e66722ac70f3c49c51465122e98bf3d9
This commit is contained in:
Marc Blank 2010-08-24 17:46:25 -07:00
parent 3299d4a0d9
commit 818214e31f
2 changed files with 18 additions and 8 deletions

View File

@ -58,7 +58,7 @@ public class AccountBackupRestore {
*/
public static void restoreAccountsIfNeeded(final Context context) {
// Don't log here; This is called often.
boolean restored = doRestoreAccounts(context, Preferences.getPreferences(context));
boolean restored = doRestoreAccounts(context, Preferences.getPreferences(context), false);
if (restored) {
// after restoring accounts, register services appropriately
Log.w(Email.LOG_TAG, "Register services after restoring accounts");
@ -151,7 +151,7 @@ public class AccountBackupRestore {
* @return true if accounts were restored (meaning services should be restarted, etc.)
*/
/* package */ synchronized static boolean doRestoreAccounts(Context context,
Preferences preferences) {
Preferences preferences, boolean unitTest) {
boolean result = false;
// 1. Quick check - if we have any accounts, get out
@ -194,8 +194,12 @@ public class AccountBackupRestore {
}
toAccount.save(context);
MailService.setupAccountManagerAccount(context, toAccount, email, calendar, contacts,
null);
// Don't simulate AccountManager in unit tests; this results in an NPE
// The unit tests only check EmailProvider based functionality
if (!unitTest) {
MailService.setupAccountManagerAccount(context, toAccount, email, calendar,
contacts, null);
}
result = true;
}
return result;

View File

@ -31,6 +31,9 @@ import android.test.suitebuilder.annotation.MediumTest;
* Technically these are functional because they use the underlying preferences framework.
*
* NOTE: These tests are destructive of any "legacy" accounts that might be lying around.
*
* You can run this entire test case with:
* runtest -c com.android.email.AccountBackupRestoreTests email
*/
@MediumTest
public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider> {
@ -164,7 +167,8 @@ public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider>
ProviderTestUtils.setupAccount("existing", true, mMockContext);
// run the restore
boolean anyRestored = AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences);
boolean anyRestored =
AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences, true);
assertFalse(anyRestored);
// make sure accounts still there
@ -186,7 +190,8 @@ public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider>
assertEquals(0, numAccounts);
// run the restore
boolean anyRestored = AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences);
boolean anyRestored =
AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences, true);
assertFalse(anyRestored);
// make sure accounts still there
@ -211,7 +216,8 @@ public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider>
backupAccount2.save(mPreferences);
// run the restore
boolean anyRestored = AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences);
boolean anyRestored =
AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences, true);
assertTrue(anyRestored);
// Check the restored accounts
@ -248,7 +254,7 @@ public class AccountBackupRestoreTests extends ProviderTestCase2<EmailProvider>
backupAccount4.save(mPreferences);
// run the restore
AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences);
AccountBackupRestore.doRestoreAccounts(mMockContext, mPreferences, true);
// Check the restored accounts
// Deep inspection is not performed here - see LegacyConversionsTests for that