Home | History | Annotate | Download | only in exchange
      1 /*
      2  * Copyright (C) 2009 Marc Blank
      3  * Licensed to The Android Open Source Project.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.exchange;
     19 
     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.emailsync.AbstractSyncService;
     25 import com.android.emailsync.SyncManager.SyncError;
     26 import com.android.exchange.provider.EmailContentSetupUtils;
     27 import com.android.exchange.utility.ExchangeTestCase;
     28 
     29 import java.util.concurrent.ConcurrentHashMap;
     30 
     31 /**
     32  * You can run this entire test case with:
     33  *   runtest -c com.android.exchange.ExchangeServiceAccountTests exchange
     34  */
     35 @MediumTest
     36 public class ExchangeServiceAccountTests extends ExchangeTestCase {
     37 
     38     public ExchangeServiceAccountTests() {
     39         super();
     40     }
     41 
     42     public void testReleaseSyncHolds() {
     43         ExchangeService exchangeService = new ExchangeService();
     44         SyncError securityErrorAccount1 =
     45             exchangeService.new SyncError(AbstractSyncService.EXIT_SECURITY_FAILURE, false);
     46         SyncError ioError =
     47             exchangeService.new SyncError(AbstractSyncService.EXIT_IO_ERROR, false);
     48         SyncError securityErrorAccount2 =
     49             exchangeService.new SyncError(AbstractSyncService.EXIT_SECURITY_FAILURE, false);
     50         // Create account and two mailboxes
     51         Account acct1 = setupTestAccount("acct1", true);
     52         Mailbox box1 = EmailContentSetupUtils.setupMailbox("box1", acct1.mId, true,
     53                 mProviderContext);
     54         Mailbox box2 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
     55                 mProviderContext);
     56         Account acct2 = setupTestAccount("acct2", true);
     57         Mailbox box3 = EmailContentSetupUtils.setupMailbox("box3", acct2.mId, true,
     58                 mProviderContext);
     59         Mailbox box4 = EmailContentSetupUtils.setupMailbox("box4", acct2.mId, true,
     60                 mProviderContext);
     61 
     62         ConcurrentHashMap<Long, SyncError> errorMap = exchangeService.mSyncErrorMap;
     63         // Add errors into the map
     64         errorMap.put(box1.mId, securityErrorAccount1);
     65         errorMap.put(box2.mId, ioError);
     66         errorMap.put(box3.mId, securityErrorAccount2);
     67         errorMap.put(box4.mId, securityErrorAccount2);
     68         // We should have 4
     69         assertEquals(4, errorMap.keySet().size());
     70         // Release the holds on acct2 (there are two of them)
     71         assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
     72                 AbstractSyncService.EXIT_SECURITY_FAILURE, acct2));
     73         // There should be two left
     74         assertEquals(2, errorMap.keySet().size());
     75         // And these are the two...
     76         assertNotNull(errorMap.get(box2.mId));
     77         assertNotNull(errorMap.get(box1.mId));
     78 
     79         // Put the two back
     80         errorMap.put(box3.mId, securityErrorAccount2);
     81         errorMap.put(box4.mId, securityErrorAccount2);
     82         // We should have 4 again
     83         assertEquals(4, errorMap.keySet().size());
     84         // Release all of the security holds
     85         assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
     86                 AbstractSyncService.EXIT_SECURITY_FAILURE, null));
     87         // There should be one left
     88         assertEquals(1, errorMap.keySet().size());
     89         // And this is the one
     90         assertNotNull(errorMap.get(box2.mId));
     91 
     92         // Release the i/o holds on account 2 (there aren't any)
     93         assertFalse(exchangeService.releaseSyncHolds(mProviderContext,
     94                 AbstractSyncService.EXIT_IO_ERROR, acct2));
     95         // There should still be one left
     96         assertEquals(1, errorMap.keySet().size());
     97 
     98         // Release the i/o holds on account 1 (there's one)
     99         assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
    100                 AbstractSyncService.EXIT_IO_ERROR, acct1));
    101         // There should still be one left
    102         assertEquals(0, errorMap.keySet().size());
    103     }
    104 
    105     public void testIsSyncable() {
    106         Account acct1 = setupTestAccount("acct1", true);
    107         Mailbox box1 = EmailContentSetupUtils.setupMailbox("box1", acct1.mId, true,
    108                 mProviderContext, Mailbox.TYPE_DRAFTS);
    109         Mailbox box2 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
    110                 mProviderContext, Mailbox.TYPE_OUTBOX);
    111         Mailbox box3 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
    112                 mProviderContext, Mailbox.TYPE_ATTACHMENT);
    113         Mailbox box4 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
    114                 mProviderContext, Mailbox.TYPE_NOT_SYNCABLE + 64);
    115         Mailbox box5 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
    116                 mProviderContext, Mailbox.TYPE_MAIL);
    117         assertFalse(ExchangeService.isSyncable(box1));
    118         assertFalse(ExchangeService.isSyncable(box2));
    119         assertFalse(ExchangeService.isSyncable(box3));
    120         assertFalse(ExchangeService.isSyncable(box4));
    121         assertTrue(ExchangeService.isSyncable(box5));
    122     }
    123 }
    124