Home | History | Annotate | Download | only in service
      1 /*
      2  * Copyright (C) 2011 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.email.service;
     18 
     19 import com.android.email.AccountTestCase;
     20 import com.android.email.provider.ProviderTestUtils;
     21 import com.android.emailcommon.provider.Account;
     22 import com.android.emailcommon.provider.EmailContent.AccountColumns;
     23 import com.android.emailcommon.provider.HostAuth;
     24 import com.android.emailcommon.utility.Utility;
     25 
     26 import android.content.ContentUris;
     27 import android.content.Context;
     28 import android.net.Uri;
     29 
     30 import java.util.NoSuchElementException;
     31 
     32 /**
     33  * Tests of the Email provider.
     34  *
     35  * You can run this entire test case with:
     36  *   runtest -c com.android.email.service.EmailBroadcastProcessorServiceTests email
     37  */
     38 public class EmailBroadcastProcessorServiceTests extends AccountTestCase {
     39 
     40     Context mMockContext;
     41 
     42     public EmailBroadcastProcessorServiceTests() {
     43         super();
     44     }
     45 
     46     @Override
     47     public void setUp() throws Exception {
     48         super.setUp();
     49         mMockContext = getMockContext();
     50     }
     51 
     52     @Override
     53     public void tearDown() throws Exception {
     54         super.tearDown();
     55     }
     56 
     57     /**
     58      * Create a simple HostAuth with protocol
     59      */
     60     private HostAuth setupSimpleHostAuth(String protocol) {
     61         HostAuth hostAuth = ProviderTestUtils.setupHostAuth(protocol, "name", false, mContext);
     62         hostAuth.mProtocol = protocol;
     63         return hostAuth;
     64     }
     65 
     66     /**
     67      * Returns the flags for the specified account. Throws an exception if the account cannot
     68      * be found.
     69      */
     70     private int getAccountFlags(long accountId) throws NoSuchElementException {
     71         Uri uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
     72         Integer flags = Utility.getFirstRowInt(mMockContext, uri,
     73                 new String[] { AccountColumns.FLAGS }, null, null, null, 0);
     74         if (flags == null) {
     75             throw new NoSuchElementException("No cursor");
     76         }
     77         return flags;
     78     }
     79 
     80     /**
     81      * Initial testing on setupSyncReportsLocked, making sure that EAS accounts aren't scheduled
     82      */
     83     public void testSetImapDeletePolicy() {
     84         // Setup accounts of each type, all with manual sync at different intervals
     85         Account account1 = ProviderTestUtils.setupAccount("eas-account1", false, mMockContext);
     86         account1.mHostAuthRecv = setupSimpleHostAuth("eas");
     87         account1.mHostAuthSend = account1.mHostAuthRecv;
     88         account1.save(mMockContext);
     89         long accountId1 = account1.mId;
     90         Account account2 = ProviderTestUtils.setupAccount("pop-account1", false, mMockContext);
     91         account2.mHostAuthRecv = setupSimpleHostAuth("pop3");
     92         account2.mHostAuthSend = setupSimpleHostAuth("smtp");
     93         account2.mFlags = 0x08;       // set delete policy
     94         account2.save(mMockContext);
     95         long accountId2 = account2.mId;
     96         Account account3 = ProviderTestUtils.setupAccount("pop-account2", false, mMockContext);
     97         account3.mHostAuthRecv = setupSimpleHostAuth("pop3");
     98         account3.mHostAuthSend = setupSimpleHostAuth("smtp");
     99         account3.save(mMockContext);
    100         long accountId3 = account3.mId;
    101         Account account4 = ProviderTestUtils.setupAccount("imap-account1", false, mMockContext);
    102         account4.mHostAuthRecv = setupSimpleHostAuth("imap");
    103         account4.mHostAuthSend = setupSimpleHostAuth("smtp");
    104         account4.mFlags = 0xa5a5a5a5; // Alternating bits; includes bad delete policy
    105         account4.save(mMockContext);
    106         long accountId4 = account4.mId;
    107         Account account5 = ProviderTestUtils.setupAccount("imap-account2", false, mMockContext);
    108         account5.mHostAuthRecv = setupSimpleHostAuth("imap");
    109         account5.mHostAuthSend = setupSimpleHostAuth("smtp");
    110         account5.mFlags = 0x0c;       // All delete policy bits set
    111         account5.save(mMockContext);
    112         long accountId5 = account5.mId;
    113         Account account6 = ProviderTestUtils.setupAccount("imap-account3", false, mMockContext);
    114         account6.mHostAuthRecv = setupSimpleHostAuth("imap");
    115         account6.mHostAuthSend = setupSimpleHostAuth("smtp");
    116         account6.mFlags = 0;         // No delete policy bits set
    117         account6.save(mMockContext);
    118         long accountId6 = account6.mId;
    119 
    120         // Run the account migration
    121         EmailBroadcastProcessorService.setImapDeletePolicy(mMockContext);
    122 
    123         // Test the results
    124         int accountFlags1 = getAccountFlags(accountId1);
    125         assertEquals(4, accountFlags1);           // not IMAP; no changes
    126         int accountFlags2 = getAccountFlags(accountId2);
    127         assertEquals(8, accountFlags2);           // not IMAP; no changes
    128         int accountFlags3 = getAccountFlags(accountId3);
    129         assertEquals(4, accountFlags3);           // not IMAP; no changes
    130         int accountFlags4 = getAccountFlags(accountId4);
    131         assertEquals(0xa5a5a5a9, accountFlags4);  // Only update delete policy bits
    132         int accountFlags5 = getAccountFlags(accountId5);
    133         assertEquals(0x00000008, accountFlags5);
    134         int accountFlags6 = getAccountFlags(accountId6);
    135         assertEquals(0x00000008, accountFlags6);
    136     }
    137 
    138 }
    139