ExchangeServiceAccountTests.java revision 4d8774462ace9a45154b2df418b9f2fe7a9c685d
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
18package com.android.exchange;
19
20import com.android.emailcommon.provider.EmailContent.Account;
21import com.android.exchange.ExchangeService.SyncError;
22import com.android.exchange.provider.EmailContentSetupUtils;
23import com.android.exchange.utility.ExchangeTestCase;
24
25import android.test.suitebuilder.annotation.MediumTest;
26
27import java.util.concurrent.ConcurrentHashMap;
28
29/**
30 * You can run this entire test case with:
31 *   runtest -c com.android.exchange.ExchangeServiceAccountTests exchange
32 */
33@MediumTest
34public class ExchangeServiceAccountTests extends ExchangeTestCase {
35
36    public ExchangeServiceAccountTests() {
37        super();
38    }
39
40    public void testReleaseSyncHolds() {
41        ExchangeService exchangeService = new ExchangeService();
42        SyncError securityErrorAccount1 =
43            exchangeService.new SyncError(AbstractSyncService.EXIT_SECURITY_FAILURE, false);
44        SyncError ioError =
45            exchangeService.new SyncError(AbstractSyncService.EXIT_IO_ERROR, false);
46        SyncError securityErrorAccount2 =
47            exchangeService.new SyncError(AbstractSyncService.EXIT_SECURITY_FAILURE, false);
48        // Create account and two mailboxes
49        Account acct1 = setupTestAccount("acct1", true);
50        Mailbox box1 = EmailContentSetupUtils.setupMailbox("box1", acct1.mId, true,
51                mProviderContext);
52        Mailbox box2 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
53                mProviderContext);
54        Account acct2 = setupTestAccount("acct2", true);
55        Mailbox box3 = EmailContentSetupUtils.setupMailbox("box3", acct2.mId, true,
56                mProviderContext);
57        Mailbox box4 = EmailContentSetupUtils.setupMailbox("box4", acct2.mId, true,
58                mProviderContext);
59
60        ConcurrentHashMap<Long, SyncError> errorMap = exchangeService.mSyncErrorMap;
61        // Add errors into the map
62        errorMap.put(box1.mId, securityErrorAccount1);
63        errorMap.put(box2.mId, ioError);
64        errorMap.put(box3.mId, securityErrorAccount2);
65        errorMap.put(box4.mId, securityErrorAccount2);
66        // We should have 4
67        assertEquals(4, errorMap.keySet().size());
68        // Release the holds on acct2 (there are two of them)
69        assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
70                AbstractSyncService.EXIT_SECURITY_FAILURE, acct2));
71        // There should be two left
72        assertEquals(2, errorMap.keySet().size());
73        // And these are the two...
74        assertNotNull(errorMap.get(box2.mId));
75        assertNotNull(errorMap.get(box1.mId));
76
77        // Put the two back
78        errorMap.put(box3.mId, securityErrorAccount2);
79        errorMap.put(box4.mId, securityErrorAccount2);
80        // We should have 4 again
81        assertEquals(4, errorMap.keySet().size());
82        // Release all of the security holds
83        assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
84                AbstractSyncService.EXIT_SECURITY_FAILURE, null));
85        // There should be one left
86        assertEquals(1, errorMap.keySet().size());
87        // And this is the one
88        assertNotNull(errorMap.get(box2.mId));
89
90        // Release the i/o holds on account 2 (there aren't any)
91        assertFalse(exchangeService.releaseSyncHolds(mProviderContext,
92                AbstractSyncService.EXIT_IO_ERROR, acct2));
93        // There should still be one left
94        assertEquals(1, errorMap.keySet().size());
95
96        // Release the i/o holds on account 1 (there's one)
97        assertTrue(exchangeService.releaseSyncHolds(mProviderContext,
98                AbstractSyncService.EXIT_IO_ERROR, acct1));
99        // There should still be one left
100        assertEquals(0, errorMap.keySet().size());
101    }
102
103    public void testIsSyncable() {
104        Account acct1 = setupTestAccount("acct1", true);
105        Mailbox box1 = EmailContentSetupUtils.setupMailbox("box1", acct1.mId, true,
106                mProviderContext, Mailbox.TYPE_DRAFTS);
107        Mailbox box2 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
108                mProviderContext, Mailbox.TYPE_OUTBOX);
109        Mailbox box3 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
110                mProviderContext, Mailbox.TYPE_ATTACHMENT);
111        Mailbox box4 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
112                mProviderContext, Mailbox.TYPE_NOT_SYNCABLE + 64);
113        Mailbox box5 = EmailContentSetupUtils.setupMailbox("box2", acct1.mId, true,
114                mProviderContext, Mailbox.TYPE_MAIL);
115        assertFalse(ExchangeService.isSyncable(null));
116        assertFalse(ExchangeService.isSyncable(box1));
117        assertFalse(ExchangeService.isSyncable(box2));
118        assertFalse(ExchangeService.isSyncable(box3));
119        assertFalse(ExchangeService.isSyncable(box4));
120        assertTrue(ExchangeService.isSyncable(box5));
121    }
122}
123