FileDocCategorySizeDatePackage
MessageComposeInstrumentationTests.javaAPI DocAndroid 1.5 API16047Wed May 06 22:42:46 BST 2009com.android.email.activity

MessageComposeInstrumentationTests

public class MessageComposeInstrumentationTests extends android.test.ActivityInstrumentationTestCase2
Various instrumentation tests for MessageCompose. It might be possible to convert these to ActivityUnitTest, which would be faster.

Fields Summary
private android.widget.EditText
mToView
private android.widget.EditText
mSubjectView
private android.widget.EditText
mMessageView
private static final String
SENDER
private static final String
REPLYTO
private static final String
RECIPIENT_TO
private static final String
RECIPIENT_CC
private static final String
RECIPIENT_BCC
private static final String
SUBJECT
private static final String
BODY
private static final String
ACTION_REPLY
Note - these are copied from private strings in MessageCompose. Make them package?
private static final String
ACTION_REPLY_ALL
private static final String
ACTION_FORWARD
private static final String
ACTION_EDIT_DRAFT
Constructors Summary
public MessageComposeInstrumentationTests()

    
      
        super("com.android.email", MessageCompose.class);
    
Methods Summary
private com.android.email.mail.MessagebuildTestMessage(java.lang.String to, java.lang.String sender, java.lang.String subject, java.lang.String content)
Build a test message that can be used as input to processSourceMessage

param
to Recipient(s) of the message
param
sender Sender(s) of the message
param
subject Subject of the message
param
content Content of the message
return
a complete Message object

        Message message = new MimeMessage();
        
        if (to != null) {
            Address[] addresses = Address.parse(to);
            message.setRecipients(RecipientType.TO, addresses);
        }
        
        if (sender != null) {
            Address[] addresses = Address.parse(sender);
            message.setFrom(Address.parse(sender)[0]);
        }
        
        if (subject != null) {
            message.setSubject(subject);
        }
        
        if (content != null) {
            TextBody body = new TextBody(content);
            message.setBody(body);
        }
        
        return message;
    
private voidcheckCommaInsert(java.lang.String before, java.lang.String after, boolean expectComma)
Check comma insertion logic for a single try on the To: field

        String expect = new String(before + (expectComma ? ", " : " ") + after);

        runTestOnUiThread(new Runnable() {
            public void run() {
                mToView.setText(before + after);
                mToView.setSelection(before.length());
            }
        });
        getInstrumentation().sendStringSync(" ");
        String result = mToView.getText().toString();
        assertEquals(expect, result);
      
     
private voidcheckFields(java.lang.String to, java.lang.String cc, java.lang.String bcc, java.lang.String subject, java.lang.String content)
Helper method to quickly check (and assert) on the to, subject, and content views.

param
to expected value (null = it must be empty)
param
cc expected value (null = it must be empty)
param
bcc expected value (null = it must be empty)
param
subject expected value (null = it must be empty)
param
content expected value (null = it must be empty)

        String toText = mToView.getText().toString();
        if (to == null) {
            assertEquals(0, toText.length());
        } else {
            assertEquals(to, toText);
        }

        String subjectText = mSubjectView.getText().toString();
        if (subject == null) {
            assertEquals(0, subjectText.length());
        } else {
            assertEquals(subject, subjectText);
        }

        String contentText = mMessageView.getText().toString();
        if (content == null) {
            assertEquals(0, contentText.length());
        } else {
            assertEquals(content, contentText);
        }
    
private voidcheckFocused(android.view.View focused)
Helper method to verify which field has the focus

param
focused The view that should be focused (all others should not have focus)

        assertEquals(focused == mToView, mToView.isFocused());
        assertEquals(focused == mSubjectView, mSubjectView.isFocused());
        assertEquals(focused == mMessageView, mMessageView.isFocused());
    
private voidresetViews()
Helper used when running multiple calls to processSourceMessage within a test method. Simply clears out the views, so that we get fresh data and not appended data. Must call from UI thread.

        mToView.setText(null);
        mSubjectView.setText(null);
        mMessageView.setText(null);
    
protected voidsetUp()

        super.setUp();
        Context context = getInstrumentation().getTargetContext();
        Account[] accounts = Preferences.getPreferences(context).getAccounts();
        if (accounts.length > 0)
        {
            // This depends on getDefaultAccount() to auto-assign the default account, if necessary
            Preferences.getPreferences(context).getDefaultAccount();
            Email.setServicesEnabled(context);
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        setActivityIntent(intent);
        final MessageCompose a = getActivity();
        mToView = (EditText) a.findViewById(R.id.to);
        mSubjectView = (EditText) a.findViewById(R.id.subject);
        mMessageView = (EditText) a.findViewById(R.id.message_content);
    
public voidtestBrowserMailToIntent()
Test for processing of a typical browser Mailto intent, e.g. action=android.intent.action.VIEW categories={android.intent.category.BROWSABLE} data=mailto:user@domain.com?subject=This%20is%20%the%subject

        
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("mailto:" + RECIPIENT_TO + "?subject=This%20is%20the%20subject");
        intent.setData(uri);
        
        final MessageCompose a = getActivity();
        final Intent i2 = new Intent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.initFromIntent(i2);
                checkFields(RECIPIENT_TO + ", ", null, null, "This is the subject", null);
                checkFocused(mMessageView);
            }
        });
    
