From 32bed4bb8e23d7322ab338773d135845f392d3cf Mon Sep 17 00:00:00 2001 From: Ben Komalo Date: Tue, 23 Aug 2011 18:02:11 -0700 Subject: [PATCH] Fix unit tests. - make attachment download service injectable - fix assertions for account manager account checks - update message compose ID's so they're consistent on tablet/phone Bug: 5198343 Change-Id: I9976f5b9e5590dd61fb0a62937d3f9203fefe236 --- res/layout-v14/recipients_area.xml | 8 +-- res/layout/recipients_area.xml | 6 +- .../email/activity/MessageCompose.java | 6 +- .../android/email/provider/EmailProvider.java | 30 +++++++- .../service/AttachmentDownloadService.java | 1 + .../email/activity/MessageComposeTests.java | 7 +- .../android/email/provider/ProviderTests.java | 68 +++++++++++++++++-- 7 files changed, 102 insertions(+), 24 deletions(-) diff --git a/res/layout-v14/recipients_area.xml b/res/layout-v14/recipients_area.xml index d05aa281d..e10934d0f 100644 --- a/res/layout-v14/recipients_area.xml +++ b/res/layout-v14/recipients_area.xml @@ -33,7 +33,7 @@ automatically saved by framework.--> + android:id="@+id/to" /> + android:id="@+id/cc" /> @@ -72,10 +72,10 @@ automatically saved by framework.--> + android:id="@+id/bcc" /> - \ No newline at end of file + diff --git a/res/layout/recipients_area.xml b/res/layout/recipients_area.xml index aae1e3dfc..d765c68cb 100644 --- a/res/layout/recipients_area.xml +++ b/res/layout/recipients_area.xml @@ -38,7 +38,7 @@ automatically saved by framework.--> android:imeOptions="actionNext" android:layout_weight="1" android:layout_toRightOf="@id/label" - android:id="@+id/to_address_field" /> + android:id="@+id/to" /> android:imeOptions="actionNext" android:layout_weight="1" android:layout_toRightOf="@id/label" - android:id="@+id/cc_address_field" /> + android:id="@+id/cc" /> @@ -83,7 +83,7 @@ automatically saved by framework.--> android:imeOptions="actionNext" android:layout_weight="1" android:layout_toRightOf="@id/label" - android:id="@+id/bcc_address_field" /> + android:id="@+id/bcc" /> diff --git a/src/com/android/email/activity/MessageCompose.java b/src/com/android/email/activity/MessageCompose.java index 22ff077d3..8ee574f44 100644 --- a/src/com/android/email/activity/MessageCompose.java +++ b/src/com/android/email/activity/MessageCompose.java @@ -662,16 +662,16 @@ public class MessageCompose extends Activity implements OnClickListener, OnFocus private void initViews() { ViewGroup toParent = UiUtilities.getViewOrNull(this, R.id.to_content); if (toParent != null) { - mToView = (MultiAutoCompleteTextView) toParent.findViewById(R.id.to_address_field); + mToView = (MultiAutoCompleteTextView) toParent.findViewById(R.id.to); ((TextView) toParent.findViewById(R.id.label)) .setText(R.string.message_compose_to_hint); ViewGroup ccParent, bccParent; ccParent = (ViewGroup) findViewById(R.id.cc_content); - mCcView = (MultiAutoCompleteTextView) ccParent.findViewById(R.id.cc_address_field); + mCcView = (MultiAutoCompleteTextView) ccParent.findViewById(R.id.cc); ((TextView) ccParent.findViewById(R.id.label)) .setText(R.string.message_compose_cc_hint); bccParent = (ViewGroup) findViewById(R.id.bcc_content); - mBccView = (MultiAutoCompleteTextView) bccParent.findViewById(R.id.bcc_address_field); + mBccView = (MultiAutoCompleteTextView) bccParent.findViewById(R.id.bcc); ((TextView) bccParent.findViewById(R.id.label)) .setText(R.string.message_compose_bcc_hint); } else { diff --git a/src/com/android/email/provider/EmailProvider.java b/src/com/android/email/provider/EmailProvider.java index 13057bb06..7cb7a46ad 100644 --- a/src/com/android/email/provider/EmailProvider.java +++ b/src/com/android/email/provider/EmailProvider.java @@ -1646,7 +1646,7 @@ public class EmailProvider extends ContentProvider { flags = values.getAsInteger(Attachment.FLAGS); } // Report all new attachments to the download service - AttachmentDownloadService.attachmentChanged(getContext(), longId, flags); + mAttachmentService.attachmentChanged(getContext(), longId, flags); } break; case MAILBOX_ID: @@ -2199,7 +2199,7 @@ outer: if (match == ATTACHMENT_ID) { if (values.containsKey(Attachment.FLAGS)) { int flags = values.getAsInteger(Attachment.FLAGS); - AttachmentDownloadService.attachmentChanged(getContext(), + mAttachmentService.attachmentChanged(getContext(), Integer.parseInt(id), flags); } } @@ -2484,7 +2484,7 @@ outer: // If this is a pop3 or imap account, create the account manager account if (HostAuth.SCHEME_IMAP.equals(protocol) || HostAuth.SCHEME_POP3.equals(protocol)) { - if (Email.DEBUG) { + if (Email.DEBUG) { Log.d(TAG, "Create AccountManager account for " + protocol + "account: " + accountCursor.getString(V21_ACCOUNT_EMAIL)); @@ -2603,4 +2603,28 @@ outer: Cursor cc = cache.get(Long.toString(id)); return (cc != null); } + + public static interface AttachmentService { + /** + * Notify the service that an attachment has changed. + */ + void attachmentChanged(Context context, long id, int flags); + } + + private final AttachmentService DEFAULT_ATTACHMENT_SERVICE = new AttachmentService() { + @Override + public void attachmentChanged(Context context, long id, int flags) { + // The default implementation delegates to the real service. + AttachmentDownloadService.attachmentChanged(context, id, flags); + } + }; + private AttachmentService mAttachmentService = DEFAULT_ATTACHMENT_SERVICE; + + /** + * Injects a custom attachment service handler. If null is specified, will reset to the + * default service. + */ + public void injectAttachmentService(AttachmentService as) { + mAttachmentService = (as == null) ? DEFAULT_ATTACHMENT_SERVICE : as; + } } diff --git a/src/com/android/email/service/AttachmentDownloadService.java b/src/com/android/email/service/AttachmentDownloadService.java index a83b25be1..f570f156c 100644 --- a/src/com/android/email/service/AttachmentDownloadService.java +++ b/src/com/android/email/service/AttachmentDownloadService.java @@ -303,6 +303,7 @@ public class AttachmentDownloadService extends Service implements Runnable { return null; } + @Override public synchronized boolean isEmpty() { return super.isEmpty() && mDownloadsInProgress.isEmpty(); } diff --git a/tests/src/com/android/email/activity/MessageComposeTests.java b/tests/src/com/android/email/activity/MessageComposeTests.java index 9af9a96e2..90e2e8b8c 100644 --- a/tests/src/com/android/email/activity/MessageComposeTests.java +++ b/tests/src/com/android/email/activity/MessageComposeTests.java @@ -937,25 +937,22 @@ public class MessageComposeTests mToView.performValidation(); // address is validated as errorneous - assertNotNull(mToView.getError()); assertFalse(messageCompose.isAddressAllValid()); // the wrong address is preserved by validation - assertEquals("foo, ", mToView.getText().toString()); + assertEquals("foo", mToView.getText().toString()); mToView.setText("a@b.c"); mToView.performValidation(); // address is validated as correct - assertNull(mToView.getError()); assertTrue(messageCompose.isAddressAllValid()); mToView.setText("a@b.c, foo"); mToView.performValidation(); - assertNotNull(mToView.getError()); assertFalse(messageCompose.isAddressAllValid()); - assertEquals("a@b.c, foo, ", mToView.getText().toString()); + assertEquals("a@b.c, foo", mToView.getText().toString()); } /** diff --git a/tests/src/com/android/email/provider/ProviderTests.java b/tests/src/com/android/email/provider/ProviderTests.java index 0377a6cb0..8c2473c2d 100644 --- a/tests/src/com/android/email/provider/ProviderTests.java +++ b/tests/src/com/android/email/provider/ProviderTests.java @@ -23,6 +23,7 @@ import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; +import android.content.ContextWrapper; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; @@ -35,6 +36,7 @@ import android.test.suitebuilder.annotation.LargeTest; import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.SmallTest; +import com.android.email.provider.EmailProvider.AttachmentService; import com.android.emailcommon.AccountManagerTypes; import com.android.emailcommon.provider.Account; import com.android.emailcommon.provider.EmailContent; @@ -80,11 +82,48 @@ public class ProviderTests extends ProviderTestCase2 { super(EmailProvider.class, EmailContent.AUTHORITY); } + // TODO: move this out to a common place. There are other places that have similar mocks. + /** + * Private context wrapper used to add back getPackageName() for these tests. + */ + private static class MockContext2 extends ContextWrapper { + + private final Context mRealContext; + + public MockContext2(Context mockContext, Context realContext) { + super(mockContext); + mRealContext = realContext; + } + + @Override + public Context getApplicationContext() { + return this; + } + + @Override + public String getPackageName() { + return mRealContext.getPackageName(); + } + + @Override + public Object getSystemService(String name) { + return mRealContext.getSystemService(name); + } + } + + private static final AttachmentService MOCK_ATTACHMENT_SERVICE = new AttachmentService() { + @Override + public void attachmentChanged(Context context, long id, int flags) { + // Noop. Don't download attachments. + } + }; + @Override public void setUp() throws Exception { super.setUp(); - mMockContext = getMockContext(); + mMockContext = new MockContext2(getMockContext(), getContext()); mProvider = getProvider(); + mProvider.injectAttachmentService(MOCK_ATTACHMENT_SERVICE); // Invalidate all caches, since we reset the database for each test ContentCache.invalidateAllCaches(); } @@ -92,6 +131,7 @@ public class ProviderTests extends ProviderTestCase2 { @Override public void tearDown() throws Exception { super.tearDown(); + mProvider.injectAttachmentService(null); } /** @@ -431,6 +471,14 @@ public class ProviderTests extends ProviderTestCase2 { assertEquals("reply html", htmlReply2, body2.mHtmlReply); assertEquals("source key", sourceKey2, body2.mSourceKey); assertEquals("intro text", introText2, body2.mIntroText); + } + + @MediumTest + public void testMessageWithAttachment() { + Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext); + long account1Id = account1.mId; + Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); + long box1Id = box1.mId; // Message with attachments and body Message message3 = ProviderTestUtils.setupMessage("message3", account1Id, box1Id, true, @@ -470,11 +518,21 @@ public class ProviderTests extends ProviderTestCase2 { } finally { c.close(); } + } + + + @MediumTest + public void testMessageSaveWithJustAttachments() { + Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext); + long account1Id = account1.mId; + Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); + long box1Id = box1.mId; + Cursor c = null; // Message with attachments but no body Message message4 = ProviderTestUtils.setupMessage("message4", account1Id, box1Id, false, false, mMockContext); - atts = new ArrayList(); + ArrayList atts = new ArrayList(); for (int i = 0; i < 3; i++) { atts.add(ProviderTestUtils.setupAttachment( -1, expectedAttachmentNames[i], expectedAttachmentSizes[i], @@ -2281,11 +2339,9 @@ public class ProviderTests extends ProviderTestCase2 { */ private boolean amAccountListHasAccount(android.accounts.Account[] amAccountList, Account account, Context context) { - HostAuth hostAuth = HostAuth.restoreHostAuthWithId(context, account.mHostAuthKeyRecv); - if (hostAuth == null) return false; - String login = hostAuth.mLogin; + String email = account.mEmailAddress; for (android.accounts.Account amAccount: amAccountList) { - if (amAccount.name.equals(login)) { + if (amAccount.name.equals(email)) { return true; } }