diff --git a/src/com/android/email/Controller.java b/src/com/android/email/Controller.java index 015b4c3ff..4b89ee43c 100644 --- a/src/com/android/email/Controller.java +++ b/src/com/android/email/Controller.java @@ -28,7 +28,6 @@ import android.os.Bundle; import android.os.IBinder; import android.os.RemoteCallbackList; import android.os.RemoteException; -import android.test.IsolatedContext; import android.util.Log; import com.android.email.mail.store.Pop3Store.Pop3Message; @@ -117,6 +116,8 @@ public class Controller { private static RemoteCallbackList sCallbackList = new RemoteCallbackList(); + private volatile boolean mInUnitTests = false; + protected Controller(Context _context) { mContext = _context.getApplicationContext(); mProviderContext = _context; @@ -124,6 +125,15 @@ public class Controller { mLegacyController.addListener(mLegacyListener); } + /** + * Mark this controller as being in use in a unit test. + * This is a kludge vs having proper mocks and dependency injection; since the Controller is a + * global singleton there isn't much else we can do. + */ + public void markForUnitTest(boolean inUnitTests) { + mInUnitTests = inUnitTests; + } + /** * Cleanup for test. Mustn't be called for the regular {@link Controller}, as it's a * singleton and lives till the process finishes. @@ -472,7 +482,7 @@ public class Controller { } /** - * Look for a specific mailbox, creating it if necessary, and return the mailbox id. + * Look for a specific system mailbox, creating it if necessary, and return the mailbox id. * This is a blocking operation and should not be called from the UI thread. * * Synchronized so multiple threads can call it (and not risk creating duplicate boxes). @@ -1148,8 +1158,10 @@ public class Controller { Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId); context.getContentResolver().delete(uri, null, null); - // For unit tests, don't run backup, security, and ui pieces - if (context instanceof IsolatedContext) return; + // For unit tests, don't run backup, security, and ui pieces. + if (mInUnitTests) { + return; + } // Clean up AccountBackupRestore.backup(context); diff --git a/tests/src/com/android/email/ControllerProviderOpsTests.java b/tests/src/com/android/email/ControllerProviderOpsTests.java index fa974e30e..a0beb27c1 100644 --- a/tests/src/com/android/email/ControllerProviderOpsTests.java +++ b/tests/src/com/android/email/ControllerProviderOpsTests.java @@ -136,15 +136,6 @@ public class ControllerProviderOpsTests extends ProviderTestCase2 assertNotNull(testMailbox); assertEquals(8, testMailbox.mFlags); // Flags should be "holds mail" assertEquals(-1L, testMailbox.mParentKey); // Parent is off the top-level - - // Test creating user mailbox; some fields have changed - mTestController.createMailbox(1L, Mailbox.TYPE_MAIL); - testMailboxId = Mailbox.findMailboxOfType(mProviderContext, 1L, Mailbox.TYPE_MAIL); - assertTrue(testMailboxId != Mailbox.NO_MAILBOX); - testMailbox = Mailbox.restoreMailboxWithId(mProviderContext, testMailboxId); - assertNotNull(testMailbox); - assertEquals(0, testMailbox.mFlags); // Flags are not set - assertEquals(0L, testMailbox.mParentKey); // Parent is not set } /** diff --git a/tests/src/com/android/email/SecurityPolicyTests.java b/tests/src/com/android/email/SecurityPolicyTests.java index ae1157563..7e1756415 100644 --- a/tests/src/com/android/email/SecurityPolicyTests.java +++ b/tests/src/com/android/email/SecurityPolicyTests.java @@ -58,6 +58,7 @@ public class SecurityPolicyTests extends ProviderTestCase2 { mMockContext = new MockContext2(getMockContext(), mContext); // Invalidate all caches, since we reset the database for each test ContentCache.invalidateAllCaches(); + Controller.getInstance(mMockContext).markForUnitTest(true); } /** @@ -65,6 +66,7 @@ public class SecurityPolicyTests extends ProviderTestCase2 { */ @Override protected void tearDown() throws Exception { + Controller.getInstance(mMockContext).markForUnitTest(false); super.tearDown(); } @@ -398,10 +400,10 @@ public class SecurityPolicyTests extends ProviderTestCase2 { * Lightweight subclass of the Controller class allows injection of mock context */ public static class TestController extends Controller { - protected TestController(Context providerContext, Context systemContext) { super(systemContext); setProviderContext(providerContext); + markForUnitTest(true); } }