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