LegacyConversionsTests.java revision fd249f61dd9c5fd1e7e0abffcb09b9e285d82bae
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.email; 18 19import com.android.email.mail.Address; 20import com.android.email.mail.Body; 21import com.android.email.mail.BodyPart; 22import com.android.email.mail.Flag; 23import com.android.email.mail.Folder; 24import com.android.email.mail.Message; 25import com.android.email.mail.MessageTestUtils; 26import com.android.email.mail.MessagingException; 27import com.android.email.mail.Part; 28import com.android.email.mail.Folder.OpenMode; 29import com.android.email.mail.Message.RecipientType; 30import com.android.email.mail.MessageTestUtils.MessageBuilder; 31import com.android.email.mail.MessageTestUtils.MultipartBuilder; 32import com.android.email.mail.internet.MimeBodyPart; 33import com.android.email.mail.internet.MimeHeader; 34import com.android.email.mail.internet.MimeMessage; 35import com.android.email.mail.internet.MimeUtility; 36import com.android.email.mail.internet.TextBody; 37import com.android.email.mail.store.LocalStore; 38import com.android.email.mail.store.LocalStoreUnitTests; 39import com.android.email.provider.EmailContent; 40import com.android.email.provider.EmailProvider; 41import com.android.email.provider.ProviderTestUtils; 42import com.android.email.provider.EmailContent.Attachment; 43import com.android.email.provider.EmailContent.Mailbox; 44 45import android.content.ContentUris; 46import android.content.Context; 47import android.database.Cursor; 48import android.net.Uri; 49import android.test.ProviderTestCase2; 50 51import java.io.IOException; 52import java.util.ArrayList; 53import java.util.Date; 54 55/** 56 * Tests of the Legacy Conversions code (used by MessagingController). 57 * 58 * NOTE: It would probably make sense to rewrite this using a MockProvider, instead of the 59 * ProviderTestCase (which is a real provider running on a temp database). This would be more of 60 * a true "unit test". 61 * 62 * You can run this entire test case with: 63 * runtest -c com.android.email.LegacyConversionsTests email 64 */ 65public class LegacyConversionsTests extends ProviderTestCase2<EmailProvider> { 66 67 private static final String UID = "UID.12345678"; 68 private static final String SENDER = "sender@android.com"; 69 private static final String RECIPIENT_TO = "recipient-to@android.com"; 70 private static final String RECIPIENT_CC = "recipient-cc@android.com"; 71 private static final String RECIPIENT_BCC = "recipient-bcc@android.com"; 72 private static final String REPLY_TO = "reply-to@android.com"; 73 private static final String SUBJECT = "This is the subject"; 74 private static final String MESSAGE_ID = "Test-Message-ID"; 75 private static final String MESSAGE_ID_2 = "Test-Message-ID-Second"; 76 77 EmailProvider mProvider; 78 Context mProviderContext; 79 Context mContext; 80 Account mLegacyAccount = null; 81 Preferences mPreferences = null; 82 83 public LegacyConversionsTests() { 84 super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY); 85 } 86 87 @Override 88 public void setUp() throws Exception { 89 super.setUp(); 90 mProviderContext = getMockContext(); 91 mContext = getContext(); 92 } 93 94 @Override 95 public void tearDown() throws Exception { 96 super.tearDown(); 97 if (mLegacyAccount != null) { 98 mLegacyAccount.delete(mPreferences); 99 } 100 } 101 102 /** 103 * TODO: basic Legacy -> Provider Message conversions 104 * TODO: basic Legacy -> Provider Body conversions 105 * TODO: rainy day tests of all kinds 106 */ 107 108 /** 109 * Test basic conversion from Store message to Provider message 110 * 111 * TODO: Not a complete test of all fields, and some fields need special tests (e.g. flags) 112 * TODO: There are many special cases in the tested function, that need to be 113 * tested here as well. 114 */ 115 public void testUpdateMessageFields() throws MessagingException { 116 MimeMessage message = buildTestMessage(RECIPIENT_TO, RECIPIENT_CC, RECIPIENT_BCC, 117 REPLY_TO, SENDER, SUBJECT, null); 118 EmailContent.Message localMessage = new EmailContent.Message(); 119 120 boolean result = LegacyConversions.updateMessageFields(localMessage, message, 1, 1); 121 assertTrue(result); 122 checkProviderMessage("testUpdateMessageFields", message, localMessage); 123 } 124 125 /** 126 * Test basic conversion from Store message to Provider message, when the provider message 127 * does not have a proper message-id. 128 */ 129 public void testUpdateMessageFieldsNoMessageId() throws MessagingException { 130 MimeMessage message = buildTestMessage(RECIPIENT_TO, RECIPIENT_CC, RECIPIENT_BCC, 131 REPLY_TO, SENDER, SUBJECT, null); 132 EmailContent.Message localMessage = new EmailContent.Message(); 133 134 // If the source message-id is null, the target should be left as-is 135 localMessage.mMessageId = MESSAGE_ID_2; 136 message.removeHeader("Message-ID"); 137 138 boolean result = LegacyConversions.updateMessageFields(localMessage, message, 1, 1); 139 assertTrue(result); 140 assertEquals(MESSAGE_ID_2, localMessage.mMessageId); 141 } 142 143 /** 144 * Build a lightweight Store message with simple field population 145 */ 146 private MimeMessage buildTestMessage(String to, String cc, String bcc, String replyTo, 147 String sender, String subject, String content) throws MessagingException { 148 MimeMessage message = new MimeMessage(); 149 150 if (to != null) { 151 Address[] addresses = Address.parse(to); 152 message.setRecipients(RecipientType.TO, addresses); 153 } 154 if (cc != null) { 155 Address[] addresses = Address.parse(cc); 156 message.setRecipients(RecipientType.CC, addresses); 157 } 158 if (bcc != null) { 159 Address[] addresses = Address.parse(bcc); 160 message.setRecipients(RecipientType.BCC, addresses); 161 } 162 if (replyTo != null) { 163 Address[] addresses = Address.parse(replyTo); 164 message.setReplyTo(addresses); 165 } 166 if (sender != null) { 167 Address[] addresses = Address.parse(sender); 168 message.setFrom(addresses[0]); 169 } 170 if (subject != null) { 171 message.setSubject(subject); 172 } 173 if (content != null) { 174 TextBody body = new TextBody(content); 175 message.setBody(body); 176 } 177 178 message.setUid(UID); 179 message.setSentDate(new Date()); 180 message.setInternalDate(new Date()); 181 message.setMessageId(MESSAGE_ID); 182 return message; 183 } 184 185 /** 186 * Basic test of body parts conversion from Store message to Provider message. 187 * This tests that a null body part simply results in null text, and does not crash 188 * or return "null". 189 * 190 * TODO very incomplete, there are many permutations to be explored 191 */ 192 public void testUpdateBodyFieldsNullText() throws MessagingException { 193 EmailContent.Body localBody = new EmailContent.Body(); 194 EmailContent.Message localMessage = new EmailContent.Message(); 195 ArrayList<Part> viewables = new ArrayList<Part>(); 196 Part emptyTextPart = new MimeBodyPart(null, "text/plain"); 197 viewables.add(emptyTextPart); 198 199 // a "null" body part of type text/plain should result in a null mTextContent 200 boolean result = LegacyConversions.updateBodyFields(localBody, localMessage, viewables); 201 assertTrue(result); 202 assertNull(localBody.mTextContent); 203 } 204 205 /** 206 * Sunny day test of adding attachments from an IMAP message. 207 */ 208 public void testAddAttachments() throws MessagingException, IOException { 209 // Prepare a local message to add the attachments to 210 final long accountId = 1; 211 final long mailboxId = 1; 212 final EmailContent.Message localMessage = ProviderTestUtils.setupMessage( 213 "local-message", accountId, mailboxId, false, true, mProviderContext); 214 215 // Prepare a legacy message with attachments 216 final Message legacyMessage = prepareLegacyMessageWithAttachments(2, false); 217 218 // Now, convert from legacy to provider and see what happens 219 ArrayList<Part> viewables = new ArrayList<Part>(); 220 ArrayList<Part> attachments = new ArrayList<Part>(); 221 MimeUtility.collectParts(legacyMessage, viewables, attachments); 222 LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments, false); 223 224 // Read back all attachments for message and check field values 225 Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId); 226 Cursor c = mProviderContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION, 227 null, null, null); 228 try { 229 assertEquals(2, c.getCount()); 230 while (c.moveToNext()) { 231 Attachment attachment = Attachment.getContent(c, Attachment.class); 232 if ("101".equals(attachment.mLocation)) { 233 checkAttachment("attachment1Part", attachments.get(0), attachment); 234 } else if ("102".equals(attachment.mLocation)) { 235 checkAttachment("attachment2Part", attachments.get(1), attachment); 236 } else { 237 fail("Unexpected attachment with location " + attachment.mLocation); 238 } 239 } 240 } finally { 241 c.close(); 242 } 243 } 244 245 /** 246 * Test that attachments aren't re-added in the DB. This supports the "partial download" 247 * nature of POP messages. 248 */ 249 public void testAddDuplicateAttachments() throws MessagingException, IOException { 250 // Prepare a local message to add the attachments to 251 final long accountId = 1; 252 final long mailboxId = 1; 253 final EmailContent.Message localMessage = ProviderTestUtils.setupMessage( 254 "local-message", accountId, mailboxId, false, true, mProviderContext); 255 256 // Prepare a legacy message with attachments 257 Message legacyMessage = prepareLegacyMessageWithAttachments(2, false); 258 259 // Now, convert from legacy to provider and see what happens 260 ArrayList<Part> viewables = new ArrayList<Part>(); 261 ArrayList<Part> attachments = new ArrayList<Part>(); 262 MimeUtility.collectParts(legacyMessage, viewables, attachments); 263 LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments, false); 264 265 // Confirm two attachment objects created 266 Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId); 267 assertEquals(2, EmailContent.count(mProviderContext, uri, null, null)); 268 269 // Now add the attachments again and confirm there are still only two 270 LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments, false); 271 assertEquals(2, EmailContent.count(mProviderContext, uri, null, null)); 272 273 // Now add a 3rd & 4th attachment and make sure the total is 4, not 2 or 6 274 legacyMessage = prepareLegacyMessageWithAttachments(4, false); 275 viewables = new ArrayList<Part>(); 276 attachments = new ArrayList<Part>(); 277 MimeUtility.collectParts(legacyMessage, viewables, attachments); 278 LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments, false); 279 assertEquals(4, EmailContent.count(mProviderContext, uri, null, null)); 280 } 281 282 /** 283 * Sunny day test of adding attachments in "local account upgrade" mode 284 */ 285 public void testLocalUpgradeAttachments() throws MessagingException, IOException { 286 // Prepare a local message to add the attachments to 287 final long accountId = 1; 288 final long mailboxId = 1; 289 final EmailContent.Message localMessage = ProviderTestUtils.setupMessage( 290 "local-upgrade", accountId, mailboxId, false, true, mProviderContext); 291 292 // Prepare a legacy message with attachments 293 final Message legacyMessage = prepareLegacyMessageWithAttachments(2, true); 294 295 // Now, convert from legacy to provider and see what happens 296 ArrayList<Part> viewables = new ArrayList<Part>(); 297 ArrayList<Part> attachments = new ArrayList<Part>(); 298 MimeUtility.collectParts(legacyMessage, viewables, attachments); 299 LegacyConversions.updateAttachments(mProviderContext, localMessage, attachments, true); 300 301 // Read back all attachments for message and check field values 302 Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, localMessage.mId); 303 Cursor c = mProviderContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION, 304 null, null, null); 305 try { 306 assertEquals(2, c.getCount()); 307 while (c.moveToNext()) { 308 Attachment attachment = Attachment.getContent(c, Attachment.class); 309 // This attachment should look as if created by modern (provider) MessageCompose. 310 // 1. find the original that it was created from 311 Part fromPart = null; 312 for (Part from : attachments) { 313 String contentType = MimeUtility.unfoldAndDecode(from.getContentType()); 314 String name = MimeUtility.getHeaderParameter(contentType, "name"); 315 if (name.equals(attachment.mFileName)) { 316 fromPart = from; 317 break; 318 } 319 } 320 assertTrue(fromPart != null); 321 // 2. Check values 322 checkAttachment(attachment.mFileName, fromPart, attachment); 323 } 324 } finally { 325 c.close(); 326 } 327 } 328 329 /** 330 * Prepare a legacy message with 1+ attachments 331 * @param numAttachments how many attachments to add 332 * @param localData if true, attachments are "local" data. false = "remote" (from server) 333 */ 334 private Message prepareLegacyMessageWithAttachments(int numAttachments, boolean localData) 335 throws MessagingException { 336 // First, build one or more attachment parts 337 MultipartBuilder mpBuilder = new MultipartBuilder("multipart/mixed"); 338 for (int i = 1; i <= numAttachments; ++i) { 339 if (localData) { 340 // generate an attachment that was generated by legacy code (e.g. donut) 341 // for test of upgrading accounts in place 342 // This creator models the code in legacy MessageCompose 343 Uri uri = Uri.parse("content://test/attachment/" + i); 344 String quotedName = "\"test-attachment-" + i + "\""; 345 MimeBodyPart bp = new MimeBodyPart( 346 new LocalStore.LocalAttachmentBody(uri, mProviderContext)); 347 bp.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpg;\n name=" + quotedName); 348 bp.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); 349 bp.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, 350 "attachment;\n filename=" + quotedName); 351 mpBuilder.addBodyPart(bp); 352 } else { 353 // generate an attachment that came from a server 354 BodyPart attachmentPart = MessageTestUtils.bodyPart("image/jpg", null); 355 356 // name=attachmentN size=N00 location=10N 357 attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TYPE, 358 "image/jpg;\n name=\"attachment" + i + "\""); 359 attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING, "base64"); 360 attachmentPart.setHeader(MimeHeader.HEADER_CONTENT_DISPOSITION, 361 "attachment;\n filename=\"attachment2\";\n size=" + i + "00"); 362 attachmentPart.setHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA, "10" + i); 363 364 mpBuilder.addBodyPart(attachmentPart); 365 } 366 } 367 368 // Now build a message with them 369 final Message legacyMessage = new MessageBuilder() 370 .setBody(new MultipartBuilder("multipart/mixed") 371 .addBodyPart(MessageTestUtils.bodyPart("text/html", null)) 372 .addBodyPart(mpBuilder.buildBodyPart()) 373 .build()) 374 .build(); 375 376 return legacyMessage; 377 } 378 379 /** 380 * Test the stringInequal helper 381 */ 382 public void testStringInequal() { 383 // Pairs that are "equal" 384 assertFalse(LegacyConversions.stringNotEqual(null, null)); 385 assertFalse(LegacyConversions.stringNotEqual(null, "")); 386 assertFalse(LegacyConversions.stringNotEqual("", null)); 387 assertFalse(LegacyConversions.stringNotEqual("", "")); 388 assertFalse(LegacyConversions.stringNotEqual("string-equal", "string-equal")); 389 // Pairs that are "inequal" 390 assertTrue(LegacyConversions.stringNotEqual(null, "string-inequal")); 391 assertTrue(LegacyConversions.stringNotEqual("", "string-inequal")); 392 assertTrue(LegacyConversions.stringNotEqual("string-inequal", null)); 393 assertTrue(LegacyConversions.stringNotEqual("string-inequal", "")); 394 assertTrue(LegacyConversions.stringNotEqual("string-inequal-a", "string-inequal-b")); 395 } 396 397 /** 398 * Compare attachment that was converted from Part (expected) to Provider Attachment (actual) 399 * 400 * TODO content URI should only be set if we also saved a file 401 * TODO other data encodings 402 */ 403 private void checkAttachment(String tag, Part expected, EmailContent.Attachment actual) 404 throws MessagingException { 405 String contentType = MimeUtility.unfoldAndDecode(expected.getContentType()); 406 String expectedName = MimeUtility.getHeaderParameter(contentType, "name"); 407 assertEquals(tag, expectedName, actual.mFileName); 408 assertEquals(tag, expected.getMimeType(), actual.mMimeType); 409 String disposition = expected.getDisposition(); 410 String sizeString = MimeUtility.getHeaderParameter(disposition, "size"); 411 long expectedSize = (sizeString != null) ? Long.parseLong(sizeString) : 0; 412 assertEquals(tag, expectedSize, actual.mSize); 413 assertEquals(tag, expected.getContentId(), actual.mContentId); 414 415 // content URI either both null or both matching 416 String expectedUriString = null; 417 Body body = expected.getBody(); 418 if (body instanceof LocalStore.LocalAttachmentBody) { 419 LocalStore.LocalAttachmentBody localBody = (LocalStore.LocalAttachmentBody) body; 420 Uri contentUri = localBody.getContentUri(); 421 if (contentUri != null) { 422 expectedUriString = contentUri.toString(); 423 } 424 } 425 assertEquals(tag, expectedUriString, actual.mContentUri); 426 427 assertTrue(tag, 0 != actual.mMessageKey); 428 429 // location is either both null or both matching 430 String expectedPartId = null; 431 String[] storeData = expected.getHeader(MimeHeader.HEADER_ANDROID_ATTACHMENT_STORE_DATA); 432 if (storeData != null && storeData.length > 0) { 433 expectedPartId = storeData[0]; 434 } 435 assertEquals(tag, expectedPartId, actual.mLocation); 436 assertEquals(tag, "B", actual.mEncoding); 437 } 438 439 /** 440 * TODO: Sunny day test of adding attachments from a POP message. 441 */ 442 443 /** 444 * Sunny day tests of converting an original message to a legacy message 445 */ 446 public void testMakeLegacyMessage() throws MessagingException { 447 // Set up and store a message in the provider 448 long account1Id = 1; 449 long mailbox1Id = 1; 450 451 // Test message 1: No body 452 EmailContent.Message localMessage1 = ProviderTestUtils.setupMessage("make-legacy", 453 account1Id, mailbox1Id, false, true, mProviderContext); 454 Message getMessage1 = LegacyConversions.makeMessage(mProviderContext, localMessage1); 455 checkLegacyMessage("no body", localMessage1, getMessage1); 456 457 // Test message 2: Simple body 458 EmailContent.Message localMessage2 = ProviderTestUtils.setupMessage("make-legacy", 459 account1Id, mailbox1Id, true, false, mProviderContext); 460 localMessage2.mTextReply = null; 461 localMessage2.mHtmlReply = null; 462 localMessage2.mIntroText = null; 463 localMessage2.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK; 464 localMessage2.save(mProviderContext); 465 Message getMessage2 = LegacyConversions.makeMessage(mProviderContext, localMessage2); 466 checkLegacyMessage("simple body", localMessage2, getMessage2); 467 468 // Test message 3: Body + replied-to text 469 EmailContent.Message localMessage3 = ProviderTestUtils.setupMessage("make-legacy", 470 account1Id, mailbox1Id, true, false, mProviderContext); 471 localMessage3.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK; 472 localMessage3.mFlags |= EmailContent.Message.FLAG_TYPE_REPLY; 473 localMessage3.save(mProviderContext); 474 Message getMessage3 = LegacyConversions.makeMessage(mProviderContext, localMessage3); 475 checkLegacyMessage("reply-to", localMessage3, getMessage3); 476 477 // Test message 4: Body + forwarded text 478 EmailContent.Message localMessage4 = ProviderTestUtils.setupMessage("make-legacy", 479 account1Id, mailbox1Id, true, false, mProviderContext); 480 localMessage4.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK; 481 localMessage4.mFlags |= EmailContent.Message.FLAG_TYPE_FORWARD; 482 localMessage4.save(mProviderContext); 483 Message getMessage4 = LegacyConversions.makeMessage(mProviderContext, localMessage4); 484 checkLegacyMessage("forwarding", localMessage4, getMessage4); 485 } 486 487 /** 488 * Check equality of a pair of converted messages 489 */ 490 private void checkProviderMessage(String tag, Message expect, EmailContent.Message actual) 491 throws MessagingException { 492 assertEquals(tag, expect.getUid(), actual.mServerId); 493 assertEquals(tag, expect.getSubject(), actual.mSubject); 494 assertEquals(tag, Address.pack(expect.getFrom()), actual.mFrom); 495 assertEquals(tag, expect.getSentDate().getTime(), actual.mTimeStamp); 496 assertEquals(tag, Address.pack(expect.getRecipients(RecipientType.TO)), actual.mTo); 497 assertEquals(tag, Address.pack(expect.getRecipients(RecipientType.CC)), actual.mCc); 498 assertEquals(tag, ((MimeMessage)expect).getMessageId(), actual.mMessageId); 499 assertEquals(tag, expect.isSet(Flag.SEEN), actual.mFlagRead); 500 assertEquals(tag, expect.isSet(Flag.FLAGGED), actual.mFlagFavorite); 501 } 502 503 /** 504 * Check equality of a pair of converted messages 505 */ 506 private void checkLegacyMessage(String tag, EmailContent.Message expect, Message actual) 507 throws MessagingException { 508 assertEquals(tag, expect.mServerId, actual.getUid()); 509 assertEquals(tag, expect.mServerTimeStamp, actual.getInternalDate().getTime()); 510 assertEquals(tag, expect.mSubject, actual.getSubject()); 511 assertEquals(tag, expect.mFrom, Address.pack(actual.getFrom())); 512 assertEquals(tag, expect.mTimeStamp, actual.getSentDate().getTime()); 513 assertEquals(tag, expect.mTo, Address.pack(actual.getRecipients(RecipientType.TO))); 514 assertEquals(tag, expect.mCc, Address.pack(actual.getRecipients(RecipientType.CC))); 515 assertEquals(tag, expect.mBcc, Address.pack(actual.getRecipients(RecipientType.BCC))); 516 assertEquals(tag, expect.mReplyTo, Address.pack(actual.getReplyTo())); 517 assertEquals(tag, expect.mMessageId, ((MimeMessage)actual).getMessageId()); 518 // check flags 519 assertEquals(tag, expect.mFlagRead, actual.isSet(Flag.SEEN)); 520 assertEquals(tag, expect.mFlagFavorite, actual.isSet(Flag.FLAGGED)); 521 522 // Check the body of the message 523 ArrayList<Part> viewables = new ArrayList<Part>(); 524 ArrayList<Part> attachments = new ArrayList<Part>(); 525 MimeUtility.collectParts(actual, viewables, attachments); 526 String get1Text = null; 527 String get1Html = null; 528 String get1TextReply = null; 529 String get1HtmlReply = null; 530 String get1TextIntro = null; 531 for (Part viewable : viewables) { 532 String text = MimeUtility.getTextFromPart(viewable); 533 boolean isHtml = viewable.getMimeType().equalsIgnoreCase("text/html"); 534 String[] headers = viewable.getHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART); 535 if (headers != null) { 536 String header = headers[0]; 537 boolean isReply = LegacyConversions.BODY_QUOTED_PART_REPLY.equalsIgnoreCase(header); 538 boolean isFwd = LegacyConversions.BODY_QUOTED_PART_FORWARD.equalsIgnoreCase(header); 539 boolean isIntro = LegacyConversions.BODY_QUOTED_PART_INTRO.equalsIgnoreCase(header); 540 if (isReply || isFwd) { 541 if (isHtml) { 542 get1HtmlReply = text; 543 } else { 544 get1TextReply = text; 545 } 546 } else if (isIntro) { 547 get1TextIntro = text; 548 } 549 // Check flags 550 int replyTypeFlags = expect.mFlags & EmailContent.Message.FLAG_TYPE_MASK; 551 if (isReply) { 552 assertEquals(tag, EmailContent.Message.FLAG_TYPE_REPLY, replyTypeFlags); 553 } 554 if (isFwd) { 555 assertEquals(tag, EmailContent.Message.FLAG_TYPE_FORWARD, replyTypeFlags); 556 } 557 } else { 558 if (isHtml) { 559 get1Html = text; 560 } else { 561 get1Text = text; 562 } 563 } 564 } 565 assertEquals(tag, expect.mText, get1Text); 566 assertEquals(tag, expect.mHtml, get1Html); 567 assertEquals(tag, expect.mTextReply, get1TextReply); 568 assertEquals(tag, expect.mHtmlReply, get1HtmlReply); 569 assertEquals(tag, expect.mIntroText, get1TextIntro); 570 571 // TODO Check the attachments 572 573// cv.put("attachment_count", attachments.size()); 574 } 575 576 /** 577 * Test conversion of a legacy account to a provider account 578 */ 579 public void testMakeProviderAccount() throws MessagingException { 580 581 setupLegacyAccount("testMakeProviderAccount", true); 582 EmailContent.Account toAccount = 583 LegacyConversions.makeAccount(mProviderContext, mLegacyAccount); 584 checkProviderAccount("testMakeProviderAccount", mLegacyAccount, toAccount); 585 } 586 587 /** 588 * Test conversion of a provider account to a legacy account 589 */ 590 public void testMakeLegacyAccount() throws MessagingException { 591 EmailContent.Account fromAccount = ProviderTestUtils.setupAccount("convert-to-legacy", 592 false, mProviderContext); 593 fromAccount.mHostAuthRecv = 594 ProviderTestUtils.setupHostAuth("legacy-recv", 0, false, mProviderContext); 595 fromAccount.mHostAuthSend = 596 ProviderTestUtils.setupHostAuth("legacy-send", 0, false, mProviderContext); 597 fromAccount.save(mProviderContext); 598 599 Account toAccount = LegacyConversions.makeLegacyAccount(mProviderContext, fromAccount); 600 checkLegacyAccount("testMakeLegacyAccount", fromAccount, toAccount); 601 } 602 603 /** 604 * Setup a legacy account in mLegacyAccount with many fields prefilled. 605 */ 606 private void setupLegacyAccount(String name, boolean saveIt) { 607 // prefs & legacy account are saved for cleanup (it's stored in the real prefs file) 608 mPreferences = Preferences.getPreferences(mProviderContext); 609 mLegacyAccount = new Account(mProviderContext); 610 611 // fill in useful fields 612 mLegacyAccount.mUuid = "test-uid-" + name; 613 mLegacyAccount.mStoreUri = "store://test/" + name; 614 mLegacyAccount.mLocalStoreUri = "local://localhost/" + name; 615 mLegacyAccount.mSenderUri = "sender://test/" + name; 616 mLegacyAccount.mDescription = "description " + name; 617 mLegacyAccount.mName = "name " + name; 618 mLegacyAccount.mEmail = "email " + name; 619 mLegacyAccount.mAutomaticCheckIntervalMinutes = 100; 620 mLegacyAccount.mLastAutomaticCheckTime = 200; 621 mLegacyAccount.mNotifyNewMail = true; 622 mLegacyAccount.mDraftsFolderName = "drafts " + name; 623 mLegacyAccount.mSentFolderName = "sent " + name; 624 mLegacyAccount.mTrashFolderName = "trash " + name; 625 mLegacyAccount.mOutboxFolderName = "outbox " + name; 626 mLegacyAccount.mAccountNumber = 300; 627 mLegacyAccount.mVibrate = true; 628 mLegacyAccount.mRingtoneUri = "ringtone://test/" + name; 629 mLegacyAccount.mSyncWindow = 400; 630 mLegacyAccount.mBackupFlags = 0; 631 mLegacyAccount.mDeletePolicy = Account.DELETE_POLICY_NEVER; 632 mLegacyAccount.mSecurityFlags = 500; 633 mLegacyAccount.mSignature = "signature " + name; 634 635 if (saveIt) { 636 mLegacyAccount.save(mPreferences); 637 } 638 } 639 640 /** 641 * Compare a provider account to the legacy account it was created from 642 */ 643 private void checkProviderAccount(String tag, Account expect, EmailContent.Account actual) 644 throws MessagingException { 645 assertEquals(tag + " description", expect.getDescription(), actual.mDisplayName); 646 assertEquals(tag + " email", expect.getEmail(), actual.mEmailAddress); 647 assertEquals(tag + " sync key", null, actual.mSyncKey); 648 assertEquals(tag + " lookback", expect.getSyncWindow(), actual.mSyncLookback); 649 assertEquals(tag + " sync intvl", expect.getAutomaticCheckIntervalMinutes(), 650 actual.mSyncInterval); 651 // These asserts are checking mHostAuthKeyRecv & mHostAuthKeySend 652 assertEquals(tag + " store", expect.getStoreUri(), actual.getStoreUri(mProviderContext)); 653 assertEquals(tag + " sender", expect.getSenderUri(), actual.getSenderUri(mProviderContext)); 654 // Synthesize & check flags 655 int expectFlags = 0; 656 if (expect.mNotifyNewMail) expectFlags |= EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL; 657 if (expect.mVibrate) expectFlags |= EmailContent.Account.FLAGS_VIBRATE; 658 expectFlags |= 659 (expect.mDeletePolicy << EmailContent.Account.FLAGS_DELETE_POLICY_SHIFT) 660 & EmailContent.Account.FLAGS_DELETE_POLICY_MASK; 661 assertEquals(tag + " flags", expectFlags, actual.mFlags); 662 assertEquals(tag + " default", false, actual.mIsDefault); 663 assertEquals(tag + " uuid", expect.getUuid(), actual.mCompatibilityUuid); 664 assertEquals(tag + " name", expect.getName(), actual.mSenderName); 665 assertEquals(tag + " ringtone", expect.getRingtone(), actual.mRingtoneUri); 666 assertEquals(tag + " proto vers", expect.mProtocolVersion, actual.mProtocolVersion); 667 assertEquals(tag + " new count", 0, actual.mNewMessageCount); 668 assertEquals(tag + " security", expect.mSecurityFlags, actual.mSecurityFlags); 669 assertEquals(tag + " sec sync key", null, actual.mSecuritySyncKey); 670 assertEquals(tag + " signature", expect.mSignature, actual.mSignature); 671 } 672 673 /** 674 * Compare a legacy account to the provider account it was created from 675 */ 676 private void checkLegacyAccount(String tag, EmailContent.Account expect, Account actual) 677 throws MessagingException { 678 int expectFlags = expect.getFlags(); 679 680 assertEquals(tag + " uuid", expect.mCompatibilityUuid, actual.mUuid); 681 assertEquals(tag + " store", expect.getStoreUri(mProviderContext), actual.mStoreUri); 682 assertTrue(actual.mLocalStoreUri.startsWith("local://localhost")); 683 assertEquals(tag + " sender", expect.getSenderUri(mProviderContext), actual.mSenderUri); 684 assertEquals(tag + " description", expect.getDisplayName(), actual.mDescription); 685 assertEquals(tag + " name", expect.getSenderName(), actual.mName); 686 assertEquals(tag + " email", expect.getEmailAddress(), actual.mEmail); 687 assertEquals(tag + " checkintvl", expect.getSyncInterval(), 688 actual.mAutomaticCheckIntervalMinutes); 689 assertEquals(tag + " checktime", 0, actual.mLastAutomaticCheckTime); 690 assertEquals(tag + " notify", 691 (expectFlags & EmailContent.Account.FLAGS_NOTIFY_NEW_MAIL) != 0, 692 actual.mNotifyNewMail); 693 assertEquals(tag + " drafts", null, actual.mDraftsFolderName); 694 assertEquals(tag + " sent", null, actual.mSentFolderName); 695 assertEquals(tag + " trash", null, actual.mTrashFolderName); 696 assertEquals(tag + " outbox", null, actual.mOutboxFolderName); 697 assertEquals(tag + " acct #", -1, actual.mAccountNumber); 698 assertEquals(tag + " vibrate", (expectFlags & EmailContent.Account.FLAGS_VIBRATE) != 0, 699 actual.mVibrate); 700 assertEquals(tag + " ", expect.getRingtone(), actual.mRingtoneUri); 701 assertEquals(tag + " sync window", expect.getSyncLookback(), actual.mSyncWindow); 702 assertEquals(tag + " backup flags", 0, actual.mBackupFlags); 703 assertEquals(tag + " proto vers", expect.mProtocolVersion, actual.mProtocolVersion); 704 assertEquals(tag + " delete policy", expect.getDeletePolicy(), actual.getDeletePolicy()); 705 assertEquals(tag + " security", expect.mSecurityFlags, actual.mSecurityFlags); 706 assertEquals(tag + " signature", expect.mSignature, actual.mSignature); 707 } 708 709 /** 710 * Test conversion of a legacy mailbox to a provider mailbox 711 */ 712 public void testMakeProviderMailbox() throws MessagingException { 713 EmailContent.Account toAccount = ProviderTestUtils.setupAccount("convert-mailbox", 714 true, mProviderContext); 715 Folder fromFolder = buildTestFolder("INBOX"); 716 Mailbox toMailbox = LegacyConversions.makeMailbox(mProviderContext, toAccount, fromFolder); 717 718 // Now test fields in created mailbox 719 assertEquals("INBOX", toMailbox.mDisplayName); 720 assertNull(toMailbox.mServerId); 721 assertNull(toMailbox.mParentServerId); 722 assertEquals(toAccount.mId, toMailbox.mAccountKey); 723 assertEquals(Mailbox.TYPE_INBOX, toMailbox.mType); 724 assertEquals(0, toMailbox.mDelimiter); 725 assertNull(toMailbox.mSyncKey); 726 assertEquals(0, toMailbox.mSyncLookback); 727 assertEquals(0, toMailbox.mSyncInterval); 728 assertEquals(0, toMailbox.mSyncTime); 729 assertEquals(100, toMailbox.mUnreadCount); 730 assertTrue(toMailbox.mFlagVisible); 731 assertEquals(0, toMailbox.mFlags); 732 assertEquals(Email.VISIBLE_LIMIT_DEFAULT, toMailbox.mVisibleLimit); 733 assertNull(toMailbox.mSyncStatus); 734 } 735 736 /** 737 * Build a lightweight Store Folder with simple field population. The folder is "open" 738 * and should be closed by the caller. 739 */ 740 private Folder buildTestFolder(String folderName) throws MessagingException { 741 String localStoreUri = 742 "local://localhost/" + mProviderContext.getDatabasePath(LocalStoreUnitTests.DB_NAME); 743 LocalStore store = (LocalStore) LocalStore.newInstance(localStoreUri, getContext(), null); 744 LocalStore.LocalFolder folder = (LocalStore.LocalFolder) store.getFolder(folderName); 745 folder.open(OpenMode.READ_WRITE, null); // this will create it 746 747 // set a few fields to test values 748 // folder.getName - set by getFolder() 749 folder.setUnreadMessageCount(100); 750 751 return folder; 752 } 753} 754