PageRenderTime 206ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/src/com/android/emailcommon/provider/MailboxTests.java

https://bitbucket.org/camcory/android_packages_apps_email
Java | 598 lines | 407 code | 100 blank | 91 comment | 1 complexity | 4ef3256c01d6f0552e04a259d586dbe6 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.emailcommon.provider;
  17. import com.android.email.provider.ContentCache;
  18. import com.android.email.provider.EmailProvider;
  19. import com.android.email.provider.ProviderTestUtils;
  20. import com.android.emailcommon.provider.EmailContent.MailboxColumns;
  21. import com.android.emailcommon.provider.EmailContent.Message;
  22. import com.android.emailcommon.provider.EmailContent.MessageColumns;
  23. import com.android.emailcommon.utility.Utility;
  24. import android.content.ContentUris;
  25. import android.content.ContentValues;
  26. import android.content.Context;
  27. import android.net.Uri;
  28. import android.os.Parcel;
  29. import android.test.MoreAsserts;
  30. import android.test.ProviderTestCase2;
  31. import android.test.suitebuilder.annotation.SmallTest;
  32. import java.util.Arrays;
  33. /**
  34. * Unit tests for the Mailbox inner class.
  35. * These tests must be locally complete - no server(s) required.
  36. */
  37. @SmallTest
  38. public class MailboxTests extends ProviderTestCase2<EmailProvider> {
  39. private static final String TEST_DISPLAY_NAME = "display-name";
  40. private static final String TEST_PARENT_SERVER_ID = "parent-server-id";
  41. private static final String TEST_SERVER_ID = "server-id";
  42. private static final String TEST_SYNC_KEY = "sync-key";
  43. private static final String TEST_SYNC_STATUS = "sync-status";
  44. private Context mMockContext;
  45. private EmailProvider mProvider;
  46. public MailboxTests() {
  47. super(EmailProvider.class, EmailContent.AUTHORITY);
  48. }
  49. @Override
  50. public void setUp() throws Exception {
  51. super.setUp();
  52. mMockContext = getMockContext();
  53. mProvider = getProvider();
  54. // Invalidate all caches, since we reset the database for each test
  55. ContentCache.invalidateAllCaches();
  56. }
  57. //////////////////////////////////////////////////////////
  58. ////// Utility methods
  59. //////////////////////////////////////////////////////////
  60. /** Returns the number of messages in a mailbox. */
  61. private int getMessageCount(long mailboxId) {
  62. return Utility.getFirstRowInt(mMockContext,
  63. ContentUris.withAppendedId(Mailbox.CONTENT_URI, mailboxId),
  64. new String[] {MailboxColumns.MESSAGE_COUNT}, null, null, null, 0);
  65. }
  66. /** Creates a new message. */
  67. private static Message createMessage(Context c, Mailbox b, boolean starred, boolean read,
  68. int flagLoaded) {
  69. Message message = ProviderTestUtils.setupMessage(
  70. "1", b.mAccountKey, b.mId, true, false, c, starred, read);
  71. message.mFlagLoaded = flagLoaded;
  72. message.save(c);
  73. return message;
  74. }
  75. //////////////////////////////////////////////////////////
  76. ////// The tests
  77. //////////////////////////////////////////////////////////
  78. /**
  79. * Test simple mailbox save/retrieve
  80. */
  81. public void testSave() {
  82. final Context c = mMockContext;
  83. Account account1 = ProviderTestUtils.setupAccount("mailbox-save", true, c);
  84. long account1Id = account1.mId;
  85. Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, c);
  86. long box1Id = box1.mId;
  87. Mailbox box2 = Mailbox.restoreMailboxWithId(c, box1Id);
  88. ProviderTestUtils.assertMailboxEqual("testMailboxSave", box1, box2);
  89. }
  90. /**
  91. * Test delete mailbox
  92. */
  93. public void testDelete() {
  94. final Context c = mMockContext;
  95. Account account1 = ProviderTestUtils.setupAccount("mailbox-delete", true, c);
  96. long account1Id = account1.mId;
  97. Mailbox box1 = ProviderTestUtils.setupMailbox("box1", account1Id, true, c);
  98. long box1Id = box1.mId;
  99. Mailbox box2 = ProviderTestUtils.setupMailbox("box2", account1Id, true, c);
  100. long box2Id = box2.mId;
  101. String selection = EmailContent.MailboxColumns.ACCOUNT_KEY + "=?";
  102. String[] selArgs = new String[] { String.valueOf(account1Id) };
  103. // make sure there are two mailboxes
  104. int numBoxes = EmailContent.count(c, Mailbox.CONTENT_URI, selection, selArgs);
  105. assertEquals(2, numBoxes);
  106. // now delete one of them
  107. Uri uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, box1Id);
  108. c.getContentResolver().delete(uri, null, null);
  109. // make sure there's only one mailbox now
  110. numBoxes = EmailContent.count(c, Mailbox.CONTENT_URI, selection, selArgs);
  111. assertEquals(1, numBoxes);
  112. // now delete the other one
  113. uri = ContentUris.withAppendedId(Mailbox.CONTENT_URI, box2Id);
  114. c.getContentResolver().delete(uri, null, null);
  115. // make sure there are no mailboxes now
  116. numBoxes = EmailContent.count(c, Mailbox.CONTENT_URI, selection, selArgs);
  117. assertEquals(0, numBoxes);
  118. }
  119. public void testGetMailboxType() {
  120. final Context c = mMockContext;
  121. Account a = ProviderTestUtils.setupAccount("acct1", true, c);
  122. Mailbox bi = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_INBOX);
  123. Mailbox bm = ProviderTestUtils.setupMailbox("b2", a.mId, true, c, Mailbox.TYPE_MAIL);
  124. assertEquals(Mailbox.TYPE_INBOX, Mailbox.getMailboxType(c, bi.mId));
  125. assertEquals(Mailbox.TYPE_MAIL, Mailbox.getMailboxType(c, bm.mId));
  126. assertEquals(-1, Mailbox.getMailboxType(c, 999999)); // mailbox not found
  127. }
  128. public void testGetDisplayName() {
  129. final Context c = mMockContext;
  130. Account a = ProviderTestUtils.setupAccount("acct1", true, c);
  131. Mailbox bi = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_INBOX);
  132. Mailbox bm = ProviderTestUtils.setupMailbox("b2", a.mId, true, c, Mailbox.TYPE_MAIL);
  133. assertEquals("b1", Mailbox.getDisplayName(c, bi.mId));
  134. assertEquals("b2", Mailbox.getDisplayName(c, bm.mId));
  135. assertEquals(null, Mailbox.getDisplayName(c, 999999)); // mailbox not found
  136. }
  137. public void testIsRefreshable() {
  138. final Context c = mMockContext;
  139. Account a = ProviderTestUtils.setupAccount("acct1", true, c);
  140. Mailbox bi = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_INBOX);
  141. Mailbox bm = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_MAIL);
  142. Mailbox bd = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_DRAFTS);
  143. Mailbox bo = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_OUTBOX);
  144. assertTrue(Mailbox.isRefreshable(c, bi.mId));
  145. assertTrue(Mailbox.isRefreshable(c, bm.mId));
  146. assertFalse(Mailbox.isRefreshable(c, bd.mId));
  147. assertFalse(Mailbox.isRefreshable(c, bo.mId));
  148. // No such mailbox
  149. assertFalse(Mailbox.isRefreshable(c, 9999999));
  150. // Magic mailboxes can't be refreshed.
  151. assertFalse(Mailbox.isRefreshable(c, Mailbox.QUERY_ALL_DRAFTS));
  152. assertFalse(Mailbox.isRefreshable(c, Mailbox.QUERY_ALL_INBOXES));
  153. }
  154. public void testCanMoveFrom() {
  155. final Context c = mMockContext;
  156. Account a = ProviderTestUtils.setupAccount("acct1", true, c);
  157. Mailbox bi = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_INBOX);
  158. Mailbox bm = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_MAIL);
  159. Mailbox bd = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_DRAFTS);
  160. Mailbox bo = ProviderTestUtils.setupMailbox("b1", a.mId, true, c, Mailbox.TYPE_OUTBOX);
  161. assertTrue(bi.canHaveMessagesMoved());
  162. assertTrue(bm.canHaveMessagesMoved());
  163. assertFalse(bd.canHaveMessagesMoved());
  164. assertFalse(bo.canHaveMessagesMoved());
  165. }
  166. public void testGetMailboxForMessageId() {
  167. final Context c = mMockContext;
  168. Mailbox b1 = ProviderTestUtils.setupMailbox("box1", 1, true, c, Mailbox.TYPE_MAIL);
  169. Mailbox b2 = ProviderTestUtils.setupMailbox("box2", 1, true, c, Mailbox.TYPE_MAIL);
  170. Message m1 = ProviderTestUtils.setupMessage("1", b1.mAccountKey, b1.mId,
  171. true, true, c, false, false);
  172. Message m2 = ProviderTestUtils.setupMessage("1", b2.mAccountKey, b2.mId,
  173. true, true, c, false, false);
  174. ProviderTestUtils.assertMailboxEqual("x", b1, Mailbox.getMailboxForMessageId(c, m1.mId));
  175. ProviderTestUtils.assertMailboxEqual("x", b2, Mailbox.getMailboxForMessageId(c, m2.mId));
  176. }
  177. public void testRestoreMailboxWithId() {
  178. final Context c = mMockContext;
  179. Mailbox testMailbox;
  180. testMailbox = ProviderTestUtils.setupMailbox("box1", 1, true, c, Mailbox.TYPE_MAIL);
  181. ProviderTestUtils.assertMailboxEqual(
  182. "x", testMailbox, Mailbox.restoreMailboxWithId(c, testMailbox.mId));
  183. testMailbox = ProviderTestUtils.setupMailbox("box2", 1, true, c, Mailbox.TYPE_MAIL);
  184. ProviderTestUtils.assertMailboxEqual(
  185. "x", testMailbox, Mailbox.restoreMailboxWithId(c, testMailbox.mId));
  186. // Unknown IDs
  187. assertNull(Mailbox.restoreMailboxWithId(c, 8));
  188. assertNull(Mailbox.restoreMailboxWithId(c, -1));
  189. assertNull(Mailbox.restoreMailboxWithId(c, Long.MAX_VALUE));
  190. }
  191. public void testRestoreMailboxForPath() {
  192. final Context c = mMockContext;
  193. Mailbox testMailbox;
  194. testMailbox = ProviderTestUtils.setupMailbox("a/b/c/box", 1, true, c, Mailbox.TYPE_MAIL);
  195. ProviderTestUtils.assertMailboxEqual(
  196. "x", testMailbox, Mailbox.restoreMailboxForPath(c, 1, "a/b/c/box"));
  197. // Same name, different account; no match
  198. assertNull(Mailbox.restoreMailboxForPath(c, 2, "a/b/c/box"));
  199. // Substring; no match
  200. assertNull(Mailbox.restoreMailboxForPath(c, 1, "a/b/c"));
  201. // Wild cards not supported; no match
  202. assertNull(Mailbox.restoreMailboxForPath(c, 1, "a/b/c/%"));
  203. }
  204. public void testFindMailboxOfType() {
  205. final Context context = mMockContext;
  206. // Create two accounts and a variety of mailbox types
  207. Account acct1 = ProviderTestUtils.setupAccount("acct1", true, context);
  208. Mailbox acct1Inbox =
  209. ProviderTestUtils.setupMailbox("Inbox1", acct1.mId, true, context, Mailbox.TYPE_INBOX);
  210. Mailbox acct1Calendar =
  211. ProviderTestUtils.setupMailbox("Cal1", acct1.mId, true, context, Mailbox.TYPE_CALENDAR);
  212. Mailbox acct1Contacts =
  213. ProviderTestUtils.setupMailbox("Con1", acct1.mId, true, context, Mailbox.TYPE_CONTACTS);
  214. Account acct2 = ProviderTestUtils.setupAccount("acct1", true, context);
  215. Mailbox acct2Inbox =
  216. ProviderTestUtils.setupMailbox("Inbox2", acct2.mId, true, context, Mailbox.TYPE_INBOX);
  217. Mailbox acct2Calendar =
  218. ProviderTestUtils.setupMailbox("Cal2", acct2.mId, true, context, Mailbox.TYPE_CALENDAR);
  219. Mailbox acct2Contacts =
  220. ProviderTestUtils.setupMailbox("Con2", acct2.mId, true, context, Mailbox.TYPE_CONTACTS);
  221. // Check that we can find them by type
  222. assertEquals(acct1Inbox.mId,
  223. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_INBOX));
  224. assertEquals(acct2Inbox.mId,
  225. Mailbox.findMailboxOfType(context, acct2.mId, Mailbox.TYPE_INBOX));
  226. assertEquals(acct1Calendar.mId,
  227. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_CALENDAR));
  228. assertEquals(acct2Calendar.mId,
  229. Mailbox.findMailboxOfType(context, acct2.mId, Mailbox.TYPE_CALENDAR));
  230. assertEquals(acct1Contacts.mId,
  231. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_CONTACTS));
  232. assertEquals(acct2Contacts.mId,
  233. Mailbox.findMailboxOfType(context, acct2.mId, Mailbox.TYPE_CONTACTS));
  234. // Check that nonexistent mailboxes are not returned
  235. assertEquals(Mailbox.NO_MAILBOX,
  236. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_DRAFTS));
  237. assertEquals(Mailbox.NO_MAILBOX,
  238. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_OUTBOX));
  239. // delete account 1 and confirm no mailboxes are returned
  240. context.getContentResolver().delete(
  241. ContentUris.withAppendedId(Account.CONTENT_URI, acct1.mId), null, null);
  242. assertEquals(Mailbox.NO_MAILBOX,
  243. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_INBOX));
  244. assertEquals(Mailbox.NO_MAILBOX,
  245. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_CALENDAR));
  246. assertEquals(Mailbox.NO_MAILBOX,
  247. Mailbox.findMailboxOfType(context, acct1.mId, Mailbox.TYPE_CONTACTS));
  248. }
  249. public void testRestoreMailboxOfType() {
  250. final Context context = getMockContext();
  251. // Create two accounts and a variety of mailbox types
  252. Account acct1 = ProviderTestUtils.setupAccount("acct1", true, context);
  253. Mailbox acct1Inbox =
  254. ProviderTestUtils.setupMailbox("Inbox1", acct1.mId, true, context, Mailbox.TYPE_INBOX);
  255. Mailbox acct1Calendar =
  256. ProviderTestUtils.setupMailbox("Cal1", acct1.mId, true, context, Mailbox.TYPE_CALENDAR);
  257. Mailbox acct1Contacts =
  258. ProviderTestUtils.setupMailbox("Con1", acct1.mId, true, context, Mailbox.TYPE_CONTACTS);
  259. Account acct2 =ProviderTestUtils.setupAccount("acct1", true, context);
  260. Mailbox acct2Inbox =
  261. ProviderTestUtils.setupMailbox("Inbox2", acct2.mId, true, context, Mailbox.TYPE_INBOX);
  262. Mailbox acct2Calendar =
  263. ProviderTestUtils.setupMailbox("Cal2", acct2.mId, true, context, Mailbox.TYPE_CALENDAR);
  264. Mailbox acct2Contacts =
  265. ProviderTestUtils.setupMailbox("Con2", acct2.mId, true, context, Mailbox.TYPE_CONTACTS);
  266. // Check that we can find them by type
  267. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct1Inbox,
  268. Mailbox.restoreMailboxOfType(context, acct1.mId, Mailbox.TYPE_INBOX));
  269. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct2Inbox,
  270. Mailbox.restoreMailboxOfType(context, acct2.mId, Mailbox.TYPE_INBOX));
  271. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct1Calendar,
  272. Mailbox.restoreMailboxOfType(context, acct1.mId, Mailbox.TYPE_CALENDAR));
  273. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct2Calendar,
  274. Mailbox.restoreMailboxOfType(context, acct2.mId, Mailbox.TYPE_CALENDAR));
  275. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct1Contacts,
  276. Mailbox.restoreMailboxOfType(context, acct1.mId, Mailbox.TYPE_CONTACTS));
  277. ProviderTestUtils.assertMailboxEqual("testRestoreMailboxOfType", acct2Contacts,
  278. Mailbox.restoreMailboxOfType(context, acct2.mId, Mailbox.TYPE_CONTACTS));
  279. }
  280. /**
  281. * Test for the message count triggers (insert/delete/move mailbox), and also
  282. * {@link EmailProvider#recalculateMessageCount}.
  283. *
  284. * It also covers:
  285. * - {@link Mailbox#getMessageCountByMailboxType(Context, int)}
  286. * - {@link Mailbox#getUnreadCountByAccountAndMailboxType(Context, long, int)}
  287. * - {@link Mailbox#getUnreadCountByMailboxType(Context, int)}
  288. * - {@link Message#getFavoriteMessageCount(Context)}
  289. * - {@link Message#getFavoriteMessageCount(Context, long)}
  290. */
  291. public void testMessageCount() {
  292. final Context c = mMockContext;
  293. // Create 2 accounts
  294. Account a1 = ProviderTestUtils.setupAccount("holdflag-1", true, c);
  295. Account a2 = ProviderTestUtils.setupAccount("holdflag-2", true, c);
  296. // Create 2 mailboxes for each account
  297. Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_INBOX);
  298. Mailbox b2 = ProviderTestUtils.setupMailbox("box2", a1.mId, true, c, Mailbox.TYPE_OUTBOX);
  299. Mailbox b3 = ProviderTestUtils.setupMailbox("box3", a2.mId, true, c, Mailbox.TYPE_INBOX);
  300. Mailbox b4 = ProviderTestUtils.setupMailbox("box4", a2.mId, true, c, Mailbox.TYPE_OUTBOX);
  301. Mailbox bt = ProviderTestUtils.setupMailbox("boxT", a2.mId, true, c, Mailbox.TYPE_TRASH);
  302. // 0. Check the initial values, just in case.
  303. assertEquals(0, getMessageCount(b1.mId));
  304. assertEquals(0, getMessageCount(b2.mId));
  305. assertEquals(0, getMessageCount(b3.mId));
  306. assertEquals(0, getMessageCount(b4.mId));
  307. assertEquals(0, getMessageCount(bt.mId));
  308. assertEquals(0, Message.getFavoriteMessageCount(c));
  309. assertEquals(0, Message.getFavoriteMessageCount(c, a1.mId));
  310. assertEquals(0, Message.getFavoriteMessageCount(c, a2.mId));
  311. assertEquals(0, Mailbox.getUnreadCountByMailboxType(c, Mailbox.TYPE_INBOX));
  312. assertEquals(0, Mailbox.getUnreadCountByMailboxType(c, Mailbox.TYPE_OUTBOX));
  313. assertEquals(0, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_INBOX));
  314. assertEquals(0, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_OUTBOX));
  315. assertEquals(0, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_TRASH));
  316. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  317. a1.mId, Mailbox.TYPE_INBOX));
  318. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  319. a1.mId, Mailbox.TYPE_OUTBOX));
  320. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  321. a1.mId, Mailbox.TYPE_TRASH));
  322. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  323. a2.mId, Mailbox.TYPE_INBOX));
  324. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  325. a2.mId, Mailbox.TYPE_OUTBOX));
  326. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  327. a2.mId, Mailbox.TYPE_TRASH));
  328. // 1. Test for insert triggers.
  329. // Create some messages
  330. // b1 (account 1, inbox): 1 message, including 1 starred
  331. Message m11 = createMessage(c, b1, true, false, Message.FLAG_LOADED_COMPLETE);
  332. // b2 (account 1, outbox): 2 message, including 1 starred
  333. Message m21 = createMessage(c, b2, false, false, Message.FLAG_LOADED_COMPLETE);
  334. Message m22 = createMessage(c, b2, true, true, Message.FLAG_LOADED_COMPLETE);
  335. // b3 (account 2, inbox): 3 message, including 1 starred
  336. Message m31 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE);
  337. Message m32 = createMessage(c, b3, false, false, Message.FLAG_LOADED_COMPLETE);
  338. Message m33 = createMessage(c, b3, true, true, Message.FLAG_LOADED_COMPLETE);
  339. // b4 (account 2, outbox) has no messages.
  340. // bt (account 2, trash) has 3 messages, including 2 starred
  341. Message mt1 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE);
  342. Message mt2 = createMessage(c, bt, true, false, Message.FLAG_LOADED_COMPLETE);
  343. Message mt3 = createMessage(c, bt, false, false, Message.FLAG_LOADED_COMPLETE);
  344. // Check message counts
  345. assertEquals(1, getMessageCount(b1.mId));
  346. assertEquals(2, getMessageCount(b2.mId));
  347. assertEquals(3, getMessageCount(b3.mId));
  348. assertEquals(0, getMessageCount(b4.mId));
  349. assertEquals(3, getMessageCount(bt.mId));
  350. // Check the simple counting methods.
  351. assertEquals(3, Message.getFavoriteMessageCount(c)); // excludes starred in trash
  352. assertEquals(2, Message.getFavoriteMessageCount(c, a1.mId));
  353. assertEquals(1, Message.getFavoriteMessageCount(c, a2.mId)); // excludes starred in trash
  354. assertEquals(3, Mailbox.getUnreadCountByMailboxType(c, Mailbox.TYPE_INBOX));
  355. assertEquals(1, Mailbox.getUnreadCountByMailboxType(c, Mailbox.TYPE_OUTBOX));
  356. assertEquals(4, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_INBOX));
  357. assertEquals(2, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_OUTBOX));
  358. assertEquals(3, Mailbox.getMessageCountByMailboxType(c, Mailbox.TYPE_TRASH));
  359. assertEquals(1, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  360. a1.mId, Mailbox.TYPE_INBOX));
  361. assertEquals(1, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  362. a1.mId, Mailbox.TYPE_OUTBOX));
  363. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  364. a1.mId, Mailbox.TYPE_TRASH));
  365. assertEquals(2, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  366. a2.mId, Mailbox.TYPE_INBOX));
  367. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  368. a2.mId, Mailbox.TYPE_OUTBOX));
  369. assertEquals(3, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  370. a2.mId, Mailbox.TYPE_TRASH));
  371. // 2. Check the "move mailbox" trigger.
  372. // Move m32 (in mailbox 3) to mailbox 4.
  373. ContentValues values = new ContentValues();
  374. values.put(MessageColumns.MAILBOX_KEY, b4.mId);
  375. getProvider().update(Message.CONTENT_URI, values, EmailContent.ID_SELECTION,
  376. new String[] {"" + m32.mId});
  377. // Check message counts
  378. assertEquals(1, getMessageCount(b1.mId));
  379. assertEquals(2, getMessageCount(b2.mId));
  380. assertEquals(2, getMessageCount(b3.mId));
  381. assertEquals(1, getMessageCount(b4.mId));
  382. // 3. Check the delete trigger.
  383. // Delete m11 (in mailbox 1)
  384. getProvider().delete(Message.CONTENT_URI, EmailContent.ID_SELECTION,
  385. new String[] {"" + m11.mId});
  386. // Delete m21 (in mailbox 2)
  387. getProvider().delete(Message.CONTENT_URI, EmailContent.ID_SELECTION,
  388. new String[] {"" + m21.mId});
  389. // Check message counts
  390. assertEquals(0, getMessageCount(b1.mId));
  391. assertEquals(1, getMessageCount(b2.mId));
  392. assertEquals(2, getMessageCount(b3.mId));
  393. assertEquals(1, getMessageCount(b4.mId));
  394. // No such mailbox type.
  395. assertEquals(0, Mailbox.getMessageCountByMailboxType(c, 99999));
  396. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c, a1.mId, 99999));
  397. assertEquals(0, Mailbox.getUnreadCountByMailboxType(c, 99999));
  398. // No such account
  399. assertEquals(0, Mailbox.getUnreadCountByAccountAndMailboxType(c,
  400. 99999, Mailbox.TYPE_INBOX));
  401. }
  402. /**
  403. * Check if update on MAILBOX_ID_ADD_TO_FIELD updates the cache properly.
  404. */
  405. public void testUpdateCacheMailboxIdAddToField() {
  406. final Context c = mMockContext;
  407. Account a1 = ProviderTestUtils.setupAccount("a1", true, c);
  408. Mailbox b1 = ProviderTestUtils.setupMailbox("box1", a1.mId, true, c, Mailbox.TYPE_INBOX);
  409. int start = Mailbox.restoreMailboxWithId(c, b1.mId).mSyncInterval;
  410. // +1 to SYNC_INTERVAL
  411. ContentValues cv = new ContentValues();
  412. cv.put(EmailContent.FIELD_COLUMN_NAME, MailboxColumns.SYNC_INTERVAL);
  413. cv.put(EmailContent.ADD_COLUMN_NAME, 1);
  414. mProvider.update(ContentUris.withAppendedId(Mailbox.ADD_TO_FIELD_URI, a1.mId), cv,
  415. null, null);
  416. // Check
  417. assertEquals(start + 1, Mailbox.restoreMailboxWithId(c, b1.mId).mSyncInterval);
  418. }
  419. private Mailbox buildTestMailbox(String serverId) {
  420. return buildTestMailbox(serverId, null);
  421. }
  422. private Mailbox buildTestMailbox(String serverId, String name) {
  423. name = (name == null) ? TEST_DISPLAY_NAME : name;
  424. Mailbox testMailbox = new Mailbox();
  425. testMailbox.mServerId = serverId;
  426. testMailbox.mDisplayName = name;
  427. testMailbox.mParentServerId = TEST_PARENT_SERVER_ID;
  428. testMailbox.mSyncKey = TEST_SYNC_KEY;
  429. testMailbox.mSyncStatus = TEST_SYNC_STATUS;
  430. testMailbox.mAccountKey = 1L;
  431. testMailbox.mDelimiter = '/';
  432. testMailbox.mFlags = 2;
  433. testMailbox.mFlagVisible = true;
  434. testMailbox.mParentKey = 3L;
  435. testMailbox.mSyncInterval = 4;
  436. testMailbox.mSyncLookback = 5;
  437. testMailbox.mSyncTime = 6L;
  438. testMailbox.mType = 7;
  439. testMailbox.mVisibleLimit = 8;
  440. testMailbox.mLastSeenMessageKey = 9L;
  441. testMailbox.mLastTouchedTime = 10L;
  442. return testMailbox;
  443. }
  444. public void testGetHashes() {
  445. final Context c = mMockContext;
  446. Mailbox testMailbox = buildTestMailbox(TEST_SERVER_ID);
  447. testMailbox.save(c);
  448. Object[] testHash;
  449. testHash = new Object[] {
  450. testMailbox.mId, TEST_DISPLAY_NAME, TEST_SERVER_ID,
  451. TEST_PARENT_SERVER_ID, 1L /*mAccountKey*/, 7 /*mType */,
  452. (int)'/' /*mDelimiter */, TEST_SYNC_KEY, 5 /*mSyncLookback*/,
  453. 4 /*mSyncInterval*/, 6L /*mSyncTime*/, true /*mFlagVisible*/, 2 /*mFlags*/,
  454. 8 /*mVisibleLimit*/, TEST_SYNC_STATUS, 3L /*mParentKey*/, 9L /*mLastSeen*/,
  455. 10L /*mLastTouchedTime*/,
  456. };
  457. MoreAsserts.assertEquals(testHash, testMailbox.getHashes());
  458. // Verify null checks happen correctly
  459. testMailbox.mDisplayName = null;
  460. testMailbox.mParentServerId = null;
  461. testMailbox.mServerId = null;
  462. testMailbox.mSyncKey = null;
  463. testMailbox.mSyncStatus = null;
  464. testMailbox.mFlagVisible = false;
  465. testHash = new Object[] {
  466. testMailbox.mId, null /*mDisplayname*/, null /*mServerId*/,
  467. null /*mParentServerId*/, 1L /*mAccountKey*/, 7 /*mType */,
  468. (int)'/' /*mDelimiter */, null /*mSyncKey*/, 5 /*mSyncLookback*/,
  469. 4 /*mSyncInterval*/, 6L /*mSyncTime*/, false /*mFlagVisible*/, 2 /*mFlags*/,
  470. 8 /*mVisibleLimit*/, null /*mSyncStatus*/, 3L /*mParentKey*/, 9L /*mLastSeen*/,
  471. 10L /*mLastTouchedTime*/,
  472. };
  473. MoreAsserts.assertEquals(testHash, testMailbox.getHashes());
  474. }
  475. public void testParcelling() {
  476. Mailbox original = buildTestMailbox("serverId", "display name for mailbox");
  477. Parcel p = Parcel.obtain();
  478. original.writeToParcel(p, 0 /* flags */);
  479. // Reset.
  480. p.setDataPosition(0);
  481. Mailbox unparcelled = Mailbox.CREATOR.createFromParcel(p);
  482. MoreAsserts.assertEquals(original.getHashes(), unparcelled.getHashes());
  483. Mailbox phony = buildTestMailbox("different ID", "display name for mailbox");
  484. assertFalse(Arrays.equals(phony.getHashes(), unparcelled.getHashes()));
  485. p.recycle();
  486. }
  487. public void testParcellingWithPartialMailbox() {
  488. Mailbox unpopulated = new Mailbox();
  489. unpopulated.mDisplayName = "the only thing filled in for some reason";
  490. Parcel p = Parcel.obtain();
  491. unpopulated.writeToParcel(p, 0 /* flags */);
  492. // Reset.
  493. p.setDataPosition(0);
  494. Mailbox unparcelled = Mailbox.CREATOR.createFromParcel(p);
  495. MoreAsserts.assertEquals(unpopulated.getHashes(), unparcelled.getHashes());
  496. p.recycle();
  497. }
  498. }