103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank/*
203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * Copyright (C) 2010 The Android Open Source Project
303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *
403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * Licensed under the Apache License, Version 2.0 (the "License");
503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * you may not use this file except in compliance with the License.
603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * You may obtain a copy of the License at
703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *
803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *      http://www.apache.org/licenses/LICENSE-2.0
903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *
1003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * Unless required by applicable law or agreed to in writing, software
1103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * distributed under the License is distributed on an "AS IS" BASIS,
1203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * See the License for the specific language governing permissions and
1403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * limitations under the License.
1503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank */
1603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
1703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankpackage com.android.email.service;
1803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
1903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.accounts.AccountManager;
2003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.ComponentName;
2103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.ContentResolver;
2203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.ContentUris;
2303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.ContentValues;
2403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.Context;
2503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport android.content.pm.PackageManager;
2603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
2703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.AccountTestCase;
2803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.Controller;
2903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.provider.AccountReconciler;
3003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.provider.EmailProvider;
3103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.provider.ProviderTestUtils;
3203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.email.service.MailService.AccountSyncReport;
3303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.emailcommon.provider.Account;
3403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.emailcommon.provider.EmailContent;
3503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport com.android.emailcommon.provider.HostAuth;
3603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
3703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport java.util.ArrayList;
3803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport java.util.HashMap;
3903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankimport java.util.List;
4003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
4103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank/**
4203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * Tests of the Email provider.
4303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *
4403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank * You can run this entire test case with:
4503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank *   runtest -c com.android.email.service.MailServiceTests email
4603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank */
4703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blankpublic class MailServiceTests extends AccountTestCase {
4803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
4903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    EmailProvider mProvider;
5003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    Context mMockContext;
5103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
5203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public MailServiceTests() {
5303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        super();
5403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
5503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
5603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    @Override
5703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void setUp() throws Exception {
5803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        super.setUp();
5903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        PackageManager pm = getContext().getPackageManager();
6003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        pm.setComponentEnabledSetting(
6103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                new ComponentName(getContext(), EasTestAuthenticatorService.class),
6203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
6303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                PackageManager.DONT_KILL_APP);
6403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        mMockContext = getMockContext();
6503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Delete any test accounts we might have created earlier
6603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        deleteTemporaryAccountManagerAccounts();
6703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
6803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
6903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    @Override
7003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void tearDown() throws Exception {
7103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        super.tearDown();
7203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Delete any test accounts we might have created earlier
7303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        deleteTemporaryAccountManagerAccounts();
7403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
7503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
7603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
7703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Confirm that the test below is functional (and non-destructive) when there are
7803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * prexisting (non-test) accounts in the account manager.
7903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
8003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void testTestReconcileAccounts() {
8103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account firstAccount = null;
8203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        final String TEST_USER_ACCOUNT = "__user_account_test_1";
8303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Context context = getContext();
8403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        try {
8503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            // Note:  Unlike calls to setupProviderAndAccountManagerAccount(), we are creating
8603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            // *real* accounts here (not in the mock provider)
8703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            createAccountManagerAccount(TEST_USER_ACCOUNT + TEST_ACCOUNT_SUFFIX);
8803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            firstAccount = ProviderTestUtils.setupAccount(TEST_USER_ACCOUNT, true, context);
8903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            // Now run the test with the "user" accounts in place
9003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            testReconcileAccounts();
9103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        } finally {
9203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            if (firstAccount != null) {
9303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                boolean firstAccountFound = false;
9403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // delete the provider account
9503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                context.getContentResolver().delete(firstAccount.getUri(), null, null);
9603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // delete the account manager account
9703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                android.accounts.Account[] accountManagerAccounts = AccountManager.get(context)
9803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                        .getAccountsByType(TEST_ACCOUNT_TYPE);
9903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                for (android.accounts.Account accountManagerAccount: accountManagerAccounts) {
10003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                    if ((TEST_USER_ACCOUNT + TEST_ACCOUNT_SUFFIX)
10103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                            .equals(accountManagerAccount.name)) {
10203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                        deleteAccountManagerAccount(accountManagerAccount);
10303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                        firstAccountFound = true;
10403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                    }
10503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                }
10603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertTrue(firstAccountFound);
10703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            }
10803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        }
10903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
11003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
11103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
11203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Note, there is some inherent risk in this test, as it creates *real* accounts in the
11303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * system (it cannot use the mock context with the Account Manager).
11403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
11503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void testReconcileAccounts() {
11603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Note that we can't use mMockContext for AccountManager interactions, as it isn't a fully
11703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // functional Context.
11803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Context context = getContext();
11903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
12003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Capture the baseline (account manager accounts) so we can measure the changes
12103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // we're making, irrespective of the number of actual accounts, and not destroy them
12203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        android.accounts.Account[] baselineAccounts =
12303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            AccountManager.get(context).getAccountsByType(TEST_ACCOUNT_TYPE);
12403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
12503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Set up three accounts, both in AccountManager and in EmailProvider
12603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account firstAccount = setupProviderAndAccountManagerAccount(getTestAccountName("1"));
12703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        setupProviderAndAccountManagerAccount(getTestAccountName("2"));
12803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        setupProviderAndAccountManagerAccount(getTestAccountName("3"));
12903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
13003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Check that they're set up properly
13103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(3, EmailContent.count(mMockContext, Account.CONTENT_URI, null, null));
13203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        android.accounts.Account[] accountManagerAccounts =
13303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                getAccountManagerAccounts(baselineAccounts);
13403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(3, accountManagerAccounts.length);
13503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
13603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Delete account "2" from AccountManager
13703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        android.accounts.Account removedAccount =
13803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            makeAccountManagerAccount(getTestAccountEmailAddress("2"));
13903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        deleteAccountManagerAccount(removedAccount);
14003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
14103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Confirm it's deleted
14203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        accountManagerAccounts = getAccountManagerAccounts(baselineAccounts);
14303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(2, accountManagerAccounts.length);
14403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
14503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Run the reconciler
14603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        ContentResolver resolver = mMockContext.getContentResolver();
14703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        MailService.reconcileAccountsWithAccountManager(context,
14803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                makeExchangeServiceAccountList(), accountManagerAccounts, mMockContext);
14903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
15003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // There should now be only two EmailProvider accounts
15103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(2, EmailContent.count(mMockContext, Account.CONTENT_URI, null, null));
15203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
15303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Ok, now we've got two of each; let's delete a provider account
15403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        resolver.delete(ContentUris.withAppendedId(Account.CONTENT_URI, firstAccount.mId),
15503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                null, null);
15603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // ...and then there was one
15703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(1, EmailContent.count(mMockContext, Account.CONTENT_URI, null, null));
15803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
15903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Run the reconciler
16003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        MailService.reconcileAccountsWithAccountManager(context,
16103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                makeExchangeServiceAccountList(), accountManagerAccounts, mMockContext);
16203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
16303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // There should now be only one AccountManager account
16403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        accountManagerAccounts = getAccountManagerAccounts(baselineAccounts);
16503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(1, accountManagerAccounts.length);
16603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // ... and it should be account "3"
16703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        assertEquals(getTestAccountEmailAddress("3"), accountManagerAccounts[0].name);
16803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
16903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
17003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
17103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Lightweight subclass of the Controller class allows injection of mock context
17203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
17303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public static class TestController extends Controller {
17403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
17503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        protected TestController(Context providerContext, Context systemContext) {
17603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            super(systemContext);
17703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            setProviderContext(providerContext);
17803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        }
17903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
18003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
18103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
18203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Create a simple HostAuth with protocol
18303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
18403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    private HostAuth setupSimpleHostAuth(String protocol) {
18503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        HostAuth hostAuth = new HostAuth();
18603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        hostAuth.mProtocol = protocol;
18703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        return hostAuth;
18803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
18903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
19003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
19103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Initial testing on setupSyncReportsLocked, making sure that EAS accounts aren't scheduled
19203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
19303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void testSetupSyncReportsLocked() {
19403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // TODO Test other functionality within setupSyncReportsLocked
19503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Setup accounts of each type, all with manual sync at different intervals
19603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account easAccount = ProviderTestUtils.setupAccount("account1", false, mMockContext);
19703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        easAccount.mHostAuthRecv = setupSimpleHostAuth("eas");
19803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        easAccount.mHostAuthSend = easAccount.mHostAuthRecv;
19903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        easAccount.mSyncInterval = 30;
20003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        easAccount.save(mMockContext);
20103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account imapAccount = ProviderTestUtils.setupAccount("account2", false, mMockContext);
20203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        imapAccount.mHostAuthRecv = setupSimpleHostAuth("imap");
20303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        imapAccount.mHostAuthSend = setupSimpleHostAuth("smtp");
20403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        imapAccount.mSyncInterval = 60;
20503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        imapAccount.save(mMockContext);
20603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account pop3Account = ProviderTestUtils.setupAccount("account3", false, mMockContext);
20703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        pop3Account.mHostAuthRecv = setupSimpleHostAuth("pop3");
20803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        pop3Account.mHostAuthSend = setupSimpleHostAuth("smtp");
20903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        pop3Account.mSyncInterval = 90;
21003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        pop3Account.save(mMockContext);
21103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
21203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Setup the SyncReport's for these Accounts
21303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        MailService mailService = new MailService();
21403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        mailService.mController = new TestController(mMockContext, getContext());
21503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        try {
21603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            mailService.setupSyncReportsLocked(MailService.SYNC_REPORTS_RESET, mMockContext);
21703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
21803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            // Get back the map created by MailService
21903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            HashMap<Long, AccountSyncReport> syncReportMap = MailService.mSyncReports;
22003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            synchronized (syncReportMap) {
22103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // Check the SyncReport's for correctness of sync interval
22203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                AccountSyncReport syncReport = syncReportMap.get(easAccount.mId);
22303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertNotNull(syncReport);
22403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // EAS sync interval should have been changed to "never"
22503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval);
22603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                syncReport = syncReportMap.get(imapAccount.mId);
22703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertNotNull(syncReport);
22803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertEquals(60, syncReport.syncInterval);
22903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                syncReport = syncReportMap.get(pop3Account.mId);
23003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertNotNull(syncReport);
23103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertEquals(90, syncReport.syncInterval);
23203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // Change the EAS account to push
23303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                ContentValues cv = new ContentValues();
23403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                cv.put(Account.SYNC_INTERVAL, Account.CHECK_INTERVAL_PUSH);
23503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                easAccount.update(mMockContext, cv);
23603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                syncReportMap.clear();
23703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                mailService.setupSyncReportsLocked(easAccount.mId, mMockContext);
23803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                syncReport = syncReportMap.get(easAccount.mId);
23903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertNotNull(syncReport);
24003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                // EAS sync interval should be "never" in this case as well
24103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank                assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval);
24203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            }
24303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        } finally {
24403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            mailService.mController.cleanupForTest();
24503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        }
24603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
24703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
24803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    /**
24903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * Test that setupSyncReports will skip over poorly-formed accounts which can be left
25003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     * over after unit tests.
25103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank     */
25203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    public void testSetupSyncReportsWithBadAccounts() {
25303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Setup accounts that trigger each skip-over case
25403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // 1: no email address
25503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account account1 = ProviderTestUtils.setupAccount("account1", false, mMockContext);
25603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account1.mHostAuthRecv = setupSimpleHostAuth("imap");
25703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account1.mHostAuthSend = setupSimpleHostAuth("smtp");
25803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account1.mSyncInterval = 30;
25903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account1.mEmailAddress = null;
26003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account1.save(mMockContext);
26103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // 2: no receiver hostauth
26203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account account2 = ProviderTestUtils.setupAccount("account2", false, mMockContext);
26303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account2.mHostAuthRecv = null;
26403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account2.mHostAuthSend = setupSimpleHostAuth("smtp");
26503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account2.mSyncInterval = 30;
26603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account2.save(mMockContext);
26703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // 3: no sender hostauth
26803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        Account account3 = ProviderTestUtils.setupAccount("account3", false, mMockContext);
26903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account3.mHostAuthRecv = setupSimpleHostAuth("imap");
27003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account3.mHostAuthSend = null;
27103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account3.mSyncInterval = 30;
27203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        account3.save(mMockContext);
27303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
27403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        // Setup the SyncReport's for these Accounts
27503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        MailService mailService = new MailService();
27603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        mailService.mController = new TestController(mMockContext, getContext());
27703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        try {
27803cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            mailService.setupSyncReportsLocked(MailService.SYNC_REPORTS_RESET, mMockContext);
27903cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            // Get back the map created by MailService - it should be empty
28003cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            HashMap<Long, AccountSyncReport> syncReportMap = MailService.mSyncReports;
28103cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            assertEquals(0, syncReportMap.size());
28203cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        } finally {
28303cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank            mailService.mController.cleanupForTest();
28403cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank        }
28503cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank
28603cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank    }
28703cd72805dab0379ed255d151f1c17cc60655fc3Marc Blank}
288