Home | History | Annotate | Download | only in email
      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;
     18 
     19 import com.android.email.provider.EmailContent;
     20 import com.android.email.provider.EmailProvider;
     21 import com.android.email.provider.ProviderTestUtils;
     22 import com.android.email.provider.EmailContent.Account;
     23 import com.android.email.provider.EmailContent.Mailbox;
     24 import com.android.email.provider.EmailContent.Message;
     25 
     26 import android.content.Context;
     27 import android.test.ProviderTestCase2;
     28 
     29 import java.util.Locale;
     30 
     31 /**
     32  * Tests of the Controller class that depend on the underlying provider.
     33  *
     34  * NOTE:  It would probably make sense to rewrite this using a MockProvider, instead of the
     35  * ProviderTestCase (which is a real provider running on a temp database).  This would be more of
     36  * a true "unit test".
     37  *
     38  * You can run this entire test case with:
     39  *   runtest -c com.android.email.ControllerProviderOpsTests email
     40  */
     41 public class ControllerProviderOpsTests extends ProviderTestCase2<EmailProvider> {
     42 
     43     EmailProvider mProvider;
     44     Context mProviderContext;
     45     Context mContext;
     46 
     47     public ControllerProviderOpsTests() {
     48         super(EmailProvider.class, EmailProvider.EMAIL_AUTHORITY);
     49     }
     50 
     51     @Override
     52     public void setUp() throws Exception {
     53         super.setUp();
     54         mProviderContext = getMockContext();
     55         mContext = getContext();
     56     }
     57 
     58     @Override
     59     public void tearDown() throws Exception {
     60         super.tearDown();
     61     }
     62 
     63     /**
     64      * Lightweight subclass of the Controller class allows injection of mock context
     65      */
     66     public static class TestController extends Controller {
     67 
     68         protected TestController(Context providerContext, Context systemContext) {
     69             super(systemContext);
     70             setProviderContext(providerContext);
     71         }
     72     }
     73 
     74     /**
     75      * These are strings that should not change per locale.
     76      */
     77     public void testGetMailboxServerName() {
     78         Controller ct = new TestController(mProviderContext, mContext);
     79 
     80         assertEquals("", ct.getMailboxServerName(-1));
     81 
     82         assertEquals("Inbox", ct.getMailboxServerName(Mailbox.TYPE_INBOX));
     83         assertEquals("Outbox", ct.getMailboxServerName(Mailbox.TYPE_OUTBOX));
     84         assertEquals("Trash", ct.getMailboxServerName(Mailbox.TYPE_TRASH));
     85         assertEquals("Sent", ct.getMailboxServerName(Mailbox.TYPE_SENT));
     86         assertEquals("Junk", ct.getMailboxServerName(Mailbox.TYPE_JUNK));
     87 
     88         // Now try again with translation
     89         Locale savedLocale = Locale.getDefault();
     90         Locale.setDefault(Locale.FRANCE);
     91         assertEquals("Inbox", ct.getMailboxServerName(Mailbox.TYPE_INBOX));
     92         assertEquals("Outbox", ct.getMailboxServerName(Mailbox.TYPE_OUTBOX));
     93         assertEquals("Trash", ct.getMailboxServerName(Mailbox.TYPE_TRASH));
     94         assertEquals("Sent", ct.getMailboxServerName(Mailbox.TYPE_SENT));
     95         assertEquals("Junk", ct.getMailboxServerName(Mailbox.TYPE_JUNK));
     96         Locale.setDefault(savedLocale);
     97     }
     98 
     99     /**
    100      * Test of Controller.createMailbox().
    101      * Sunny day test only - creates a mailbox that does not exist.
    102      * Does not test duplication, bad accountID, or any other bad input.
    103      */
    104     public void testCreateMailbox() {
    105         Account account = ProviderTestUtils.setupAccount("mailboxid", true, mProviderContext);
    106         long accountId = account.mId;
    107 
    108         long oldBoxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS);
    109         assertEquals(Mailbox.NO_MAILBOX, oldBoxId);
    110 
    111         Controller ct = new TestController(mProviderContext, mContext);
    112         ct.createMailbox(accountId, Mailbox.TYPE_DRAFTS);
    113         long boxId = Mailbox.findMailboxOfType(mProviderContext, accountId, Mailbox.TYPE_DRAFTS);
    114 
    115         // check that the drafts mailbox exists
    116         assertTrue("mailbox exists", boxId != Mailbox.NO_MAILBOX);
    117     }
    118 
    119     /**
    120      * Test of Controller.findOrCreateMailboxOfType().
    121      * Checks:
    122      * - finds correctly the ID of existing mailbox
    123      * - creates non-existing mailbox
    124      * - creates only once a new mailbox
    125      * - when accountId or mailboxType are -1, returns NO_MAILBOX
    126      */
    127     public void testFindOrCreateMailboxOfType() {
    128         Account account = ProviderTestUtils.setupAccount("mailboxid", true, mProviderContext);
    129         long accountId = account.mId;
    130         Mailbox box = ProviderTestUtils.setupMailbox("box", accountId, false, mProviderContext);
    131         final int boxType = Mailbox.TYPE_TRASH;
    132         box.mType = boxType;
    133         box.save(mProviderContext);
    134         long boxId = box.mId;
    135 
    136         Controller ct = new TestController(mProviderContext, mContext);
    137         long testBoxId = ct.findOrCreateMailboxOfType(accountId, boxType);
    138 
    139         // check it found the right mailbox id
    140         assertEquals(boxId, testBoxId);
    141 
    142         long boxId2 = ct.findOrCreateMailboxOfType(accountId, Mailbox.TYPE_DRAFTS);
    143         assertTrue("mailbox created", boxId2 != Mailbox.NO_MAILBOX);
    144         assertTrue("with different id", testBoxId != boxId2);
    145 
    146         // check it doesn't create twice when existing
    147         long boxId3 = ct.findOrCreateMailboxOfType(accountId, Mailbox.TYPE_DRAFTS);
    148         assertEquals("don't create if exists", boxId3, boxId2);
    149 
    150         // check invalid aruments
    151         assertEquals(Mailbox.NO_MAILBOX, ct.findOrCreateMailboxOfType(-1, Mailbox.TYPE_DRAFTS));
    152         assertEquals(Mailbox.NO_MAILBOX, ct.findOrCreateMailboxOfType(accountId, -1));
    153     }
    154 
    155     /**
    156      * Test the "delete message" function.  Sunny day:
    157      *    - message/mailbox/account all exist
    158      *    - trash mailbox exists
    159      */
    160     public void testDeleteMessage() {
    161         Account account1 = ProviderTestUtils.setupAccount("message-delete", true, mProviderContext);
    162         long account1Id = account1.mId;
    163         Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mProviderContext);
    164         long box1Id = box1.mId;
    165         Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account1Id, false, mProviderContext);
    166         box2.mType = EmailContent.Mailbox.TYPE_TRASH;
    167         box2.save(mProviderContext);
    168         long box2Id = box2.mId;
    169 
    170         Message message1 = ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false,
    171                 true, mProviderContext);
    172         long message1Id = message1.mId;
    173 
    174         Controller ct = new TestController(mProviderContext, mContext);
    175 
    176         ct.deleteMessage(message1Id, account1Id);
    177 
    178         // now read back a fresh copy and confirm it's in the trash
    179         Message message1get = EmailContent.Message.restoreMessageWithId(mProviderContext,
    180                 message1Id);
    181         assertEquals(box2Id, message1get.mMailboxKey);
    182 
    183         // Now repeat test with accountId "unknown"
    184         Message message2 = ProviderTestUtils.setupMessage("message2", account1Id, box1Id, false,
    185                 true, mProviderContext);
    186         long message2Id = message2.mId;
    187 
    188         ct.deleteMessage(message2Id, -1);
    189 
    190         // now read back a fresh copy and confirm it's in the trash
    191         Message message2get = EmailContent.Message.restoreMessageWithId(mProviderContext,
    192                 message2Id);
    193         assertEquals(box2Id, message2get.mMailboxKey);
    194     }
    195 
    196     /**
    197      * Test deleting message when there is no trash mailbox
    198      */
    199     public void testDeleteMessageNoTrash() {
    200         Account account1 =
    201                 ProviderTestUtils.setupAccount("message-delete-notrash", true, mProviderContext);
    202         long account1Id = account1.mId;
    203         Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, mProviderContext);
    204         long box1Id = box1.mId;
    205 
    206         Message message1 =
    207                 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true,
    208                         mProviderContext);
    209         long message1Id = message1.mId;
    210 
    211         Controller ct = new TestController(mProviderContext, mContext);
    212 
    213         ct.deleteMessage(message1Id, account1Id);
    214 
    215         // now read back a fresh copy and confirm it's in the trash
    216         Message message1get =
    217                 EmailContent.Message.restoreMessageWithId(mProviderContext, message1Id);
    218 
    219         // check the new mailbox and see if it looks right
    220         assertFalse(-1 == message1get.mMailboxKey);
    221         assertFalse(box1Id == message1get.mMailboxKey);
    222         Mailbox mailbox2get = EmailContent.Mailbox.restoreMailboxWithId(mProviderContext,
    223                 message1get.mMailboxKey);
    224         assertEquals(EmailContent.Mailbox.TYPE_TRASH, mailbox2get.mType);
    225     }
    226 
    227     /**
    228      * Test read/unread flag
    229      */
    230     public void testReadUnread() {
    231         Account account1 = ProviderTestUtils.setupAccount("read-unread", false, mProviderContext);
    232         account1.mHostAuthRecv
    233                 = ProviderTestUtils.setupHostAuth("read-unread", 0, false, mProviderContext);
    234         account1.save(mProviderContext);
    235         long account1Id = account1.mId;
    236         long box1Id = 2;
    237 
    238         Message message1 =
    239                 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true,
    240                         mProviderContext);
    241         long message1Id = message1.mId;
    242 
    243         Controller ct = new TestController(mProviderContext, mContext);
    244 
    245         // test setting to "read"
    246         ct.setMessageRead(message1Id, true);
    247         Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
    248         assertTrue(message1get.mFlagRead);
    249 
    250         // test setting to "unread"
    251         ct.setMessageRead(message1Id, false);
    252         message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
    253         assertFalse(message1get.mFlagRead);
    254     }
    255 
    256     /**
    257      * Test favorites flag
    258      */
    259     public void testFavorites() {
    260         Account account1 = ProviderTestUtils.setupAccount("favorites", false, mProviderContext);
    261         account1.mHostAuthRecv
    262                 = ProviderTestUtils.setupHostAuth("favorites", 0, false, mProviderContext);
    263         account1.save(mProviderContext);
    264         long account1Id = account1.mId;
    265         long box1Id = 2;
    266 
    267         Message message1 =
    268                 ProviderTestUtils.setupMessage("message1", account1Id, box1Id, false, true,
    269                         mProviderContext);
    270         long message1Id = message1.mId;
    271 
    272         Controller ct = new TestController(mProviderContext, mContext);
    273 
    274         // test setting to "favorite"
    275         ct.setMessageFavorite(message1Id, true);
    276         Message message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
    277         assertTrue(message1get.mFlagFavorite);
    278 
    279         // test setting to "not favorite"
    280         ct.setMessageFavorite(message1Id, false);
    281         message1get = Message.restoreMessageWithId(mProviderContext, message1Id);
    282         assertFalse(message1get.mFlagFavorite);
    283     }
    284 
    285     /**
    286      * TODO: releasing associated data (e.g. attachments, embedded images)
    287      */
    288 
    289     /**
    290      * TODO: test isMessagingController()
    291      */
    292 }
    293