Fix attachmentExists (recognize mContentBytes)

* Add unit test for attachmentExists

Bug: 2937856
Change-Id: I020f58740618c084676bbf1cdfad3795edb07688
This commit is contained in:
Marc Blank 2010-08-24 17:17:54 -07:00
parent 3299d4a0d9
commit 4dcb1c5fda
5 changed files with 57 additions and 6 deletions

View File

@ -838,7 +838,7 @@ public class Controller {
final long accountId) {
Attachment attachInfo = Attachment.restoreAttachmentWithId(mProviderContext, attachmentId);
if (Utility.attachmentExists(mProviderContext, accountId, attachInfo)) {
if (Utility.attachmentExists(mProviderContext, attachInfo)) {
// The attachment has already been downloaded, so we will just "pretend" to download it
// This presumably is for POP3 messages
synchronized (mListeners) {

View File

@ -1832,7 +1832,7 @@ public class MessagingController implements Runnable {
new MessagingException("The attachment is null"));
return;
}
if (Utility.attachmentExists(mContext, accountId, attachment)) {
if (Utility.attachmentExists(mContext, attachment)) {
mListeners.loadAttachmentFinished(accountId, messageId, attachmentId);
return;
}

View File

@ -877,8 +877,12 @@ public class Utility {
};
}
public static boolean attachmentExists(Context context, long accountId, Attachment attachment) {
if ((attachment == null) || (TextUtils.isEmpty(attachment.mContentUri))) {
public static boolean attachmentExists(Context context, Attachment attachment) {
if (attachment == null) {
return false;
} else if (attachment.mContentBytes != null) {
return true;
} else if (TextUtils.isEmpty(attachment.mContentUri)) {
Log.w(Email.LOG_TAG, "attachmentExists ContentUri null.");
return false;
}
@ -917,7 +921,7 @@ public class Utility {
if (msg == null) return false;
Attachment[] atts = Attachment.restoreAttachmentsWithMessageId(context, messageId);
for (Attachment att: atts) {
if (!attachmentExists(context, msg.mAccountKey, att)) {
if (!attachmentExists(context, att)) {
// If the attachment doesn't exist and isn't marked for download, we're in trouble
// since the outbound message will be stuck indefinitely in the Outbox. Instead,
// we'll just delete the attachment and continue; this is far better than the

View File

@ -791,7 +791,7 @@ public abstract class MessageViewFragmentBase extends Fragment implements View.O
// If the attachment is loaded, show 100% progress
// Note that for POP3 messages, the user will only see "Open" and "Save" since the entire
// message is loaded before being shown.
if (Utility.attachmentExists(mContext, mAccountId, attachment)) {
if (Utility.attachmentExists(mContext, attachment)) {
// Hide "Load", show "View" and "Save"
attachmentProgress.setVisibility(View.VISIBLE);
attachmentProgress.setProgress(100);

View File

@ -19,11 +19,18 @@ package com.android.email;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.ProviderTestUtils;
import com.android.email.provider.EmailContent.Account;
import com.android.email.provider.EmailContent.Attachment;
import com.android.email.provider.EmailContent.Mailbox;
import com.android.email.provider.EmailContent.Message;
import android.content.Context;
import android.test.ProviderTestCase2;
import android.test.suitebuilder.annotation.MediumTest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* This is a series of medium tests for the Utility class. These tests must be locally
* complete - no server(s) required.
@ -71,4 +78,44 @@ public class UtilityMediumTests extends ProviderTestCase2<EmailProvider> {
acct = Utility.findExistingAccount(mMockContext, account1.mId, "address-ha1", "login-ha1");
assertNull(acct);
}
public void testAttachmentExists() throws IOException {
Account account = ProviderTestUtils.setupAccount("account", true, mMockContext);
// We return false with null attachment
assertFalse(Utility.attachmentExists(mMockContext, null));
Mailbox mailbox =
ProviderTestUtils.setupMailbox("mailbox", account.mId, true, mMockContext);
Message message = ProviderTestUtils.setupMessage("foo", account.mId, mailbox.mId, false,
true, mMockContext);
Attachment attachment = ProviderTestUtils.setupAttachment(message.mId, "filename.ext",
69105, true, mMockContext);
attachment.mContentBytes = null;
// With no contentUri, we should return false
assertFalse(Utility.attachmentExists(mMockContext, attachment));
attachment.mContentBytes = new byte[0];
// With contentBytes set, we should return true
assertTrue(Utility.attachmentExists(mMockContext, attachment));
attachment.mContentBytes = null;
// Generate a file name in our data directory, and use that for contentUri
File file = mMockContext.getFileStreamPath("test.att");
// Delete the file if it already exists
if (file.exists()) {
assertTrue(file.delete());
}
// Should return false, because the file doesn't exist
assertFalse(Utility.attachmentExists(mMockContext, attachment));
assertTrue(file.createNewFile());
// Put something in the file
FileWriter writer = new FileWriter(file);
writer.write("Foo");
writer.flush();
writer.close();
attachment.mContentUri = "file://" + file.getAbsolutePath();
// Now, this should return true
assertTrue(Utility.attachmentExists(mMockContext, attachment));
}
}