From 818214e31f56f2d6d8db70cad8a2fc044b455639 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 24 Aug 2010 17:46:25 -0700 Subject: [PATCH] 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 --- src/com/android/email/AccountBackupRestore.java | 12 ++++++++---- .../android/email/AccountBackupRestoreTests.java | 14 ++++++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/com/android/email/AccountBackupRestore.java b/src/com/android/email/AccountBackupRestore.java index 58c097b1f..6fa67efef 100644 --- a/src/com/android/email/AccountBackupRestore.java +++ b/src/com/android/email/AccountBackupRestore.java @@ -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; diff --git a/tests/src/com/android/email/AccountBackupRestoreTests.java b/tests/src/com/android/email/AccountBackupRestoreTests.java index 150dbaaeb..77cdd3e90 100644 --- a/tests/src/com/android/email/AccountBackupRestoreTests.java +++ b/tests/src/com/android/email/AccountBackupRestoreTests.java @@ -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 { @@ -164,7 +167,8 @@ public class AccountBackupRestoreTests extends ProviderTestCase2 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 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 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 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