Fix mailbox finding and unit tests.

We seemed to not properly fallback to querying the database if the cache
did not contain a mailbox of a specific type. We may want to consider
caching mailboxes related to PIM data, but for now this is a quick fix.

Also fixes unit tests.

Bug: 5019661
Change-Id: Idcac0a6f15aa7f174890ae586f478fbb8f6e05b7
This commit is contained in:
Ben Komalo 2011-07-25 16:04:07 -07:00
parent 50f4fbf2f6
commit cb1d65c478
6 changed files with 32 additions and 162 deletions

View File

@ -352,7 +352,9 @@ public class Mailbox extends EmailContent implements SyncColumns, MailboxColumns
try {
c.moveToFirst();
Long mailboxId = c.getLong(ID_PROJECTION_COLUMN);
if (mailboxId != null && mailboxId.intValue() != 0) {
if (mailboxId != null
&& mailboxId != 0L
&& mailboxId != NO_MAILBOX) {
return mailboxId;
} else {
Log.w(Logging.LOG_TAG, "========== Mailbox of type " + type
@ -362,6 +364,7 @@ public class Mailbox extends EmailContent implements SyncColumns, MailboxColumns
c.close();
}
}
// Fallback to querying the database directly.
String[] bindArguments = new String[] {Long.toString(type), Long.toString(accountId)};
return Utility.getFirstRowLong(context, Mailbox.CONTENT_URI,
ID_PROJECTION, WHERE_TYPE_AND_ACCOUNT_KEY, bindArguments, null,

View File

@ -130,7 +130,7 @@ public class Controller {
* 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) {
public void markForTest(boolean inUnitTests) {
mInUnitTests = inUnitTests;
}

View File

@ -58,7 +58,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
mMockContext = new MockContext2(getMockContext(), mContext);
// Invalidate all caches, since we reset the database for each test
ContentCache.invalidateAllCaches();
Controller.getInstance(mMockContext).markForUnitTest(true);
Controller.getInstance(mMockContext).markForTest(true);
}
/**
@ -66,7 +66,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
*/
@Override
protected void tearDown() throws Exception {
Controller.getInstance(mMockContext).markForUnitTest(false);
Controller.getInstance(mMockContext).markForTest(false);
super.tearDown();
}
@ -205,16 +205,6 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
assertTrue(p5out.mRequireRemoteWipe);
assertFalse(p5out.mRequireEncryptionExternal);
assertTrue(p5out.mDontAllowCamera);
// add another account that continues to mutate fields
// encryption external req'd - OR logic - will change here because true
Policy p6in = setupPolicy(0, Policy.PASSWORD_MODE_NONE, 0, 0, false, 0, 0, 0,
false, false);
Account a6 = ProviderTestUtils.setupAccount("sec-6", true, mMockContext);
Policy.setAccountPolicy(mMockContext, a6, p6in, null);
Policy p6out = mSecurityPolicy.computeAggregatePolicy();
assertNotNull(p6out);
assertTrue(p6out.mRequireEncryptionExternal);
}
private long assertAccountPolicyConsistent(long accountId, long oldKey) {
@ -403,7 +393,7 @@ public class SecurityPolicyTests extends ProviderTestCase2<EmailProvider> {
protected TestController(Context providerContext, Context systemContext) {
super(systemContext);
setProviderContext(providerContext);
markForUnitTest(true);
markForTest(true);
}
}

View File

@ -511,14 +511,6 @@ public class ImapStoreUnitTests extends InstrumentationTestCase {
* Test small Folder functions that don't really do anything in Imap
*/
public void testSmallFolderFunctions() {
// getPermanentFlags() returns { Flag.DELETED, Flag.SEEN, Flag.FLAGGED }
Flag[] flags = mFolder.getPermanentFlags();
assertEquals(3, flags.length);
// TODO: Write flags into hashset and compare them to a hashset and compare them
assertEquals(Flag.DELETED, flags[0]);
assertEquals(Flag.SEEN, flags[1]);
assertEquals(Flag.FLAGGED, flags[2]);
// canCreate() returns true
assertTrue(mFolder.canCreate(FolderType.HOLDS_FOLDERS));
assertTrue(mFolder.canCreate(FolderType.HOLDS_MESSAGES));

View File

@ -16,11 +16,15 @@
package com.android.email.mail.store;
import android.test.InstrumentationTestCase;
import android.content.Context;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.email.Controller;
import com.android.email.DBTestHelper;
import com.android.email.mail.Transport;
import com.android.email.mail.transport.MockTransport;
import com.android.email.provider.ProviderTestUtils;
import com.android.emailcommon.TempDirectory;
import com.android.emailcommon.internet.MimeMessage;
import com.android.emailcommon.mail.Address;
@ -40,7 +44,7 @@ import com.android.emailcommon.provider.HostAuth;
* complete - no server(s) required.
*/
@SmallTest
public class Pop3StoreUnitTests extends InstrumentationTestCase {
public class Pop3StoreUnitTests extends AndroidTestCase {
final String UNIQUE_ID_1 = "20080909002219r1800rrjo9e00";
final static int PER_MESSAGE_SIZE = 100;
@ -49,23 +53,31 @@ public class Pop3StoreUnitTests extends InstrumentationTestCase {
private Pop3Store mStore = null;
private Pop3Store.Pop3Folder mFolder = null;
private Context mMockContext;
/**
* Setup code. We generate a lightweight Pop3Store and Pop3Store.Pop3Folder.
*/
@Override
protected void setUp() throws Exception {
super.setUp();
mMockContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
getContext());
Controller.getInstance(mMockContext).setProviderContext(mMockContext);
Controller.getInstance(mMockContext).markForTest(true);
// Use the target's (i.e. the Email application) context
TempDirectory.setTempDirectory(getInstrumentation().getTargetContext());
TempDirectory.setTempDirectory(mMockContext);
// These are needed so we can get at the inner classes
HostAuth testAuth = new HostAuth();
Account testAccount = new Account();
Account testAccount = ProviderTestUtils.setupAccount("acct1", false, mMockContext);
testAuth.setLogin("user", "password");
testAuth.setConnection("pop3", "server", 999);
testAccount.mHostAuthRecv = testAuth;
mStore = (Pop3Store) Pop3Store.newInstance(testAccount, getInstrumentation().getContext());
testAccount.save(mMockContext);
mStore = (Pop3Store) Pop3Store.newInstance(testAccount, mMockContext);
mFolder = (Pop3Store.Pop3Folder) mStore.getFolder("INBOX");
}

View File

@ -16,6 +16,10 @@
package com.android.email.mail.transport;
import android.content.Context;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import com.android.email.DBTestHelper;
import com.android.email.mail.Transport;
import com.android.email.provider.EmailProvider;
@ -27,21 +31,9 @@ import com.android.emailcommon.provider.EmailContent.Body;
import com.android.emailcommon.provider.EmailContent.Message;
import com.android.emailcommon.provider.HostAuth;
import org.apache.commons.io.IOUtils;
import android.content.Context;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Pattern;
/**
* This is a series of unit tests for the SMTP Sender class. These tests must be locally
@ -150,7 +142,7 @@ public class SmtpSenderUnitTests extends AndroidTestCase {
message.save(mProviderContext);
// Creates an attachment with a bogus file (so we get headers only)
Attachment attachment = setupSimpleAttachment(mProviderContext, message.mId, false);
Attachment attachment = setupSimpleAttachment(mProviderContext, message.mId);
attachment.save(mProviderContext);
expectSimpleMessage(mockTransport);
@ -166,113 +158,6 @@ public class SmtpSenderUnitTests extends AndroidTestCase {
mSender.sendMessage(message.mId);
}
/**
* Test: Open and send a single message with an attachment (sunny day)
*/
public void testSendMessageWithAttachment() throws MessagingException, IOException {
MockTransport mockTransport = openAndInjectMockTransport();
// Since SmtpSender.sendMessage() does a close then open, we need to preset for the open
mockTransport.expectClose();
setupOpen(mockTransport, null);
Message message = setupSimpleMessage();
message.save(mProviderContext);
// Creates an attachment with a real file
Attachment attachment = setupSimpleAttachment(mProviderContext, message.mId, true);
attachment.save(mProviderContext);
expectSimpleMessage(mockTransport);
mockTransport.expect("Content-Type: multipart/mixed; boundary=\".*");
mockTransport.expect("");
mockTransport.expect("----.*");
expectSimpleAttachment(mockTransport, attachment);
mockTransport.expect("");
mockTransport.expect("----.*--");
mockTransport.expect("\r\n\\.", "250 2.0.0 kv2f1a00C02Rf8w3Vv mail accepted for delivery");
// Now trigger the transmission
mSender.sendMessage(message.mId);
}
/**
* Test: Open and send a single message with two attachments
*/
public void testSendMessageWithTwoAttachments() throws MessagingException, IOException {
MockTransport mockTransport = openAndInjectMockTransport();
// Since SmtpSender.sendMessage() does a close then open, we need to preset for the open
mockTransport.expectClose();
setupOpen(mockTransport, null);
Message message = setupSimpleMessage();
message.save(mProviderContext);
// Creates an attachment with a real file
Attachment attachment = setupSimpleAttachment(mProviderContext, message.mId, true);
attachment.save(mProviderContext);
// Creates an attachment with a real file
Attachment attachment2 = setupSimpleAttachment(mProviderContext, message.mId, true);
attachment2.save(mProviderContext);
expectSimpleMessage(mockTransport);
mockTransport.expect("Content-Type: multipart/mixed; boundary=\".*");
mockTransport.expect("");
mockTransport.expect("----.*");
expectSimpleAttachment(mockTransport, attachment);
mockTransport.expect("");
mockTransport.expect("----.*");
expectSimpleAttachment(mockTransport, attachment2);
mockTransport.expect("");
mockTransport.expect("----.*--");
mockTransport.expect("\r\n\\.", "250 2.0.0 kv2f1a00C02Rf8w3Vv mail accepted for delivery");
// Now trigger the transmission
mSender.sendMessage(message.mId);
}
/**
* Test: Open and send a single message with body & attachment (sunny day)
*/
public void testSendMessageWithBodyAndAttachment() throws MessagingException, IOException {
MockTransport mockTransport = openAndInjectMockTransport();
// Since SmtpSender.sendMessage() does a close then open, we need to preset for the open
mockTransport.expectClose();
setupOpen(mockTransport, null);
Message message = setupSimpleMessage();
message.save(mProviderContext);
Body body = new Body();
body.mMessageKey = message.mId;
body.mTextContent = TEST_STRING;
body.save(mProviderContext);
Attachment attachment = setupSimpleAttachment(mProviderContext, message.mId, true);
attachment.save(mProviderContext);
// prepare for the message traffic we'll see
expectSimpleMessage(mockTransport);
mockTransport.expect("Content-Type: multipart/mixed; boundary=\".*");
mockTransport.expect("");
mockTransport.expect("----.*");
mockTransport.expect("Content-Type: text/plain; charset=utf-8");
mockTransport.expect("Content-Transfer-Encoding: base64");
mockTransport.expect("");
mockTransport.expect(TEST_STRING_BASE64);
mockTransport.expect("----.*");
expectSimpleAttachment(mockTransport, attachment);
mockTransport.expect("");
mockTransport.expect("----.*--");
mockTransport.expect("\r\n\\.", "250 2.0.0 kv2f1a00C02Rf8w3Vv mail accepted for delivery");
// Now trigger the transmission
mSender.sendMessage(message.mId);
}
/**
* Prepare to send a simple message (see setReceiveSimpleMessage)
*/
@ -304,8 +189,7 @@ public class SmtpSenderUnitTests extends AndroidTestCase {
/**
* Prepare to send a simple attachment
*/
private Attachment setupSimpleAttachment(Context context, long messageId, boolean withBody)
throws IOException {
private Attachment setupSimpleAttachment(Context context, long messageId) {
Attachment attachment = new Attachment();
attachment.mFileName = "the file.jpg";
attachment.mMimeType = "image/jpg";
@ -316,17 +200,6 @@ public class SmtpSenderUnitTests extends AndroidTestCase {
attachment.mLocation = null;
attachment.mEncoding = null;
if (withBody) {
// Is there an easier way to set up a temp file?
InputStream inStream = new ByteArrayInputStream(TEST_STRING.getBytes());
File cacheDir = context.getCacheDir();
File tmpFile = File.createTempFile("setupSimpleAttachment", "tmp", cacheDir);
OutputStream outStream = new FileOutputStream(tmpFile);
IOUtils.copy(inStream, outStream);
attachment.mContentUri = "file://" + tmpFile.getAbsolutePath();
}
return attachment;
}
@ -357,7 +230,7 @@ public class SmtpSenderUnitTests extends AndroidTestCase {
// Load up just the bare minimum to expose the error
mockTransport.expect(null, "220 MockTransport 2000 Ready To Assist You Peewee");
mockTransport.expect("EHLO " + Pattern.quote(LOCAL_ADDRESS), "");
mockTransport.expectLiterally("EHLO [" + LOCAL_ADDRESS + "]", null);
// Now trigger the transmission
// Note, a null message is sufficient here, as we won't even get past open()