PageRenderTime 62ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/src/com/android/email/activity/RecentMailboxManagerTest.java

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