Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 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 | |
Marc Blank | f3ff0ba | 2011-05-19 14:14:14 -0700 | [diff] [blame] | 19 | import android.accounts.AccountManager; |
| 20 | import android.accounts.AuthenticatorException; |
| 21 | import android.accounts.OperationCanceledException; |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 23 | import android.content.ContentUris; |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 24 | import android.content.ContentValues; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 25 | import android.content.Context; |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 26 | import android.content.ContextWrapper; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 27 | import android.database.Cursor; |
Makoto Onuki | 574854b | 2010-07-30 13:53:59 -0700 | [diff] [blame] | 28 | import android.database.sqlite.SQLiteDatabase; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Andrew Stadler | 4a8c70c | 2009-08-18 12:14:15 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 31 | import android.os.Environment; |
Andrew Stadler | 4a8c70c | 2009-08-18 12:14:15 -0700 | [diff] [blame] | 32 | import android.os.Parcel; |
Makoto Onuki | e37881a | 2010-03-02 13:03:27 -0800 | [diff] [blame] | 33 | import android.test.MoreAsserts; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 34 | import android.test.ProviderTestCase2; |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 35 | import android.test.suitebuilder.annotation.LargeTest; |
| 36 | import android.test.suitebuilder.annotation.MediumTest; |
| 37 | import android.test.suitebuilder.annotation.SmallTest; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 38 | |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 39 | import com.android.email.SecurityPolicy; |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 40 | import com.android.email.provider.EmailProvider.AttachmentService; |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 41 | import com.android.emailcommon.AccountManagerTypes; |
| 42 | import com.android.emailcommon.provider.Account; |
| 43 | import com.android.emailcommon.provider.EmailContent; |
| 44 | import com.android.emailcommon.provider.EmailContent.AccountColumns; |
| 45 | import com.android.emailcommon.provider.EmailContent.Attachment; |
| 46 | import com.android.emailcommon.provider.EmailContent.AttachmentColumns; |
| 47 | import com.android.emailcommon.provider.EmailContent.Body; |
| 48 | import com.android.emailcommon.provider.EmailContent.BodyColumns; |
| 49 | import com.android.emailcommon.provider.EmailContent.MailboxColumns; |
| 50 | import com.android.emailcommon.provider.EmailContent.Message; |
| 51 | import com.android.emailcommon.provider.EmailContent.MessageColumns; |
Marc Blank | 2bdf7ee | 2011-06-30 16:03:36 -0700 | [diff] [blame] | 52 | import com.android.emailcommon.provider.EmailContent.PolicyColumns; |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 53 | import com.android.emailcommon.provider.HostAuth; |
| 54 | import com.android.emailcommon.provider.Mailbox; |
| 55 | import com.android.emailcommon.provider.Policy; |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 56 | import com.android.emailcommon.utility.TextUtilities; |
| 57 | import com.android.emailcommon.utility.Utility; |
| 58 | |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 59 | import java.io.File; |
| 60 | import java.io.IOException; |
| 61 | import java.util.ArrayList; |
| 62 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Tests of the Email provider. |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 65 | * |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 66 | * You can run this entire test case with: |
| 67 | * runtest -c com.android.email.provider.ProviderTests email |
Makoto Onuki | 261d6c3 | 2010-09-14 16:28:50 -0700 | [diff] [blame] | 68 | * |
| 69 | * TODO: Add tests for cursor notification mechanism. (setNotificationUri and notifyChange) |
| 70 | * We can't test the entire notification mechanism with a mock content resolver, because which URI |
| 71 | * to notify when notifyChange() is called is in the actual content resolver. |
| 72 | * Implementing the same mechanism in a mock one is pointless. Instead what we could do is check |
| 73 | * what notification URI each cursor has, and with which URI is notified when |
| 74 | * inserting/updating/deleting. (The former require a new method from AbstractCursor) |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 75 | */ |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 76 | @LargeTest |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 77 | public class ProviderTests extends ProviderTestCase2<EmailProvider> { |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 78 | |
Makoto Onuki | 9d5aaea | 2010-12-02 16:33:49 -0800 | [diff] [blame] | 79 | private EmailProvider mProvider; |
| 80 | private Context mMockContext; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 81 | |
| 82 | public ProviderTests() { |
Marc Blank | 31d9acb | 2011-02-11 15:05:17 -0800 | [diff] [blame] | 83 | super(EmailProvider.class, EmailContent.AUTHORITY); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 84 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 85 | |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 86 | // TODO: move this out to a common place. There are other places that have similar mocks. |
| 87 | /** |
| 88 | * Private context wrapper used to add back getPackageName() for these tests. |
| 89 | */ |
| 90 | private static class MockContext2 extends ContextWrapper { |
| 91 | |
| 92 | private final Context mRealContext; |
| 93 | |
| 94 | public MockContext2(Context mockContext, Context realContext) { |
| 95 | super(mockContext); |
| 96 | mRealContext = realContext; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public Context getApplicationContext() { |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public String getPackageName() { |
| 106 | return mRealContext.getPackageName(); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public Object getSystemService(String name) { |
| 111 | return mRealContext.getSystemService(name); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | private static final AttachmentService MOCK_ATTACHMENT_SERVICE = new AttachmentService() { |
| 116 | @Override |
| 117 | public void attachmentChanged(Context context, long id, int flags) { |
| 118 | // Noop. Don't download attachments. |
| 119 | } |
| 120 | }; |
| 121 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 122 | @Override |
| 123 | public void setUp() throws Exception { |
| 124 | super.setUp(); |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 125 | mMockContext = new MockContext2(getMockContext(), getContext()); |
Makoto Onuki | 9d5aaea | 2010-12-02 16:33:49 -0800 | [diff] [blame] | 126 | mProvider = getProvider(); |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 127 | mProvider.injectAttachmentService(MOCK_ATTACHMENT_SERVICE); |
Marc Blank | 349055a | 2010-11-17 09:47:36 -0800 | [diff] [blame] | 128 | // Invalidate all caches, since we reset the database for each test |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 129 | ContentCache.invalidateAllCaches(); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void tearDown() throws Exception { |
| 134 | super.tearDown(); |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 135 | mProvider.injectAttachmentService(null); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /** |
Andrew Stadler | 0d00889 | 2009-09-22 18:31:10 -0700 | [diff] [blame] | 139 | * TODO: Database upgrade tests |
| 140 | */ |
| 141 | |
Todd Kennedy | 2220877 | 2011-04-22 15:45:11 -0700 | [diff] [blame] | 142 | ////////////////////////////////////////////////////////// |
| 143 | ////// Utility methods |
| 144 | ////////////////////////////////////////////////////////// |
| 145 | |
| 146 | /** Sets the message count of all mailboxes to {@code -1}. */ |
| 147 | private void setMinusOneToMessageCounts() { |
| 148 | ContentValues values = new ContentValues(); |
| 149 | values.put(MailboxColumns.MESSAGE_COUNT, -1); |
| 150 | |
| 151 | // EmailProvider.update() doesn't allow updating messageCount, so directly use the DB. |
| 152 | SQLiteDatabase db = getProvider().getDatabase(mMockContext); |
| 153 | db.update(Mailbox.TABLE_NAME, values, null, null); |
| 154 | } |
| 155 | |
| 156 | /** Returns the number of messages in a mailbox. */ |
| 157 | private int getMessageCount(long mailboxId) { |
| 158 | return Utility.getFirstRowInt(mMockContext, |
| 159 | ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId), |
| 160 | new String[] {MailboxColumns.MESSAGE_COUNT}, null, null, null, 0); |
| 161 | } |
| 162 | |
| 163 | /** Creates a new message. */ |
| 164 | private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read, |
| 165 | int flagLoaded) { |
| 166 | Message message = ProviderTestUtils.setupMessage( |
| 167 | "1", b.mAccountKey, b.mId, true, false, c, starred, read); |
| 168 | message.mFlagLoaded = flagLoaded; |
| 169 | message.save(c); |
| 170 | return message; |
| 171 | } |
| 172 | |
| 173 | ////////////////////////////////////////////////////////// |
| 174 | ////// The tests |
| 175 | ////////////////////////////////////////////////////////// |
| 176 | |
Andrew Stadler | 0d00889 | 2009-09-22 18:31:10 -0700 | [diff] [blame] | 177 | /** |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 178 | * Test simple account save/retrieve |
| 179 | */ |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 180 | @SmallTest |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 181 | public void testAccountSave() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 182 | Account account1 = ProviderTestUtils.setupAccount("account-save", true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 183 | long account1Id = account1.mId; |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 184 | |
Marc Blank | f5418f1 | 2011-06-13 15:32:27 -0700 | [diff] [blame] | 185 | Account account2 = Account.restoreAccountWithId(mMockContext, account1Id); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 186 | |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 187 | ProviderTestUtils.assertAccountEqual("testAccountSave", account1, account2); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 188 | } |
satok | 41f775b | 2009-07-22 09:58:08 +0900 | [diff] [blame] | 189 | |
Andrew Stadler | 4a8c70c | 2009-08-18 12:14:15 -0700 | [diff] [blame] | 190 | /** |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 191 | * Test simple account save/retrieve with predefined hostauth records |
| 192 | */ |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 193 | @SmallTest |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 194 | public void testAccountSaveHostAuth() { |
| 195 | Account account1 = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext); |
| 196 | // add hostauth data, which should be saved the first time |
| 197 | account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-hostauth-recv", -1, false, |
| 198 | mMockContext); |
| 199 | account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-hostauth-send", -1, false, |
| 200 | mMockContext); |
| 201 | account1.save(mMockContext); |
| 202 | long account1Id = account1.mId; |
| 203 | |
| 204 | // Confirm account reads back correctly |
Marc Blank | f5418f1 | 2011-06-13 15:32:27 -0700 | [diff] [blame] | 205 | Account account1get = Account.restoreAccountWithId(mMockContext, account1Id); |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 206 | ProviderTestUtils.assertAccountEqual("testAccountSave", account1, account1get); |
| 207 | |
| 208 | // Confirm hostauth fields can be accessed & read back correctly |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 209 | HostAuth hostAuth1get = HostAuth.restoreHostAuthWithId(mMockContext, |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 210 | account1get.mHostAuthKeyRecv); |
| 211 | ProviderTestUtils.assertHostAuthEqual("testAccountSaveHostAuth-recv", |
| 212 | account1.mHostAuthRecv, hostAuth1get); |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 213 | HostAuth hostAuth2get = HostAuth.restoreHostAuthWithId(mMockContext, |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 214 | account1get.mHostAuthKeySend); |
| 215 | ProviderTestUtils.assertHostAuthEqual("testAccountSaveHostAuth-send", |
| 216 | account1.mHostAuthSend, hostAuth2get); |
| 217 | } |
| 218 | |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 219 | public void testAccountGetHostAuthSend() { |
| 220 | Account account = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext); |
| 221 | account.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-hostauth-send", -1, false, |
| 222 | mMockContext); |
| 223 | account.save(mMockContext); |
| 224 | HostAuth authGet; |
| 225 | HostAuth authTest; |
| 226 | |
| 227 | authTest = account.mHostAuthSend; |
| 228 | assertNotNull(authTest); |
| 229 | assertTrue(account.mHostAuthKeySend != 0); |
| 230 | |
| 231 | // HostAuth is not changed |
| 232 | authGet = account.getOrCreateHostAuthSend(mMockContext); |
| 233 | assertTrue(authGet == authTest); // return the same object |
| 234 | |
| 235 | // New HostAuth; based upon mHostAuthKeyRecv |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 236 | authTest = HostAuth.restoreHostAuthWithId(mMockContext, |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 237 | account.mHostAuthKeySend); |
| 238 | account.mHostAuthSend = null; |
| 239 | authGet = account.getOrCreateHostAuthSend(mMockContext); |
| 240 | assertNotNull(authGet); |
| 241 | assertNotNull(account.mHostAuthSend); |
| 242 | ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthSend-1", authTest, authGet); |
| 243 | |
| 244 | // New HostAuth; completely empty |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 245 | authTest = new HostAuth(); |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 246 | account.mHostAuthSend = null; |
| 247 | account.mHostAuthKeySend = 0; |
| 248 | authGet = account.getOrCreateHostAuthSend(mMockContext); |
| 249 | assertNotNull(authGet); |
| 250 | assertNotNull(account.mHostAuthSend); |
| 251 | ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthSendv-2", authTest, authGet); |
| 252 | } |
| 253 | |
| 254 | public void testAccountGetHostAuthRecv() { |
| 255 | Account account = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext); |
| 256 | account.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-hostauth-recv", -1, false, |
| 257 | mMockContext); |
| 258 | account.save(mMockContext); |
| 259 | HostAuth authGet; |
| 260 | HostAuth authTest; |
| 261 | |
| 262 | authTest = account.mHostAuthRecv; |
| 263 | assertNotNull(authTest); |
| 264 | assertTrue(account.mHostAuthKeyRecv != 0); |
| 265 | |
| 266 | // HostAuth is not changed |
| 267 | authGet = account.getOrCreateHostAuthRecv(mMockContext); |
| 268 | assertTrue(authGet == authTest); // return the same object |
| 269 | |
| 270 | // New HostAuth; based upon mHostAuthKeyRecv |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 271 | authTest = HostAuth.restoreHostAuthWithId(mMockContext, |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 272 | account.mHostAuthKeyRecv); |
| 273 | account.mHostAuthRecv = null; |
| 274 | authGet = account.getOrCreateHostAuthRecv(mMockContext); |
| 275 | assertNotNull(authGet); |
| 276 | assertNotNull(account.mHostAuthRecv); |
| 277 | ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthRecv-1", authTest, authGet); |
| 278 | |
| 279 | // New HostAuth; completely empty |
Ben Komalo | 12b82d9 | 2011-05-19 15:18:12 -0700 | [diff] [blame] | 280 | authTest = new HostAuth(); |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 281 | account.mHostAuthRecv = null; |
| 282 | account.mHostAuthKeyRecv = 0; |
| 283 | authGet = account.getOrCreateHostAuthRecv(mMockContext); |
| 284 | assertNotNull(authGet); |
| 285 | assertNotNull(account.mHostAuthRecv); |
| 286 | ProviderTestUtils.assertHostAuthEqual("testAccountGetHostAuthRecv-2", authTest, authGet); |
| 287 | } |
| 288 | |
Andrew Stadler | da8836a | 2009-09-15 14:10:12 -0700 | [diff] [blame] | 289 | /** |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 290 | * Simple test of account parceling. The rather torturous path is to ensure that the |
Andrew Stadler | 4a8c70c | 2009-08-18 12:14:15 -0700 | [diff] [blame] | 291 | * account is really flattened all the way down to a parcel and back. |
| 292 | */ |
| 293 | public void testAccountParcel() { |
| 294 | Account account1 = ProviderTestUtils.setupAccount("parcel", false, mMockContext); |
| 295 | Bundle b = new Bundle(); |
| 296 | b.putParcelable("account", account1); |
| 297 | Parcel p = Parcel.obtain(); |
| 298 | b.writeToParcel(p, 0); |
| 299 | p.setDataPosition(0); // rewind it for reading |
| 300 | Bundle b2 = new Bundle(Account.class.getClassLoader()); |
| 301 | b2.readFromParcel(p); |
| 302 | Account account2 = (Account) b2.getParcelable("account"); |
| 303 | p.recycle(); |
| 304 | |
| 305 | ProviderTestUtils.assertAccountEqual("testAccountParcel", account1, account2); |
| 306 | } |
| 307 | |
Makoto Onuki | e37881a | 2010-03-02 13:03:27 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Test for {@link Account#getShortcutSafeUri()} and |
Todd Kennedy | 040ddf6 | 2011-02-04 09:29:13 -0800 | [diff] [blame] | 310 | * {@link Account#getAccountIdFromShortcutSafeUri}. |
Makoto Onuki | e37881a | 2010-03-02 13:03:27 -0800 | [diff] [blame] | 311 | */ |
| 312 | public void testAccountShortcutSafeUri() { |
| 313 | final Account account1 = ProviderTestUtils.setupAccount("account-1", true, mMockContext); |
| 314 | final Account account2 = ProviderTestUtils.setupAccount("account-2", true, mMockContext); |
| 315 | final long account1Id = account1.mId; |
| 316 | final long account2Id = account2.mId; |
| 317 | |
| 318 | final Uri uri1 = account1.getShortcutSafeUri(); |
| 319 | final Uri uri2 = account2.getShortcutSafeUri(); |
| 320 | |
| 321 | // Check the path part of the URIs. |
| 322 | MoreAsserts.assertEquals(new String[] {"account", account1.mCompatibilityUuid}, |
| 323 | uri1.getPathSegments().toArray()); |
| 324 | MoreAsserts.assertEquals(new String[] {"account", account2.mCompatibilityUuid}, |
| 325 | uri2.getPathSegments().toArray()); |
| 326 | |
| 327 | assertEquals(account1Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, uri1)); |
| 328 | assertEquals(account2Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, uri2)); |
| 329 | |
| 330 | // Test for the Eclair(2.0-2.1) style URI. |
| 331 | assertEquals(account1Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, |
| 332 | getEclairStyleShortcutUri(account1))); |
| 333 | assertEquals(account2Id, Account.getAccountIdFromShortcutSafeUri(mMockContext, |
| 334 | getEclairStyleShortcutUri(account2))); |
| 335 | } |
| 336 | |
| 337 | private static Uri getEclairStyleShortcutUri(Account account) { |
| 338 | // We used _id instead of UUID only on Eclair(2.0-2.1). |
| 339 | return Account.CONTENT_URI.buildUpon().appendEncodedPath("" + account.mId).build(); |
| 340 | } |
| 341 | |
Marc Blank | 8d8f86e | 2010-06-25 11:28:07 -0700 | [diff] [blame] | 342 | public void testGetProtocol() { |
| 343 | Account account1 = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext); |
| 344 | // add hostauth data, with protocol |
Todd Kennedy | fe68c0e | 2011-02-17 10:27:31 -0800 | [diff] [blame] | 345 | account1.mHostAuthRecv = ProviderTestUtils.setupHostAuth("eas", "account-hostauth-recv", |
Marc Blank | 8d8f86e | 2010-06-25 11:28:07 -0700 | [diff] [blame] | 346 | false, mMockContext); |
| 347 | // Note that getProtocol uses the receive host auth, so the protocol here shouldn't matter |
| 348 | // to the test result |
Todd Kennedy | fe68c0e | 2011-02-17 10:27:31 -0800 | [diff] [blame] | 349 | account1.mHostAuthSend = ProviderTestUtils.setupHostAuth("foo", "account-hostauth-send", |
Marc Blank | 8d8f86e | 2010-06-25 11:28:07 -0700 | [diff] [blame] | 350 | false, mMockContext); |
| 351 | account1.save(mMockContext); |
Makoto Onuki | 25144e2 | 2010-08-26 15:08:19 -0700 | [diff] [blame] | 352 | assertEquals("eas", Account.getProtocol(mMockContext, account1.mId)); |
| 353 | assertEquals("eas", account1.getProtocol(mMockContext)); |
Marc Blank | 8d8f86e | 2010-06-25 11:28:07 -0700 | [diff] [blame] | 354 | Account account2 = ProviderTestUtils.setupAccount("account-nohostauth", false, |
| 355 | mMockContext); |
| 356 | account2.save(mMockContext); |
| 357 | // Make sure that we return null when there's no host auth |
| 358 | assertNull(Account.getProtocol(mMockContext, account2.mId)); |
| 359 | assertNull(account2.getProtocol(mMockContext)); |
| 360 | // And when there's no account |
| 361 | assertNull(Account.getProtocol(mMockContext, 0)); |
| 362 | } |
| 363 | |
Makoto Onuki | 652be6f | 2010-03-02 17:49:47 -0800 | [diff] [blame] | 364 | public void testAccountIsValidId() { |
| 365 | final Account account1 = ProviderTestUtils.setupAccount("account-1", true, mMockContext); |
| 366 | final Account account2 = ProviderTestUtils.setupAccount("account-2", true, mMockContext); |
| 367 | |
| 368 | assertTrue(Account.isValidId(mMockContext, account1.mId)); |
| 369 | assertTrue(Account.isValidId(mMockContext, account2.mId)); |
| 370 | |
| 371 | assertFalse(Account.isValidId(mMockContext, 1234567)); // Some random ID |
Makoto Onuki | c133e6f | 2010-03-05 15:04:11 -0800 | [diff] [blame] | 372 | assertFalse(Account.isValidId(mMockContext, -1)); |
| 373 | assertFalse(Account.isValidId(mMockContext, -500)); |
Makoto Onuki | 652be6f | 2010-03-02 17:49:47 -0800 | [diff] [blame] | 374 | } |
| 375 | |
satok | 41f775b | 2009-07-22 09:58:08 +0900 | [diff] [blame] | 376 | private final static String[] MAILBOX_UNREAD_COUNT_PROJECTION = new String [] { |
| 377 | MailboxColumns.UNREAD_COUNT |
| 378 | }; |
| 379 | private final static int MAILBOX_UNREAD_COUNT_COLMUN = 0; |
| 380 | |
| 381 | /** |
| 382 | * Get the value of the unread count in the mailbox of the account. |
| 383 | * This can be different from the actual number of unread messages in that mailbox. |
satok | 41f775b | 2009-07-22 09:58:08 +0900 | [diff] [blame] | 384 | */ |
| 385 | private int getUnreadCount(long mailboxId) { |
| 386 | String text = null; |
| 387 | Cursor c = null; |
| 388 | try { |
| 389 | c = mMockContext.getContentResolver().query( |
| 390 | Mailbox.CONTENT_URI, |
| 391 | MAILBOX_UNREAD_COUNT_PROJECTION, |
| 392 | EmailContent.RECORD_ID + "=?", |
| 393 | new String[] { String.valueOf(mailboxId) }, |
| 394 | null); |
| 395 | c.moveToFirst(); |
| 396 | text = c.getString(MAILBOX_UNREAD_COUNT_COLMUN); |
| 397 | } finally { |
| 398 | c.close(); |
| 399 | } |
| 400 | return Integer.valueOf(text); |
| 401 | } |
| 402 | |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 403 | private static String[] expectedAttachmentNames = |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 404 | new String[] {"attachment1.doc", "attachment2.xls", "attachment3"}; |
| 405 | // The lengths need to be kept in ascending order |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 406 | private static long[] expectedAttachmentSizes = new long[] {31415L, 97701L, 151213L}; |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 407 | |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 408 | /* |
| 409 | * Returns null if the message has no body. |
| 410 | */ |
| 411 | private Body loadBodyForMessageId(long messageId) { |
| 412 | Cursor c = null; |
| 413 | try { |
| 414 | c = mMockContext.getContentResolver().query( |
| 415 | EmailContent.Body.CONTENT_URI, |
| 416 | EmailContent.Body.CONTENT_PROJECTION, |
| 417 | EmailContent.Body.MESSAGE_KEY + "=?", |
| 418 | new String[] {String.valueOf(messageId)}, |
| 419 | null); |
| 420 | int numBodies = c.getCount(); |
| 421 | assertTrue("at most one body", numBodies < 2); |
| 422 | return c.moveToFirst() ? EmailContent.getContent(c, Body.class) : null; |
| 423 | } finally { |
| 424 | c.close(); |
| 425 | } |
| 426 | } |
| 427 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 428 | /** |
| 429 | * Test simple message save/retrieve |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 430 | * |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 431 | * TODO: serverId vs. serverIntId |
| 432 | */ |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 433 | @MediumTest |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 434 | public void testMessageSave() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 435 | Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 436 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 437 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 438 | long box1Id = box1.mId; |
| 439 | |
| 440 | // Test a simple message (saved with no body) |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 441 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 442 | true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 443 | long message1Id = message1.mId; |
| 444 | Message message1get = EmailContent.Message.restoreMessageWithId(mMockContext, message1Id); |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 445 | ProviderTestUtils.assertMessageEqual("testMessageSave", message1, message1get); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 446 | |
| 447 | // Test a message saved with a body |
| 448 | // Note that it will read back w/o the text & html so we must extract those |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 449 | Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 450 | true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 451 | long message2Id = message2.mId; |
| 452 | String text2 = message2.mText; |
| 453 | String html2 = message2.mHtml; |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 454 | String textReply2 = message2.mTextReply; |
| 455 | String htmlReply2 = message2.mHtmlReply; |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 456 | long sourceKey2 = message2.mSourceKey; |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 457 | String introText2 = message2.mIntroText; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 458 | message2.mText = null; |
| 459 | message2.mHtml = null; |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 460 | message2.mTextReply = null; |
| 461 | message2.mHtmlReply = null; |
Marc Blank | e256983 | 2009-09-07 16:03:02 -0700 | [diff] [blame] | 462 | message2.mSourceKey = 0; |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 463 | message2.mIntroText = null; |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 464 | Message message2get = EmailContent.Message.restoreMessageWithId(mMockContext, message2Id); |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 465 | ProviderTestUtils.assertMessageEqual("testMessageSave", message2, message2get); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 466 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 467 | // Now see if there's a body saved with the right stuff |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 468 | Body body2 = loadBodyForMessageId(message2Id); |
| 469 | assertEquals("body text", text2, body2.mTextContent); |
| 470 | assertEquals("body html", html2, body2.mHtmlContent); |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 471 | assertEquals("reply text", textReply2, body2.mTextReply); |
| 472 | assertEquals("reply html", htmlReply2, body2.mHtmlReply); |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 473 | assertEquals("source key", sourceKey2, body2.mSourceKey); |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 474 | assertEquals("intro text", introText2, body2.mIntroText); |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | @MediumTest |
| 478 | public void testMessageWithAttachment() { |
| 479 | Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext); |
| 480 | long account1Id = account1.mId; |
| 481 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 482 | long box1Id = box1.mId; |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 483 | |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 484 | // Message with attachments and body |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 485 | Message message3 = ProviderTestUtils.setupMessage("message3", account1Id, box1Id, true, |
| 486 | false, mMockContext); |
| 487 | ArrayList<Attachment> atts = new ArrayList<Attachment>(); |
| 488 | for (int i = 0; i < 3; i++) { |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 489 | atts.add(ProviderTestUtils.setupAttachment( |
| 490 | -1, expectedAttachmentNames[i], expectedAttachmentSizes[i], |
| 491 | false, mMockContext)); |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 492 | } |
| 493 | message3.mAttachments = atts; |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 494 | message3.save(mMockContext); |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 495 | long message3Id = message3.mId; |
| 496 | |
| 497 | // Now check the attachments; there should be three and they should match name and size |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 498 | Cursor c = null; |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 499 | try { |
| 500 | // Note that there is NO guarantee of the order of returned records in the general case, |
| 501 | // so we specifically ask for ordering by size. The expectedAttachmentSizes array must |
| 502 | // be kept sorted by size (ascending) for this test to work properly |
| 503 | c = mMockContext.getContentResolver().query( |
| 504 | Attachment.CONTENT_URI, |
| 505 | Attachment.CONTENT_PROJECTION, |
| 506 | Attachment.MESSAGE_KEY + "=?", |
| 507 | new String[] { |
| 508 | String.valueOf(message3Id) |
| 509 | }, |
| 510 | Attachment.SIZE); |
| 511 | int numAtts = c.getCount(); |
| 512 | assertEquals(3, numAtts); |
| 513 | int i = 0; |
| 514 | while (c.moveToNext()) { |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 515 | Attachment actual = EmailContent.getContent(c, Attachment.class); |
| 516 | ProviderTestUtils.assertAttachmentEqual("save-message3", atts.get(i), actual); |
| 517 | i++; |
| 518 | } |
| 519 | } finally { |
| 520 | c.close(); |
| 521 | } |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | |
| 525 | @MediumTest |
| 526 | public void testMessageSaveWithJustAttachments() { |
| 527 | Account account1 = ProviderTestUtils.setupAccount("message-save", true, mMockContext); |
| 528 | long account1Id = account1.mId; |
| 529 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 530 | long box1Id = box1.mId; |
| 531 | Cursor c = null; |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 532 | |
| 533 | // Message with attachments but no body |
| 534 | Message message4 = ProviderTestUtils.setupMessage("message4", account1Id, box1Id, false, |
| 535 | false, mMockContext); |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 536 | ArrayList<Attachment> atts = new ArrayList<Attachment>(); |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 537 | for (int i = 0; i < 3; i++) { |
| 538 | atts.add(ProviderTestUtils.setupAttachment( |
| 539 | -1, expectedAttachmentNames[i], expectedAttachmentSizes[i], |
| 540 | false, mMockContext)); |
| 541 | } |
| 542 | message4.mAttachments = atts; |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 543 | message4.save(mMockContext); |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 544 | long message4Id = message4.mId; |
| 545 | |
| 546 | // Now check the attachments; there should be three and they should match name and size |
| 547 | c = null; |
satok | 41f775b | 2009-07-22 09:58:08 +0900 | [diff] [blame] | 548 | |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 549 | try { |
| 550 | // Note that there is NO guarantee of the order of returned records in the general case, |
| 551 | // so we specifically ask for ordering by size. The expectedAttachmentSizes array must |
| 552 | // be kept sorted by size (ascending) for this test to work properly |
| 553 | c = mMockContext.getContentResolver().query( |
| 554 | Attachment.CONTENT_URI, |
| 555 | Attachment.CONTENT_PROJECTION, |
| 556 | Attachment.MESSAGE_KEY + "=?", |
| 557 | new String[] { |
| 558 | String.valueOf(message4Id) |
| 559 | }, |
| 560 | Attachment.SIZE); |
| 561 | int numAtts = c.getCount(); |
| 562 | assertEquals(3, numAtts); |
| 563 | int i = 0; |
| 564 | while (c.moveToNext()) { |
| 565 | Attachment actual = EmailContent.getContent(c, Attachment.class); |
| 566 | ProviderTestUtils.assertAttachmentEqual("save-message4", atts.get(i), actual); |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 567 | i++; |
| 568 | } |
| 569 | } finally { |
| 570 | c.close(); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 571 | } |
Mihai Preda | 44144d0 | 2009-08-18 18:55:59 +0200 | [diff] [blame] | 572 | |
| 573 | // test EmailContent.restoreAttachmentsWitdMessageId() |
| 574 | Attachment[] attachments = |
| 575 | Attachment.restoreAttachmentsWithMessageId(mMockContext, message4Id); |
| 576 | int size = attachments.length; |
| 577 | assertEquals(3, size); |
| 578 | for (int i = 0; i < size; ++i) { |
| 579 | ProviderTestUtils.assertAttachmentEqual("save-message4", atts.get(i), attachments[i]); |
| 580 | } |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 581 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 582 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 583 | /** |
Marc Blank | e7b9e4a | 2010-09-01 19:06:15 -0700 | [diff] [blame] | 584 | * Test that saving a message creates the proper snippet for that message |
| 585 | */ |
| 586 | public void testMessageSaveAddsSnippet() { |
| 587 | Account account = ProviderTestUtils.setupAccount("message-snippet", true, mMockContext); |
| 588 | Mailbox box = ProviderTestUtils.setupMailbox("box1", account.mId, true, mMockContext); |
| 589 | |
| 590 | // Create a message without a body, unsaved |
| 591 | Message message = ProviderTestUtils.setupMessage("message", account.mId, box.mId, false, |
| 592 | false, mMockContext); |
| 593 | message.mText = "This is some text"; |
| 594 | message.mHtml = "<html>This is some text</html>"; |
| 595 | message.save(mMockContext); |
| 596 | Message restoredMessage = Message.restoreMessageWithId(mMockContext, message.mId); |
| 597 | // We should have the plain text as the snippet |
Marc Blank | 76f6147 | 2011-03-26 19:19:35 -0700 | [diff] [blame] | 598 | assertEquals(restoredMessage.mSnippet, |
| 599 | TextUtilities.makeSnippetFromPlainText(message.mText)); |
Marc Blank | e7b9e4a | 2010-09-01 19:06:15 -0700 | [diff] [blame] | 600 | |
| 601 | // Start again |
| 602 | message = ProviderTestUtils.setupMessage("message", account.mId, box.mId, false, |
| 603 | false, mMockContext); |
| 604 | message.mText = null; |
| 605 | message.mHtml = "<html>This is some text</html>"; |
| 606 | message.save(mMockContext); |
| 607 | restoredMessage = Message.restoreMessageWithId(mMockContext, message.mId); |
| 608 | // We should have the plain text as the snippet |
Marc Blank | 76f6147 | 2011-03-26 19:19:35 -0700 | [diff] [blame] | 609 | assertEquals(restoredMessage.mSnippet, |
| 610 | TextUtilities.makeSnippetFromHtmlText(message.mHtml)); |
Marc Blank | e7b9e4a | 2010-09-01 19:06:15 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 614 | * TODO: update account |
| 615 | */ |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 616 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 617 | /** |
| 618 | * TODO: update mailbox |
| 619 | */ |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 620 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 621 | /** |
| 622 | * TODO: update message |
| 623 | */ |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 624 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 625 | /** |
| 626 | * Test delete account |
| 627 | * TODO: hostauth |
| 628 | */ |
| 629 | public void testAccountDelete() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 630 | Account account1 = ProviderTestUtils.setupAccount("account-delete-1", true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 631 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 632 | Account account2 = ProviderTestUtils.setupAccount("account-delete-2", true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 633 | long account2Id = account2.mId; |
| 634 | |
| 635 | // make sure there are two accounts |
| 636 | int numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null); |
| 637 | assertEquals(2, numBoxes); |
| 638 | |
| 639 | // now delete one of them |
| 640 | Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id); |
| 641 | mMockContext.getContentResolver().delete(uri, null, null); |
| 642 | |
| 643 | // make sure there's only one account now |
| 644 | numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null); |
| 645 | assertEquals(1, numBoxes); |
| 646 | |
| 647 | // now delete the other one |
| 648 | uri = ContentUris.withAppendedId(Account.CONTENT_URI, account2Id); |
| 649 | mMockContext.getContentResolver().delete(uri, null, null); |
| 650 | |
| 651 | // make sure there are no accounts now |
| 652 | numBoxes = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null); |
| 653 | assertEquals(0, numBoxes); |
| 654 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 655 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 656 | /** |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 657 | * Test for Body.lookupBodyIdWithMessageId() |
| 658 | * Verifies that: |
| 659 | * - for a message without body, -1 is returned. |
| 660 | * - for a mesage with body, the id matches the one from loadBodyForMessageId. |
| 661 | */ |
| 662 | public void testLookupBodyIdWithMessageId() { |
| 663 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 664 | Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext); |
| 665 | long account1Id = account1.mId; |
| 666 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 667 | long box1Id = box1.mId; |
| 668 | |
| 669 | // 1. create message with no body, check that returned bodyId is -1 |
| 670 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 671 | true, mMockContext); |
| 672 | long message1Id = message1.mId; |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 673 | long bodyId1 = Body.lookupBodyIdWithMessageId(mMockContext, message1Id); |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 674 | assertEquals(bodyId1, -1); |
| 675 | |
| 676 | // 2. create message with body, check that returned bodyId is correct |
| 677 | Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 678 | true, mMockContext); |
| 679 | long message2Id = message2.mId; |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 680 | long bodyId2 = Body.lookupBodyIdWithMessageId(mMockContext, message2Id); |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 681 | Body body = loadBodyForMessageId(message2Id); |
| 682 | assertNotNull(body); |
| 683 | assertEquals(body.mId, bodyId2); |
| 684 | } |
| 685 | |
| 686 | /** |
| 687 | * Test for Body.updateBodyWithMessageId(). |
| 688 | * 1. - create message without body, |
| 689 | * - update its body (set TEXT_CONTENT) |
| 690 | * - check correct updated body is read back |
| 691 | * |
| 692 | * 2. - create message with body, |
| 693 | * - update body (set TEXT_CONTENT) |
| 694 | * - check correct updated body is read back |
| 695 | */ |
| 696 | public void testUpdateBodyWithMessageId() { |
| 697 | Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext); |
| 698 | long account1Id = account1.mId; |
| 699 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 700 | long box1Id = box1.mId; |
| 701 | |
| 702 | final String textContent = "foobar some odd text"; |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 703 | final String htmlContent = "and some html"; |
| 704 | final String textReply = "plain text reply"; |
| 705 | final String htmlReply = "or the html reply"; |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 706 | final String introText = "fred wrote:"; |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 707 | |
| 708 | ContentValues values = new ContentValues(); |
| 709 | values.put(BodyColumns.TEXT_CONTENT, textContent); |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 710 | values.put(BodyColumns.HTML_CONTENT, htmlContent); |
| 711 | values.put(BodyColumns.TEXT_REPLY, textReply); |
| 712 | values.put(BodyColumns.HTML_REPLY, htmlReply); |
Marc Blank | e256983 | 2009-09-07 16:03:02 -0700 | [diff] [blame] | 713 | values.put(BodyColumns.SOURCE_MESSAGE_KEY, 17); |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 714 | values.put(BodyColumns.INTRO_TEXT, introText); |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 715 | |
| 716 | // 1 |
| 717 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 718 | true, mMockContext); |
| 719 | long message1Id = message1.mId; |
| 720 | Body body1 = loadBodyForMessageId(message1Id); |
| 721 | assertNull(body1); |
| 722 | Body.updateBodyWithMessageId(mMockContext, message1Id, values); |
| 723 | body1 = loadBodyForMessageId(message1Id); |
| 724 | assertNotNull(body1); |
| 725 | assertEquals(body1.mTextContent, textContent); |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 726 | assertEquals(body1.mHtmlContent, htmlContent); |
| 727 | assertEquals(body1.mTextReply, textReply); |
| 728 | assertEquals(body1.mHtmlReply, htmlReply); |
Marc Blank | e256983 | 2009-09-07 16:03:02 -0700 | [diff] [blame] | 729 | assertEquals(body1.mSourceKey, 17); |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 730 | assertEquals(body1.mIntroText, introText); |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 731 | |
| 732 | // 2 |
| 733 | Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 734 | true, mMockContext); |
| 735 | long message2Id = message2.mId; |
| 736 | Body body2 = loadBodyForMessageId(message2Id); |
| 737 | assertNotNull(body2); |
| 738 | assertTrue(!body2.mTextContent.equals(textContent)); |
| 739 | Body.updateBodyWithMessageId(mMockContext, message2Id, values); |
| 740 | body2 = loadBodyForMessageId(message1Id); |
| 741 | assertNotNull(body2); |
| 742 | assertEquals(body2.mTextContent, textContent); |
Andrew Stadler | 936babc | 2009-09-01 23:19:12 -0700 | [diff] [blame] | 743 | assertEquals(body2.mHtmlContent, htmlContent); |
| 744 | assertEquals(body2.mTextReply, textReply); |
| 745 | assertEquals(body2.mHtmlReply, htmlReply); |
Marc Blank | e256983 | 2009-09-07 16:03:02 -0700 | [diff] [blame] | 746 | assertEquals(body2.mSourceKey, 17); |
Marc Blank | 5fc57ec | 2009-09-22 18:38:28 -0700 | [diff] [blame] | 747 | assertEquals(body2.mIntroText, introText); |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | /** |
Andrew Stadler | c41c47f | 2009-09-25 14:54:32 -0700 | [diff] [blame] | 751 | * Test body retrieve methods |
| 752 | */ |
| 753 | public void testBodyRetrieve() { |
| 754 | // No account needed |
| 755 | // No mailbox needed |
| 756 | Message message1 = ProviderTestUtils.setupMessage("bodyretrieve", 1, 1, true, |
| 757 | true, mMockContext); |
| 758 | long messageId = message1.mId; |
| 759 | |
| 760 | assertEquals(message1.mText, |
| 761 | Body.restoreBodyTextWithMessageId(mMockContext, messageId)); |
| 762 | assertEquals(message1.mHtml, |
| 763 | Body.restoreBodyHtmlWithMessageId(mMockContext, messageId)); |
| 764 | assertEquals(message1.mTextReply, |
| 765 | Body.restoreReplyTextWithMessageId(mMockContext, messageId)); |
| 766 | assertEquals(message1.mHtmlReply, |
| 767 | Body.restoreReplyHtmlWithMessageId(mMockContext, messageId)); |
| 768 | assertEquals(message1.mIntroText, |
| 769 | Body.restoreIntroTextWithMessageId(mMockContext, messageId)); |
Mihai Preda | 1575e78 | 2010-02-15 14:56:40 +0100 | [diff] [blame] | 770 | assertEquals(message1.mSourceKey, |
| 771 | Body.restoreBodySourceKey(mMockContext, messageId)); |
Andrew Stadler | c41c47f | 2009-09-25 14:54:32 -0700 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /** |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 775 | * Test delete body. |
| 776 | * 1. create message without body (message id 1) |
| 777 | * 2. create message with body (message id 2. The body has _id 1 and messageKey 2). |
| 778 | * 3. delete first message. |
| 779 | * 4. verify that body for message 2 has not been deleted. |
| 780 | * 5. delete message 2, verify body is deleted. |
| 781 | */ |
| 782 | public void testDeleteBody() { |
| 783 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 784 | |
| 785 | // Create account and mailboxes |
| 786 | Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext); |
| 787 | long account1Id = account1.mId; |
| 788 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 789 | long box1Id = box1.mId; |
| 790 | |
| 791 | // 1. create message without body |
| 792 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 793 | true, mMockContext); |
| 794 | long message1Id = message1.mId; |
| 795 | |
| 796 | // 2. create message with body |
| 797 | Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 798 | true, mMockContext); |
| 799 | long message2Id = message2.mId; |
| 800 | // verify body is there |
| 801 | assertNotNull(loadBodyForMessageId(message2Id)); |
| 802 | |
| 803 | // 3. delete first message |
| 804 | resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message1Id), null, null); |
Marc Blank | c0c9c33 | 2009-08-19 19:07:29 -0700 | [diff] [blame] | 805 | |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 806 | // 4. verify body for second message wasn't deleted |
| 807 | assertNotNull(loadBodyForMessageId(message2Id)); |
| 808 | |
| 809 | // 5. delete second message, check its body is deleted |
| 810 | resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message2Id), null, null); |
| 811 | assertNull(loadBodyForMessageId(message2Id)); |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * Test delete orphan bodies. |
| 816 | * 1. create message without body (message id 1) |
| 817 | * 2. create message with body (message id 2. Body has _id 1 and messageKey 2). |
| 818 | * 3. delete first message. |
| 819 | * 4. delete some other mailbox -- this triggers delete orphan bodies. |
| 820 | * 5. verify that body for message 2 has not been deleted. |
| 821 | */ |
| 822 | public void testDeleteOrphanBodies() { |
| 823 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 824 | |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 825 | // Create account and two mailboxes |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 826 | Account account1 = ProviderTestUtils.setupAccount("orphaned body", true, mMockContext); |
| 827 | long account1Id = account1.mId; |
| 828 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 829 | long box1Id = box1.mId; |
| 830 | Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account1Id, true, mMockContext); |
| 831 | long box2Id = box2.mId; |
| 832 | |
| 833 | // 1. create message without body |
| 834 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 835 | true, mMockContext); |
| 836 | long message1Id = message1.mId; |
| 837 | |
| 838 | // 2. create message with body |
| 839 | Message message2 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 840 | true, mMockContext); |
| 841 | long message2Id = message2.mId; |
| 842 | //verify body is there |
Mihai Preda | fb7974f | 2009-08-03 15:05:50 +0200 | [diff] [blame] | 843 | assertNotNull(loadBodyForMessageId(message2Id)); |
| 844 | |
| 845 | // 3. delete first message |
| 846 | resolver.delete(ContentUris.withAppendedId(Message.CONTENT_URI, message1Id), null, null); |
| 847 | |
| 848 | // 4. delete some mailbox (because it triggers "delete orphan bodies") |
| 849 | resolver.delete(ContentUris.withAppendedId(Mailbox.CONTENT_URI, box2Id), null, null); |
| 850 | |
| 851 | // 5. verify body for second message wasn't deleted during "delete orphan bodies" |
| 852 | assertNotNull(loadBodyForMessageId(message2Id)); |
| 853 | } |
| 854 | |
| 855 | /** |
Marc Blank | 0efe738 | 2010-09-27 18:29:50 -0700 | [diff] [blame] | 856 | * Note that we can't use EmailContent.count() here because it uses a projection including |
| 857 | * count(*), and count(*) is incompatible with a LIMIT (i.e. the limit would be applied to the |
| 858 | * single column returned with count(*), rather than to the query itself) |
| 859 | */ |
| 860 | private int count(Context context, Uri uri, String selection, String[] selectionArgs) { |
| 861 | Cursor c = context.getContentResolver().query(uri, EmailContent.ID_PROJECTION, selection, |
| 862 | selectionArgs, null); |
| 863 | try { |
| 864 | return c.getCount(); |
| 865 | } finally { |
| 866 | c.close(); |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | public void testMessageQueryWithLimit() { |
| 871 | final Context context = mMockContext; |
| 872 | |
| 873 | // Create account and two mailboxes |
| 874 | Account acct = ProviderTestUtils.setupAccount("orphaned body", true, context); |
| 875 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context); |
| 876 | Mailbox box2 = ProviderTestUtils.setupMailbox("box2", acct.mId, true, context); |
| 877 | |
| 878 | // Create 4 messages in box1 |
| 879 | ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, false, true, context); |
| 880 | ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, false, true, context); |
| 881 | ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, false, true, context); |
| 882 | ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, false, true, context); |
| 883 | |
| 884 | // Create 4 messages in box2 |
| 885 | ProviderTestUtils.setupMessage("message1", acct.mId, box2.mId, false, true, context); |
| 886 | ProviderTestUtils.setupMessage("message2", acct.mId, box2.mId, false, true, context); |
| 887 | ProviderTestUtils.setupMessage("message3", acct.mId, box2.mId, false, true, context); |
| 888 | ProviderTestUtils.setupMessage("message4", acct.mId, box2.mId, false, true, context); |
| 889 | |
| 890 | // Check normal case, special case (limit 1), and arbitrary limits |
| 891 | assertEquals(8, count(mMockContext, Message.CONTENT_URI, null, null)); |
| 892 | assertEquals(1, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 1), |
| 893 | null, null)); |
| 894 | assertEquals(3, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 3), |
| 895 | null, null)); |
| 896 | assertEquals(8, count(mMockContext, EmailContent.uriWithLimit(Message.CONTENT_URI, 100), |
| 897 | null, null)); |
| 898 | |
| 899 | // Check that it works with selection/selection args |
| 900 | String[] args = new String[] {Long.toString(box1.mId)}; |
| 901 | assertEquals(4, count(mMockContext, Message.CONTENT_URI, |
| 902 | MessageColumns.MAILBOX_KEY + "=?", args)); |
| 903 | assertEquals(1, count(mMockContext, |
| 904 | EmailContent.uriWithLimit(Message.CONTENT_URI, 1), |
| 905 | MessageColumns.MAILBOX_KEY + "=?", args)); |
| 906 | } |
| 907 | |
| 908 | /** |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 909 | * Test delete orphan messages |
| 910 | * 1. create message without body (message id 1) |
| 911 | * 2. create message with body (message id 2. Body has _id 1 and messageKey 2). |
| 912 | * 3. delete first message. |
| 913 | * 4. delete some other mailbox -- this triggers delete orphan bodies. |
| 914 | * 5. verify that body for message 2 has not been deleted. |
| 915 | */ |
| 916 | public void testDeleteOrphanMessages() { |
| 917 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 918 | final Context context = mMockContext; |
| 919 | |
| 920 | // Create account and two mailboxes |
| 921 | Account acct = ProviderTestUtils.setupAccount("orphaned body", true, context); |
| 922 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context); |
| 923 | Mailbox box2 = ProviderTestUtils.setupMailbox("box2", acct.mId, true, context); |
| 924 | |
| 925 | // Create 4 messages in box1 |
| 926 | Message msg1_1 = |
| 927 | ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, false, true, context); |
| 928 | Message msg1_2 = |
| 929 | ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, false, true, context); |
| 930 | Message msg1_3 = |
| 931 | ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, false, true, context); |
| 932 | Message msg1_4 = |
| 933 | ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, false, true, context); |
| 934 | |
| 935 | // Create 4 messages in box2 |
| 936 | Message msg2_1 = |
| 937 | ProviderTestUtils.setupMessage("message1", acct.mId, box2.mId, false, true, context); |
| 938 | Message msg2_2 = |
| 939 | ProviderTestUtils.setupMessage("message2", acct.mId, box2.mId, false, true, context); |
| 940 | Message msg2_3 = |
| 941 | ProviderTestUtils.setupMessage("message3", acct.mId, box2.mId, false, true, context); |
| 942 | Message msg2_4 = |
| 943 | ProviderTestUtils.setupMessage("message4", acct.mId, box2.mId, false, true, context); |
| 944 | |
| 945 | // Delete 2 from each mailbox |
| 946 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_1.mId), |
| 947 | null, null); |
| 948 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_2.mId), |
| 949 | null, null); |
| 950 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_1.mId), |
| 951 | null, null); |
| 952 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_2.mId), |
| 953 | null, null); |
| 954 | |
| 955 | // There should be 4 items in the deleted item table |
| 956 | assertEquals(4, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null)); |
| 957 | |
| 958 | // Update 2 from each mailbox |
| 959 | ContentValues v = new ContentValues(); |
| 960 | v.put(MessageColumns.DISPLAY_NAME, "--updated--"); |
| 961 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_3.mId), |
| 962 | v, null, null); |
| 963 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg1_4.mId), |
| 964 | v, null, null); |
| 965 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_3.mId), |
| 966 | v, null, null); |
| 967 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, msg2_4.mId), |
| 968 | v, null, null); |
| 969 | |
| 970 | // There should be 4 items in the updated item table |
| 971 | assertEquals(4, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null)); |
| 972 | |
| 973 | // Manually add 2 messages from a "deleted" mailbox to deleted and updated tables |
| 974 | // Use a value > 2 for the deleted box id |
| 975 | long delBoxId = 10; |
| 976 | // Create 4 messages in the "deleted" mailbox |
| 977 | Message msgX_A = |
| 978 | ProviderTestUtils.setupMessage("messageA", acct.mId, delBoxId, false, false, context); |
| 979 | Message msgX_B = |
| 980 | ProviderTestUtils.setupMessage("messageB", acct.mId, delBoxId, false, false, context); |
| 981 | Message msgX_C = |
| 982 | ProviderTestUtils.setupMessage("messageC", acct.mId, delBoxId, false, false, context); |
| 983 | Message msgX_D = |
| 984 | ProviderTestUtils.setupMessage("messageD", acct.mId, delBoxId, false, false, context); |
| 985 | |
| 986 | ContentValues cv; |
| 987 | // We have to assign id's manually because there are no autoincrement id's for these tables |
| 988 | // Start with an id that won't exist, since id's in these tables must be unique |
| 989 | long msgId = 10; |
| 990 | // It's illegal to manually insert these, so we need to catch the exception |
| 991 | // NOTE: The insert succeeds, and then throws the exception |
| 992 | try { |
| 993 | cv = msgX_A.toContentValues(); |
| 994 | cv.put(EmailContent.RECORD_ID, msgId++); |
| 995 | resolver.insert(Message.DELETED_CONTENT_URI, cv); |
| 996 | } catch (IllegalArgumentException e) { |
| 997 | } |
| 998 | try { |
| 999 | cv = msgX_B.toContentValues(); |
| 1000 | cv.put(EmailContent.RECORD_ID, msgId++); |
| 1001 | resolver.insert(Message.DELETED_CONTENT_URI, cv); |
| 1002 | } catch (IllegalArgumentException e) { |
| 1003 | } |
| 1004 | try { |
| 1005 | cv = msgX_C.toContentValues(); |
| 1006 | cv.put(EmailContent.RECORD_ID, msgId++); |
| 1007 | resolver.insert(Message.UPDATED_CONTENT_URI, cv); |
| 1008 | } catch (IllegalArgumentException e) { |
| 1009 | } |
| 1010 | try { |
| 1011 | cv = msgX_D.toContentValues(); |
| 1012 | cv.put(EmailContent.RECORD_ID, msgId++); |
| 1013 | resolver.insert(Message.UPDATED_CONTENT_URI, cv); |
| 1014 | } catch (IllegalArgumentException e) { |
| 1015 | } |
| 1016 | |
| 1017 | // There should be 6 items in the deleted and updated tables |
| 1018 | assertEquals(6, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null)); |
| 1019 | assertEquals(6, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null)); |
| 1020 | |
| 1021 | // Delete the orphans |
Marc Blank | 2bdf7ee | 2011-06-30 16:03:36 -0700 | [diff] [blame] | 1022 | EmailProvider.deleteMessageOrphans(EmailProvider.getReadableDatabase(context), |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1023 | Message.DELETED_TABLE_NAME); |
Marc Blank | 2bdf7ee | 2011-06-30 16:03:36 -0700 | [diff] [blame] | 1024 | EmailProvider.deleteMessageOrphans(EmailProvider.getReadableDatabase(context), |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1025 | Message.UPDATED_TABLE_NAME); |
| 1026 | |
| 1027 | // There should now be 4 messages in each of the deleted and updated tables again |
| 1028 | assertEquals(4, EmailContent.count(context, Message.UPDATED_CONTENT_URI, null, null)); |
| 1029 | assertEquals(4, EmailContent.count(context, Message.DELETED_CONTENT_URI, null, null)); |
| 1030 | } |
| 1031 | |
| 1032 | /** |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1033 | * Test delete message |
| 1034 | * TODO: body |
| 1035 | * TODO: attachments |
| 1036 | */ |
| 1037 | public void testMessageDelete() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1038 | Account account1 = ProviderTestUtils.setupAccount("message-delete", true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1039 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1040 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1041 | long box1Id = box1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1042 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 1043 | true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1044 | long message1Id = message1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1045 | Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false, |
| 1046 | true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1047 | long message2Id = message2.mId; |
| 1048 | |
| 1049 | String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " + |
| 1050 | EmailContent.MessageColumns.MAILBOX_KEY + "=?"; |
| 1051 | String[] selArgs = new String[] { String.valueOf(account1Id), String.valueOf(box1Id) }; |
| 1052 | |
| 1053 | // make sure there are two messages |
| 1054 | int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1055 | assertEquals(2, numMessages); |
| 1056 | |
| 1057 | // now delete one of them |
| 1058 | Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id); |
| 1059 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1060 | |
| 1061 | // make sure there's only one message now |
| 1062 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1063 | assertEquals(1, numMessages); |
| 1064 | |
| 1065 | // now delete the other one |
| 1066 | uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id); |
| 1067 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1068 | |
| 1069 | // make sure there are no messages now |
| 1070 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1071 | assertEquals(0, numMessages); |
| 1072 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1073 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1074 | /** |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1075 | * Test delete synced message |
| 1076 | * TODO: body |
| 1077 | * TODO: attachments |
| 1078 | */ |
| 1079 | public void testSyncedMessageDelete() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1080 | Account account1 = ProviderTestUtils.setupAccount("synced-message-delete", true, |
| 1081 | mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1082 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1083 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1084 | long box1Id = box1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1085 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 1086 | true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1087 | long message1Id = message1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1088 | Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false, |
| 1089 | true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1090 | long message2Id = message2.mId; |
| 1091 | |
| 1092 | String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " |
| 1093 | + EmailContent.MessageColumns.MAILBOX_KEY + "=?"; |
| 1094 | String[] selArgs = new String[] { |
| 1095 | String.valueOf(account1Id), String.valueOf(box1Id) |
| 1096 | }; |
| 1097 | |
| 1098 | // make sure there are two messages |
| 1099 | int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1100 | assertEquals(2, numMessages); |
| 1101 | |
| 1102 | // make sure we start with no synced deletions |
| 1103 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1104 | selArgs); |
| 1105 | assertEquals(0, numMessages); |
| 1106 | |
| 1107 | // now delete one of them SYNCED |
| 1108 | Uri uri = ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message1Id); |
| 1109 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1110 | |
| 1111 | // make sure there's only one message now |
| 1112 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1113 | assertEquals(1, numMessages); |
| 1114 | |
| 1115 | // make sure there's one synced deletion now |
| 1116 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1117 | selArgs); |
| 1118 | assertEquals(1, numMessages); |
| 1119 | |
| 1120 | // now delete the other one NOT SYNCED |
| 1121 | uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id); |
| 1122 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1123 | |
| 1124 | // make sure there are no messages now |
| 1125 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1126 | assertEquals(0, numMessages); |
| 1127 | |
| 1128 | // make sure there's still one deletion now |
| 1129 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1130 | selArgs); |
| 1131 | assertEquals(1, numMessages); |
| 1132 | } |
| 1133 | |
| 1134 | /** |
| 1135 | * Test message update |
| 1136 | * TODO: body |
| 1137 | * TODO: attachments |
| 1138 | */ |
| 1139 | public void testMessageUpdate() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1140 | Account account1 = ProviderTestUtils.setupAccount("message-update", true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1141 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1142 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1143 | long box1Id = box1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1144 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, |
| 1145 | true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1146 | long message1Id = message1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1147 | Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false, |
| 1148 | true, mMockContext); |
Andrew Stadler | ddc871d | 2009-06-29 23:08:40 -0700 | [diff] [blame] | 1149 | long message2Id = message2.mId; |
| 1150 | ContentResolver cr = mMockContext.getContentResolver(); |
| 1151 | |
| 1152 | String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " |
| 1153 | + EmailContent.MessageColumns.MAILBOX_KEY + "=?"; |
| 1154 | String[] selArgs = new String[] { |
| 1155 | String.valueOf(account1Id), String.valueOf(box1Id) |
| 1156 | }; |
| 1157 | |
| 1158 | // make sure there are two messages |
| 1159 | int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1160 | assertEquals(2, numMessages); |
| 1161 | |
| 1162 | // change the first one |
| 1163 | Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id); |
| 1164 | ContentValues cv = new ContentValues(); |
| 1165 | cv.put(MessageColumns.FROM_LIST, "from-list"); |
| 1166 | cr.update(uri, cv, null, null); |
| 1167 | |
| 1168 | // make sure there's no updated message |
| 1169 | numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection, |
| 1170 | selArgs); |
| 1171 | assertEquals(0, numMessages); |
| 1172 | |
| 1173 | // get the message back from the provider, make sure the change "stuck" |
| 1174 | Message restoredMessage = Message.restoreMessageWithId(mMockContext, message1Id); |
| 1175 | assertEquals("from-list", restoredMessage.mFrom); |
| 1176 | |
| 1177 | // change the second one |
| 1178 | uri = ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2Id); |
| 1179 | cv = new ContentValues(); |
| 1180 | cv.put(MessageColumns.FROM_LIST, "from-list"); |
| 1181 | cr.update(uri, cv, null, null); |
| 1182 | |
| 1183 | // make sure there's one updated message |
| 1184 | numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection, |
| 1185 | selArgs); |
| 1186 | assertEquals(1, numMessages); |
| 1187 | |
| 1188 | // get the message back from the provider, make sure the change "stuck", |
| 1189 | // as before |
| 1190 | restoredMessage = Message.restoreMessageWithId(mMockContext, message2Id); |
| 1191 | assertEquals("from-list", restoredMessage.mFrom); |
| 1192 | |
| 1193 | // get the original message back from the provider |
| 1194 | Cursor c = cr.query(Message.UPDATED_CONTENT_URI, Message.CONTENT_PROJECTION, null, null, |
| 1195 | null); |
| 1196 | try { |
| 1197 | assertTrue(c.moveToFirst()); |
| 1198 | Message originalMessage = EmailContent.getContent(c, Message.class); |
| 1199 | // make sure this has the original value |
| 1200 | assertEquals("from message2", originalMessage.mFrom); |
| 1201 | // Should only be one |
| 1202 | assertFalse(c.moveToNext()); |
| 1203 | } finally { |
| 1204 | c.close(); |
| 1205 | } |
| 1206 | |
| 1207 | // delete the second message |
| 1208 | cr.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2Id), null, null); |
| 1209 | |
| 1210 | // hey, presto! the change should be gone |
| 1211 | numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection, |
| 1212 | selArgs); |
| 1213 | assertEquals(0, numMessages); |
| 1214 | |
| 1215 | // and there should now be a deleted record |
| 1216 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1217 | selArgs); |
| 1218 | assertEquals(1, numMessages); |
| 1219 | } |
| 1220 | |
| 1221 | /** |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1222 | * TODO: cascaded delete account |
| 1223 | * TODO: hostauth |
| 1224 | * TODO: body |
| 1225 | * TODO: attachments |
| 1226 | * TODO: create other account, mailbox & messages and confirm the right objects were deleted |
| 1227 | */ |
| 1228 | public void testCascadeDeleteAccount() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1229 | Account account1 = ProviderTestUtils.setupAccount("account-delete-cascade", true, |
| 1230 | mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1231 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1232 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1233 | long box1Id = box1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1234 | /* Message message1 = */ ProviderTestUtils.setupMessage("message1", account1Id, box1Id, |
| 1235 | false, true, mMockContext); |
| 1236 | /* Message message2 = */ ProviderTestUtils.setupMessage("message2", account1Id, box1Id, |
| 1237 | false, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1238 | |
| 1239 | // make sure there is one account, one mailbox, and two messages |
| 1240 | int numAccounts = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null); |
| 1241 | assertEquals(1, numAccounts); |
| 1242 | int numBoxes = EmailContent.count(mMockContext, Mailbox.CONTENT_URI, null, null); |
| 1243 | assertEquals(1, numBoxes); |
| 1244 | int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1245 | assertEquals(2, numMessages); |
| 1246 | |
| 1247 | // delete the account |
| 1248 | Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id); |
| 1249 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1250 | |
| 1251 | // make sure there are no accounts, mailboxes, or messages |
| 1252 | numAccounts = EmailContent.count(mMockContext, Account.CONTENT_URI, null, null); |
| 1253 | assertEquals(0, numAccounts); |
| 1254 | numBoxes = EmailContent.count(mMockContext, Mailbox.CONTENT_URI, null, null); |
| 1255 | assertEquals(0, numBoxes); |
| 1256 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1257 | assertEquals(0, numMessages); |
| 1258 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1259 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1260 | /** |
| 1261 | * Test cascaded delete mailbox |
| 1262 | * TODO: body |
| 1263 | * TODO: attachments |
| 1264 | * TODO: create other mailbox & messages and confirm the right objects were deleted |
| 1265 | */ |
| 1266 | public void testCascadeDeleteMailbox() { |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1267 | Account account1 = ProviderTestUtils.setupAccount("mailbox-delete-cascade", true, |
| 1268 | mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1269 | long account1Id = account1.mId; |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1270 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1271 | long box1Id = box1.mId; |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1272 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1273 | false, true, mMockContext); |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1274 | Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, |
Andrew Stadler | 28448e7 | 2009-07-06 10:49:38 -0700 | [diff] [blame] | 1275 | false, true, mMockContext); |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1276 | Message message3 = ProviderTestUtils.setupMessage("message3", account1Id, box1Id, |
| 1277 | false, true, mMockContext); |
| 1278 | Message message4 = ProviderTestUtils.setupMessage("message4", account1Id, box1Id, |
| 1279 | false, true, mMockContext); |
| 1280 | ProviderTestUtils.setupMessage("message5", account1Id, box1Id, false, true, mMockContext); |
| 1281 | ProviderTestUtils.setupMessage("message6", account1Id, box1Id, false, true, mMockContext); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1282 | |
| 1283 | String selection = EmailContent.MessageColumns.ACCOUNT_KEY + "=? AND " + |
| 1284 | EmailContent.MessageColumns.MAILBOX_KEY + "=?"; |
| 1285 | String[] selArgs = new String[] { String.valueOf(account1Id), String.valueOf(box1Id) }; |
| 1286 | |
Marc Blank | 0e1595c | 2009-11-18 17:11:33 -0800 | [diff] [blame] | 1287 | // make sure there are six messages |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1288 | int numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1289 | assertEquals(6, numMessages); |
| 1290 | |
| 1291 | ContentValues cv = new ContentValues(); |
| 1292 | cv.put(Message.SERVER_ID, "SERVER_ID"); |
| 1293 | ContentResolver resolver = mMockContext.getContentResolver(); |
| 1294 | |
| 1295 | // Update two messages |
| 1296 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message1.mId), |
| 1297 | cv, null, null); |
| 1298 | resolver.update(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message2.mId), |
| 1299 | cv, null, null); |
| 1300 | // Delete two messages |
| 1301 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message3.mId), |
| 1302 | null, null); |
| 1303 | resolver.delete(ContentUris.withAppendedId(Message.SYNCED_CONTENT_URI, message4.mId), |
| 1304 | null, null); |
| 1305 | |
| 1306 | // There should now be two messages in updated/deleted, and 4 in messages |
| 1307 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1308 | assertEquals(4, numMessages); |
| 1309 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1310 | selArgs); |
| 1311 | assertEquals(2, numMessages); |
| 1312 | numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection, |
| 1313 | selArgs); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1314 | assertEquals(2, numMessages); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1315 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1316 | // now delete the mailbox |
| 1317 | Uri uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, box1Id); |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1318 | resolver.delete(uri, null, null); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1319 | |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1320 | // there should now be zero messages in all three tables |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1321 | numMessages = EmailContent.count(mMockContext, Message.CONTENT_URI, selection, selArgs); |
| 1322 | assertEquals(0, numMessages); |
Marc Blank | ef83299 | 2009-10-13 16:25:00 -0700 | [diff] [blame] | 1323 | numMessages = EmailContent.count(mMockContext, Message.DELETED_CONTENT_URI, selection, |
| 1324 | selArgs); |
| 1325 | assertEquals(0, numMessages); |
| 1326 | numMessages = EmailContent.count(mMockContext, Message.UPDATED_CONTENT_URI, selection, |
| 1327 | selArgs); |
| 1328 | assertEquals(0, numMessages); |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1329 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1330 | |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1331 | /** |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1332 | * Test cascaded delete message |
| 1333 | * Confirms that deleting a message will also delete its body & attachments |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1334 | */ |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1335 | public void testCascadeMessageDelete() { |
| 1336 | Account account1 = ProviderTestUtils.setupAccount("message-cascade", true, mMockContext); |
| 1337 | long account1Id = account1.mId; |
| 1338 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mMockContext); |
| 1339 | long box1Id = box1.mId; |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1340 | |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1341 | // Each message has a body, and also give each 2 attachments |
| 1342 | Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, true, |
| 1343 | false, mMockContext); |
| 1344 | ArrayList<Attachment> atts = new ArrayList<Attachment>(); |
| 1345 | for (int i = 0; i < 2; i++) { |
| 1346 | atts.add(ProviderTestUtils.setupAttachment( |
| 1347 | -1, expectedAttachmentNames[i], expectedAttachmentSizes[i], |
| 1348 | false, mMockContext)); |
| 1349 | } |
| 1350 | message1.mAttachments = atts; |
| 1351 | message1.save(mMockContext); |
| 1352 | long message1Id = message1.mId; |
| 1353 | |
| 1354 | Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, true, |
| 1355 | false, mMockContext); |
| 1356 | atts = new ArrayList<Attachment>(); |
| 1357 | for (int i = 0; i < 2; i++) { |
| 1358 | atts.add(ProviderTestUtils.setupAttachment( |
| 1359 | -1, expectedAttachmentNames[i], expectedAttachmentSizes[i], |
| 1360 | false, mMockContext)); |
| 1361 | } |
| 1362 | message2.mAttachments = atts; |
| 1363 | message2.save(mMockContext); |
| 1364 | long message2Id = message2.mId; |
| 1365 | |
| 1366 | // Set up to test total counts of bodies & attachments for our test messages |
| 1367 | String bodySelection = BodyColumns.MESSAGE_KEY + " IN (?,?)"; |
| 1368 | String attachmentSelection = AttachmentColumns.MESSAGE_KEY + " IN (?,?)"; |
| 1369 | String[] selArgs = new String[] { String.valueOf(message1Id), String.valueOf(message2Id) }; |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1370 | |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1371 | // make sure there are two bodies |
| 1372 | int numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs); |
| 1373 | assertEquals(2, numBodies); |
| 1374 | |
| 1375 | // make sure there are four attachments |
| 1376 | int numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI, |
| 1377 | attachmentSelection, selArgs); |
| 1378 | assertEquals(4, numAttachments); |
| 1379 | |
| 1380 | // now delete one of the messages |
| 1381 | Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1Id); |
| 1382 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1383 | |
| 1384 | // there should be one body and two attachments |
| 1385 | numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs); |
| 1386 | assertEquals(1, numBodies); |
| 1387 | |
| 1388 | numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI, |
| 1389 | attachmentSelection, selArgs); |
| 1390 | assertEquals(2, numAttachments); |
| 1391 | |
| 1392 | // now delete the other message |
| 1393 | uri = ContentUris.withAppendedId(Message.CONTENT_URI, message2Id); |
| 1394 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1395 | |
| 1396 | // make sure there are no bodies or attachments |
| 1397 | numBodies = EmailContent.count(mMockContext, Body.CONTENT_URI, bodySelection, selArgs); |
| 1398 | assertEquals(0, numBodies); |
| 1399 | |
| 1400 | numAttachments = EmailContent.count(mMockContext, Attachment.CONTENT_URI, |
| 1401 | attachmentSelection, selArgs); |
| 1402 | assertEquals(0, numAttachments); |
| 1403 | } |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 1404 | |
Marc Blank | 976f929 | 2009-07-15 15:08:53 -0700 | [diff] [blame] | 1405 | /** |
| 1406 | * Test that our unique file name algorithm works as expected. Since this test requires an |
| 1407 | * SD card, we check the environment first, and return immediately if none is mounted. |
| 1408 | * @throws IOException |
| 1409 | */ |
| 1410 | public void testCreateUniqueFile() throws IOException { |
| 1411 | // Delete existing files, if they exist |
| 1412 | if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| 1413 | return; |
| 1414 | } |
| 1415 | try { |
| 1416 | String fileName = "A11achm3n1.doc"; |
| 1417 | File uniqueFile = Attachment.createUniqueFile(fileName); |
| 1418 | assertEquals(fileName, uniqueFile.getName()); |
| 1419 | if (uniqueFile.createNewFile()) { |
| 1420 | uniqueFile = Attachment.createUniqueFile(fileName); |
| 1421 | assertEquals("A11achm3n1-2.doc", uniqueFile.getName()); |
| 1422 | if (uniqueFile.createNewFile()) { |
| 1423 | uniqueFile = Attachment.createUniqueFile(fileName); |
| 1424 | assertEquals("A11achm3n1-3.doc", uniqueFile.getName()); |
| 1425 | } |
| 1426 | } |
| 1427 | fileName = "A11achm3n1"; |
| 1428 | uniqueFile = Attachment.createUniqueFile(fileName); |
| 1429 | assertEquals(fileName, uniqueFile.getName()); |
| 1430 | if (uniqueFile.createNewFile()) { |
| 1431 | uniqueFile = Attachment.createUniqueFile(fileName); |
| 1432 | assertEquals("A11achm3n1-2", uniqueFile.getName()); |
| 1433 | } |
| 1434 | } finally { |
| 1435 | File directory = Environment.getExternalStorageDirectory(); |
| 1436 | // These are the files that should be created earlier in the test. Make sure |
| 1437 | // they are deleted for the next go-around |
| 1438 | String[] fileNames = new String[] {"A11achm3n1.doc", "A11achm3n1-2.doc", "A11achm3n1"}; |
| 1439 | int length = fileNames.length; |
| 1440 | for (int i = 0; i < length; i++) { |
| 1441 | File file = new File(directory, fileNames[i]); |
| 1442 | if (file.exists()) { |
| 1443 | file.delete(); |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | } |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 1448 | |
| 1449 | /** |
| 1450 | * Test retrieving attachments by message ID (using EmailContent.Attachment.MESSAGE_ID_URI) |
| 1451 | */ |
| 1452 | public void testGetAttachmentByMessageIdUri() { |
| 1453 | |
| 1454 | // Note, we don't strictly need accounts, mailboxes or messages to run this test. |
| 1455 | Attachment a1 = ProviderTestUtils.setupAttachment(1, "a1", 100, true, mMockContext); |
| 1456 | Attachment a2 = ProviderTestUtils.setupAttachment(1, "a2", 200, true, mMockContext); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1457 | ProviderTestUtils.setupAttachment(2, "a3", 300, true, mMockContext); |
| 1458 | ProviderTestUtils.setupAttachment(2, "a4", 400, true, mMockContext); |
Andrew Stadler | 4119218 | 2009-07-16 16:03:40 -0700 | [diff] [blame] | 1459 | |
| 1460 | // Now ask for the attachments of message id=1 |
| 1461 | // Note: Using the "sort by size" trick to bring them back in expected order |
| 1462 | Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, 1); |
| 1463 | Cursor c = mMockContext.getContentResolver().query(uri, Attachment.CONTENT_PROJECTION, |
| 1464 | null, null, Attachment.SIZE); |
| 1465 | assertEquals(2, c.getCount()); |
| 1466 | |
| 1467 | try { |
| 1468 | c.moveToFirst(); |
| 1469 | Attachment a1Get = EmailContent.getContent(c, Attachment.class); |
| 1470 | ProviderTestUtils.assertAttachmentEqual("getAttachByUri-1", a1, a1Get); |
| 1471 | c.moveToNext(); |
| 1472 | Attachment a2Get = EmailContent.getContent(c, Attachment.class); |
| 1473 | ProviderTestUtils.assertAttachmentEqual("getAttachByUri-2", a2, a2Get); |
| 1474 | } finally { |
| 1475 | c.close(); |
| 1476 | } |
| 1477 | } |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1478 | |
| 1479 | /** |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1480 | * Test deleting attachments by message ID (using EmailContent.Attachment.MESSAGE_ID_URI) |
| 1481 | */ |
| 1482 | public void testDeleteAttachmentByMessageIdUri() { |
| 1483 | ContentResolver mockResolver = mMockContext.getContentResolver(); |
| 1484 | |
| 1485 | // Note, we don't strictly need accounts, mailboxes or messages to run this test. |
| 1486 | ProviderTestUtils.setupAttachment(1, "a1", 100, true, mMockContext); |
| 1487 | ProviderTestUtils.setupAttachment(1, "a2", 200, true, mMockContext); |
| 1488 | Attachment a3 = ProviderTestUtils.setupAttachment(2, "a3", 300, true, mMockContext); |
| 1489 | Attachment a4 = ProviderTestUtils.setupAttachment(2, "a4", 400, true, mMockContext); |
| 1490 | |
| 1491 | // Delete all attachments for message id=1 |
| 1492 | Uri uri = ContentUris.withAppendedId(Attachment.MESSAGE_ID_URI, 1); |
| 1493 | mockResolver.delete(uri, null, null); |
| 1494 | |
| 1495 | // Read back all attachments and confirm that we have the expected remaining attachments |
| 1496 | // (the attachments that are set for message id=2). Note order-by size to simplify test. |
| 1497 | Cursor c = mockResolver.query(Attachment.CONTENT_URI, Attachment.CONTENT_PROJECTION, |
| 1498 | null, null, Attachment.SIZE); |
| 1499 | assertEquals(2, c.getCount()); |
| 1500 | |
| 1501 | try { |
| 1502 | c.moveToFirst(); |
| 1503 | Attachment a3Get = EmailContent.getContent(c, Attachment.class); |
| 1504 | ProviderTestUtils.assertAttachmentEqual("getAttachByUri-3", a3, a3Get); |
| 1505 | c.moveToNext(); |
| 1506 | Attachment a4Get = EmailContent.getContent(c, Attachment.class); |
| 1507 | ProviderTestUtils.assertAttachmentEqual("getAttachByUri-4", a4, a4Get); |
| 1508 | } finally { |
| 1509 | c.close(); |
| 1510 | } |
| 1511 | } |
| 1512 | |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 1513 | @SmallTest |
Marc Blank | 6353774 | 2011-06-22 15:45:17 -0700 | [diff] [blame] | 1514 | public void testGetDefaultAccountNoneExplicitlySet() { |
| 1515 | Account account1 = ProviderTestUtils.setupAccount("account-default-1", false, mMockContext); |
| 1516 | account1.mIsDefault = false; |
| 1517 | account1.save(mMockContext); |
| 1518 | |
| 1519 | // We should find account1 as default |
| 1520 | long defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1521 | assertEquals(defaultAccountId, account1.mId); |
| 1522 | |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 1523 | Account account2 = ProviderTestUtils.setupAccount("account-default-2", false, mMockContext); |
Marc Blank | 6353774 | 2011-06-22 15:45:17 -0700 | [diff] [blame] | 1524 | account2.mIsDefault = false; |
| 1525 | account2.save(mMockContext); |
| 1526 | |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 1527 | Account account3 = ProviderTestUtils.setupAccount("account-default-3", false, mMockContext); |
| 1528 | account3.mIsDefault = false; |
| 1529 | account3.save(mMockContext); |
| 1530 | |
| 1531 | // We should find the earliest one as the default, so that it can be consistent on |
| 1532 | // repeated calls. |
Marc Blank | 6353774 | 2011-06-22 15:45:17 -0700 | [diff] [blame] | 1533 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
Ben Komalo | 52e6611 | 2011-07-13 15:16:55 -0700 | [diff] [blame] | 1534 | assertTrue(defaultAccountId == account1.mId); |
Marc Blank | 6353774 | 2011-06-22 15:45:17 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
Andrew Stadler | 6c21942 | 2009-09-10 11:52:36 -0700 | [diff] [blame] | 1537 | /** |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1538 | * Tests of default account behavior |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1539 | * |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1540 | * 1. Simple set/get |
| 1541 | * 2. Moving default between 3 accounts |
| 1542 | * 3. Delete default, make sure another becomes default |
| 1543 | */ |
| 1544 | public void testSetGetDefaultAccount() { |
| 1545 | // There should be no default account if there are no accounts |
| 1546 | long defaultAccountId = Account.getDefaultAccountId(mMockContext); |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1547 | assertEquals(Account.NO_ACCOUNT, defaultAccountId); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1548 | |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1549 | Account account1 = ProviderTestUtils.setupAccount("account-default-1", false, mMockContext); |
| 1550 | account1.mIsDefault = false; |
| 1551 | account1.save(mMockContext); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1552 | long account1Id = account1.mId; |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1553 | Account account2 = ProviderTestUtils.setupAccount("account-default-2", false, mMockContext); |
| 1554 | account2.mIsDefault = false; |
| 1555 | account2.save(mMockContext); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1556 | long account2Id = account2.mId; |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1557 | Account account3 = ProviderTestUtils.setupAccount("account-default-3", false, mMockContext); |
| 1558 | account3.mIsDefault = false; |
| 1559 | account3.save(mMockContext); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1560 | long account3Id = account3.mId; |
| 1561 | |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1562 | // With three accounts, but none marked default, confirm that the first one is the default. |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 1563 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
Ben Komalo | acd985e | 2011-07-13 16:51:35 -0700 | [diff] [blame] | 1564 | assertTrue(defaultAccountId == account1Id); |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 1565 | |
| 1566 | updateIsDefault(account1, true); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1567 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1568 | assertEquals(account1Id, defaultAccountId); |
| 1569 | |
Marc Blank | 531ae9d | 2009-07-22 15:55:45 -0700 | [diff] [blame] | 1570 | updateIsDefault(account2, true); |
| 1571 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1572 | assertEquals(account2Id, defaultAccountId); |
| 1573 | |
| 1574 | updateIsDefault(account3, true); |
| 1575 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1576 | assertEquals(account3Id, defaultAccountId); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1577 | |
| 1578 | // Now delete a non-default account and confirm no change |
| 1579 | Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, account1Id); |
| 1580 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1581 | |
Marc Blank | 531ae9d | 2009-07-22 15:55:45 -0700 | [diff] [blame] | 1582 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1583 | assertEquals(account3Id, defaultAccountId); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1584 | |
| 1585 | // Now confirm deleting the default account and it switches to another one |
| 1586 | uri = ContentUris.withAppendedId(Account.CONTENT_URI, account3Id); |
| 1587 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1588 | |
Marc Blank | 531ae9d | 2009-07-22 15:55:45 -0700 | [diff] [blame] | 1589 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
| 1590 | assertEquals(account2Id, defaultAccountId); |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1591 | |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 1592 | // Now delete the final account and confirm there are no default accounts again |
| 1593 | uri = ContentUris.withAppendedId(Account.CONTENT_URI, account2Id); |
| 1594 | mMockContext.getContentResolver().delete(uri, null, null); |
| 1595 | |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1596 | defaultAccountId = Account.getDefaultAccountId(mMockContext); |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 1597 | assertEquals(-1, defaultAccountId); |
Andrew Stadler | 54c1f2b | 2009-07-21 16:44:16 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Andrew Stadler | 9e2c6bd | 2009-07-22 15:13:30 -0700 | [diff] [blame] | 1600 | private void updateIsDefault(Account account, boolean newState) { |
| 1601 | account.setDefaultAccount(newState); |
| 1602 | ContentValues cv = new ContentValues(); |
| 1603 | cv.put(AccountColumns.IS_DEFAULT, account.mIsDefault); |
| 1604 | account.update(mMockContext, cv); |
| 1605 | } |
Marc Blank | 758a532 | 2009-07-30 11:41:31 -0700 | [diff] [blame] | 1606 | |
| 1607 | public static Message setupUnreadMessage(String name, long accountId, long mailboxId, |
| 1608 | boolean addBody, boolean saveIt, Context context) { |
| 1609 | Message msg = |
| 1610 | ProviderTestUtils.setupMessage(name, accountId, mailboxId, addBody, false, context); |
| 1611 | msg.mFlagRead = false; |
| 1612 | if (saveIt) { |
| 1613 | msg.save(context); |
| 1614 | } |
| 1615 | return msg; |
| 1616 | } |
| 1617 | |
| 1618 | public void testUnreadCountTriggers() { |
| 1619 | // Start with one account and three mailboxes |
| 1620 | Account account = ProviderTestUtils.setupAccount("triggers", true, mMockContext); |
| 1621 | Mailbox boxA = ProviderTestUtils.setupMailbox("boxA", account.mId, true, mMockContext); |
| 1622 | Mailbox boxB = ProviderTestUtils.setupMailbox("boxB", account.mId, true, mMockContext); |
| 1623 | Mailbox boxC = ProviderTestUtils.setupMailbox("boxC", account.mId, true, mMockContext); |
| 1624 | |
| 1625 | // Make sure there are no unreads |
| 1626 | assertEquals(0, getUnreadCount(boxA.mId)); |
| 1627 | assertEquals(0, getUnreadCount(boxB.mId)); |
| 1628 | assertEquals(0, getUnreadCount(boxC.mId)); |
| 1629 | |
| 1630 | // Create 4 unread messages (only 3 named) in boxA |
| 1631 | Message message1 = setupUnreadMessage("message1", account.mId, boxA.mId, |
| 1632 | false, true, mMockContext); |
| 1633 | Message message2= setupUnreadMessage("message2", account.mId, boxA.mId, |
| 1634 | false, true, mMockContext); |
| 1635 | Message message3 = setupUnreadMessage("message3", account.mId, boxA.mId, |
| 1636 | false, true, mMockContext); |
| 1637 | setupUnreadMessage("message4", account.mId, boxC.mId, false, true, mMockContext); |
| 1638 | |
| 1639 | // Make sure the unreads are where we expect them |
| 1640 | assertEquals(3, getUnreadCount(boxA.mId)); |
| 1641 | assertEquals(0, getUnreadCount(boxB.mId)); |
| 1642 | assertEquals(1, getUnreadCount(boxC.mId)); |
| 1643 | |
| 1644 | // After deleting message 1, the count in box A should be decremented (to 2) |
| 1645 | ContentResolver cr = mMockContext.getContentResolver(); |
| 1646 | Uri uri = ContentUris.withAppendedId(Message.CONTENT_URI, message1.mId); |
| 1647 | cr.delete(uri, null, null); |
| 1648 | assertEquals(2, getUnreadCount(boxA.mId)); |
| 1649 | assertEquals(0, getUnreadCount(boxB.mId)); |
| 1650 | assertEquals(1, getUnreadCount(boxC.mId)); |
| 1651 | |
| 1652 | // Move message 2 to box B, leaving 1 in box A and 1 in box B |
| 1653 | message2.mMailboxKey = boxB.mId; |
| 1654 | ContentValues cv = new ContentValues(); |
| 1655 | cv.put(MessageColumns.MAILBOX_KEY, boxB.mId); |
| 1656 | cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message2.mId), cv, null, null); |
| 1657 | assertEquals(1, getUnreadCount(boxA.mId)); |
| 1658 | assertEquals(1, getUnreadCount(boxB.mId)); |
| 1659 | assertEquals(1, getUnreadCount(boxC.mId)); |
| 1660 | |
| 1661 | // Mark message 3 (from box A) read, leaving 0 in box A |
| 1662 | cv.clear(); |
| 1663 | cv.put(MessageColumns.FLAG_READ, 1); |
| 1664 | cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null); |
| 1665 | assertEquals(0, getUnreadCount(boxA.mId)); |
| 1666 | assertEquals(1, getUnreadCount(boxB.mId)); |
| 1667 | assertEquals(1, getUnreadCount(boxC.mId)); |
| 1668 | |
| 1669 | // Move message 3 to box C; should be no change (it's read) |
| 1670 | message3.mMailboxKey = boxC.mId; |
| 1671 | cv.clear(); |
| 1672 | cv.put(MessageColumns.MAILBOX_KEY, boxC.mId); |
| 1673 | cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null); |
| 1674 | assertEquals(0, getUnreadCount(boxA.mId)); |
| 1675 | assertEquals(1, getUnreadCount(boxB.mId)); |
| 1676 | assertEquals(1, getUnreadCount(boxC.mId)); |
| 1677 | |
| 1678 | // Mark message 3 unread; it's now in box C, so that box's count should go up to 3 |
| 1679 | cv.clear(); |
| 1680 | cv.put(MessageColumns.FLAG_READ, 0); |
| 1681 | cr.update(ContentUris.withAppendedId(Message.CONTENT_URI, message3.mId), cv, null, null); |
| 1682 | assertEquals(0, getUnreadCount(boxA.mId)); |
| 1683 | assertEquals(1, getUnreadCount(boxB.mId)); |
| 1684 | assertEquals(2, getUnreadCount(boxC.mId)); |
| 1685 | } |
Mihai Preda | 9627d01 | 2009-08-12 12:51:26 +0200 | [diff] [blame] | 1686 | |
| 1687 | /** |
| 1688 | * Test for EmailProvider.createIndex(). |
| 1689 | * Check that it returns exacly the same string as the one used previously for index creation. |
| 1690 | */ |
| 1691 | public void testCreateIndex() { |
| 1692 | String oldStr = "create index message_" + MessageColumns.TIMESTAMP |
| 1693 | + " on " + Message.TABLE_NAME + " (" + MessageColumns.TIMESTAMP + ");"; |
| 1694 | String newStr = EmailProvider.createIndex(Message.TABLE_NAME, MessageColumns.TIMESTAMP); |
| 1695 | assertEquals(newStr, oldStr); |
| 1696 | } |
Marc Blank | c0c9c33 | 2009-08-19 19:07:29 -0700 | [diff] [blame] | 1697 | |
Marc Blank | 0e1595c | 2009-11-18 17:11:33 -0800 | [diff] [blame] | 1698 | public void testDatabaseCorruptionRecovery() { |
| 1699 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 1700 | final Context context = mMockContext; |
| 1701 | |
| 1702 | // Create account and two mailboxes |
| 1703 | Account acct = ProviderTestUtils.setupAccount("acct1", true, context); |
| 1704 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context); |
| 1705 | |
| 1706 | // Create 4 messages in box1 with bodies |
| 1707 | ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, true, true, context); |
| 1708 | ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, true, true, context); |
| 1709 | ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, true, true, context); |
| 1710 | ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, true, true, context); |
| 1711 | |
| 1712 | // Confirm there are four messages |
| 1713 | int count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1714 | assertEquals(4, count); |
| 1715 | // Confirm there are four bodies |
| 1716 | count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null); |
| 1717 | assertEquals(4, count); |
| 1718 | |
| 1719 | // Find the EmailProvider.db file |
| 1720 | File dbFile = mMockContext.getDatabasePath(EmailProvider.DATABASE_NAME); |
| 1721 | // The EmailProvider.db database should exist (the provider creates it automatically) |
| 1722 | assertTrue(dbFile != null); |
| 1723 | assertTrue(dbFile.exists()); |
| 1724 | // Delete it, and confirm it is gone |
| 1725 | assertTrue(dbFile.delete()); |
| 1726 | assertFalse(dbFile.exists()); |
| 1727 | |
| 1728 | // Find the EmailProviderBody.db file |
| 1729 | dbFile = mMockContext.getDatabasePath(EmailProvider.BODY_DATABASE_NAME); |
| 1730 | // The EmailProviderBody.db database should still exist |
| 1731 | assertTrue(dbFile != null); |
| 1732 | assertTrue(dbFile.exists()); |
| 1733 | |
| 1734 | // URI to uncache the databases |
| 1735 | // This simulates the Provider starting up again (otherwise, it will still be pointing to |
| 1736 | // the already opened files) |
| 1737 | // Note that we only have access to the EmailProvider via the ContentResolver; therefore, |
| 1738 | // we cannot directly call into the provider and use a URI for this |
| 1739 | resolver.update(EmailProvider.INTEGRITY_CHECK_URI, null, null, null); |
| 1740 | |
| 1741 | // TODO We should check for the deletion of attachment files once this is implemented in |
| 1742 | // the provider |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1743 | |
Marc Blank | 0e1595c | 2009-11-18 17:11:33 -0800 | [diff] [blame] | 1744 | // Explanation for what happens below... |
| 1745 | // The next time the database is created by the provider, it will notice that there's |
| 1746 | // already a EmailProviderBody.db file. In this case, it will delete that database to |
| 1747 | // ensure that both are in sync (and empty) |
| 1748 | |
| 1749 | // Confirm there are no bodies |
| 1750 | count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null); |
| 1751 | assertEquals(0, count); |
| 1752 | |
| 1753 | // Confirm there are no messages |
| 1754 | count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1755 | assertEquals(0, count); |
| 1756 | } |
| 1757 | |
| 1758 | public void testBodyDatabaseCorruptionRecovery() { |
| 1759 | final ContentResolver resolver = mMockContext.getContentResolver(); |
| 1760 | final Context context = mMockContext; |
| 1761 | |
| 1762 | // Create account and two mailboxes |
| 1763 | Account acct = ProviderTestUtils.setupAccount("acct1", true, context); |
| 1764 | Mailbox box1 = ProviderTestUtils.setupMailbox("box1", acct.mId, true, context); |
| 1765 | |
| 1766 | // Create 4 messages in box1 with bodies |
| 1767 | ProviderTestUtils.setupMessage("message1", acct.mId, box1.mId, true, true, context); |
| 1768 | ProviderTestUtils.setupMessage("message2", acct.mId, box1.mId, true, true, context); |
| 1769 | ProviderTestUtils.setupMessage("message3", acct.mId, box1.mId, true, true, context); |
| 1770 | ProviderTestUtils.setupMessage("message4", acct.mId, box1.mId, true, true, context); |
| 1771 | |
| 1772 | // Confirm there are four messages |
| 1773 | int count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1774 | assertEquals(4, count); |
| 1775 | // Confirm there are four bodies |
| 1776 | count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null); |
| 1777 | assertEquals(4, count); |
| 1778 | |
| 1779 | // Find the EmailProviderBody.db file |
| 1780 | File dbFile = mMockContext.getDatabasePath(EmailProvider.BODY_DATABASE_NAME); |
| 1781 | // The EmailProviderBody.db database should exist (the provider creates it automatically) |
| 1782 | assertTrue(dbFile != null); |
| 1783 | assertTrue(dbFile.exists()); |
| 1784 | // Delete it, and confirm it is gone |
| 1785 | assertTrue(dbFile.delete()); |
| 1786 | assertFalse(dbFile.exists()); |
| 1787 | |
| 1788 | // Find the EmailProvider.db file |
| 1789 | dbFile = mMockContext.getDatabasePath(EmailProvider.DATABASE_NAME); |
| 1790 | // The EmailProviderBody.db database should still exist |
| 1791 | assertTrue(dbFile != null); |
| 1792 | assertTrue(dbFile.exists()); |
| 1793 | |
| 1794 | // URI to uncache the databases |
| 1795 | // This simulates the Provider starting up again (otherwise, it will still be pointing to |
| 1796 | // the already opened files) |
| 1797 | // Note that we only have access to the EmailProvider via the ContentResolver; therefore, |
| 1798 | // we cannot directly call into the provider and use a URI for this |
| 1799 | resolver.update(EmailProvider.INTEGRITY_CHECK_URI, null, null, null); |
| 1800 | |
| 1801 | // TODO We should check for the deletion of attachment files once this is implemented in |
| 1802 | // the provider |
| 1803 | |
| 1804 | // Explanation for what happens below... |
| 1805 | // The next time the body database is created by the provider, it will notice that there's |
| 1806 | // already a populated EmailProvider.db file. In this case, it will delete that database to |
| 1807 | // ensure that both are in sync (and empty) |
| 1808 | |
| 1809 | // Confirm there are no messages |
| 1810 | count = EmailContent.count(mMockContext, Message.CONTENT_URI, null, null); |
| 1811 | assertEquals(0, count); |
| 1812 | |
| 1813 | // Confirm there are no bodies |
| 1814 | count = EmailContent.count(mMockContext, Body.CONTENT_URI, null, null); |
| 1815 | assertEquals(0, count); |
| 1816 | } |
Marc Blank | 694257c | 2010-02-11 18:14:01 -0800 | [diff] [blame] | 1817 | |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1818 | public void testAccountIsSecurityHold() { |
| 1819 | final Context context = mMockContext; |
| 1820 | Account acct1 = ProviderTestUtils.setupAccount("acct1", true, context); |
| 1821 | |
| 1822 | Account acct2 = ProviderTestUtils.setupAccount("acct2", false, context); |
| 1823 | acct2.mFlags |= Account.FLAGS_SECURITY_HOLD; |
| 1824 | acct2.save(context); |
| 1825 | |
| 1826 | assertFalse(Account.isSecurityHold(context, acct1.mId)); |
| 1827 | assertTrue(Account.isSecurityHold(context, acct2.mId)); |
| 1828 | assertFalse(Account.isSecurityHold(context, 9999999)); // No such account |
| 1829 | } |
| 1830 | |
| 1831 | public void testClearAccountHoldFlags() { |
| 1832 | Account a1 = ProviderTestUtils.setupAccount("holdflag-1", false, mMockContext); |
| 1833 | a1.mFlags = Account.FLAGS_NOTIFY_NEW_MAIL; |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 1834 | a1.mPolicy = new Policy(); |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1835 | a1.save(mMockContext); |
| 1836 | Account a2 = ProviderTestUtils.setupAccount("holdflag-2", false, mMockContext); |
| 1837 | a2.mFlags = Account.FLAGS_VIBRATE_ALWAYS | Account.FLAGS_SECURITY_HOLD; |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 1838 | a2.mPolicy = new Policy(); |
Makoto Onuki | bcf3232 | 2010-07-27 12:52:46 -0700 | [diff] [blame] | 1839 | a2.save(mMockContext); |
| 1840 | |
| 1841 | // bulk clear |
| 1842 | Account.clearSecurityHoldOnAllAccounts(mMockContext); |
| 1843 | |
| 1844 | // confirm new values as expected - no hold flags; other flags unmolested |
| 1845 | Account a1a = Account.restoreAccountWithId(mMockContext, a1.mId); |
| 1846 | assertEquals(Account.FLAGS_NOTIFY_NEW_MAIL, a1a.mFlags); |
| 1847 | Account a2a = Account.restoreAccountWithId(mMockContext, a2.mId); |
| 1848 | assertEquals(Account.FLAGS_VIBRATE_ALWAYS, a2a.mFlags); |
| 1849 | } |
Makoto Onuki | 574854b | 2010-07-30 13:53:59 -0700 | [diff] [blame] | 1850 | |
Makoto Onuki | 833fe73 | 2010-08-02 18:16:13 -0700 | [diff] [blame] | 1851 | private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read) { |
Todd Kennedy | 543953a | 2011-01-24 14:11:20 -0800 | [diff] [blame] | 1852 | return ProviderTestUtils.setupMessage( |
| 1853 | "1", b.mAccountKey, b.mId, true, true, c, starred, read); |
| 1854 | } |
| 1855 | |
Makoto Onuki | 5247ab8 | 2010-08-23 17:21:53 -0700 | [diff] [blame] | 1856 | public void testAccountIsEasAccount() { |
Makoto Onuki | 6d8bfa6 | 2010-08-09 16:51:40 -0700 | [diff] [blame] | 1857 | Account account = new Account(); |
Makoto Onuki | 25144e2 | 2010-08-26 15:08:19 -0700 | [diff] [blame] | 1858 | // No hostauth |
Makoto Onuki | 5247ab8 | 2010-08-23 17:21:53 -0700 | [diff] [blame] | 1859 | assertFalse(account.isEasAccount(mMockContext)); |
Makoto Onuki | 6d8bfa6 | 2010-08-09 16:51:40 -0700 | [diff] [blame] | 1860 | |
Makoto Onuki | 25144e2 | 2010-08-26 15:08:19 -0700 | [diff] [blame] | 1861 | checkAccountIsEasAccount(null, false); |
| 1862 | checkAccountIsEasAccount("", false); |
| 1863 | checkAccountIsEasAccount("x", false); |
| 1864 | checkAccountIsEasAccount("eas", true); |
| 1865 | } |
Makoto Onuki | 6d8bfa6 | 2010-08-09 16:51:40 -0700 | [diff] [blame] | 1866 | |
Makoto Onuki | 25144e2 | 2010-08-26 15:08:19 -0700 | [diff] [blame] | 1867 | private void checkAccountIsEasAccount(String protocol, boolean expected) { |
| 1868 | Account account = ProviderTestUtils.setupAccount("account", false, mMockContext); |
| 1869 | account.mHostAuthRecv = ProviderTestUtils.setupHostAuth(protocol, "account-hostauth-recv", |
Todd Kennedy | fe68c0e | 2011-02-17 10:27:31 -0800 | [diff] [blame] | 1870 | false, mMockContext); |
Makoto Onuki | 25144e2 | 2010-08-26 15:08:19 -0700 | [diff] [blame] | 1871 | account.save(mMockContext); |
| 1872 | assertEquals(expected, account.isEasAccount(mMockContext)); |
Makoto Onuki | 6d8bfa6 | 2010-08-09 16:51:40 -0700 | [diff] [blame] | 1873 | } |
Marc Blank | bca4e6e | 2010-08-23 21:39:35 -0700 | [diff] [blame] | 1874 | |
| 1875 | public void testGetKeyColumnLong() { |
| 1876 | final Context c = mMockContext; |
| 1877 | Account a = ProviderTestUtils.setupAccount("acct", true, c); |
| 1878 | Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a.mId, true, c, Mailbox.TYPE_MAIL); |
| 1879 | Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a.mId, true, c, Mailbox.TYPE_MAIL); |
| 1880 | Message m1 = createMessage(c, b1, false, false); |
| 1881 | Message m2 = createMessage(c, b2, false, false); |
| 1882 | assertEquals(a.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.ACCOUNT_KEY)); |
| 1883 | assertEquals(a.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.ACCOUNT_KEY)); |
| 1884 | assertEquals(b1.mId, Message.getKeyColumnLong(c, m1.mId, MessageColumns.MAILBOX_KEY)); |
| 1885 | assertEquals(b2.mId, Message.getKeyColumnLong(c, m2.mId, MessageColumns.MAILBOX_KEY)); |
| 1886 | } |
| 1887 | |
Makoto Onuki | aef9515 | 2010-12-10 13:36:18 -0800 | [diff] [blame] | 1888 | public void testGetAccountIdForMessageId() { |
| 1889 | final Context c = mMockContext; |
| 1890 | Account a1 = ProviderTestUtils.setupAccount("acct1", true, c); |
| 1891 | Account a2 = ProviderTestUtils.setupAccount("acct2", true, c); |
| 1892 | Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_MAIL); |
| 1893 | Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a2.mId, true, c, Mailbox.TYPE_MAIL); |
| 1894 | Message m1 = createMessage(c, b1, false, false); |
| 1895 | Message m2 = createMessage(c, b2, false, false); |
| 1896 | |
| 1897 | assertEquals(a1.mId, Account.getAccountIdForMessageId(c, m1.mId)); |
| 1898 | assertEquals(a2.mId, Account.getAccountIdForMessageId(c, m2.mId)); |
| 1899 | |
| 1900 | // message desn't exist |
| 1901 | assertEquals(-1, Account.getAccountIdForMessageId(c, 12345)); |
| 1902 | } |
| 1903 | |
Todd Kennedy | 200c6bd | 2011-04-20 16:22:41 -0700 | [diff] [blame] | 1904 | public void testGetAccountForMessageId() { |
| 1905 | final Context c = mMockContext; |
| 1906 | Account a = ProviderTestUtils.setupAccount("acct", true, c); |
| 1907 | Message m1 = ProviderTestUtils.setupMessage("1", a.mId, 1, true, true, c, false, false); |
| 1908 | Message m2 = ProviderTestUtils.setupMessage("1", a.mId, 2, true, true, c, false, false); |
| 1909 | ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m1.mId)); |
| 1910 | ProviderTestUtils.assertAccountEqual("x", a, Account.getAccountForMessageId(c, m2.mId)); |
| 1911 | } |
| 1912 | |
Makoto Onuki | e357f58 | 2010-08-27 10:25:03 -0700 | [diff] [blame] | 1913 | public void testGetAccountGetInboxIdTest() { |
| 1914 | final Context c = mMockContext; |
| 1915 | |
| 1916 | // Prepare some data with red-herrings. |
| 1917 | Account a1 = ProviderTestUtils.setupAccount("acct1", true, c); |
| 1918 | Account a2 = ProviderTestUtils.setupAccount("acct2", true, c); |
| 1919 | Mailbox b1i = ProviderTestUtils.setupMailbox("b1i", a1.mId, true, c, Mailbox.TYPE_INBOX); |
| 1920 | Mailbox b2a = ProviderTestUtils.setupMailbox("b2a", a2.mId, true, c, Mailbox.TYPE_MAIL); |
| 1921 | Mailbox b2i = ProviderTestUtils.setupMailbox("b2b", a2.mId, true, c, Mailbox.TYPE_INBOX); |
| 1922 | |
| 1923 | assertEquals(b2i.mId, Account.getInboxId(c, a2.mId)); |
Makoto Onuki | d25d87c | 2010-09-24 14:50:20 -0700 | [diff] [blame] | 1924 | |
| 1925 | // No account found. |
| 1926 | assertEquals(-1, Account.getInboxId(c, 999999)); |
Makoto Onuki | e357f58 | 2010-08-27 10:25:03 -0700 | [diff] [blame] | 1927 | } |
| 1928 | |
Makoto Onuki | 261d6c3 | 2010-09-14 16:28:50 -0700 | [diff] [blame] | 1929 | /** |
| 1930 | * Check if update to {@link Account#RESET_NEW_MESSAGE_COUNT_URI} resets the new message count. |
| 1931 | */ |
| 1932 | public void testResetNewMessageCount() { |
| 1933 | final Context c = mMockContext; |
| 1934 | final ContentResolver cr = c.getContentResolver(); |
| 1935 | |
| 1936 | // Prepare test data |
| 1937 | Account a1 = ProviderTestUtils.setupAccount("acct1", false, c); |
| 1938 | a1.mNewMessageCount = 1; |
| 1939 | a1.save(c); |
| 1940 | Account a2 = ProviderTestUtils.setupAccount("acct2", false, c); |
| 1941 | a2.mNewMessageCount = 2; |
| 1942 | a2.save(c); |
| 1943 | Account a3 = ProviderTestUtils.setupAccount("acct3", false, c); |
| 1944 | a3.mNewMessageCount = 3; |
| 1945 | a3.save(c); |
| 1946 | Account a4 = ProviderTestUtils.setupAccount("acct4", false, c); |
| 1947 | a4.mNewMessageCount = 4; |
| 1948 | a4.save(c); |
| 1949 | Account a5 = ProviderTestUtils.setupAccount("acct5", false, c); |
| 1950 | a5.mNewMessageCount = 5; |
| 1951 | a5.save(c); |
| 1952 | |
| 1953 | // With ID in URI, no selection |
| 1954 | cr.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a1.mId), |
| 1955 | null, null, null); |
| 1956 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 1957 | assertEquals(2, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount); |
| 1958 | assertEquals(3, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount); |
| 1959 | assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount); |
| 1960 | assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount); |
| 1961 | |
| 1962 | // No ID in URI, with selection |
| 1963 | cr.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, |
| 1964 | EmailContent.ID_SELECTION, new String[] {Long.toString(a2.mId)}); |
| 1965 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 1966 | assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount); |
| 1967 | assertEquals(3, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount); |
| 1968 | assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount); |
| 1969 | assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount); |
| 1970 | |
| 1971 | // With ID, with selection |
| 1972 | cr.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a3.mId), null, |
| 1973 | EmailContent.ID_SELECTION, new String[] {Long.toString(a3.mId)}); |
| 1974 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 1975 | assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount); |
| 1976 | assertEquals(0, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount); |
| 1977 | assertEquals(4, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount); |
| 1978 | assertEquals(5, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount); |
| 1979 | |
| 1980 | // No ID in URI, no selection |
| 1981 | cr.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, null, null); |
| 1982 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 1983 | assertEquals(0, Account.restoreAccountWithId(c, a2.mId).mNewMessageCount); |
| 1984 | assertEquals(0, Account.restoreAccountWithId(c, a3.mId).mNewMessageCount); |
| 1985 | assertEquals(0, Account.restoreAccountWithId(c, a4.mId).mNewMessageCount); |
| 1986 | assertEquals(0, Account.restoreAccountWithId(c, a5.mId).mNewMessageCount); |
| 1987 | } |
Makoto Onuki | 899c5b8 | 2010-09-26 16:16:21 -0700 | [diff] [blame] | 1988 | |
Makoto Onuki | 9d5aaea | 2010-12-02 16:33:49 -0800 | [diff] [blame] | 1989 | /** |
| 1990 | * Check if update on ACCOUNT_ID_ADD_TO_FIELD updates the cache properly. |
| 1991 | */ |
| 1992 | public void testUpdateCacheAccountIdAddToField() { |
| 1993 | final Context c = mMockContext; |
| 1994 | Account a1 = ProviderTestUtils.setupAccount("a1", true, c); |
| 1995 | |
| 1996 | int start = Account.restoreAccountWithId(c, a1.mId).mNewMessageCount; |
| 1997 | |
| 1998 | // +1 to NEW_MESSAGE_COUNT |
| 1999 | ContentValues cv = new ContentValues(); |
| 2000 | cv.put(EmailContent.FIELD_COLUMN_NAME, AccountColumns.NEW_MESSAGE_COUNT); |
| 2001 | cv.put(EmailContent.ADD_COLUMN_NAME, 1); |
| 2002 | mProvider.update(ContentUris.withAppendedId(Account.ADD_TO_FIELD_URI, a1.mId), cv, |
| 2003 | null, null); |
| 2004 | |
| 2005 | // Check |
| 2006 | assertEquals(start + 1, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 2007 | } |
| 2008 | |
| 2009 | /** |
| 2010 | * Check if update on ACCOUNT_RESET_NEW_COUNT updates the cache properly. |
| 2011 | */ |
| 2012 | public void testUpdateCacheAccountResetNewCount() { |
| 2013 | final Context c = mMockContext; |
| 2014 | Account a1 = ProviderTestUtils.setupAccount("a1", true, c); |
| 2015 | |
| 2016 | // precondition |
| 2017 | assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0); |
| 2018 | |
| 2019 | // Reset |
| 2020 | mProvider.update(Account.RESET_NEW_MESSAGE_COUNT_URI, null, null, null); |
| 2021 | |
| 2022 | // Check |
| 2023 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 2024 | } |
| 2025 | |
| 2026 | /** |
| 2027 | * Check if update on ACCOUNT_RESET_NEW_COUNT_ID updates the cache properly. |
| 2028 | */ |
| 2029 | public void testUpdateCacheAccountResetNewCountId() { |
| 2030 | final Context c = mMockContext; |
| 2031 | Account a1 = ProviderTestUtils.setupAccount("a1", true, c); |
| 2032 | |
| 2033 | // precondition |
| 2034 | assertTrue(Account.restoreAccountWithId(c, a1.mId).mNewMessageCount > 0); |
| 2035 | |
| 2036 | // Reset |
| 2037 | mProvider.update(ContentUris.withAppendedId(Account.RESET_NEW_MESSAGE_COUNT_URI, a1.mId), |
| 2038 | null, null, null); |
| 2039 | |
| 2040 | // Check |
| 2041 | assertEquals(0, Account.restoreAccountWithId(c, a1.mId).mNewMessageCount); |
| 2042 | } |
| 2043 | |
| 2044 | /** |
Marc Blank | d306ba3 | 2010-12-30 14:55:27 -0800 | [diff] [blame] | 2045 | * Check that we're handling illegal uri's properly (by throwing an exception unless it's a |
| 2046 | * query for an id of -1, in which case we return a zero-length cursor) |
| 2047 | */ |
| 2048 | public void testIllegalUri() { |
| 2049 | final ContentResolver cr = mMockContext.getContentResolver(); |
| 2050 | |
| 2051 | ContentValues cv = new ContentValues(); |
| 2052 | Uri uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/fooble"); |
| 2053 | try { |
| 2054 | cr.insert(uri, cv); |
| 2055 | fail("Insert should have thrown exception"); |
| 2056 | } catch (IllegalArgumentException e) { |
| 2057 | } |
| 2058 | try { |
| 2059 | cr.update(uri, cv, null, null); |
| 2060 | fail("Update should have thrown exception"); |
| 2061 | } catch (IllegalArgumentException e) { |
| 2062 | } |
| 2063 | try { |
| 2064 | cr.delete(uri, null, null); |
| 2065 | fail("Delete should have thrown exception"); |
| 2066 | } catch (IllegalArgumentException e) { |
| 2067 | } |
| 2068 | try { |
| 2069 | cr.query(uri, EmailContent.ID_PROJECTION, null, null, null); |
| 2070 | fail("Query should have thrown exception"); |
| 2071 | } catch (IllegalArgumentException e) { |
| 2072 | } |
| 2073 | uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/mailbox/fred"); |
| 2074 | try { |
| 2075 | cr.query(uri, EmailContent.ID_PROJECTION, null, null, null); |
| 2076 | fail("Query should have thrown exception"); |
| 2077 | } catch (IllegalArgumentException e) { |
| 2078 | } |
| 2079 | uri = Uri.parse("content://" + EmailContent.AUTHORITY + "/mailbox/-1"); |
| 2080 | Cursor c = cr.query(uri, EmailContent.ID_PROJECTION, null, null, null); |
| 2081 | assertNotNull(c); |
| 2082 | assertEquals(0, c.getCount()); |
| 2083 | c.close(); |
| 2084 | } |
Todd Kennedy | 2220877 | 2011-04-22 15:45:11 -0700 | [diff] [blame] | 2085 | |
| 2086 | /** |
| 2087 | * Verify {@link EmailProvider#recalculateMessageCount(android.database.sqlite.SQLiteDatabase)} |
| 2088 | */ |
| 2089 | public void testRecalculateMessageCounts() { |
| 2090 | final Context c = mMockContext; |
| 2091 | |
| 2092 | // Create accounts |
| 2093 | Account a1 = ProviderTestUtils.setupAccount("holdflag-1", true, c); |
| 2094 | Account a2 = ProviderTestUtils.setupAccount("holdflag-2", true, c); |
| 2095 | |
| 2096 | // Create mailboxes for each account |
| 2097 | Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_INBOX); |
| 2098 | Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a1.mId, true, c, Mailbox.TYPE_OUTBOX); |
| 2099 | Mailbox b3 = ProviderTestUtils.setupMailbox("box3", a2.mId, true, c, Mailbox.TYPE_INBOX); |
| 2100 | Mailbox b4 = ProviderTestUtils.setupMailbox("box4", a2.mId, true, c, Mailbox.TYPE_OUTBOX); |
| 2101 | Mailbox bt = ProviderTestUtils.setupMailbox("boxT", a2.mId, true, c, Mailbox.TYPE_TRASH); |
| 2102 | |
| 2103 | // Create some messages |
| 2104 | // b1 (account 1, inbox): 1 message, including 1 starred |
| 2105 | Message m11 = createMessage(c, b1, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2106 | |
| 2107 | // b2 (account 1, outbox): 2 message, including 1 starred |
| 2108 | Message m21 = createMessage(c, b2, false, false, Message.FLAG_LOADED_COMPLETE); |
| 2109 | Message m22 = createMessage(c, b2, true, true, Message.FLAG_LOADED_COMPLETE); |
| 2110 | |
| 2111 | // b3 (account 2, inbox): 3 message, including 1 starred |
| 2112 | Message m31 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE); |
| 2113 | Message m32 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE); |
| 2114 | Message m33 = createMessage(c, b3, true, true, Message.FLAG_LOADED_COMPLETE); |
| 2115 | |
| 2116 | // b4 (account 2, outbox) has no messages. |
| 2117 | |
| 2118 | // bt (account 2, trash) has 3 messages, including 2 starred |
| 2119 | Message mt1 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2120 | Message mt2 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2121 | Message mt3 = createMessage(c, bt, false, false, Message.FLAG_LOADED_COMPLETE); |
| 2122 | |
| 2123 | // Verifiy initial message counts |
| 2124 | assertEquals(1, getMessageCount(b1.mId)); |
| 2125 | assertEquals(2, getMessageCount(b2.mId)); |
| 2126 | assertEquals(3, getMessageCount(b3.mId)); |
| 2127 | assertEquals(0, getMessageCount(b4.mId)); |
| 2128 | assertEquals(3, getMessageCount(bt.mId)); |
| 2129 | |
| 2130 | // Whew. The setup is done; now let's actually get to the test |
| 2131 | |
| 2132 | // First, invalidate the message counts. |
| 2133 | setMinusOneToMessageCounts(); |
| 2134 | assertEquals(-1, getMessageCount(b1.mId)); |
| 2135 | assertEquals(-1, getMessageCount(b2.mId)); |
| 2136 | assertEquals(-1, getMessageCount(b3.mId)); |
| 2137 | assertEquals(-1, getMessageCount(b4.mId)); |
| 2138 | assertEquals(-1, getMessageCount(bt.mId)); |
| 2139 | |
| 2140 | // Batch update. |
| 2141 | SQLiteDatabase db = getProvider().getDatabase(mMockContext); |
| 2142 | EmailProvider.recalculateMessageCount(db); |
| 2143 | |
| 2144 | // Check message counts are valid again |
| 2145 | assertEquals(1, getMessageCount(b1.mId)); |
| 2146 | assertEquals(2, getMessageCount(b2.mId)); |
| 2147 | assertEquals(3, getMessageCount(b3.mId)); |
| 2148 | assertEquals(0, getMessageCount(b4.mId)); |
| 2149 | assertEquals(3, getMessageCount(bt.mId)); |
| 2150 | } |
| 2151 | |
| 2152 | /** Creates an account */ |
| 2153 | private Account createAccount(Context c, String name, HostAuth recvAuth, HostAuth sendAuth) { |
| 2154 | Account account = ProviderTestUtils.setupAccount(name, false, c); |
| 2155 | if (recvAuth != null) { |
| 2156 | account.mHostAuthKeyRecv = recvAuth.mId; |
| 2157 | if (sendAuth == null) { |
| 2158 | account.mHostAuthKeySend = recvAuth.mId; |
| 2159 | } |
| 2160 | } |
| 2161 | if (sendAuth != null) { |
| 2162 | account.mHostAuthKeySend = sendAuth.mId; |
| 2163 | } |
| 2164 | account.save(c); |
| 2165 | return account; |
| 2166 | } |
| 2167 | |
| 2168 | /** Creates a mailbox; redefine as we need version 17 mailbox values */ |
| 2169 | private Mailbox createMailbox(Context c, String displayName, String serverId, long parentKey, |
| 2170 | long accountId) { |
| 2171 | Mailbox box = new Mailbox(); |
| 2172 | |
| 2173 | box.mDisplayName = displayName; |
| 2174 | box.mServerId = serverId; |
| 2175 | box.mParentKey = parentKey; |
| 2176 | box.mAccountKey = accountId; |
| 2177 | // Don't care about the fields below ... set them for giggles |
| 2178 | box.mType = Mailbox.TYPE_MAIL; |
| 2179 | box.mDelimiter = '/'; |
| 2180 | box.mSyncKey = "sync-key"; |
| 2181 | box.mSyncLookback = 2; |
Marc Blank | f5418f1 | 2011-06-13 15:32:27 -0700 | [diff] [blame] | 2182 | box.mSyncInterval = Account.CHECK_INTERVAL_NEVER; |
Todd Kennedy | 2220877 | 2011-04-22 15:45:11 -0700 | [diff] [blame] | 2183 | box.mSyncTime = 3; |
| 2184 | box.mFlagVisible = true; |
| 2185 | box.mFlags = 5; |
| 2186 | box.mVisibleLimit = 6; |
| 2187 | box.save(c); |
| 2188 | return box; |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * Asserts equality between two mailboxes. We define this as we don't have implementations |
| 2193 | * for Mailbox#equals(). |
| 2194 | */ |
| 2195 | private void assertEquals(Mailbox expected, Mailbox actual) { |
| 2196 | if (expected == null && actual == null) return; |
| 2197 | assertTrue(expected != null && actual != null); |
| 2198 | assertEqualsExceptServerId(expected, actual, expected.mServerId); |
| 2199 | } |
| 2200 | |
| 2201 | /** |
| 2202 | * Asserts equality between the two mailboxes EXCEPT for the server id. The given server |
| 2203 | * ID is the expected value. |
| 2204 | */ |
| 2205 | private void assertEqualsExceptServerId(Mailbox expected, Mailbox actual, String serverId) { |
| 2206 | if (expected == null && actual == null) return; |
| 2207 | |
| 2208 | assertTrue(expected != null && actual != null); |
| 2209 | assertEquals(expected.mDisplayName, actual.mDisplayName); |
| 2210 | assertEquals(serverId, actual.mServerId); |
| 2211 | assertEquals(expected.mParentKey, actual.mParentKey); |
| 2212 | assertEquals(expected.mAccountKey, actual.mAccountKey); |
| 2213 | } |
| 2214 | |
| 2215 | /** Verifies updating the DB from v17 to v18 works as expected */ |
| 2216 | public void testUpgradeFromVersion17ToVersion18() { |
| 2217 | final Context c = mMockContext; |
| 2218 | // Create accounts |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 2219 | Account a1 = createAccount(c, "exchange", |
Todd Kennedy | 2220877 | 2011-04-22 15:45:11 -0700 | [diff] [blame] | 2220 | ProviderTestUtils.setupHostAuth("eas", "exchange.host.com", true, c), |
| 2221 | null); |
| 2222 | Account a2 = createAccount(c, "imap", |
| 2223 | ProviderTestUtils.setupHostAuth("imap", "imap.host.com", true, c), |
| 2224 | ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, c)); |
| 2225 | Account a3 = createAccount(c, "pop3", |
| 2226 | ProviderTestUtils.setupHostAuth("pop3", "imap.host.com", true, c), |
| 2227 | ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, c)); |
| 2228 | |
| 2229 | // Create mailboxes; some w/ valid parent IDs, others without |
| 2230 | Mailbox b11 = createMailbox(c, "box1", "12", 0L, a1.mId); |
| 2231 | Mailbox b12 = createMailbox(c, "box2", "67", -1L, a1.mId); |
| 2232 | Mailbox b13 = createMailbox(c, "box3", "18", b12.mId, a1.mId); |
| 2233 | |
| 2234 | Mailbox b21 = createMailbox(c, "box4", null, 0L, a2.mId); |
| 2235 | Mailbox b22 = createMailbox(c, "box4/foo/bar", "will-be-replaced", 0L, a2.mId); |
| 2236 | Mailbox b23 = createMailbox(c, "box5", null, -1L, a2.mId); |
| 2237 | Mailbox b24 = createMailbox(c, "box6", "box5/box6", b23.mId, a2.mId); |
| 2238 | |
| 2239 | Mailbox b31 = createMailbox(c, "box7", "12", 0L, a3.mId); |
| 2240 | Mailbox b32 = createMailbox(c, "box8/foo/bar", "will-be-replaced", 0L, a3.mId); |
| 2241 | Mailbox b33 = createMailbox(c, "box9", "box9", -1L, a3.mId); |
| 2242 | Mailbox b34 = createMailbox(c, "boxA", "box9/boxA", b33.mId, a3.mId); |
| 2243 | |
| 2244 | // Sanity check the mailboxes that were just added |
| 2245 | Mailbox testMailbox; |
| 2246 | testMailbox = Mailbox.restoreMailboxWithId(c, b11.mId); |
| 2247 | assertEquals(b11, testMailbox); |
| 2248 | testMailbox = Mailbox.restoreMailboxWithId(c, b12.mId); |
| 2249 | assertEquals(b12, testMailbox); |
| 2250 | testMailbox = Mailbox.restoreMailboxWithId(c, b13.mId); |
| 2251 | assertEquals(b13, testMailbox); |
| 2252 | testMailbox = Mailbox.restoreMailboxWithId(c, b21.mId); |
| 2253 | assertEqualsExceptServerId(b21, testMailbox, null); |
| 2254 | testMailbox = Mailbox.restoreMailboxWithId(c, b22.mId); |
| 2255 | assertEqualsExceptServerId(b22, testMailbox, "will-be-replaced"); |
| 2256 | testMailbox = Mailbox.restoreMailboxWithId(c, b23.mId); |
| 2257 | assertEquals(b23, testMailbox); |
| 2258 | testMailbox = Mailbox.restoreMailboxWithId(c, b24.mId); |
| 2259 | assertEquals(b24, testMailbox); |
| 2260 | testMailbox = Mailbox.restoreMailboxWithId(c, b31.mId); |
| 2261 | assertEqualsExceptServerId(b31, testMailbox, "12"); |
| 2262 | testMailbox = Mailbox.restoreMailboxWithId(c, b32.mId); |
| 2263 | assertEqualsExceptServerId(b32, testMailbox, "will-be-replaced"); |
| 2264 | testMailbox = Mailbox.restoreMailboxWithId(c, b33.mId); |
| 2265 | assertEquals(b33, testMailbox); |
| 2266 | testMailbox = Mailbox.restoreMailboxWithId(c, b34.mId); |
| 2267 | assertEquals(b34, testMailbox); |
| 2268 | |
| 2269 | SQLiteDatabase db = getProvider().getDatabase(mMockContext); |
| 2270 | EmailProvider.upgradeFromVersion17ToVersion18(db); |
| 2271 | |
| 2272 | // Verify that only IMAP/POP3 mailboxes w/ a parent key of '0' are changed |
| 2273 | // Exchange mailboxes; none should be changed |
| 2274 | testMailbox = Mailbox.restoreMailboxWithId(c, b11.mId); |
| 2275 | assertEquals(b11, testMailbox); |
| 2276 | testMailbox = Mailbox.restoreMailboxWithId(c, b12.mId); |
| 2277 | assertEquals(b12, testMailbox); |
| 2278 | testMailbox = Mailbox.restoreMailboxWithId(c, b13.mId); |
| 2279 | assertEquals(b13, testMailbox); |
| 2280 | |
| 2281 | // IMAP mailboxes; only mailboxes w/ a parent id of '0' are changed |
| 2282 | testMailbox = Mailbox.restoreMailboxWithId(c, b21.mId); |
| 2283 | assertEqualsExceptServerId(b21, testMailbox, "box4"); |
| 2284 | testMailbox = Mailbox.restoreMailboxWithId(c, b22.mId); |
| 2285 | assertEqualsExceptServerId(b22, testMailbox, "box4/foo/bar"); |
| 2286 | testMailbox = Mailbox.restoreMailboxWithId(c, b23.mId); |
| 2287 | assertEquals(b23, testMailbox); |
| 2288 | testMailbox = Mailbox.restoreMailboxWithId(c, b24.mId); |
| 2289 | assertEquals(b24, testMailbox); |
| 2290 | |
| 2291 | // POP3 mailboxes; only mailboxes w/ a parent id of '0' are changed |
| 2292 | testMailbox = Mailbox.restoreMailboxWithId(c, b31.mId); |
| 2293 | assertEqualsExceptServerId(b31, testMailbox, "box7"); |
| 2294 | testMailbox = Mailbox.restoreMailboxWithId(c, b32.mId); |
| 2295 | assertEqualsExceptServerId(b32, testMailbox, "box8/foo/bar"); |
| 2296 | testMailbox = Mailbox.restoreMailboxWithId(c, b33.mId); |
| 2297 | assertEquals(b33, testMailbox); |
| 2298 | testMailbox = Mailbox.restoreMailboxWithId(c, b34.mId); |
| 2299 | assertEquals(b34, testMailbox); |
| 2300 | } |
Makoto Onuki | 4c4e4c3 | 2011-05-13 14:09:22 -0700 | [diff] [blame] | 2301 | |
Marc Blank | f3ff0ba | 2011-05-19 14:14:14 -0700 | [diff] [blame] | 2302 | /** |
| 2303 | * Determine whether a list of AccountManager accounts includes a given EmailProvider account |
| 2304 | * @param amAccountList a list of AccountManager accounts |
| 2305 | * @param account an EmailProvider account |
| 2306 | * @param context the caller's context (our test provider's context) |
| 2307 | * @return whether or not the EmailProvider account is represented in AccountManager |
| 2308 | */ |
| 2309 | private boolean amAccountListHasAccount(android.accounts.Account[] amAccountList, |
| 2310 | Account account, Context context) { |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 2311 | String email = account.mEmailAddress; |
Marc Blank | f3ff0ba | 2011-05-19 14:14:14 -0700 | [diff] [blame] | 2312 | for (android.accounts.Account amAccount: amAccountList) { |
Ben Komalo | 32bed4b | 2011-08-23 18:02:11 -0700 | [diff] [blame] | 2313 | if (amAccount.name.equals(email)) { |
Marc Blank | f3ff0ba | 2011-05-19 14:14:14 -0700 | [diff] [blame] | 2314 | return true; |
| 2315 | } |
| 2316 | } |
| 2317 | return false; |
| 2318 | } |
| 2319 | |
Marc Blank | 6e418aa | 2011-06-18 18:03:11 -0700 | [diff] [blame] | 2320 | public void testAutoCacheNewContent() { |
| 2321 | Account account = ProviderTestUtils.setupAccount("account-hostauth", false, mMockContext); |
| 2322 | // add hostauth data, which should be saved the first time |
| 2323 | account.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-hostauth-recv", -1, false, |
| 2324 | mMockContext); |
| 2325 | account.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-hostauth-send", -1, false, |
| 2326 | mMockContext); |
| 2327 | account.save(mMockContext); |
| 2328 | assertTrue(mProvider.isCached(Account.CONTENT_URI, account.mId)); |
| 2329 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, account.mHostAuthRecv.mId)); |
| 2330 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, account.mHostAuthSend.mId)); |
| 2331 | } |
| 2332 | |
| 2333 | /** Creates a mailbox; redefine as we need version 17 mailbox values */ |
| 2334 | private Mailbox createTypeMailbox(Context c, long accountId, int type) { |
| 2335 | Mailbox box = new Mailbox(); |
| 2336 | |
| 2337 | box.mDisplayName = "foo"; |
| 2338 | box.mServerId = "1:1"; |
| 2339 | box.mParentKey = 0; |
| 2340 | box.mAccountKey = accountId; |
| 2341 | // Don't care about the fields below ... set them for giggles |
| 2342 | box.mType = type; |
| 2343 | box.save(c); |
| 2344 | return box; |
| 2345 | } |
| 2346 | |
| 2347 | public void testAutoCacheInvalidate() { |
| 2348 | // Create 3 accounts with hostauth and 3 mailboxes each (2 of which are pre-cached) |
| 2349 | Account a = ProviderTestUtils.setupAccount("account1", false, mMockContext); |
| 2350 | a.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-recv", -1, false, |
| 2351 | mMockContext); |
| 2352 | a.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-send", -1, false, |
| 2353 | mMockContext); |
| 2354 | a.save(mMockContext); |
| 2355 | Mailbox a1 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_INBOX); |
| 2356 | Mailbox a2 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_MAIL); |
| 2357 | Mailbox a3 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_DRAFTS); |
| 2358 | Account b = ProviderTestUtils.setupAccount("account2", false, mMockContext); |
| 2359 | b.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-recv", -1, false, |
| 2360 | mMockContext); |
| 2361 | b.mHostAuthSend = ProviderTestUtils.setupHostAuth("accoun-send", -1, false, |
| 2362 | mMockContext); |
| 2363 | b.save(mMockContext); |
| 2364 | Mailbox b1 = createTypeMailbox(mMockContext, b.mId, Mailbox.TYPE_OUTBOX); |
| 2365 | Mailbox b2 = createTypeMailbox(mMockContext, b.mId, Mailbox.TYPE_MAIL); |
| 2366 | Mailbox b3 = createTypeMailbox(mMockContext, b.mId, Mailbox.TYPE_SENT); |
| 2367 | Account c = ProviderTestUtils.setupAccount("account3", false, mMockContext); |
| 2368 | c.mHostAuthRecv = ProviderTestUtils.setupHostAuth("account-recv", -1, false, |
| 2369 | mMockContext); |
| 2370 | c.mHostAuthSend = ProviderTestUtils.setupHostAuth("account-send", -1, false, |
| 2371 | mMockContext); |
| 2372 | c.save(mMockContext); |
| 2373 | Mailbox c1 = createTypeMailbox(mMockContext, c.mId, Mailbox.TYPE_SEARCH); |
| 2374 | Mailbox c2 = createTypeMailbox(mMockContext, c.mId, Mailbox.TYPE_MAIL); |
| 2375 | Mailbox c3 = createTypeMailbox(mMockContext, c.mId, Mailbox.TYPE_TRASH); |
| 2376 | |
| 2377 | // Confirm expected cache state |
| 2378 | assertTrue(mProvider.isCached(Account.CONTENT_URI, a.mId)); |
| 2379 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, a.mHostAuthRecv.mId)); |
| 2380 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, a.mHostAuthSend.mId)); |
| 2381 | assertTrue(mProvider.isCached(Account.CONTENT_URI, b.mId)); |
| 2382 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, b.mHostAuthRecv.mId)); |
| 2383 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, b.mHostAuthSend.mId)); |
| 2384 | assertTrue(mProvider.isCached(Account.CONTENT_URI, c.mId)); |
| 2385 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, c.mHostAuthRecv.mId)); |
| 2386 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, c.mHostAuthSend.mId)); |
| 2387 | |
| 2388 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, a1.mId)); |
| 2389 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, a2.mId)); |
| 2390 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, a3.mId)); |
| 2391 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, b1.mId)); |
| 2392 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, b2.mId)); |
| 2393 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, b3.mId)); |
| 2394 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, c1.mId)); |
| 2395 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, c2.mId)); |
| 2396 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, c3.mId)); |
| 2397 | |
| 2398 | // Delete account b |
| 2399 | EmailContent.delete(mMockContext, Account.CONTENT_URI, b.mId); |
| 2400 | |
| 2401 | // Confirm cache state |
| 2402 | assertTrue(mProvider.isCached(Account.CONTENT_URI, a.mId)); |
| 2403 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, a.mHostAuthRecv.mId)); |
| 2404 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, a.mHostAuthSend.mId)); |
| 2405 | assertFalse(mProvider.isCached(Account.CONTENT_URI, b.mId)); |
| 2406 | assertFalse(mProvider.isCached(HostAuth.CONTENT_URI, b.mHostAuthRecv.mId)); |
| 2407 | assertFalse(mProvider.isCached(HostAuth.CONTENT_URI, b.mHostAuthSend.mId)); |
| 2408 | assertTrue(mProvider.isCached(Account.CONTENT_URI, c.mId)); |
| 2409 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, c.mHostAuthRecv.mId)); |
| 2410 | assertTrue(mProvider.isCached(HostAuth.CONTENT_URI, c.mHostAuthSend.mId)); |
| 2411 | |
| 2412 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, a1.mId)); |
| 2413 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, a2.mId)); |
| 2414 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, a3.mId)); |
| 2415 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, b1.mId)); |
| 2416 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, b2.mId)); |
| 2417 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, b3.mId)); |
| 2418 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, c1.mId)); |
| 2419 | assertFalse(mProvider.isCached(Mailbox.CONTENT_URI, c2.mId)); |
| 2420 | assertTrue(mProvider.isCached(Mailbox.CONTENT_URI, c3.mId)); |
| 2421 | } |
| 2422 | |
Marc Blank | f3ff0ba | 2011-05-19 14:14:14 -0700 | [diff] [blame] | 2423 | /** |
| 2424 | * Remove a single pop/imap account from the AccountManager |
| 2425 | * @param accountManager our AccountManager |
| 2426 | * @param name the name of the test account to remove |
| 2427 | */ |
| 2428 | private void removeAccountManagerAccount(AccountManager accountManager, String name) { |
| 2429 | try { |
| 2430 | accountManager.removeAccount( |
| 2431 | new android.accounts.Account(name, AccountManagerTypes.TYPE_POP_IMAP), |
| 2432 | null, null).getResult(); |
| 2433 | } catch (OperationCanceledException e) { |
| 2434 | } catch (AuthenticatorException e) { |
| 2435 | } catch (IOException e) { |
| 2436 | } |
| 2437 | } |
| 2438 | |
| 2439 | /** |
| 2440 | * Remove all test accounts from the AccountManager |
| 2441 | * @param accountManager the AccountManager |
| 2442 | */ |
| 2443 | private void cleanupTestAccountManagerAccounts(AccountManager accountManager) { |
| 2444 | android.accounts.Account[] amAccountList = |
| 2445 | accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP); |
| 2446 | for (android.accounts.Account account: amAccountList) { |
| 2447 | if (account.name.startsWith(AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX)) { |
| 2448 | removeAccountManagerAccount(accountManager, account.name); |
| 2449 | } |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | /** Verifies updating the DB from v21 to v22 works as expected */ |
| 2454 | public void testUpgradeFromVersion21ToVersion22() { |
| 2455 | String imapTestLogin = |
| 2456 | AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX + "imap.host.com"; |
| 2457 | String pop3TestLogin = |
| 2458 | AccountReconciler.ACCOUNT_MANAGER_ACCOUNT_TEST_PREFIX + "pop3.host.com"; |
| 2459 | AccountManager accountManager = AccountManager.get(mContext); |
| 2460 | |
| 2461 | // Create provider accounts (one of each type) |
| 2462 | Account a1 = createAccount(mMockContext, "exchange", |
| 2463 | ProviderTestUtils.setupHostAuth("eas", "exchange.host.com", true, mMockContext), |
| 2464 | null); |
| 2465 | HostAuth h2 = |
| 2466 | ProviderTestUtils.setupHostAuth("imap", "imap.host.com", false, mMockContext); |
| 2467 | h2.mLogin = imapTestLogin; |
| 2468 | h2.save(mMockContext); |
| 2469 | Account a2 = createAccount(mMockContext, "imap", h2, |
| 2470 | ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, mMockContext)); |
| 2471 | HostAuth h3 = |
| 2472 | ProviderTestUtils.setupHostAuth("pop3", "pop3.host.com", false, mMockContext); |
| 2473 | h3.mLogin = pop3TestLogin; |
| 2474 | h3.save(mMockContext); |
| 2475 | Account a3 = createAccount(mMockContext, "pop3", h3, |
| 2476 | ProviderTestUtils.setupHostAuth("smtp", "smtp.host.com", true, mMockContext)); |
| 2477 | |
| 2478 | // Get the current list of AccountManager accounts (we have to use the real context here), |
| 2479 | // whereas we use the mock context for EmailProvider (this is because the mock context |
| 2480 | // doesn't implement AccountManager hooks) |
| 2481 | android.accounts.Account[] amAccountList = |
| 2482 | accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP); |
| 2483 | // There shouldn't be AccountManager accounts for these |
| 2484 | assertFalse(amAccountListHasAccount(amAccountList, a1, mMockContext)); |
| 2485 | assertFalse(amAccountListHasAccount(amAccountList, a2, mMockContext)); |
| 2486 | assertFalse(amAccountListHasAccount(amAccountList, a3, mMockContext)); |
| 2487 | |
| 2488 | amAccountList = null; |
| 2489 | try { |
| 2490 | // Upgrade the database |
| 2491 | SQLiteDatabase db = getProvider().getDatabase(mMockContext); |
| 2492 | EmailProvider.upgradeFromVersion21ToVersion22(db, getContext()); |
| 2493 | |
| 2494 | // The pop3 and imap account should now be in account manager |
| 2495 | amAccountList = accountManager.getAccountsByType(AccountManagerTypes.TYPE_POP_IMAP); |
| 2496 | assertFalse(amAccountListHasAccount(amAccountList, a1, mMockContext)); |
| 2497 | assertTrue(amAccountListHasAccount(amAccountList, a2, mMockContext)); |
| 2498 | assertTrue(amAccountListHasAccount(amAccountList, a3, mMockContext)); |
| 2499 | } finally { |
| 2500 | cleanupTestAccountManagerAccounts(accountManager); |
| 2501 | } |
| 2502 | } |
Marc Blank | 2bdf7ee | 2011-06-30 16:03:36 -0700 | [diff] [blame] | 2503 | |
| 2504 | public void testCleanupOrphans() { |
| 2505 | EmailProvider ep = getProvider(); |
| 2506 | SQLiteDatabase db = ep.getDatabase(mMockContext); |
| 2507 | |
| 2508 | Account a = ProviderTestUtils.setupAccount("account1", true, mMockContext); |
| 2509 | // Mailbox a1 and a3 won't have a valid account |
| 2510 | Mailbox a1 = createTypeMailbox(mMockContext, -1, Mailbox.TYPE_INBOX); |
| 2511 | Mailbox a2 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_MAIL); |
| 2512 | Mailbox a3 = createTypeMailbox(mMockContext, -1, Mailbox.TYPE_DRAFTS); |
| 2513 | Mailbox a4 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_SENT); |
| 2514 | Mailbox a5 = createTypeMailbox(mMockContext, a.mId, Mailbox.TYPE_TRASH); |
| 2515 | // Mailbox ax isn't even saved; use an obviously invalid id |
| 2516 | Mailbox ax = new Mailbox(); |
| 2517 | ax.mId = 69105; |
| 2518 | |
| 2519 | // Message mt2 is an orphan, as is mt4 |
| 2520 | Message m1 = createMessage(mMockContext, a1, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2521 | Message m2 = createMessage(mMockContext, a2, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2522 | Message m3 = createMessage(mMockContext, a3, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2523 | Message m4 = createMessage(mMockContext, a4, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2524 | Message m5 = createMessage(mMockContext, a5, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2525 | Message mx = createMessage(mMockContext, ax, true, false, Message.FLAG_LOADED_COMPLETE); |
| 2526 | |
| 2527 | // Two orphan policies |
| 2528 | Policy p1 = new Policy(); |
| 2529 | p1.save(mMockContext); |
| 2530 | Policy p2 = new Policy(); |
| 2531 | p2.save(mMockContext); |
| 2532 | Policy p3 = new Policy(); |
Marc Blank | 2736c1a | 2011-10-20 10:13:02 -0700 | [diff] [blame] | 2533 | SecurityPolicy.setAccountPolicy(mMockContext, a, p3, "0"); |
Marc Blank | 2bdf7ee | 2011-06-30 16:03:36 -0700 | [diff] [blame] | 2534 | |
| 2535 | // We don't want anything cached or the tests below won't work. Note that |
| 2536 | // deleteUnlinked is only called by EmailProvider when the caches are empty |
| 2537 | ContentCache.invalidateAllCaches(); |
| 2538 | // Delete orphaned mailboxes/messages/policies |
| 2539 | ep.deleteUnlinked(db, Mailbox.TABLE_NAME, MailboxColumns.ACCOUNT_KEY, AccountColumns.ID, |
| 2540 | Account.TABLE_NAME); |
| 2541 | ep.deleteUnlinked(db, Message.TABLE_NAME, MessageColumns.ACCOUNT_KEY, AccountColumns.ID, |
| 2542 | Account.TABLE_NAME); |
| 2543 | ep.deleteUnlinked(db, Policy.TABLE_NAME, PolicyColumns.ID, AccountColumns.POLICY_KEY, |
| 2544 | Account.TABLE_NAME); |
| 2545 | |
| 2546 | // Make sure the orphaned mailboxes are gone |
| 2547 | assertNull(Mailbox.restoreMailboxWithId(mMockContext, a1.mId)); |
| 2548 | assertNotNull(Mailbox.restoreMailboxWithId(mMockContext, a2.mId)); |
| 2549 | assertNull(Mailbox.restoreMailboxWithId(mMockContext, a3.mId)); |
| 2550 | assertNotNull(Mailbox.restoreMailboxWithId(mMockContext, a4.mId)); |
| 2551 | assertNotNull(Mailbox.restoreMailboxWithId(mMockContext, a5.mId)); |
| 2552 | assertNull(Mailbox.restoreMailboxWithId(mMockContext, ax.mId)); |
| 2553 | |
| 2554 | // Make sure orphaned messages are gone |
| 2555 | assertNull(Message.restoreMessageWithId(mMockContext, m1.mId)); |
| 2556 | assertNotNull(Message.restoreMessageWithId(mMockContext, m2.mId)); |
| 2557 | assertNull(Message.restoreMessageWithId(mMockContext, m3.mId)); |
| 2558 | assertNotNull(Message.restoreMessageWithId(mMockContext, m4.mId)); |
| 2559 | assertNotNull(Message.restoreMessageWithId(mMockContext, m5.mId)); |
| 2560 | assertNull(Message.restoreMessageWithId(mMockContext, mx.mId)); |
| 2561 | |
| 2562 | // Make sure orphaned policies are gone |
| 2563 | assertNull(Policy.restorePolicyWithId(mMockContext, p1.mId)); |
| 2564 | assertNull(Policy.restorePolicyWithId(mMockContext, p2.mId)); |
| 2565 | a = Account.restoreAccountWithId(mMockContext, a.mId); |
| 2566 | assertNotNull(Policy.restorePolicyWithId(mMockContext, a.mPolicyKey)); |
| 2567 | } |
Andrew Stadler | 7143d96 | 2009-06-25 00:02:29 -0700 | [diff] [blame] | 2568 | } |