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