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
This commit is contained in:
Ben Komalo 2011-08-23 18:02:11 -07:00
parent 44cbb82756
commit 32bed4bb8e
7 changed files with 102 additions and 24 deletions

View File

@ -33,7 +33,7 @@ automatically saved by framework.-->
<com.android.email.activity.ChipsAddressTextView
style="@style/EmailRecipientEditTextView"
android:layout_toRightOf="@id/label"
android:id="@+id/to_address_field" />
android:id="@+id/to" />
</RelativeLayout>
<LinearLayout
@ -56,7 +56,7 @@ automatically saved by framework.-->
<com.android.email.activity.ChipsAddressTextView
style="@style/EmailRecipientEditTextView"
android:layout_toRightOf="@id/label"
android:id="@+id/cc_address_field" />
android:id="@+id/cc" />
</RelativeLayout>
@ -72,10 +72,10 @@ automatically saved by framework.-->
<com.android.email.activity.ChipsAddressTextView
style="@style/EmailRecipientEditTextView"
android:layout_toRightOf="@id/label"
android:id="@+id/bcc_address_field" />
android:id="@+id/bcc" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>

View File

@ -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" />
</RelativeLayout>
<LinearLayout
@ -64,7 +64,7 @@ automatically saved by framework.-->
android:imeOptions="actionNext"
android:layout_weight="1"
android:layout_toRightOf="@id/label"
android:id="@+id/cc_address_field" />
android:id="@+id/cc" />
</RelativeLayout>
@ -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" />
</RelativeLayout>

View File

@ -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 {

View File

@ -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;
}
}

View File

@ -303,6 +303,7 @@ public class AttachmentDownloadService extends Service implements Runnable {
return null;
}
@Override
public synchronized boolean isEmpty() {
return super.isEmpty() && mDownloadsInProgress.isEmpty();
}

View File

@ -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());
}
/**

View File

@ -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<EmailProvider> {
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<EmailProvider> {
@Override
public void tearDown() throws Exception {
super.tearDown();
mProvider.injectAttachmentService(null);
}
/**
@ -431,6 +471,14 @@ public class ProviderTests extends ProviderTestCase2<EmailProvider> {
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<EmailProvider> {
} 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<Attachment>();
ArrayList<Attachment> atts = new ArrayList<Attachment>();
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<EmailProvider> {
*/
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;
}
}