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.provider;
18
19import com.android.email.Utility;
20import com.android.email.provider.EmailContent.Account;
21import com.android.email.provider.EmailContent.Attachment;
22import com.android.email.provider.EmailContent.HostAuth;
23import com.android.email.provider.EmailContent.Mailbox;
24import com.android.email.provider.EmailContent.Message;
25
26import android.content.Context;
27import android.test.MoreAsserts;
28
29import junit.framework.Assert;
30
31public class ProviderTestUtils extends Assert {
32
33    /**
34     * No constructor - statics only
35     */
36    private ProviderTestUtils() {
37    }
38
39    /**
40     * Create an account for test purposes
41     */
42    public static Account setupAccount(String name, boolean saveIt, Context context) {
43        Account account = new Account();
44
45        account.mDisplayName = name;
46        account.mEmailAddress = name + "@android.com";
47        account.mSyncKey = "sync-key-" + name;
48        account.mSyncLookback = 1;
49        account.mSyncInterval = EmailContent.Account.CHECK_INTERVAL_NEVER;
50        account.mHostAuthKeyRecv = 0;
51        account.mHostAuthKeySend = 0;
52        account.mFlags = 4;
53        account.mIsDefault = true;
54        account.mCompatibilityUuid = "test-uid-" + name;
55        account.mSenderName = name;
56        account.mRingtoneUri = "content://ringtone-" + name;
57        account.mProtocolVersion = "2.5" + name;
58        account.mNewMessageCount = 5 + name.length();
59        account.mSecurityFlags = 7;
60        account.mSecuritySyncKey = "sec-sync-key-" + name;
61        account.mSignature = "signature-" + name;
62        if (saveIt) {
63            account.save(context);
64        }
65        return account;
66    }
67
68    /**
69     * Create a hostauth record for test purposes
70     */
71    public static HostAuth setupHostAuth(String name, long accountId, boolean saveIt,
72            Context context) {
73        return setupHostAuth("protocol", name, accountId, saveIt, context);
74    }
75
76    /**
77     * Create a hostauth record for test purposes
78     */
79    public static HostAuth setupHostAuth(String protocol, String name, long accountId,
80            boolean saveIt, Context context) {
81        HostAuth hostAuth = new HostAuth();
82
83        hostAuth.mProtocol = protocol + "-" + name;
84        hostAuth.mAddress = "address-" + name;
85        hostAuth.mPort = 100;
86        hostAuth.mFlags = 200;
87        hostAuth.mLogin = "login-" + name;
88        hostAuth.mPassword = "password-" + name;
89        hostAuth.mDomain = "domain-" + name;
90        hostAuth.mAccountKey = accountId;
91
92        if (saveIt) {
93            hostAuth.save(context);
94        }
95        return hostAuth;
96    }
97
98    /**
99     * Create a mailbox for test purposes
100     */
101    public static Mailbox setupMailbox(String name, long accountId, boolean saveIt,
102            Context context) {
103        return setupMailbox(name, accountId, saveIt, context, Mailbox.TYPE_MAIL);
104    }
105
106    public static Mailbox setupMailbox(String name, long accountId, boolean saveIt,
107            Context context, int type) {
108        Mailbox box = new Mailbox();
109
110        box.mDisplayName = name;
111        box.mServerId = "serverid-" + name;
112        box.mParentServerId = "parent-serverid-" + name;
113        box.mAccountKey = accountId;
114        box.mType = type;
115        box.mDelimiter = 1;
116        box.mSyncKey = "sync-key-" + name;
117        box.mSyncLookback = 2;
118        box.mSyncInterval = EmailContent.Account.CHECK_INTERVAL_NEVER;
119        box.mSyncTime = 3;
120        // Should always be saved as zero
121        box.mUnreadCount = 0;
122        box.mFlagVisible = true;
123        box.mFlags = 5;
124        box.mVisibleLimit = 6;
125
126        if (saveIt) {
127            box.save(context);
128        }
129        return box;
130    }
131
132    /**
133     * Create a message for test purposes
134     *
135     * TODO: body
136     * TODO: attachments
137     */
138    public static Message setupMessage(String name, long accountId, long mailboxId,
139            boolean addBody, boolean saveIt, Context context) {
140        Message message = new Message();
141
142        message.mDisplayName = name;
143        message.mTimeStamp = 100 + name.length();
144        message.mSubject = "subject " + name;
145        message.mFlagRead = true;
146        message.mFlagLoaded = Message.FLAG_LOADED_UNLOADED;
147        message.mFlagFavorite = true;
148        message.mFlagAttachment = true;
149        message.mFlags = 0;
150
151        message.mServerId = "serverid " + name;
152        message.mServerTimeStamp = 300 + name.length();
153        message.mClientId = "clientid " + name;
154        message.mMessageId = "messageid " + name;
155
156        message.mMailboxKey = mailboxId;
157        message.mAccountKey = accountId;
158
159        message.mFrom = "from " + name;
160        message.mTo = "to " + name;
161        message.mCc = "cc " + name;
162        message.mBcc = "bcc " + name;
163        message.mReplyTo = "replyto " + name;
164
165        message.mMeetingInfo = "123" + accountId + mailboxId + name.length();
166
167        if (addBody) {
168            message.mText = "body text " + name;
169            message.mHtml = "body html " + name;
170            message.mTextReply = "reply text " + name;
171            message.mHtmlReply = "reply html " + name;
172            message.mSourceKey = 400 + name.length();
173            message.mIntroText = "intro text " + name;
174        }
175
176        if (saveIt) {
177            message.save(context);
178        }
179        return message;
180    }
181
182    /**
183     * Create a test attachment.  A few fields are specified by params, and all other fields
184     * are generated using pseudo-unique values.
185     *
186     * @param messageId the message to attach to
187     * @param fileName the "file" to indicate in the attachment
188     * @param length the "length" of the attachment
189     * @param saveIt if true, write the new attachment directly to the DB
190     * @param context use this context
191     */
192    public static Attachment setupAttachment(long messageId, String fileName, long length,
193            boolean saveIt, Context context) {
194        Attachment att = new Attachment();
195        att.mSize = length;
196        att.mFileName = fileName;
197        att.mContentId = "contentId " + fileName;
198        att.mContentUri = "contentUri " + fileName;
199        att.mMessageKey = messageId;
200        att.mMimeType = "mimeType " + fileName;
201        att.mLocation = "location " + fileName;
202        att.mEncoding = "encoding " + fileName;
203        att.mContent = "content " + fileName;
204        att.mFlags = 0;
205        att.mContentBytes = Utility.toUtf8("content " + fileName);
206        if (saveIt) {
207            att.save(context);
208        }
209        return att;
210    }
211
212    private static void assertEmailContentEqual(String caller, EmailContent expect,
213            EmailContent actual) {
214        if (expect == actual) {
215            return;
216        }
217
218        assertEquals(caller + " mId", expect.mId, actual.mId);
219        assertEquals(caller + " mBaseUri", expect.mBaseUri, actual.mBaseUri);
220    }
221
222    /**
223     * Compare two accounts for equality
224     *
225     * TODO: check host auth?
226     */
227    public static void assertAccountEqual(String caller, Account expect, Account actual) {
228        if (expect == actual) {
229            return;
230        }
231
232        assertEmailContentEqual(caller, expect, actual);
233        assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName);
234        assertEquals(caller + " mEmailAddress", expect.mEmailAddress, actual.mEmailAddress);
235        assertEquals(caller + " mSyncKey", expect.mSyncKey, actual.mSyncKey);
236
237        assertEquals(caller + " mSyncLookback", expect.mSyncLookback, actual.mSyncLookback);
238        assertEquals(caller + " mSyncInterval", expect.mSyncInterval, actual.mSyncInterval);
239        assertEquals(caller + " mHostAuthKeyRecv", expect.mHostAuthKeyRecv,
240                actual.mHostAuthKeyRecv);
241        assertEquals(caller + " mHostAuthKeySend", expect.mHostAuthKeySend,
242                actual.mHostAuthKeySend);
243        assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags);
244        assertEquals(caller + " mIsDefault", expect.mIsDefault, actual.mIsDefault);
245        assertEquals(caller + " mCompatibilityUuid", expect.mCompatibilityUuid,
246                actual.mCompatibilityUuid);
247        assertEquals(caller + " mSenderName", expect.mSenderName, actual.mSenderName);
248        assertEquals(caller + " mRingtoneUri", expect.mRingtoneUri, actual.mRingtoneUri);
249        assertEquals(caller + " mProtocolVersion", expect.mProtocolVersion,
250                actual.mProtocolVersion);
251        assertEquals(caller + " mNewMessageCount", expect.mNewMessageCount,
252                actual.mNewMessageCount);
253        assertEquals(caller + " mSecurityFlags", expect.mSecurityFlags, actual.mSecurityFlags);
254        assertEquals(caller + " mSecuritySyncKey", expect.mSecuritySyncKey,
255                actual.mSecuritySyncKey);
256        assertEquals(caller + " mSignature", expect.mSignature, actual.mSignature);
257    }
258
259    /**
260     * Compare two hostauth records for equality
261     */
262    public static void assertHostAuthEqual(String caller, HostAuth expect, HostAuth actual) {
263        if (expect == actual) {
264            return;
265        }
266
267        assertEmailContentEqual(caller, expect, actual);
268        assertEquals(caller + " mProtocol", expect.mProtocol, actual.mProtocol);
269        assertEquals(caller + " mAddress", expect.mAddress, actual.mAddress);
270        assertEquals(caller + " mPort", expect.mPort, actual.mPort);
271        assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags);
272        assertEquals(caller + " mLogin", expect.mLogin, actual.mLogin);
273        assertEquals(caller + " mPassword", expect.mPassword, actual.mPassword);
274        assertEquals(caller + " mDomain", expect.mDomain, actual.mDomain);
275        // This field is dead and is not checked
276//      assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey);
277    }
278
279    /**
280     * Compare two mailboxes for equality
281     */
282    public static void assertMailboxEqual(String caller, Mailbox expect, Mailbox actual) {
283        if (expect == actual) {
284            return;
285        }
286
287        assertEmailContentEqual(caller, expect, actual);
288        assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName);
289        assertEquals(caller + " mServerId", expect.mServerId, actual.mServerId);
290        assertEquals(caller + " mParentServerId", expect.mParentServerId, actual.mParentServerId);
291        assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey);
292        assertEquals(caller + " mType", expect.mType, actual.mType);
293        assertEquals(caller + " mDelimiter", expect.mDelimiter, actual.mDelimiter);
294        assertEquals(caller + " mSyncKey", expect.mSyncKey, actual.mSyncKey);
295        assertEquals(caller + " mSyncLookback", expect.mSyncLookback, actual.mSyncLookback);
296        assertEquals(caller + " mSyncInterval", expect.mSyncInterval, actual.mSyncInterval);
297        assertEquals(caller + " mSyncTime", expect.mSyncTime, actual.mSyncTime);
298        assertEquals(caller + " mUnreadCount", expect.mUnreadCount, actual.mUnreadCount);
299        assertEquals(caller + " mFlagVisible", expect.mFlagVisible, actual.mFlagVisible);
300        assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags);
301        assertEquals(caller + " mVisibleLimit", expect.mVisibleLimit, actual.mVisibleLimit);
302    }
303
304    /**
305     * Compare two messages for equality
306     *
307     * TODO: body?
308     * TODO: attachments?
309     */
310    public static void assertMessageEqual(String caller, Message expect, Message actual) {
311        if (expect == actual) {
312            return;
313        }
314
315        assertEmailContentEqual(caller, expect, actual);
316        assertEquals(caller + " mDisplayName", expect.mDisplayName, actual.mDisplayName);
317        assertEquals(caller + " mTimeStamp", expect.mTimeStamp, actual.mTimeStamp);
318        assertEquals(caller + " mSubject", expect.mSubject, actual.mSubject);
319        assertEquals(caller + " mFlagRead = false", expect.mFlagRead, actual.mFlagRead);
320        assertEquals(caller + " mFlagLoaded", expect.mFlagLoaded, actual.mFlagLoaded);
321        assertEquals(caller + " mFlagFavorite", expect.mFlagFavorite, actual.mFlagFavorite);
322        assertEquals(caller + " mFlagAttachment", expect.mFlagAttachment, actual.mFlagAttachment);
323        assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags);
324
325        assertEquals(caller + " mServerId", expect.mServerId, actual.mServerId);
326        assertEquals(caller + " mServerTimeStamp", expect.mServerTimeStamp,actual.mServerTimeStamp);
327        assertEquals(caller + " mClientId", expect.mClientId, actual.mClientId);
328        assertEquals(caller + " mMessageId", expect.mMessageId, actual.mMessageId);
329
330        assertEquals(caller + " mMailboxKey", expect.mMailboxKey, actual.mMailboxKey);
331        assertEquals(caller + " mAccountKey", expect.mAccountKey, actual.mAccountKey);
332
333        assertEquals(caller + " mFrom", expect.mFrom, actual.mFrom);
334        assertEquals(caller + " mTo", expect.mTo, actual.mTo);
335        assertEquals(caller + " mCc", expect.mCc, actual.mCc);
336        assertEquals(caller + " mBcc", expect.mBcc, actual.mBcc);
337        assertEquals(caller + " mReplyTo", expect.mReplyTo, actual.mReplyTo);
338
339        assertEquals(caller + " mMeetingInfo", expect.mMeetingInfo, actual.mMeetingInfo);
340
341        assertEquals(caller + " mText", expect.mText, actual.mText);
342        assertEquals(caller + " mHtml", expect.mHtml, actual.mHtml);
343        assertEquals(caller + " mTextReply", expect.mTextReply, actual.mTextReply);
344        assertEquals(caller + " mHtmlReply", expect.mHtmlReply, actual.mHtmlReply);
345        assertEquals(caller + " mSourceKey", expect.mSourceKey, actual.mSourceKey);
346        assertEquals(caller + " mIntroText", expect.mIntroText, actual.mIntroText);
347    }
348
349    /**
350     * Compare to attachments for equality
351     *
352     * TODO: file / content URI mapping?  Compare the actual files?
353     */
354    public static void assertAttachmentEqual(String caller, Attachment expect, Attachment actual) {
355        if (expect == actual) {
356            return;
357        }
358
359        assertEmailContentEqual(caller, expect, actual);
360        assertEquals(caller + " mSize", expect.mSize, actual.mSize);
361        assertEquals(caller + " mFileName", expect.mFileName, actual.mFileName);
362        assertEquals(caller + " mContentId", expect.mContentId, actual.mContentId);
363        assertEquals(caller + " mContentUri", expect.mContentUri, actual.mContentUri);
364        assertEquals(caller + " mMessageKey", expect.mMessageKey, actual.mMessageKey);
365        assertEquals(caller + " mMimeType", expect.mMimeType, actual.mMimeType);
366        assertEquals(caller + " mLocation", expect.mLocation, actual.mLocation);
367        assertEquals(caller + " mEncoding", expect.mEncoding, actual.mEncoding);
368        assertEquals(caller + " mContent", expect.mContent, actual.mContent);
369        assertEquals(caller + " mFlags", expect.mFlags, actual.mFlags);
370        MoreAsserts.assertEquals(caller + " mContentBytes",
371                expect.mContentBytes, actual.mContentBytes);
372    }
373}
374