ControllerProviderOpsTests.java revision 4be3bc1ec8a52308ee1998f0a31d7e491e06b3c1
1/* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.email; 18 19import com.android.email.provider.EmailContent; 20import com.android.email.provider.EmailContent.Account; 21import com.android.email.provider.EmailContent.Body; 22import com.android.email.provider.EmailContent.HostAuth; 23import com.android.email.provider.EmailContent.Mailbox; 24import com.android.email.provider.EmailContent.Message; 25import com.android.email.provider.EmailProvider; 26import com.android.email.provider.ProviderTestUtils; 27 28import android.content.Context; 29import android.net.Uri; 30import android.test.ProviderTestCase2; 31 32import java.util.Locale; 33 34/** 35 * Tests of the Controller class that depend on the underlying provider. 36 * 37 * NOTE: It would probably make sense to rewrite this using a MockProvider, instead of the 38 * ProviderTestCase (which is a real provider running on a temp database). This would be more of 39 * a true "unit test". 40 * 41 * You can run this entire test case with: 42 * runtest -c com.android.email.ControllerProviderOpsTests email 43 */ 44public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider> { 45 46 private Context mProviderContext; 47 private Context mContext; 48 private TestController mTestController; 49 50 51 public ControllerProviderOpsTests() { 52 super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY); 53 } 54 55 @Override 56 public void setUp() throws Exception { 57 super.setUp(); 58 mProviderContext = getMockContext(); 59 mContext = getContext(); 60 mTestController = new TestController(mProviderContext, mContext); 61 } 62 63 @Override 64 public void tearDown() throws Exception { 65 super.tearDown(); 66 mTestController.cleanupForTest(); 67 } 68 69 /** 70 * Lightweight subclass of the Controller class allows injection of mock context 71 */ 72 public static class TestController extends Controller { 73 74 protected TestController(Context providerContext, Context systemContext) { 75 super(systemContext); 76 setProviderContext(providerContext); 77 } 78 } 79 80 /** 81 * These are strings that should not change per locale. 82 */ 83 public void testGetMailboxServerName() { 84 assertEquals("", mTestController.getMailboxServerName(-1)); 85 86 assertEquals("Inbox", mTestController.getMailboxServerName(Mailbox.TYPE_INBOX)); 87 assertEquals("Outbox", mTestController.getMailboxServerName(Mailbox.TYPE_OUTBOX)); 88 assertEquals("Trash", mTestController.getMailboxServerName(Mailbox.TYPE_TRASH)); 89 assertEquals("Sent", mTestController.getMailboxServerName(Mailbox.TYPE_SENT)); 90 assertEquals("Junk", mTestController.getMailboxServerName(Mailbox.TYPE_JUNK)); 91 92 // Now try again with translation 93 Locale savedLocale = Locale.getDefault(); 94 Locale.setDefault(Locale.FRANCE); 95 assertEquals("Inbox", mTestController.getMailboxServerName(Mailbox.TYPE_INBOX)); 96 assertEquals("Outbox", mTestController.getMailboxServerName(Mailbox.TYPE_OUTBOX)); 97 assertEquals("Trash", mTestController.getMailboxServerName(Mailbox.TYPE_TRASH)); 98 assertEquals("Sent", mTestController.getMailboxServerName(Mailbox.TYPE_SENT)); 99 assertEquals("Junk", mTestController.getMailboxServerName(Mailbox.TYPE_JUNK)); 100 Locale.setDefault(savedLocale); 101 } 102 103 /** 104 * Test of Controller.createMailbox(). 105 * Sunny day test only - creates a mailbox that does not exist. 106 * Does not test duplication, bad accountID, or any other bad input. 107 */ 108 public void testCreateMailbox() { 109 Account account = ProviderTestUtils.setupAccount("mailboxid", true, mProviderContext); 110 long accountId = account.mId; 111 112 long oldBoxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS); 113 assertEquals(Mailbox.NO_MAILBOX, oldBoxId); 114 115 mTestController.createMailbox(accountId, Mailbox.TYPE_DRAFTS); 116 long boxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS); 117 118 // check that the drafts mailbox exists 119 assertTrue("mailbox exists", boxId != Mailbox.NO_MAILBOX); 120 } 121 122 /** 123 * Test of Controller.findOrCreateMailboxOfType(). 124 * Checks: 125 * - finds correctly the ID of existing mailbox 126 * - creates non-existing mailbox 127 * - creates only once a new mailbox 128 * - when accountId or mailboxType are -1, returns NO_MAILBOX 129 */ 130 public void testFindOrCreateMailboxOfType() { 131 Account account = ProviderTestUtils.setupAccount("mailboxid", true, mProviderContext); 132 long accountId = account.mId; 133 Mailbox box = ProviderTestUtils.setupMailbox("box", accountId, false, mProviderContext); 134 final int boxType = Mailbox.TYPE_TRASH; 135 box.mType = boxType; 136 box.save(mProviderContext); 137 long boxId = box.mId; 138 139 long testBoxId = mTestController.findOrCreateMailboxOfType(accountId, boxType); 140 141 // check it found the right mailbox id 142 assertEquals(boxId, testBoxId); 143 144 long boxId2 = mTestController.findOrCreateMailboxOfType(accountId, Mailbox.TYPE_DRAFTS); 145 assertTrue("mailbox created", boxId2 != Mailbox.NO_MAILBOX); 146 assertTrue("with different id", testBoxId != boxId2); 147 148 // check it doesn't create twice when existing 149 long boxId3 = mTestController.findOrCreateMailboxOfType(accountId, Mailbox.TYPE_DRAFTS); 150 assertEquals("don't create if exists", boxId3, boxId2); 151 152 // check invalid aruments 153 assertEquals(Mailbox.NO_MAILBOX, 154 mTestController.findOrCreateMailboxOfType(-1, Mailbox.TYPE_DRAFTS)); 155 assertEquals(Mailbox.NO_MAILBOX, mTestController.findOrCreateMailboxOfType(accountId, -1)); 156 } 157 158 /** 159 * Test the "delete message" function. Sunny day: 160 * - message/mailbox/account all exist 161 * - trash mailbox exists 162 */ 163 public void testDeleteMessage() { 164 Account account1 = ProviderTestUtils.setupAccount("message-delete", true, mProviderContext); 165 long account1Id = account1.mId; 166 Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mProviderContext); 167 long box1Id = box1.mId; 168 Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account1Id, false, mProviderContext); 169 box2.mType = EmailContent.Mailbox.TYPE_TRASH; 170 box2.save(mProviderContext); 171 long box2Id = box2.mId; 172 173 Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, 174 true, mProviderContext); 175 long message1Id = message1.mId; 176 177 mTestController.deleteMessage(message1Id, account1Id); 178 179 // now read back a fresh copy and confirm it's in the trash 180 Message message1get = EmailContent.Message.restoreMessageWithId(mProviderContext, 181 message1Id); 182 assertEquals(box2Id, message1get.mMailboxKey); 183 184 // Now repeat test with accountId "unknown" 185 Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false, 186 true, mProviderContext); 187 long message2Id = message2.mId; 188 189 mTestController.deleteMessage(message2Id, -1); 190 191 // now read back a fresh copy and confirm it's in the trash 192 Message message2get = EmailContent.Message.restoreMessageWithId(mProviderContext, 193 message2Id); 194 assertEquals(box2Id, message2get.mMailboxKey); 195 } 196 197 /** 198 * Test deleting message when there is no trash mailbox 199 */ 200 public void testDeleteMessageNoTrash() { 201 Account account1 = 202 ProviderTestUtils.setupAccount("message-delete-notrash", true, mProviderContext); 203 long account1Id = account1.mId; 204 Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mProviderContext); 205 long box1Id = box1.mId; 206 207 Message message1 = 208 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true, 209 mProviderContext); 210 long message1Id = message1.mId; 211 212 mTestController.deleteMessage(message1Id, account1Id); 213 214 // now read back a fresh copy and confirm it's in the trash 215 Message message1get = 216 EmailContent.Message.restoreMessageWithId(mProviderContext, message1Id); 217 218 // check the new mailbox and see if it looks right 219 assertFalse(-1 == message1get.mMailboxKey); 220 assertFalse(box1Id == message1get.mMailboxKey); 221 Mailbox mailbox2get = EmailContent.Mailbox.restoreMailboxWithId(mProviderContext, 222 message1get.mMailboxKey); 223 assertEquals(EmailContent.Mailbox.TYPE_TRASH, mailbox2get.mType); 224 } 225 226 /** 227 * Test read/unread flag 228 */ 229 public void testReadUnread() { 230 Account account1 = ProviderTestUtils.setupAccount("read-unread", false, mProviderContext); 231 account1.mHostAuthRecv 232 = ProviderTestUtils.setupHostAuth("read-unread", 0, false, mProviderContext); 233 account1.save(mProviderContext); 234 long account1Id = account1.mId; 235 long box1Id = 2; 236 237 Message message1 = 238 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true, 239 mProviderContext); 240 long message1Id = message1.mId; 241 242 // test setting to "read" 243 mTestController.setMessageRead(message1Id, true); 244 Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id); 245 assertTrue(message1get.mFlagRead); 246 247 // test setting to "unread" 248 mTestController.setMessageRead(message1Id, false); 249 message1get = Message.restoreMessageWithId(mProviderContext, message1Id); 250 assertFalse(message1get.mFlagRead); 251 } 252 253 /** 254 * Test favorites flag 255 */ 256 public void testFavorites() { 257 Account account1 = ProviderTestUtils.setupAccount("favorites", false, mProviderContext); 258 account1.mHostAuthRecv 259 = ProviderTestUtils.setupHostAuth("favorites", 0, false, mProviderContext); 260 account1.save(mProviderContext); 261 long account1Id = account1.mId; 262 long box1Id = 2; 263 264 Message message1 = 265 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true, 266 mProviderContext); 267 long message1Id = message1.mId; 268 269 // test setting to "favorite" 270 mTestController.setMessageFavorite(message1Id, true); 271 Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id); 272 assertTrue(message1get.mFlagFavorite); 273 274 // test setting to "not favorite" 275 mTestController.setMessageFavorite(message1Id, false); 276 message1get = Message.restoreMessageWithId(mProviderContext, message1Id); 277 assertFalse(message1get.mFlagFavorite); 278 } 279 280 public void testGetAndDeleteAttachmentMailbox() { 281 Mailbox box = mTestController.getAttachmentMailbox(); 282 assertNotNull(box); 283 Mailbox anotherBox = mTestController.getAttachmentMailbox(); 284 assertNotNull(anotherBox); 285 // We should always get back the same Mailbox row 286 assertEquals(box.mId, anotherBox.mId); 287 // Add two messages to this mailbox 288 ProviderTestUtils.setupMessage("message1", 0, box.mId, false, true, 289 mProviderContext); 290 ProviderTestUtils.setupMessage("message2", 0, box.mId, false, true, 291 mProviderContext); 292 // Make sure we can find them where they are expected 293 assertEquals(2, EmailContent.count(mProviderContext, Message.CONTENT_URI, 294 Message.MAILBOX_KEY + "=?", new String[] {Long.toString(box.mId)})); 295 // Delete them 296 mTestController.deleteAttachmentMessages(); 297 // Make sure they're gone 298 assertEquals(0, EmailContent.count(mProviderContext, Message.CONTENT_URI, 299 Message.MAILBOX_KEY + "=?", new String[] {Long.toString(box.mId)})); 300 } 301 302 public void testLoadMessageFromUri() throws Exception { 303 // Create a simple message 304 Message msg = new Message(); 305 String text = "This is some text"; 306 msg.mText = text; 307 String sender = "sender@host.com"; 308 msg.mFrom = sender; 309 // Save this away 310 msg.save(mProviderContext); 311 312 Uri fileUri = ProviderTestUtils.createTempEmlFile(mProviderContext, msg, 313 mContext.getFilesDir()); 314 315 // Load the message via Controller and a Uri 316 Message loadedMsg = mTestController.loadMessageFromUri(fileUri); 317 318 // Check server id, mailbox key, account key, and from 319 assertNotNull(loadedMsg); 320 assertTrue(loadedMsg.mServerId.startsWith(Controller.ATTACHMENT_MESSAGE_UID_PREFIX)); 321 Mailbox box = mTestController.getAttachmentMailbox(); 322 assertNotNull(box); 323 assertEquals(box.mId, loadedMsg.mMailboxKey); 324 assertEquals(0, loadedMsg.mAccountKey); 325 assertEquals(loadedMsg.mFrom, sender); 326 // Check body text 327 String loadedMsgText = Body.restoreBodyTextWithMessageId(mProviderContext, loadedMsg.mId); 328 assertEquals(text, loadedMsgText); 329 } 330 331 /** 332 * Create a simple HostAuth with protocol 333 */ 334 private HostAuth setupSimpleHostAuth(String protocol) { 335 HostAuth hostAuth = new HostAuth(); 336 hostAuth.mProtocol = protocol; 337 return hostAuth; 338 } 339 340 public void testIsMessagingController() { 341 Account account1 = ProviderTestUtils.setupAccount("account1", false, 342 mProviderContext); 343 account1.mHostAuthRecv = setupSimpleHostAuth("eas"); 344 account1.save(mProviderContext); 345 assertFalse(mTestController.isMessagingController(account1)); 346 Account account2 = ProviderTestUtils.setupAccount("account2", false, 347 mProviderContext); 348 account2.mHostAuthRecv = setupSimpleHostAuth("imap"); 349 account2.save(mProviderContext); 350 assertTrue(mTestController.isMessagingController(account2)); 351 Account account3 = ProviderTestUtils.setupAccount("account3", false, 352 mProviderContext); 353 account3.mHostAuthRecv = setupSimpleHostAuth("pop3"); 354 account3.save(mProviderContext); 355 assertTrue(mTestController.isMessagingController(account3)); 356 Account account4 = ProviderTestUtils.setupAccount("account4", false, 357 mProviderContext); 358 account4.mHostAuthRecv = setupSimpleHostAuth("smtp"); 359 account4.save(mProviderContext); 360 assertFalse(mTestController.isMessagingController(account4)); 361 // There should be values for all of these accounts in the legacy map 362 assertNotNull(mTestController.mLegacyControllerMap.get(account1.mId)); 363 assertNotNull(mTestController.mLegacyControllerMap.get(account2.mId)); 364 assertNotNull(mTestController.mLegacyControllerMap.get(account3.mId)); 365 assertNotNull(mTestController.mLegacyControllerMap.get(account4.mId)); 366 // The map should have the expected values 367 assertFalse(mTestController.mLegacyControllerMap.get(account1.mId)); 368 assertTrue(mTestController.mLegacyControllerMap.get(account2.mId)); 369 assertTrue(mTestController.mLegacyControllerMap.get(account3.mId)); 370 assertFalse(mTestController.mLegacyControllerMap.get(account4.mId)); 371 // This second pass should pull values from the cache 372 assertFalse(mTestController.isMessagingController(account1)); 373 assertTrue(mTestController.isMessagingController(account2)); 374 assertTrue(mTestController.isMessagingController(account3)); 375 assertFalse(mTestController.isMessagingController(account4)); 376 } 377 378 /** 379 * TODO: releasing associated data (e.g. attachments, embedded images) 380 */ 381} 382