Home | History | Annotate | Download | only in adapter
      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.exchange.adapter;
     18 
     19 import android.content.ContentResolver;
     20 import android.test.suitebuilder.annotation.MediumTest;
     21 
     22 import com.android.emailcommon.provider.Account;
     23 import com.android.emailcommon.provider.Mailbox;
     24 import com.android.emailcommon.service.SyncWindow;
     25 import com.android.exchange.EasSyncService;
     26 import com.android.exchange.provider.EmailContentSetupUtils;
     27 
     28 import java.io.IOException;
     29 import java.util.HashMap;
     30 
     31 /**
     32  * You can run this entire test case with:
     33  *   runtest -c com.android.exchange.adapter.FolderSyncParserTests exchange
     34  */
     35 @MediumTest
     36 public class FolderSyncParserTests extends SyncAdapterTestCase<EmailSyncAdapter> {
     37 
     38     // We increment this to generate unique server id's
     39     private int mServerIdCount = 0;
     40     private final long mCreationTime = System.currentTimeMillis();
     41 
     42     public FolderSyncParserTests() {
     43         super();
     44     }
     45 
     46     public void testIsValidMailFolder() throws IOException {
     47         EasSyncService service = getTestService();
     48         EmailSyncAdapter adapter = new EmailSyncAdapter(service);
     49         FolderSyncParser parser = new FolderSyncParser(getTestInputStream(), adapter);
     50         HashMap<String, Mailbox> mailboxMap = new HashMap<String, Mailbox>();
     51         // The parser needs the mAccount set
     52         parser.mAccount = mAccount;
     53         mAccount.save(getContext());
     54 
     55         // Don't save the box; just create it, and give it a server id
     56         Mailbox boxMailType = EmailContentSetupUtils.setupMailbox("box1", mAccount.mId, false,
     57                 mProviderContext, Mailbox.TYPE_MAIL);
     58         boxMailType.mServerId = "__1:1";
     59         // Automatically valid since TYPE_MAIL
     60         assertTrue(parser.isValidMailFolder(boxMailType, mailboxMap));
     61 
     62         Mailbox boxCalendarType = EmailContentSetupUtils.setupMailbox("box", mAccount.mId, false,
     63                 mProviderContext, Mailbox.TYPE_CALENDAR);
     64         Mailbox boxContactsType = EmailContentSetupUtils.setupMailbox("box", mAccount.mId, false,
     65                 mProviderContext, Mailbox.TYPE_CONTACTS);
     66         Mailbox boxTasksType = EmailContentSetupUtils.setupMailbox("box", mAccount.mId, false,
     67                 mProviderContext, Mailbox.TYPE_TASKS);
     68         // Automatically invalid since TYPE_CALENDAR and TYPE_CONTACTS
     69         assertFalse(parser.isValidMailFolder(boxCalendarType, mailboxMap));
     70         assertFalse(parser.isValidMailFolder(boxContactsType, mailboxMap));
     71         assertFalse(parser.isValidMailFolder(boxTasksType, mailboxMap));
     72 
     73         // Unknown boxes are invalid unless they have a parent that's valid
     74         Mailbox boxUnknownType = EmailContentSetupUtils.setupMailbox("box", mAccount.mId, false,
     75                 mProviderContext, Mailbox.TYPE_UNKNOWN);
     76         assertFalse(parser.isValidMailFolder(boxUnknownType, mailboxMap));
     77         boxUnknownType.mParentServerId = boxMailType.mServerId;
     78         // We shouldn't find the parent yet
     79         assertFalse(parser.isValidMailFolder(boxUnknownType, mailboxMap));
     80         // Put the mailbox in the map; the unknown box should now be valid
     81         mailboxMap.put(boxMailType.mServerId, boxMailType);
     82         assertTrue(parser.isValidMailFolder(boxUnknownType, mailboxMap));
     83 
     84         // Clear the map, but save away the parent box
     85         mailboxMap.clear();
     86         assertFalse(parser.isValidMailFolder(boxUnknownType, mailboxMap));
     87         boxMailType.save(mProviderContext);
     88         // The box should now be valid
     89         assertTrue(parser.isValidMailFolder(boxUnknownType, mailboxMap));
     90 
     91         // Somewhat harder case.  The parent will be in the map, but also unknown.  The parent's
     92         // parent will be in the database.
     93         Mailbox boxParentUnknownType = EmailContentSetupUtils.setupMailbox("box", mAccount.mId,
     94                 false, mProviderContext, Mailbox.TYPE_UNKNOWN);
     95         assertFalse(parser.isValidMailFolder(boxParentUnknownType, mailboxMap));
     96         // Give the unknown type parent a parent (boxMailType)
     97         boxParentUnknownType.mServerId = "__1:2";
     98         boxParentUnknownType.mParentServerId = boxMailType.mServerId;
     99         // Give our unknown box an unknown parent
    100         boxUnknownType.mParentServerId = boxParentUnknownType.mServerId;
    101         // Confirm the box is still invalid
    102         assertFalse(parser.isValidMailFolder(boxUnknownType, mailboxMap));
    103         // Put the unknown type parent into the mailbox map
    104         mailboxMap.put(boxParentUnknownType.mServerId, boxParentUnknownType);
    105         // Our unknown box should now be valid, because 1) the parent is unknown, BUT 2) the
    106         // parent's parent is a mail type
    107         assertTrue(parser.isValidMailFolder(boxUnknownType, mailboxMap));
    108     }
    109 
    110     private Mailbox setupBoxSync(int interval, int lookback, String serverId) {
    111         // Don't save the box; just create it, and give it a server id
    112         Mailbox box = EmailContentSetupUtils.setupMailbox("box1", mAccount.mId, false,
    113                 mProviderContext, Mailbox.TYPE_MAIL);
    114         box.mSyncInterval = interval;
    115         box.mSyncLookback = lookback;
    116         if (serverId != null) {
    117             box.mServerId = serverId;
    118         } else {
    119             box.mServerId = "serverId-" + mCreationTime + '-' + mServerIdCount++;
    120         }
    121         box.save(mProviderContext);
    122         return box;
    123     }
    124 
    125     private boolean syncOptionsSame(Mailbox a, Mailbox b) {
    126         if (a.mSyncInterval != b.mSyncInterval) return false;
    127         if (a.mSyncLookback != b.mSyncLookback) return false;
    128         return true;
    129     }
    130 
    131     public void testSaveAndRestoreMailboxSyncOptions() throws IOException {
    132         EasSyncService service = getTestService();
    133         EmailSyncAdapter adapter = new EmailSyncAdapter(service);
    134         FolderSyncParser parser = new FolderSyncParser(getTestInputStream(), adapter);
    135         mAccount.save(mProviderContext);
    136 
    137         parser.mAccount = mAccount;
    138         parser.mAccountId = mAccount.mId;
    139         parser.mAccountIdAsString = Long.toString(mAccount.mId);
    140         parser.mContext = mProviderContext;
    141         parser.mContentResolver = mProviderContext.getContentResolver();
    142 
    143         // Don't save the box; just create it, and give it a server id
    144         Mailbox box1 = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    145                 null);
    146         Mailbox box2 = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    147                 null);
    148         Mailbox boxa = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_1_MONTH,
    149                 null);
    150         Mailbox boxb = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_2_WEEKS,
    151                 null);
    152         Mailbox boxc = setupBoxSync(Account.CHECK_INTERVAL_PUSH, SyncWindow.SYNC_WINDOW_UNKNOWN,
    153                 null);
    154         Mailbox boxd = setupBoxSync(Account.CHECK_INTERVAL_PUSH, SyncWindow.SYNC_WINDOW_UNKNOWN,
    155                 null);
    156         Mailbox boxe = setupBoxSync(Account.CHECK_INTERVAL_PUSH, SyncWindow.SYNC_WINDOW_1_DAY,
    157                 null);
    158 
    159         // Save the options (for a, b, c, d, e);
    160         parser.saveMailboxSyncOptions();
    161         // There should be 5 entries in the map, and they should be the correct ones
    162         assertNotNull(parser.mSyncOptionsMap.get(boxa.mServerId));
    163         assertNotNull(parser.mSyncOptionsMap.get(boxb.mServerId));
    164         assertNotNull(parser.mSyncOptionsMap.get(boxc.mServerId));
    165         assertNotNull(parser.mSyncOptionsMap.get(boxd.mServerId));
    166         assertNotNull(parser.mSyncOptionsMap.get(boxe.mServerId));
    167 
    168         // Delete all the mailboxes in the account
    169         ContentResolver cr = mProviderContext.getContentResolver();
    170         cr.delete(Mailbox.CONTENT_URI, Mailbox.ACCOUNT_KEY + "=?",
    171                 new String[] {parser.mAccountIdAsString});
    172 
    173         // Create new boxes, all with default values for interval & window
    174         Mailbox box1x = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    175                 box1.mServerId);
    176         Mailbox box2x = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    177                 box2.mServerId);
    178         Mailbox boxax = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    179                 boxa.mServerId);
    180         Mailbox boxbx = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    181                 boxb.mServerId);
    182         Mailbox boxcx = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    183                 boxc.mServerId);
    184         Mailbox boxdx = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    185                 boxd.mServerId);
    186         Mailbox boxex = setupBoxSync(Account.CHECK_INTERVAL_NEVER, SyncWindow.SYNC_WINDOW_UNKNOWN,
    187                 boxe.mServerId);
    188 
    189         // Restore the sync options
    190         parser.restoreMailboxSyncOptions();
    191         box1x = Mailbox.restoreMailboxWithId(mProviderContext, box1x.mId);
    192         box2x = Mailbox.restoreMailboxWithId(mProviderContext, box2x.mId);
    193         boxax = Mailbox.restoreMailboxWithId(mProviderContext, boxax.mId);
    194         boxbx = Mailbox.restoreMailboxWithId(mProviderContext, boxbx.mId);
    195         boxcx = Mailbox.restoreMailboxWithId(mProviderContext, boxcx.mId);
    196         boxdx = Mailbox.restoreMailboxWithId(mProviderContext, boxdx.mId);
    197         boxex = Mailbox.restoreMailboxWithId(mProviderContext, boxex.mId);
    198 
    199         assertTrue(syncOptionsSame(box1, box1x));
    200         assertTrue(syncOptionsSame(box2, box2x));
    201         assertTrue(syncOptionsSame(boxa, boxax));
    202         assertTrue(syncOptionsSame(boxb, boxbx));
    203         assertTrue(syncOptionsSame(boxc, boxcx));
    204         assertTrue(syncOptionsSame(boxd, boxdx));
    205         assertTrue(syncOptionsSame(boxe, boxex));
    206     }
    207 }
    208