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 17 package com.android.email.provider; 18 19 import com.android.emailcommon.internet.Rfc822Output; 20 import com.android.emailcommon.provider.Account; 21 import com.android.emailcommon.provider.EmailContent; 22 import com.android.emailcommon.provider.EmailContent.Attachment; 23 import com.android.emailcommon.provider.EmailContent.Body; 24 import com.android.emailcommon.provider.EmailContent.Message; 25 import com.android.emailcommon.provider.HostAuth; 26 import com.android.emailcommon.provider.Mailbox; 27 import com.android.emailcommon.utility.Utility; 28 29 import android.content.ContentUris; 30 import android.content.Context; 31 import android.net.Uri; 32 import android.test.MoreAsserts; 33 34 import java.io.File; 35 import java.io.FileOutputStream; 36 37 import junit.framework.Assert; 38 39 public class ProviderTestUtils extends Assert { 40 41 /** 42 * No constructor - statics only 43 */ 44 private ProviderTestUtils() { 45 } 46 47 /** 48 * Create an account for test purposes 49 */ 50 public static Account setupAccount(String name, boolean saveIt, Context context) { 51 Account account = new Account(); 52 53 account.mDisplayName = name; 54 account.mEmailAddress = name + "@android.com"; 55 account.mSyncKey = "sync-key-" + name; 56 account.mSyncLookback = 1; 57 account.mSyncInterval = Account.CHECK_INTERVAL_NEVER; 58 account.mHostAuthKeyRecv = 0; 59 account.mHostAuthKeySend = 0; 60 account.mFlags = 4; 61 account.mCompatibilityUuid = "test-uid-" + name; 62 account.mSenderName = name; 63 account.mProtocolVersion = "2.5" + name; 64 account.mNewMessageCount = 5 + name.length(); 65 account.mPolicyKey = 0; 66 account.mSecuritySyncKey = "sec-sync-key-" + name; 67 account.mSignature = "signature-" + name; 68 if (saveIt) { 69 account.save(context); 70 } 71 return account; 72 } 73 74 /** 75 * Lightweight way of deleting an account for testing. 76 */ 77 public static void deleteAccount(Context context, long accountId) { 78 context.getContentResolver().delete(ContentUris.withAppendedId( 79 Account.CONTENT_URI, accountId), null, null); 80 } 81 82 /** 83 * Create a hostauth record for test purposes 84 */ 85 public static HostAuth setupHostAuth(String name, long accountId, boolean saveIt, 86 Context context) { 87 return setupHostAuth("protocol", name, saveIt, context); 88 } 89 90 /** 91 * Create a hostauth record for test purposes 92 */ 93 public static HostAuth setupHostAuth(String protocol, String name, boolean saveIt, 94 Context context) { 95 HostAuth hostAuth = new HostAuth(); 96 97 hostAuth.mProtocol = protocol; 98 hostAuth.mAddress = "address-" + name; 99 hostAuth.mPort = 100; 100 hostAuth.mFlags = 200; 101 hostAuth.mLogin = "login-" + name; 102 hostAuth.mPassword = "password-" + name; 103 hostAuth.mDomain = "domain-" + name; 104 105 if (saveIt) { 106 hostAuth.save(context); 107 } 108 return hostAuth; 109 } 110 111 /** 112 * Create a mailbox for test purposes 113 */ 114 public static Mailbox setupMailbox(String name, long accountId, boolean saveIt, 115 Context context) { 116 return setupMailbox(name, accountId, saveIt, context, Mailbox.TYPE_MAIL); 117 } 118 public static Mailbox setupMailbox(String name, long accountId, boolean saveIt, 119 Context context, int type) { 120 return setupMailbox(name, accountId, saveIt, context, type, '/'); 121 } 122 public static Mailbox setupMailbox(String name, long accountId, boolean saveIt, 123 Context context, int type, char delimiter) { 124 Mailbox box = new Mailbox(); 125 126 int delimiterIndex = name.lastIndexOf(delimiter); 127 String displayName = name; 128 if (delimiterIndex > 0) { 129 displayName = name.substring(delimiterIndex + 1); 130 } 131 box.mDisplayName = displayName; 132 box.mServerId = name; 133 box.mParentServerId = "parent-serverid-" + name; 134 box.mParentKey = 4; 135 box.mAccountKey = accountId; 136 box.mType = type; 137 box.mDelimiter = delimiter; 138 box.mSyncKey = "sync-key-" + name; 139 box.mSyncLookback = 2; 140 box.mSyncInterval = Account.CHECK_INTERVAL_NEVER; 141 box.mSyncTime = 3; 142 box.mFlagVisible = true; 143 box.mFlags = 5; 144 145 if (saveIt) { 146 box.save(context); 147 } 148 return box; 149 } 150 151 /** 152 * Create a message for test purposes 153 */ 154 public static Message setupMessage(String name, long accountId, long mailboxId, 155 boolean addBody, boolean saveIt, Context context) { 156 // Default starred, read, (backword compatibility) 157 return setupMessage(name, accountId, mailboxId, addBody, saveIt, context, true, true); 158 } 159 160 /** 161 * Create a message for test purposes 162 */ 163 public static Message setupMessage(String name, long accountId, long mailboxId, 164 boolean addBody, boolean saveIt, Context context, boolean starred, boolean read) { 165 Message message = new Message(); 166 167 message.mDisplayName = name; 168 message.mTimeStamp = 100 + name.length(); 169 message.mSubject = "subject " + name; 170 message.mFlagRead = read; 171 message.mFlagSeen = read; 172 message.mFlagLoaded = Message.FLAG_LOADED_UNLOADED; 173 message.mFlagFavorite = starred; 174 message.mFlagAttachment = true; 175 message.mFlags = 0; 176 177 message.mServerId = "serverid " + name; 178 message.mServerTimeStamp = 300 + name.length(); 179 message.mMessageId = "messageid " + name; 180 181 message.mMailboxKey = mailboxId; 182 message.mAccountKey = accountId; 183 184 message.mFrom = "from " + name; 185 message.mTo = "to " + name; 186 message.mCc = "cc " + name; 187 message.mBcc = "bcc " + name; 188 message.mReplyTo = "replyto " + name; 189 190 message.mMeetingInfo = "123" + accountId + mailboxId + name.length(); 191 192 if (addBody) { 193 message.mText = "body text " + name; 194 message.mHtml = "body html " + name; 195 message.mTextReply = "reply text " + name; 196 message.mHtmlReply = "reply html " + name; 197 message.mSourceKey = 400 + name.length(); 198 message.mIntroText = "intro text " + name; 199 } 200 201 if (saveIt) { 202 message.save(context); 203 } 204 return message; 205 } 206 207 /** 208 * Create a test body 209 * 210 * @param messageId the message this body belongs to 211 * @param textContent the plain text for the body 212 * @param htmlContent the html text for the body 213 * @param saveIt if true, write the new attachment directly to the DB 214 * @param context use this context 215 */ 216 public static Body setupBody(long messageId, String textContent, String htmlContent, 217 boolean saveIt, Context context) { 218 Body body = new Body(); 219 body.mMessageKey = messageId; 220 body.mHtmlContent = htmlContent; 221 body.mTextContent = textContent; 222 body.mSourceKey = messageId + 0x1000; 223 if (saveIt) { 224 body.save(context); 225 } 226 return body; 227 } 228 229 /** 230 * Create a test attachment. A few fields are specified by params, and all other fields 231 * are generated using pseudo-unique values. 232 * 233 * @param messageId the message to attach to 234 * @param fileName the "file" to indicate in the attachment 235 * @param length the "length" of the attachment 236 * @param flags the flags to set in the attachment 237 * @param saveIt if true, write the new attachment directly to the DB 238 * @param context use this context 239 */ 240 public static Attachment setupAttachment(long messageId, String fileName, long length, 241 int flags, boolean saveIt, Context context) { 242 Attachment att = new Attachment(); 243 att.mSize = length; 244 att.mFileName = fileName; 245 att.mContentId = "contentId " + fileName; 246 att.setContentUri("contentUri " + fileName); 247 att.mMessageKey = messageId; 248 att.mMimeType = "mimeType " + fileName; 249 att.mLocation = "location " + fileName; 250 att.mEncoding = "encoding " + fileName; 251 att.mContent = "content " + fileName; 252 att.mFlags = flags; 253 att.mContentBytes = Utility.toUtf8("content " + fileName); 254 att.mAccountKey = messageId + 0x1000; 255 if (saveIt) { 256 att.save(context); 257 } 258 return att; 259 } 260 261 /** 262 * Create a test attachment with flags = 0 (see above) 263 * 264 * @param messageId the message to attach to 265 * @param fileName the "file" to indicate in the attachment 266 * @param length the "length" of the attachment 267 * @param saveIt if true, write the new attachment directly to the DB 268 * @param context use this context 269 */ 270 public static Attachment setupAttachment(long messageId, String fileName, long length, 271 boolean saveIt, Context context) { 272 return setupAttachment(messageId, fileName, length, 0, saveIt, context); 273 } 274 275 private static void assertEmailContentEqual(String caller, EmailContent expect, 276 EmailContent actual) { 277 if (expect == actual) { 278 return; 279 } 280 281 assertEquals(caller + " mId", expect.mId, actual.mId); 282 assertEquals(caller + " mBaseUri", expect.mBaseUri, actual.mBaseUri); 283 } 284 285 /** 286 * Compare two accounts for equality 287 * 288 * TODO: check host auth? 289 */ 290 public static void assertAccountEqual(String caller, Account expect, Account actual) { 291 if (expect == actual) { 292 return; 293 } 294 295 assertEmailContentEqual(caller, expect, actual); 296 assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName); 297 assertEquals(caller + " mEmailAddress", expect.mEmailAddress, actual.mEmailAddress); 298 assertEquals(caller + " mSyncKey", expect.mSyncKey, actual.mSyncKey); 299 300 assertEquals(caller + " mSyncLookback", expect.mSyncLookback, actual.mSyncLookback); 301 assertEquals(caller + " mSyncInterval", expect.mSyncInterval, actual.mSyncInterval); 302 assertEquals(caller + " mHostAuthKeyRecv", expect.mHostAuthKeyRecv, 303 actual.mHostAuthKeyRecv); 304 assertEquals(caller + " mHostAuthKeySend", expect.mHostAuthKeySend, 305 actual.mHostAuthKeySend); 306 assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags); 307 assertEquals(caller + " mCompatibilityUuid", expect.mCompatibilityUuid, 308 actual.mCompatibilityUuid); 309 assertEquals(caller + " mSenderName", expect.mSenderName, actual.mSenderName); 310 assertEquals(caller + " mProtocolVersion", expect.mProtocolVersion, 311 actual.mProtocolVersion); 312 assertEquals(caller + " mNewMessageCount", expect.mNewMessageCount, 313 actual.mNewMessageCount); 314 assertEquals(caller + " mSecuritySyncKey", expect.mSecuritySyncKey, 315 actual.mSecuritySyncKey); 316 assertEquals(caller + " mSignature", expect.mSignature, actual.mSignature); 317 assertEquals(caller + " mPolicyKey", expect.mPolicyKey, actual.mPolicyKey); 318 assertEquals(caller + " mPingDuration", expect.mPingDuration, actual.mPingDuration); 319 } 320 321 /** 322 * Compare two hostauth records for equality 323 */ 324 public static void assertHostAuthEqual(String caller, HostAuth expect, HostAuth actual) { 325 assertHostAuthEqual(caller, expect, actual, true); 326 } 327 328 public static void assertHostAuthEqual(String caller, HostAuth expect, HostAuth actual, 329 boolean testEmailContent) { 330 if (expect == actual) { 331 return; 332 } 333 334 if (testEmailContent) { 335 assertEmailContentEqual(caller, expect, actual); 336 } 337 assertEquals(caller + " mProtocol", expect.mProtocol, actual.mProtocol); 338 assertEquals(caller + " mAddress", expect.mAddress, actual.mAddress); 339 assertEquals(caller + " mPort", expect.mPort, actual.mPort); 340 assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags); 341 assertEquals(caller + " mLogin", expect.mLogin, actual.mLogin); 342 assertEquals(caller + " mPassword", expect.mPassword, actual.mPassword); 343 assertEquals(caller + " mDomain", expect.mDomain, actual.mDomain); 344 // This field is dead and is not checked 345 // assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey); 346 } 347 348 /** 349 * Compare two mailboxes for equality 350 */ 351 public static void assertMailboxEqual(String caller, Mailbox expect, Mailbox actual) { 352 if (expect == actual) { 353 return; 354 } 355 356 assertEmailContentEqual(caller, expect, actual); 357 assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName); 358 assertEquals(caller + " mServerId", expect.mServerId, actual.mServerId); 359 assertEquals(caller + " mParentServerId", expect.mParentServerId, actual.mParentServerId); 360 assertEquals(caller + " mParentKey", expect.mParentKey, actual.mParentKey); 361 assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey); 362 assertEquals(caller + " mType", expect.mType, actual.mType); 363 assertEquals(caller + " mDelimiter", expect.mDelimiter, actual.mDelimiter); 364 assertEquals(caller + " mSyncKey", expect.mSyncKey, actual.mSyncKey); 365 assertEquals(caller + " mSyncLookback", expect.mSyncLookback, actual.mSyncLookback); 366 assertEquals(caller + " mSyncInterval", expect.mSyncInterval, actual.mSyncInterval); 367 assertEquals(caller + " mSyncTime", expect.mSyncTime, actual.mSyncTime); 368 assertEquals(caller + " mFlagVisible", expect.mFlagVisible, actual.mFlagVisible); 369 assertEquals(caller + " mSyncStatus", expect.mSyncStatus, actual.mSyncStatus); 370 assertEquals(caller + " mLastTouchedTime", expect.mLastTouchedTime, actual.mLastTouchedTime); 371 assertEquals(caller + " mUiSyncStatus", expect.mUiSyncStatus, actual.mUiSyncStatus); 372 assertEquals(caller + " mUiLastSyncResult", expect.mUiLastSyncResult, actual.mUiLastSyncResult); 373 assertEquals(caller + " mTotalCount", expect.mTotalCount, actual.mTotalCount); 374 assertEquals(caller + " mHierarchicalName", expect.mHierarchicalName, actual.mHierarchicalName); 375 assertEquals(caller + " mLastFullSyncTime", expect.mLastFullSyncTime, actual.mLastFullSyncTime); 376 } 377 378 /** 379 * Compare two messages for equality 380 * 381 * TODO: body? 382 * TODO: attachments? 383 */ 384 public static void assertMessageEqual(String caller, Message expect, Message actual) { 385 if (expect == actual) { 386 return; 387 } 388 389 assertEmailContentEqual(caller, expect, actual); 390 assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName); 391 assertEquals(caller + " mTimeStamp", expect.mTimeStamp, actual.mTimeStamp); 392 assertEquals(caller + " mSubject", expect.mSubject, actual.mSubject); 393 assertEquals(caller + " mFlagRead = false", expect.mFlagRead, actual.mFlagRead); 394 assertEquals(caller + " mFlagRead = false", expect.mFlagSeen, actual.mFlagSeen); 395 assertEquals(caller + " mFlagLoaded", expect.mFlagLoaded, actual.mFlagLoaded); 396 assertEquals(caller + " mFlagFavorite", expect.mFlagFavorite, actual.mFlagFavorite); 397 assertEquals(caller + " mFlagAttachment", expect.mFlagAttachment, actual.mFlagAttachment); 398 assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags); 399 400 assertEquals(caller + " mServerId", expect.mServerId, actual.mServerId); 401 assertEquals(caller + " mServerTimeStamp", expect.mServerTimeStamp,actual.mServerTimeStamp); 402 assertEquals(caller + " mDraftInfo", expect.mDraftInfo,actual.mDraftInfo); 403 assertEquals(caller + " mMessageId", expect.mMessageId, actual.mMessageId); 404 405 assertEquals(caller + " mMailboxKey", expect.mMailboxKey, actual.mMailboxKey); 406 assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey); 407 assertEquals(caller + " mMainMailboxKey", expect.mMainMailboxKey, actual.mMainMailboxKey); 408 409 assertEquals(caller + " mFrom", expect.mFrom, actual.mFrom); 410 assertEquals(caller + " mTo", expect.mTo, actual.mTo); 411 assertEquals(caller + " mCc", expect.mCc, actual.mCc); 412 assertEquals(caller + " mBcc", expect.mBcc, actual.mBcc); 413 assertEquals(caller + " mReplyTo", expect.mReplyTo, actual.mReplyTo); 414 415 assertEquals(caller + " mMeetingInfo", expect.mMeetingInfo, actual.mMeetingInfo); 416 417 assertEquals(caller + " mSnippet", expect.mSnippet, actual.mSnippet); 418 419 assertEquals(caller + " mProtocolSearchInfo", expect.mProtocolSearchInfo, actual.mProtocolSearchInfo); 420 421 assertEquals(caller + " mThreadTopic", expect.mThreadTopic, actual.mThreadTopic); 422 423 assertEquals(caller + " mSyncData", expect.mSyncData, actual.mSyncData); 424 425 assertEquals(caller + " mSyncData", expect.mServerConversationId, actual.mServerConversationId); 426 427 assertEquals(caller + " mText", expect.mText, actual.mText); 428 assertEquals(caller + " mHtml", expect.mHtml, actual.mHtml); 429 assertEquals(caller + " mTextReply", expect.mTextReply, actual.mTextReply); 430 assertEquals(caller + " mHtmlReply", expect.mHtmlReply, actual.mHtmlReply); 431 assertEquals(caller + " mSourceKey", expect.mSourceKey, actual.mSourceKey); 432 assertEquals(caller + " mIntroText", expect.mIntroText, actual.mIntroText); 433 assertEquals(caller + " mQuotedTextStartPos", expect.mQuotedTextStartPos, actual.mQuotedTextStartPos); 434 } 435 436 /** 437 * Compare to attachments for equality 438 * 439 * TODO: file / content URI mapping? Compare the actual files? 440 */ 441 public static void assertAttachmentEqual(String caller, Attachment expect, Attachment actual) { 442 if (expect == actual) { 443 return; 444 } 445 446 assertEmailContentEqual(caller, expect, actual); 447 assertEquals(caller + " mFileName", expect.mFileName, actual.mFileName); 448 assertEquals(caller + " mMimeType", expect.mMimeType, actual.mMimeType); 449 assertEquals(caller + " mSize", expect.mSize, actual.mSize); 450 assertEquals(caller + " mContentId", expect.mContentId, actual.mContentId); 451 assertEquals(caller + " mContentUri", expect.getContentUri(), actual.getContentUri()); 452 assertEquals(caller + " mCachedFileUri", expect.getCachedFileUri(), actual.getCachedFileUri()); 453 assertEquals(caller + " mMessageKey", expect.mMessageKey, actual.mMessageKey); 454 assertEquals(caller + " mLocation", expect.mLocation, actual.mLocation); 455 assertEquals(caller + " mEncoding", expect.mEncoding, actual.mEncoding); 456 assertEquals(caller + " mContent", expect.mContent, actual.mContent); 457 assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags); 458 MoreAsserts.assertEquals(caller + " mContentBytes", 459 expect.mContentBytes, actual.mContentBytes); 460 assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey); 461 } 462 463 /** 464 * Create a temporary EML file based on {@code msg} in the directory {@code directory}. 465 */ 466 public static Uri createTempEmlFile(Context context, Message msg, File directory) 467 throws Exception { 468 // Write out the message in rfc822 format 469 File outputFile = File.createTempFile("message", "tmp", directory); 470 assertNotNull(outputFile); 471 FileOutputStream outputStream = new FileOutputStream(outputFile); 472 Rfc822Output.writeTo(context, msg, outputStream, true, false, null); 473 outputStream.close(); 474 475 return Uri.fromFile(outputFile); 476 } 477 } 478