public voidtestCommaInserting()
Tests for the comma-inserting logic. The logic is applied equally to To: Cc: and Bcc: but we only run the full set on To:

        // simple appending cases
        checkCommaInsert("a", "", false);
        checkCommaInsert("a@", "", false);
        checkCommaInsert("a@b", "", false);
        checkCommaInsert("a@b.", "", true); // non-optimal, but matches current implementation
        checkCommaInsert("a@b.c", "", true);
        
        // confirm works properly for internal editing
        checkCommaInsert("me@foo.com, you", " they@bar.com", false);
        checkCommaInsert("me@foo.com, you@", "they@bar.com", false);
        checkCommaInsert("me@foo.com, you@bar", " they@bar.com", false);
        checkCommaInsert("me@foo.com, you@bar.", " they@bar.com", true); // non-optimal
        checkCommaInsert("me@foo.com, you@bar.com", " they@bar.com", true);
        
        // check a couple of multi-period cases
        checkCommaInsert("me.myself@foo", "", false);
        checkCommaInsert("me.myself@foo.com", "", true);
        checkCommaInsert("me@foo.co.uk", "", true);
        
        // cases that should not append because there's already a comma
        checkCommaInsert("a@b.c,", "", false);
        checkCommaInsert("me@foo.com, you@bar.com,", " they@bar.com", false);
        checkCommaInsert("me.myself@foo.com,", "", false);
        checkCommaInsert("me@foo.co.uk,", "", false);
    
public voidtestIntentHeaderExtras()
Test for processing of Intent EXTRA_* fields that impact the headers: Intent.EXTRA_EMAIL, Intent.EXTRA_CC, Intent.EXTRA_BCC, Intent.EXTRA_SUBJECT

        
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { RECIPIENT_TO });
        intent.putExtra(Intent.EXTRA_CC, new String[] { RECIPIENT_CC });
        intent.putExtra(Intent.EXTRA_BCC, new String[] { RECIPIENT_BCC });
        intent.putExtra(Intent.EXTRA_SUBJECT, SUBJECT);
        
        final MessageCompose a = getActivity();
        final Intent i2 = new Intent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.initFromIntent(i2);
                checkFields(RECIPIENT_TO + ", ", RECIPIENT_CC, RECIPIENT_BCC, SUBJECT, null);
                checkFocused(mMessageView);
            }
        });
    
public voidtestIntentSendPlainText()
Test for processing of a typical browser "share" intent, e.g. type="text/plain", EXTRA_TEXT="http:link.server.com"

        
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_TEXT, BODY);
        
        final MessageCompose a = getActivity();
        final Intent i2 = new Intent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.initFromIntent(i2);
                checkFields(null, null, null, null, BODY);
                checkFocused(mToView);
            }
        });
    
public voidtestPreconditions()
The name 'test preconditions' is a convention to signal that if this test doesn't pass, the test case was not set up properly and it might explain any and all failures in other tests. This is not guaranteed to run before other tests, as junit uses reflection to find the tests.

        assertNotNull(mToView);
        assertEquals(0, mToView.length());
        assertNotNull(mSubjectView);
        assertEquals(0, mSubjectView.length());
        assertNotNull(mMessageView);
        assertEquals(0, mMessageView.length());
    
public voidtestProcessSourceMessageDraft()
Test processSourceMessage() for EDIT_DRAFT Reply and ReplyAll should map: To = to Subject = Subject Body = body (has cursor) TODO check CC and BCC handling too

        
        final Message message = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY);
        Intent intent = new Intent(ACTION_EDIT_DRAFT);
        final MessageCompose a = getActivity();
        a.setIntent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.processSourceMessage(message);
                checkFields(RECIPIENT_TO + ", ", null, null, SUBJECT, BODY);
                checkFocused(mMessageView);
            }
        });
        
        // if subject is null, then cursor should be there instead
        
        message.setSubject("");
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                resetViews();
                a.processSourceMessage(message);
                checkFields(RECIPIENT_TO + ", ", null, null, null, BODY);
                checkFocused(mSubjectView);
            }
        });
        
    
public voidtestProcessSourceMessageForward()
Test processSourceMessage() for FORWARD To = empty (and has cursor) Subject = Fwd: Subject Body = empty

        
        final Message message = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY);
        Intent intent = new Intent(ACTION_FORWARD);
        final MessageCompose a = getActivity();
        a.setIntent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.processSourceMessage(message);
                checkFields(null, null, null, "Fwd: " + SUBJECT, null);
                checkFocused(mToView);
            }
        });
    
public voidtestProcessSourceMessageReply()
Test a couple of variations of processSourceMessage() for REPLY To = Reply-To or From: (if REPLY) To = (Reply-To or From:) + To: + Cc: (if REPLY_ALL) Subject = Re: Subject Body = empty (and has cursor) TODO test REPLY_ALL

        
        final Message message = buildTestMessage(RECIPIENT_TO, SENDER, SUBJECT, BODY);
        Intent intent = new Intent(ACTION_REPLY);
        final MessageCompose a = getActivity();
        a.setIntent(intent);
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                a.processSourceMessage(message);
                checkFields(SENDER + ", ", null, null, "Re: " + SUBJECT, null);
                checkFocused(mMessageView);
            }
        });
        
        message.setFrom(null);
        message.setReplyTo(Address.parse(REPLYTO));
        
        runTestOnUiThread(new Runnable() {
            public void run() {
                resetViews();
                a.processSourceMessage(message);
                checkFields(REPLYTO + ", ", null, null, "Re: " + SUBJECT, null);
                checkFocused(mMessageView);
            }
        });