2011-06-03 23:54:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.email.activity;
|
|
|
|
|
2011-07-11 19:27:11 +00:00
|
|
|
import android.content.ContentValues;
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.test.AndroidTestCase;
|
2011-07-14 17:21:41 +00:00
|
|
|
import android.test.suitebuilder.annotation.MediumTest;
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
|
2011-07-14 17:21:41 +00:00
|
|
|
import com.android.email.Controller;
|
2011-06-21 17:04:49 +00:00
|
|
|
import com.android.email.DBTestHelper;
|
2011-06-03 23:54:52 +00:00
|
|
|
import com.android.email.MockClock;
|
|
|
|
import com.android.email.provider.ContentCache;
|
|
|
|
import com.android.email.provider.ProviderTestUtils;
|
2011-07-11 19:27:11 +00:00
|
|
|
import com.android.emailcommon.provider.EmailContent.MailboxColumns;
|
2011-06-03 23:54:52 +00:00
|
|
|
import com.android.emailcommon.provider.Mailbox;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2011-07-14 17:21:41 +00:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the recent mailbox manager.
|
|
|
|
*
|
|
|
|
* You can run this entire test case with:
|
|
|
|
* runtest -c com.android.email.activity.RecentMailboxManagerTest email
|
|
|
|
*/
|
2011-07-14 17:21:41 +00:00
|
|
|
@MediumTest
|
2011-06-21 17:04:49 +00:00
|
|
|
public class RecentMailboxManagerTest extends AndroidTestCase {
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
private Context mMockContext;
|
|
|
|
private MockClock mMockClock;
|
|
|
|
private RecentMailboxManager mManager;
|
|
|
|
private Mailbox[] mMailboxArray;
|
2011-06-21 17:04:49 +00:00
|
|
|
|
2011-06-03 23:54:52 +00:00
|
|
|
public RecentMailboxManagerTest() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2011-06-21 17:04:49 +00:00
|
|
|
mMockContext = DBTestHelper.ProviderContextSetupHelper.getProviderContext(
|
|
|
|
getContext());
|
2011-06-03 23:54:52 +00:00
|
|
|
mMockClock = new MockClock();
|
|
|
|
RecentMailboxManager.sClock = mMockClock;
|
|
|
|
mManager = RecentMailboxManager.getInstance(mMockContext);
|
2011-07-14 17:21:41 +00:00
|
|
|
Controller.getInstance(mMockContext).setProviderContext(mMockContext);
|
2011-06-03 23:54:52 +00:00
|
|
|
mMailboxArray = new Mailbox[] {
|
|
|
|
ProviderTestUtils.setupMailbox("inbox", 1L, true, mMockContext, Mailbox.TYPE_INBOX),
|
|
|
|
ProviderTestUtils.setupMailbox("drafts", 1L, true, mMockContext, Mailbox.TYPE_DRAFTS),
|
|
|
|
ProviderTestUtils.setupMailbox("outbox", 1L, true, mMockContext, Mailbox.TYPE_OUTBOX),
|
|
|
|
ProviderTestUtils.setupMailbox("sent", 1L, true, mMockContext, Mailbox.TYPE_SENT),
|
|
|
|
ProviderTestUtils.setupMailbox("trash", 1L, true, mMockContext, Mailbox.TYPE_TRASH),
|
|
|
|
ProviderTestUtils.setupMailbox("junk", 1L, true, mMockContext, Mailbox.TYPE_JUNK),
|
|
|
|
ProviderTestUtils.setupMailbox("abbott", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
|
|
|
|
ProviderTestUtils.setupMailbox("costello", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
|
|
|
|
ProviderTestUtils.setupMailbox("bud_lou", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
|
2011-06-06 21:58:39 +00:00
|
|
|
ProviderTestUtils.setupMailbox("laurel", 1L, true, mMockContext, Mailbox.TYPE_MAIL),
|
2011-07-11 19:27:11 +00:00
|
|
|
ProviderTestUtils.setupMailbox("hardy", 1L, true, mMockContext, Mailbox.TYPE_MAIL)
|
2011-06-03 23:54:52 +00:00
|
|
|
};
|
Improve EmailContent caching...
* Guarantee that up to 16 Account (with HostAuths), and Policy rows
are always cached. Also, 6 commonly used Mailboxes per Account
(Inbox, Outbox, Drafts, Sent, Trash, and Search)
* Precache these rows when EmailProvider starts up
* Ensure that newly added, precachable rows are cached when created
* Clean up some inefficient/wrong caching code
* Fix a commonly called method in NotificationManager in which we
load a single Mailbox row using selection vs withAppendedId
* Confirm that we don't read from the database in typical use and
heavy message loading
* Add a special URI for finding mailbox by type (using the cache)
* Add special-case code for EmailContent.count(Account.CONTENT_URI)
which is used in a number of places (including on the UI thread)
and whose value is easily determined
* Add a special URI to get the default account id
* Confirm that all unit tests work
The goal here is to be able to load all Account, HostAuth, Policy,
and Mailbox objects (by id) without worrying about disk access.
There will still be a single disk read for uncommon Mailbox reads,
but this should be considered acceptable.
Change-Id: Ibc9aa7acc73185e360b0b6f3053b90a985e97210
TODO: Unit tests
2011-06-19 01:03:11 +00:00
|
|
|
// Invalidate all caches, since we reset the database for each test
|
|
|
|
ContentCache.invalidateAllCaches();
|
2011-06-03 23:54:52 +00:00
|
|
|
}
|
|
|
|
|
2011-06-06 21:58:39 +00:00
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
|
|
|
RecentMailboxManager.sInstance = null;
|
|
|
|
super.tearDown();
|
|
|
|
}
|
|
|
|
|
2011-06-03 23:54:52 +00:00
|
|
|
public void testTouch() throws Exception {
|
2011-07-14 17:21:41 +00:00
|
|
|
Set<Integer> defaultRecents = new HashSet<Integer>() {{
|
|
|
|
for (int type : RecentMailboxManager.DEFAULT_RECENT_TYPES) {
|
|
|
|
add(type);
|
|
|
|
}
|
|
|
|
}};
|
|
|
|
|
2011-06-03 23:54:52 +00:00
|
|
|
// Ensure all accounts can be touched
|
|
|
|
for (Mailbox mailbox : mMailboxArray) {
|
|
|
|
// Safety ... default touch time
|
|
|
|
Mailbox untouchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
|
2011-07-14 17:21:41 +00:00
|
|
|
if (!defaultRecents.contains(mailbox.mType)) {
|
|
|
|
assertEquals(0L, untouchedMailbox.mLastTouchedTime);
|
|
|
|
}
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
// Touch the mailbox
|
2011-07-14 17:21:41 +00:00
|
|
|
mManager.touch(1L, mailbox.mId).get();
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
// Touch time is actually set
|
|
|
|
Mailbox touchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
|
|
|
|
assertEquals(mMockClock.getTime(), touchedMailbox.mLastTouchedTime);
|
|
|
|
|
|
|
|
mMockClock.advance(1000L);
|
|
|
|
}
|
|
|
|
// Now ensure touching one didn't affect the others
|
|
|
|
long touchTime = MockClock.DEFAULT_TIME;
|
|
|
|
for (Mailbox mailbox : mMailboxArray) {
|
|
|
|
// Touch time is actually set
|
|
|
|
Mailbox touchedMailbox = Mailbox.restoreMailboxWithId(mMockContext, mailbox.mId);
|
|
|
|
assertEquals(touchTime, touchedMailbox.mLastTouchedTime);
|
|
|
|
touchTime += 1000L;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-06 21:58:39 +00:00
|
|
|
/** Test default list */
|
|
|
|
public void testGetMostRecent01() throws Exception {
|
2011-06-03 23:54:52 +00:00
|
|
|
ArrayList<Long> testList;
|
|
|
|
|
|
|
|
// test default list
|
2011-06-21 17:04:49 +00:00
|
|
|
// With exclusions
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, true);
|
2011-06-21 17:04:49 +00:00
|
|
|
assertEquals("w/ exclusions", 0, testList.size());
|
|
|
|
|
|
|
|
// Without exclusions -- we'll get "default" list.
|
|
|
|
testList = mManager.getMostRecent(1L, false);
|
|
|
|
assertEquals("w/o exclusions", 2, testList.size());
|
|
|
|
|
|
|
|
assertEquals(mMailboxArray[1].mId, (long) testList.get(0)); // Drafts
|
|
|
|
assertEquals(mMailboxArray[3].mId, (long) testList.get(1)); // Sent
|
2011-06-06 21:58:39 +00:00
|
|
|
}
|
2011-06-03 23:54:52 +00:00
|
|
|
|
2011-06-06 21:58:39 +00:00
|
|
|
/** Test recent list not full */
|
|
|
|
public void testGetMostRecent02() throws Exception {
|
|
|
|
ArrayList<Long> testList;
|
2011-06-03 23:54:52 +00:00
|
|
|
// need to wait for the last one to ensure getMostRecent() has something to work on
|
2011-07-17 23:08:58 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId).get(); // costello
|
2011-06-03 23:54:52 +00:00
|
|
|
|
2011-07-14 17:21:41 +00:00
|
|
|
// test recent list not full, so is padded with default mailboxes
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, false);
|
2011-07-17 23:08:58 +00:00
|
|
|
assertEquals(3, testList.size());
|
|
|
|
assertEquals(mMailboxArray[7].mId, (long) testList.get(0)); // costello
|
|
|
|
assertEquals(mMailboxArray[1].mId, (long) testList.get(1)); // Drafts
|
|
|
|
assertEquals(mMailboxArray[3].mId, (long) testList.get(2)); // Sent
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, true);
|
|
|
|
assertEquals(1, testList.size());
|
|
|
|
assertEquals(mMailboxArray[7].mId, (long) testList.get(0));
|
2011-06-06 21:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Test full recent list */
|
|
|
|
public void testGetMostRecent03() throws Exception {
|
|
|
|
ArrayList<Long> testList;
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
// touch some more mailboxes
|
2011-07-14 17:21:41 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId); // trash
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[8].mId); // bud_lou
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId); // costello
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[9].mId).get(); // laurel
|
2011-06-03 23:54:52 +00:00
|
|
|
|
|
|
|
// test full recent list
|
|
|
|
testList = mManager.getMostRecent(1L, false);
|
|
|
|
assertEquals(5, testList.size());
|
2011-07-14 17:21:41 +00:00
|
|
|
assertEquals(mMailboxArray[8].mId, (long) testList.get(0)); // bud_lou
|
|
|
|
assertEquals(mMailboxArray[7].mId, (long) testList.get(1)); // costello
|
|
|
|
assertEquals(mMailboxArray[9].mId, (long) testList.get(2)); // laurel
|
|
|
|
assertEquals(mMailboxArray[2].mId, (long) testList.get(3)); // outbox
|
|
|
|
assertEquals(mMailboxArray[4].mId, (long) testList.get(4)); // trash
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, true);
|
2011-07-14 17:21:41 +00:00
|
|
|
assertEquals(3, testList.size());
|
2011-06-03 23:54:52 +00:00
|
|
|
assertEquals(mMailboxArray[8].mId, (long) testList.get(0));
|
|
|
|
assertEquals(mMailboxArray[7].mId, (long) testList.get(1));
|
2011-07-14 17:21:41 +00:00
|
|
|
assertEquals(mMailboxArray[9].mId, (long) testList.get(2));
|
2011-06-06 21:58:39 +00:00
|
|
|
}
|
2011-06-03 23:54:52 +00:00
|
|
|
|
2011-06-06 21:58:39 +00:00
|
|
|
/** Test limit for system mailboxes */
|
|
|
|
public void testGetMostRecent04() throws Exception {
|
|
|
|
ArrayList<Long> testList;
|
|
|
|
|
2011-07-14 17:21:41 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[0].mId); // inbox
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[1].mId); // drafts
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId).get(); // trash
|
2011-06-03 23:54:52 +00:00
|
|
|
|
2011-07-17 23:08:58 +00:00
|
|
|
// nothing but system mailboxes, but inbox is never included
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, false);
|
2011-07-17 23:08:58 +00:00
|
|
|
assertEquals(4, testList.size());
|
2011-06-03 23:54:52 +00:00
|
|
|
assertEquals(mMailboxArray[1].mId, (long) testList.get(0));
|
2011-07-17 23:08:58 +00:00
|
|
|
assertEquals(mMailboxArray[2].mId, (long) testList.get(1));
|
|
|
|
assertEquals(mMailboxArray[3].mId, (long) testList.get(2));
|
|
|
|
assertEquals(mMailboxArray[4].mId, (long) testList.get(3));
|
2011-06-03 23:54:52 +00:00
|
|
|
testList = mManager.getMostRecent(1L, true);
|
|
|
|
assertEquals(0, testList.size());
|
|
|
|
}
|
2011-06-06 21:58:39 +00:00
|
|
|
|
|
|
|
/** Test limit for user mailboxes */
|
|
|
|
public void testGetMostRecent05() throws Exception {
|
|
|
|
ArrayList<Long> testList;
|
|
|
|
|
|
|
|
// test limit for the filtered list
|
2011-07-14 17:21:41 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[6].mId); // abbott
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[7].mId); // costello
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[8].mId); // bud_lou
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[9].mId); // laurel
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[10].mId); // hardy
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[1].mId); // drafts
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[2].mId); // outbox
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[3].mId); // sent
|
2011-07-17 23:08:58 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[4].mId); // trash
|
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, mMailboxArray[5].mId).get(); // junk
|
2011-06-06 21:58:39 +00:00
|
|
|
|
|
|
|
// nothing but user mailboxes
|
|
|
|
testList = mManager.getMostRecent(1L, false);
|
|
|
|
assertEquals(5, testList.size());
|
|
|
|
assertEquals(mMailboxArray[1].mId, (long) testList.get(0));
|
2011-07-17 23:08:58 +00:00
|
|
|
assertEquals(mMailboxArray[5].mId, (long) testList.get(1));
|
2011-06-06 21:58:39 +00:00
|
|
|
assertEquals(mMailboxArray[2].mId, (long) testList.get(2));
|
|
|
|
assertEquals(mMailboxArray[3].mId, (long) testList.get(3));
|
|
|
|
assertEquals(mMailboxArray[4].mId, (long) testList.get(4));
|
|
|
|
testList = mManager.getMostRecent(1L, true);
|
|
|
|
assertEquals(5, testList.size());
|
|
|
|
assertEquals(mMailboxArray[6].mId, (long) testList.get(0));
|
|
|
|
assertEquals(mMailboxArray[8].mId, (long) testList.get(1));
|
|
|
|
assertEquals(mMailboxArray[7].mId, (long) testList.get(2));
|
|
|
|
assertEquals(mMailboxArray[10].mId, (long) testList.get(3));
|
|
|
|
assertEquals(mMailboxArray[9].mId, (long) testList.get(4));
|
|
|
|
}
|
2011-07-11 19:27:11 +00:00
|
|
|
|
|
|
|
public void testDoesNotIncludeExtraMailboxes() throws Exception {
|
|
|
|
ArrayList<Long> testList;
|
|
|
|
|
|
|
|
// The search mailbox should not be visible.
|
|
|
|
Mailbox searchMailbox = ProviderTestUtils.setupMailbox(
|
|
|
|
"search", 1L, true, mMockContext, Mailbox.TYPE_SEARCH);
|
|
|
|
ContentValues cv = new ContentValues();
|
|
|
|
cv.put(MailboxColumns.FLAG_VISIBLE, false);
|
|
|
|
searchMailbox.mFlagVisible = false;
|
|
|
|
searchMailbox.update(mMockContext, cv);
|
|
|
|
|
2011-07-14 17:21:41 +00:00
|
|
|
mMockClock.advance(1000L); mManager.touch(1L, searchMailbox.mId).get();
|
2011-07-11 19:27:11 +00:00
|
|
|
|
|
|
|
// Ensure search mailbox isn't returned
|
|
|
|
testList = mManager.getMostRecent(1L, false);
|
|
|
|
assertFalse(testList.contains(searchMailbox.mId));
|
|
|
|
testList = mManager.getMostRecent(1L, true);
|
|
|
|
assertFalse(testList.contains(searchMailbox.mId));
|
|
|
|
}
|
2011-06-03 23:54:52 +00:00
|
|
|
}
|