• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.activity;
18 
19 import android.content.ContentValues;
20 import android.content.Context;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.MediumTest;
23 
24 import com.android.email.Controller;
25 import com.android.email.DBTestHelper;
26 import com.android.email.MockClock;
27 import com.android.email.provider.ContentCache;
28 import com.android.email.provider.ProviderTestUtils;
29 import com.android.emailcommon.provider.EmailContent.MailboxColumns;
30 import com.android.emailcommon.provider.Mailbox;
31 
32 import java.util.ArrayList;
33 import java.util.HashSet;
34 import java.util.Set;
35 
36 /**
37  * Tests for the recent mailbox manager.
38  *
39  * You can run this entire test case with:
40  *   runtest -c com.android.email.activity.RecentMailboxManagerTest email
41  */
42 @MediumTest
43 public class RecentMailboxManagerTest extends AndroidTestCase {
44 
45     private Context mMockContext;
46     private MockClock mMockClock;
47     private RecentMailboxManager mManager;
48     private Mailbox[] mMailboxArray;
49 
RecentMailboxManagerTest()50     public RecentMailboxManagerTest() {
51     }
52 
53     @Override
setUp()54     public void setUp() throws Exception {
55         super.setUp();
56         mMockContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
57                 getContext());
58         mMockClock = new MockClock();
59         RecentMailboxManager.sClock = mMockClock;
60         mManager = RecentMailboxManager.getInstance(mMockContext);
61         Controller.getInstance(mMockContext).setProviderContext(mMockContext);
62         mMailboxArray = new Mailbox[] {
63             ProviderTestUtils.setupMailbox("inbox", 1L, true, mMockContext, Mailbox.TYPE_INBOX),
64             ProviderTestUtils.setupMailbox("drafts", 1L, true, mMockContext, Mailbox.TYPE_DRAFTS),
65             ProviderTestUtils.setupMailbox("outbox", 1L, true, mMockContext, Mailbox.TYPE_OUTBOX),
66             ProviderTestUtils.setupMailbox("sent", 1L, true, mMockContext, Mailbox.TYPE_SENT),
67             ProviderTestUtils.setupMailbox("trash", 1L, true, mMockContext, Mailbox.TYPE_TRASH),
68             ProviderTestUtils.setupMailbox("junk", 1L, true, mMockContext, Mailbox.TYPE_JUNK),
69             ProviderTestUtils.setupMailbox("abbott", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
70             ProviderTestUtils.setupMailbox("costello", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
71             ProviderTestUtils.setupMailbox("bud_lou", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
72             ProviderTestUtils.setupMailbox("laurel", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
73             ProviderTestUtils.setupMailbox("hardy", 1L, true, mMockContext, Mailbox.TYPE_MAIL)
74         };
75         // Invalidate all caches, since we reset the database for each test
76         ContentCache.invalidateAllCaches();
77     }
78 
79     @Override
tearDown()80     protected void tearDown() throws Exception {
81         RecentMailboxManager.sInstance = null;
82         super.tearDown();
83     }
84 
testTouch()85     public void testTouch() throws Exception {
86         Set<Integer> defaultRecents = new HashSet<Integer>() {{
87             for (int type : RecentMailboxManager.DEFAULT_RECENT_TYPES) {
88                 add(type);
89             }
90         }};
91 
92         // Ensure all accounts can be touched
93         for (Mailbox mailbox : mMailboxArray) {
94             // Safety ... default touch time
95             Mailbox untouchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
96             if (!defaultRecents.contains(mailbox.mType)) {
97                 assertEquals(0L, untouchedMailbox.mLastTouchedTime);
98             }
99 
100             // Touch the mailbox
101             mManager.touch(1L, mailbox.mId).get();
102 
103             // Touch time is actually set
104             Mailbox touchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
105             assertEquals(mMockClock.getTime(), touchedMailbox.mLastTouchedTime);
106 
107             mMockClock.advance(1000L);
108         }
109         // Now ensure touching one didn't affect the others
110         long touchTime = MockClock.DEFAULT_TIME;
111         for (Mailbox mailbox : mMailboxArray) {
112             // Touch time is actually set
113             Mailbox touchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
114             assertEquals(touchTime, touchedMailbox.mLastTouchedTime);
115             touchTime += 1000L;
116         }
117     }
118 
119     /** Test default list */
testGetMostRecent01()120     public void testGetMostRecent01() throws Exception {
121         ArrayList<Long> testList;
122 
123         // test default list
124         // With exclusions
125         testList = mManager.getMostRecent(1L, true);
126         assertEquals("w/ exclusions", 0, testList.size());
127 
128         // Without exclusions -- we'll get "default" list.
129         testList = mManager.getMostRecent(1L, false);
130         assertEquals("w/o exclusions", 2, testList.size());
131 
132         assertEquals(mMailboxArray[1].mId, (long) testList.get(0)); // Drafts
133         assertEquals(mMailboxArray[3].mId, (long) testList.get(1)); // Sent
134     }
135 
136     /** Test recent list not full */
testGetMostRecent02()137     public void testGetMostRecent02() throws Exception {
138         ArrayList<Long> testList;
139         // need to wait for the last one to ensure getMostRecent() has something to work on
140         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId).get(); // costello
141 
142         // test recent list not full, so is padded with default mailboxes
143         testList = mManager.getMostRecent(1L, false);
144         assertEquals(3, testList.size());
145         assertEquals(mMailboxArray[7].mId, (long) testList.get(0)); // costello
146         assertEquals(mMailboxArray[1].mId, (long) testList.get(1)); // Drafts
147         assertEquals(mMailboxArray[3].mId, (long) testList.get(2)); // Sent
148         testList = mManager.getMostRecent(1L, true);
149         assertEquals(1, testList.size());
150         assertEquals(mMailboxArray[7].mId, (long) testList.get(0));
151     }
152 
153     /** Test full recent list */
testGetMostRecent03()154     public void testGetMostRecent03() throws Exception {
155         ArrayList<Long> testList;
156 
157         // touch some more mailboxes
158         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
159         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId); // trash
160         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
161         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[8].mId); // bud_lou
162         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId); // costello
163         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[9].mId).get(); // laurel
164 
165         // test full recent list
166         testList = mManager.getMostRecent(1L, false);
167         assertEquals(5, testList.size());
168         assertEquals(mMailboxArray[8].mId, (long) testList.get(0)); // bud_lou
169         assertEquals(mMailboxArray[7].mId, (long) testList.get(1)); // costello
170         assertEquals(mMailboxArray[9].mId, (long) testList.get(2)); // laurel
171         assertEquals(mMailboxArray[2].mId, (long) testList.get(3)); // outbox
172         assertEquals(mMailboxArray[4].mId, (long) testList.get(4)); // trash
173         testList = mManager.getMostRecent(1L, true);
174         assertEquals(3, testList.size());
175         assertEquals(mMailboxArray[8].mId, (long) testList.get(0));
176         assertEquals(mMailboxArray[7].mId, (long) testList.get(1));
177         assertEquals(mMailboxArray[9].mId, (long) testList.get(2));
178     }
179 
180     /** Test limit for system mailboxes */
testGetMostRecent04()181     public void testGetMostRecent04() throws Exception {
182         ArrayList<Long> testList;
183 
184         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[0].mId); // inbox
185         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[1].mId); // drafts
186         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
187         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
188         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId).get(); // trash
189 
190         // nothing but system mailboxes, but inbox is never included
191         testList = mManager.getMostRecent(1L, false);
192         assertEquals(4, testList.size());
193         assertEquals(mMailboxArray[1].mId, (long) testList.get(0));
194         assertEquals(mMailboxArray[2].mId, (long) testList.get(1));
195         assertEquals(mMailboxArray[3].mId, (long) testList.get(2));
196         assertEquals(mMailboxArray[4].mId, (long) testList.get(3));
197         testList = mManager.getMostRecent(1L, true);
198         assertEquals(0, testList.size());
199     }
200 
201     /** Test limit for user mailboxes */
testGetMostRecent05()202     public void testGetMostRecent05() throws Exception {
203         ArrayList<Long> testList;
204 
205         // test limit for the filtered list
206         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[6].mId); // abbott
207         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId); // costello
208         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[8].mId); // bud_lou
209         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[9].mId); // laurel
210         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[10].mId); // hardy
211         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[1].mId); // drafts
212         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
213         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
214         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId); // trash
215         mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[5].mId).get(); // junk
216 
217         // nothing but user mailboxes
218         testList = mManager.getMostRecent(1L, false);
219         assertEquals(5, testList.size());
220         assertEquals(mMailboxArray[1].mId, (long) testList.get(0));
221         assertEquals(mMailboxArray[5].mId, (long) testList.get(1));
222         assertEquals(mMailboxArray[2].mId, (long) testList.get(2));
223         assertEquals(mMailboxArray[3].mId, (long) testList.get(3));
224         assertEquals(mMailboxArray[4].mId, (long) testList.get(4));
225         testList = mManager.getMostRecent(1L, true);
226         assertEquals(5, testList.size());
227         assertEquals(mMailboxArray[6].mId, (long) testList.get(0));
228         assertEquals(mMailboxArray[8].mId, (long) testList.get(1));
229         assertEquals(mMailboxArray[7].mId, (long) testList.get(2));
230         assertEquals(mMailboxArray[10].mId, (long) testList.get(3));
231         assertEquals(mMailboxArray[9].mId, (long) testList.get(4));
232     }
233 
testDoesNotIncludeExtraMailboxes()234     public void testDoesNotIncludeExtraMailboxes() throws Exception {
235         ArrayList<Long> testList;
236 
237         // The search mailbox should not be visible.
238         Mailbox searchMailbox = ProviderTestUtils.setupMailbox(
239                 "search", 1L, true, mMockContext, Mailbox.TYPE_SEARCH);
240         ContentValues cv = new ContentValues();
241         cv.put(MailboxColumns.FLAG_VISIBLE, false);
242         searchMailbox.mFlagVisible = false;
243         searchMailbox.update(mMockContext, cv);
244 
245         mMockClock.advance(1000L); mManager.touch(1L, searchMailbox.mId).get();
246 
247         // Ensure search mailbox isn't returned
248         testList = mManager.getMostRecent(1L, false);
249         assertFalse(testList.contains(searchMailbox.mId));
250         testList = mManager.getMostRecent(1L, true);
251         assertFalse(testList.contains(searchMailbox.mId));
252     }
253 }
254