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 android.content.Context;
     20 import android.test.LoaderTestCase;
     21 
     22 import com.android.email.DBTestHelper;
     23 import com.android.email.MessageListContext;
     24 import com.android.email.provider.ProviderTestUtils;
     25 import com.android.emailcommon.provider.Account;
     26 import com.android.emailcommon.provider.Mailbox;
     27 
     28 public class MessagesAdapterTests extends LoaderTestCase {
     29     // Account ID that's probably not in the database.
     30     private static final long NO_SUCH_ACCOUNT_ID = 1234567890123L;
     31 
     32     // Mailbox ID that's probably not in the database.
     33     private static final long NO_SUCH_MAILBOX_ID = 1234567890123L;
     34 
     35     // Isolated Context for providers.
     36     private Context mProviderContext;
     37 
     38     @Override
     39     protected void setUp() throws Exception {
     40         mProviderContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(getContext());
     41     }
     42 
     43     private long createAccount(boolean isEas) {
     44         Account acct = ProviderTestUtils.setupAccount("acct1", false, mProviderContext);
     45         String proto = isEas ? "eas" : "non-eas";
     46         acct.mHostAuthRecv =
     47                 ProviderTestUtils.setupHostAuth(proto, "hostauth", true, mProviderContext);
     48         acct.mHostAuthKeyRecv = acct.mHostAuthRecv.mId;
     49         acct.save(mProviderContext);
     50         return acct.mId;
     51     }
     52 
     53     private long createMailbox(long accountId, int type) {
     54         Mailbox box = ProviderTestUtils.setupMailbox("name", accountId, false, mProviderContext);
     55         box.mType = type;
     56         box.save(mProviderContext);
     57         return box.mId;
     58     }
     59 
     60     private MessagesAdapter.MessagesCursor getLoaderResult(long accountId, long mailboxId) {
     61         return (MessagesAdapter.MessagesCursor) getLoaderResultSynchronously(
     62                 MessagesAdapter.createLoader(
     63                         mProviderContext,
     64                         MessageListContext.forMailbox(accountId, mailboxId)));
     65     }
     66 
     67     /**
     68      * Test for normal case.  (account, mailbox found)
     69      */
     70     public void testLoad() {
     71         final long accountId = createAccount(false);
     72         final long mailboxId = createMailbox(accountId, Mailbox.TYPE_MAIL);
     73 
     74         MessagesAdapter.MessagesCursor result = getLoaderResult(accountId, mailboxId);
     75         assertTrue(result.mIsFound);
     76         assertEquals(accountId, result.mAccount.mId);
     77         assertEquals(mailboxId, result.mMailbox.mId);
     78         assertFalse(result.mIsEasAccount);
     79         assertTrue(result.mIsRefreshable);
     80     }
     81 
     82     /**
     83      * Load -- isEas = true
     84      */
     85     public void testLoadEas() {
     86         final long accountId = createAccount(true);
     87         final long mailboxId = createMailbox(accountId, Mailbox.TYPE_MAIL);
     88 
     89         MessagesAdapter.MessagesCursor result = getLoaderResult(accountId, mailboxId);
     90         assertTrue(result.mIsFound);
     91         assertEquals(accountId, result.mAccount.mId);
     92         assertEquals(mailboxId, result.mMailbox.mId);
     93         assertTrue(result.mIsEasAccount);
     94         assertTrue(result.mIsRefreshable);
     95     }
     96 
     97     /**
     98      * Load -- drafts, not refreshable.
     99      */
    100     public void testLoadNotRefreshable() {
    101         final long accountId = createAccount(false);
    102         final long mailboxId = createMailbox(accountId, Mailbox.TYPE_DRAFTS);
    103 
    104         MessagesAdapter.MessagesCursor result = getLoaderResult(accountId, mailboxId);
    105         assertTrue(result.mIsFound);
    106         assertEquals(accountId, result.mAccount.mId);
    107         assertEquals(mailboxId, result.mMailbox.mId);
    108         assertFalse(result.mIsEasAccount);
    109         assertFalse(result.mIsRefreshable);
    110     }
    111 
    112     /**
    113      * Mailbox not found.
    114      */
    115     public void testMailboxNotFound() {
    116         MessagesAdapter.MessagesCursor result = getLoaderResult(
    117                 createAccount(false), NO_SUCH_MAILBOX_ID);
    118         assertFalse(result.mIsFound);
    119         assertNull(result.mAccount);
    120         assertNull(result.mMailbox);
    121         assertFalse(result.mIsEasAccount);
    122         assertFalse(result.mIsRefreshable);
    123     }
    124 
    125     /**
    126      * Account not found.
    127      */
    128     public void testAccountNotFound() {
    129         final long mailboxId = createMailbox(NO_SUCH_ACCOUNT_ID, Mailbox.TYPE_MAIL);
    130 
    131         MessagesAdapter.MessagesCursor result = getLoaderResult(NO_SUCH_ACCOUNT_ID, mailboxId);
    132         assertFalse(result.mIsFound);
    133         assertNull(result.mAccount);
    134         assertNull(result.mMailbox);
    135         assertFalse(result.mIsEasAccount);
    136         assertFalse(result.mIsRefreshable);
    137     }
    138 
    139     /**
    140      * Magic mailbox.  (always found)
    141      */
    142     public void testMagicMailbox() {
    143         MessagesAdapter.MessagesCursor result = getLoaderResult(
    144                 Account.ACCOUNT_ID_COMBINED_VIEW, Mailbox.QUERY_ALL_INBOXES);
    145         assertTrue(result.mIsFound);
    146         assertNull(result.mAccount);
    147         assertNull(result.mMailbox);
    148         assertFalse(result.mIsEasAccount);
    149         assertFalse(result.mIsRefreshable);
    150     }
    151 }
    152