Home | History | Annotate | Download | only in activity
      1 /*
      2  * Copyright (C) 2011 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.DBTestHelper;
     20 import com.android.email.provider.ProviderTestUtils;
     21 import com.android.emailcommon.provider.Account;
     22 import com.android.emailcommon.provider.Mailbox;
     23 
     24 import android.content.Context;
     25 import android.test.AndroidTestCase;
     26 
     27 /**
     28  * Unit tests for {@link MailboxListFragment.FindParentMailboxTask}.
     29  */
     30 public class FindParentMailboxTaskTest extends AndroidTestCase {
     31     private Context mProviderContext;
     32 
     33     /** ID of the account created by {@link #setUpMailboxes}. */
     34     private long mAccountId;
     35 
     36     /**
     37      * IDs for the mailboxes created by {@link #setUpMailboxes}.
     38      *
     39      * Mailbox hierarchy:
     40      * <pre>
     41      * |-Inbox
     42      * |-Parent
     43      *   |-Child1
     44      *   |-Child2
     45      *     |-GrandChild1
     46      *     |-GrandChild2
     47      * </pre>
     48      */
     49     private long mIdInbox;
     50     private long mIdParent;
     51     private long mIdChild1;
     52     private long mIdChild2;
     53     private long mIdGrandChild1;
     54     private long mIdGrandChild2;
     55 
     56     @Override
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59 
     60         mProviderContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
     61                 getContext());
     62         setUpMailboxes();
     63     }
     64 
     65     /**
     66      * Set up a test account and mailboxes.
     67      */
     68     private void setUpMailboxes() {
     69         Account a = ProviderTestUtils.setupAccount("a", true, mProviderContext);
     70         mAccountId = a.mId;
     71 
     72         mIdInbox = createMailboxAndGetId("Inbox", a, Mailbox.TYPE_INBOX, Mailbox.NO_MAILBOX);
     73         mIdParent = createMailboxAndGetId("P", a, Mailbox.TYPE_MAIL, Mailbox.NO_MAILBOX);
     74         mIdChild1 = createMailboxAndGetId("C1", a, Mailbox.TYPE_MAIL, mIdParent);
     75         mIdChild2 = createMailboxAndGetId("C2", a, Mailbox.TYPE_MAIL, mIdParent);
     76         mIdGrandChild1 = createMailboxAndGetId("G1", a, Mailbox.TYPE_MAIL, mIdChild2);
     77         mIdGrandChild2 = createMailboxAndGetId("G2", a, Mailbox.TYPE_MAIL, mIdChild2);
     78     }
     79 
     80     private long createMailboxAndGetId(String name, Account account, int type,
     81             long parentMailboxId) {
     82         Mailbox m = ProviderTestUtils.setupMailbox(name, account.mId, false, mProviderContext,
     83                 type);
     84         m.mParentKey = parentMailboxId;
     85         m.save(mProviderContext);
     86         return m.mId;
     87     }
     88 
     89     /**
     90      * Tests for two-pane.  (highlighting is enabled)
     91      */
     92     public void testWithHighlight() {
     93         /*
     94          * In the comments below, [MAILBOX] indicates "highlighted", and MAILBOX* indicates
     95          * "selected".
     96          */
     97         /*
     98          * from:
     99          * - [Child2]
    100          *   - GChild1
    101          *   - GChild2
    102          *
    103          * to:
    104          * - Parent
    105          *   - Child1
    106          *   - [Child2]*
    107          */
    108         doCheckWithHighlight(
    109                 mIdChild2, // Current parent
    110                 mIdChild2, // Current highlighted
    111 
    112                 mIdParent, // Next root
    113                 mIdChild2, // Next highlighted
    114                 mIdChild2 // Next selected
    115                 );
    116 
    117         /*
    118          * from:
    119          * - Child2
    120          *   - [GChild1]
    121          *   - GChild2
    122          *
    123          * to:
    124          * - [Parent]*
    125          *   - Child1
    126          *   - Child2
    127          */
    128         doCheckWithHighlight(
    129                 mIdChild2, // Current parent
    130                 mIdGrandChild1, // Current highlighted
    131 
    132                 mIdParent, // Next root
    133                 mIdParent, // Next highlighted
    134                 mIdParent // Next selected
    135                 );
    136 
    137         /*
    138          * from:
    139          * - [Parent]
    140          *   - Child1
    141          *   - Child2
    142          *
    143          * to:
    144          * - Inbox
    145          * - [Parent]*
    146          */
    147         doCheckWithHighlight(
    148                 mIdParent, // Current parent
    149                 mIdParent, // Current highlighted
    150 
    151                 Mailbox.NO_MAILBOX, // Next root
    152                 mIdParent, // Next highlighted
    153                 mIdParent // Next selected
    154                 );
    155 
    156         /*
    157          * from:
    158          * - Parent
    159          *   - [Child1]
    160          *   - Child2
    161          *
    162          * to:
    163          * - [Inbox]*
    164          * - Parent
    165          */
    166         doCheckWithHighlight(
    167                 mIdParent, // Current parent
    168                 mIdChild1, // Current highlighted
    169 
    170                 Mailbox.NO_MAILBOX, // Next root
    171                 mIdInbox, // Next highlighted
    172                 mIdInbox // Next selected
    173                 );
    174 
    175         /*
    176          * Special case.
    177          * Up from root view, with "Parent" highlighted.  "Up" will be disabled in this case, but
    178          * if we were to run the task, it'd work as if the current parent mailbox is gone.
    179          * i.e. just show the top level mailboxes, with Inbox highlighted.
    180          *
    181          * from:
    182          * - Inbox
    183          * - [Parent]
    184          *
    185          * to:
    186          * - [Inbox]
    187          * - Parent
    188          */
    189         doCheckWithHighlight(
    190                 Mailbox.NO_MAILBOX, // Current parent
    191                 mIdParent, // Current highlighted
    192 
    193                 Mailbox.NO_MAILBOX, // Next root
    194                 mIdInbox, // Next highlighted
    195                 mIdInbox // Next selected
    196                 );
    197 
    198         /*
    199          * Special case.
    200          * Current parent mailbox is gone.  The result should be same as the above.
    201          *
    202          * from:
    203          *  (current mailbox just removed)
    204          *
    205          * to:
    206          * - [Inbox]
    207          * - Parent
    208          */
    209         doCheckWithHighlight(
    210                 12312234234L, // Current parent
    211                 mIdParent, // Current highlighted
    212 
    213                 Mailbox.NO_MAILBOX, // Next root
    214                 mIdInbox, // Next highlighted
    215                 mIdInbox // Next selected
    216                 );
    217     }
    218 
    219     private void doCheckWithHighlight(
    220             long parentMailboxId, long highlightedMailboxId,
    221             long expectedNextParent, long expectedNextHighlighted, long expectedNextSelected) {
    222         doCheck(true, parentMailboxId, highlightedMailboxId,
    223                 expectedNextParent, expectedNextHighlighted, expectedNextSelected);
    224     }
    225 
    226     /**
    227      * Tests for one-pane.  (highlighting is disable)
    228      */
    229     public void testWithNoHighlight() {
    230         /*
    231          * from:
    232          * - Child2
    233          *   - GChild1
    234          *   - GChild2
    235          *
    236          * to:
    237          * - Parent
    238          *   - Child1
    239          *   - Child2
    240          */
    241         doCheckWithNoHighlight(
    242                 mIdChild2, // Current parent
    243                 mIdParent // Next root
    244                 );
    245         /*
    246          * from:
    247          * - Parent
    248          *   - Child1
    249          *   - Child2
    250          *
    251          * to:
    252          * - Inbox
    253          * - Parent
    254          */
    255         doCheckWithNoHighlight(
    256                 mIdParent, // Current parent
    257                 Mailbox.NO_MAILBOX // Next root
    258                 );
    259 
    260         /*
    261          * Special case.
    262          * Current parent mailbox is gone.  The top-level mailboxes should be shown.
    263          *
    264          * from:
    265          *  (current mailbox just removed)
    266          *
    267          * to:
    268          * - Inbox
    269          * - Parent
    270          */
    271         doCheckWithNoHighlight(
    272                 12312234234L, // Current parent
    273                 Mailbox.NO_MAILBOX // Next root
    274                 );
    275     }
    276 
    277     private void doCheckWithNoHighlight(long parentMailboxId, long expectedNextParent) {
    278         doCheck(false, parentMailboxId, Mailbox.NO_MAILBOX,
    279                 expectedNextParent, Mailbox.NO_MAILBOX,
    280                 expectedNextParent /* parent should always be selected */);
    281     }
    282 
    283     private void doCheck(boolean enableHighlight,
    284             long parentMailboxId, long highlightedMailboxId,
    285             long expectedNextParent, long expectedNextHighlighted, long expectedNextSelected) {
    286         ResultCallback result = new ResultCallback();
    287 
    288         MailboxListFragment.FindParentMailboxTask task
    289                 = new MailboxListFragment.FindParentMailboxTask(
    290                 mProviderContext, null, mAccountId, enableHighlight, parentMailboxId,
    291                 highlightedMailboxId, result);
    292 
    293         // Can't execute an async task on the test thread, so emulate execution...
    294         task.onSuccess(task.doInBackground((Void[]) null));
    295 
    296         assertEquals("parent", expectedNextParent, result.mNextParentMailboxId);
    297         assertEquals("highlighted", expectedNextHighlighted, result.mNextHighlightedMailboxId);
    298         assertEquals("selected", expectedNextSelected, result.mNextSelectedMailboxId);
    299     }
    300 
    301     private static class ResultCallback
    302             implements MailboxListFragment.FindParentMailboxTask.ResultCallback {
    303         public long mNextParentMailboxId;
    304         public long mNextHighlightedMailboxId;
    305         public long mNextSelectedMailboxId;
    306 
    307         @Override
    308         public void onResult(long nextParentMailboxId, long nextHighlightedMailboxId,
    309                 long nextSelectedMailboxId) {
    310             mNextParentMailboxId = nextParentMailboxId;
    311             mNextHighlightedMailboxId = nextHighlightedMailboxId;
    312             mNextSelectedMailboxId = nextSelectedMailboxId;
    313         }
    314     }
    315 }
    316