Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2010 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.activity;
     18 
     19 import com.android.email.provider.EmailProvider;
     20 import com.android.email.provider.ProviderTestUtils;
     21 import com.android.emailcommon.provider.Account;
     22 import com.android.emailcommon.provider.EmailContent;
     23 import com.android.emailcommon.provider.EmailContent.Message;
     24 import com.android.emailcommon.provider.Mailbox;
     25 
     26 import android.content.Context;
     27 import android.database.Cursor;
     28 import android.test.ProviderTestCase2;
     29 
     30 import junit.framework.Assert;
     31 
     32 public class MailboxFragmentAdapterTest extends ProviderTestCase2<EmailProvider> {
     33 
     34     private Context mMockContext;
     35 
     36     public MailboxFragmentAdapterTest() {
     37         super(EmailProvider.class, EmailContent.AUTHORITY);
     38     }
     39 
     40     @Override
     41     public void setUp() throws Exception {
     42         super.setUp();
     43         mMockContext = getMockContext();
     44     }
     45 
     46     public void testBuildCombinedMailboxes() {
     47         final Context c = mMockContext;
     48 
     49         // Prepare test data
     50         Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
     51         Account a2 = ProviderTestUtils.setupAccount("a2", true, c);
     52         Account a3 = ProviderTestUtils.setupAccount("a3", true, c);
     53 
     54         Mailbox b1i = ProviderTestUtils.setupMailbox("box1i", a1.mId, true, c, Mailbox.TYPE_INBOX);
     55         Mailbox b2i = ProviderTestUtils.setupMailbox("box2i", a2.mId, true, c, Mailbox.TYPE_INBOX);
     56         Mailbox b3i = ProviderTestUtils.setupMailbox("box3i", a3.mId, true, c, Mailbox.TYPE_INBOX);
     57         Mailbox b1o = ProviderTestUtils.setupMailbox("box1i", a1.mId, true, c, Mailbox.TYPE_OUTBOX);
     58         Mailbox b2o = ProviderTestUtils.setupMailbox("box2i", a2.mId, true, c, Mailbox.TYPE_OUTBOX);
     59         Mailbox b1d = ProviderTestUtils.setupMailbox("box1d", a1.mId, true, c, Mailbox.TYPE_DRAFTS);
     60         Mailbox b2d = ProviderTestUtils.setupMailbox("box2d", a2.mId, true, c, Mailbox.TYPE_DRAFTS);
     61         Mailbox b1t = ProviderTestUtils.setupMailbox("box1t", a1.mId, true, c, Mailbox.TYPE_TRASH);
     62         Mailbox b2t = ProviderTestUtils.setupMailbox("box2t", a2.mId, true, c, Mailbox.TYPE_TRASH);
     63 
     64         createMessage(c, b1i, false, false, Message.FLAG_LOADED_COMPLETE);
     65         createMessage(c, b2i, true, true, Message.FLAG_LOADED_COMPLETE);
     66         createMessage(c, b2i, true, false, Message.FLAG_LOADED_COMPLETE);
     67         // "unloaded" messages will not affect 'favorite' message count
     68         createMessage(c, b3i, true, true, Message.FLAG_LOADED_UNLOADED);
     69 
     70         createMessage(c, b1o, true, true, Message.FLAG_LOADED_COMPLETE);
     71         createMessage(c, b2o, false, true, Message.FLAG_LOADED_COMPLETE);
     72 
     73         createMessage(c, b1d, false, true, Message.FLAG_LOADED_COMPLETE);
     74         createMessage(c, b2d, false, true, Message.FLAG_LOADED_COMPLETE);
     75         createMessage(c, b2d, false, true, Message.FLAG_LOADED_COMPLETE);
     76         createMessage(c, b2d, false, true, Message.FLAG_LOADED_COMPLETE);
     77 
     78         // Starred message in trash; All Starred excludes it.
     79         createMessage(c, b2t, true, true, Message.FLAG_LOADED_UNLOADED);
     80 
     81         // Kick the method
     82         Cursor cursor = MailboxFragmentAdapter.CombinedMailboxLoader.buildCombinedMailboxes(c,
     83                 null);
     84 
     85         // Check the result
     86         assertEquals(4, cursor.getCount());
     87 
     88         // Row 1 -- combined inbox (with unread count)
     89         assertTrue(cursor.moveToFirst());
     90         checkSpecialMailboxRow(cursor, Mailbox.QUERY_ALL_INBOXES, Mailbox.TYPE_INBOX, 2);
     91 
     92         // Row 2 -- all starred (with total count)
     93         assertTrue(cursor.moveToNext());
     94         checkSpecialMailboxRow(cursor, Mailbox.QUERY_ALL_FAVORITES, Mailbox.TYPE_MAIL, 3);
     95 
     96         // Row 3 -- all drafts (with total count)
     97         assertTrue(cursor.moveToNext());
     98         checkSpecialMailboxRow(cursor, Mailbox.QUERY_ALL_DRAFTS, Mailbox.TYPE_DRAFTS, 4);
     99 
    100         // Row 4 -- combined outbox (with total count)
    101         assertTrue(cursor.moveToNext());
    102         checkSpecialMailboxRow(cursor, Mailbox.QUERY_ALL_OUTBOX, Mailbox.TYPE_OUTBOX, 2);
    103     }
    104 
    105     private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read,
    106             int flagLoaded) {
    107         Message message = ProviderTestUtils.setupMessage(
    108                 "1", b.mAccountKey, b.mId, true, false, c, starred, read);
    109         message.mFlagLoaded = flagLoaded;
    110         message.save(c);
    111         return message;
    112     }
    113 
    114     private static void checkSpecialMailboxRow(Cursor cursor, long id, int type,
    115             int count) {
    116         // _id must always be >= 0; otherwise ListView gets confused.
    117         Assert.assertTrue(cursor.getLong(cursor.getColumnIndex("_id")) >= 0);
    118         Assert.assertEquals(id, MailboxFragmentAdapter.getId(cursor));
    119         Assert.assertEquals(type, MailboxFragmentAdapter.getType(cursor));
    120         Assert.assertEquals(count, MailboxFragmentAdapter.getMessageCount(cursor));
    121         Assert.assertEquals(count, MailboxFragmentAdapter.getUnreadCount(cursor));
    122         Assert.assertEquals(Account.ACCOUNT_ID_COMBINED_VIEW,
    123                 MailboxFragmentAdapter.getAccountId(cursor));
    124     }
    125 }
    126