Fix MailService unit test

Bug: 2844726
Change-Id: I168b3db49bf422b33d05f25cfff1c7be15150c2b
This commit is contained in:
Marc Blank 2010-10-07 11:36:25 -07:00
parent bb5fd95e75
commit fe6e3eae2a
2 changed files with 46 additions and 45 deletions

View File

@ -20,20 +20,15 @@ import com.android.email.AccountBackupRestore;
import com.android.email.Controller; import com.android.email.Controller;
import com.android.email.Email; import com.android.email.Email;
import com.android.email.NotificationController; import com.android.email.NotificationController;
import com.android.email.R;
import com.android.email.SecurityPolicy; import com.android.email.SecurityPolicy;
import com.android.email.Utility; import com.android.email.Utility;
import com.android.email.activity.ContactStatusLoader;
import com.android.email.activity.Welcome;
import com.android.email.mail.Address;
import com.android.email.mail.MessagingException; import com.android.email.mail.MessagingException;
import com.android.email.provider.EmailContent; import com.android.email.provider.EmailContent;
import com.android.email.provider.EmailProvider;
import com.android.email.provider.EmailContent.Account; import com.android.email.provider.EmailContent.Account;
import com.android.email.provider.EmailContent.AccountColumns; import com.android.email.provider.EmailContent.AccountColumns;
import com.android.email.provider.EmailContent.HostAuth; import com.android.email.provider.EmailContent.HostAuth;
import com.android.email.provider.EmailContent.Mailbox; import com.android.email.provider.EmailContent.Mailbox;
import com.android.email.provider.EmailContent.Message;
import com.android.email.provider.EmailProvider;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback; import android.accounts.AccountManagerCallback;
@ -42,8 +37,6 @@ import android.accounts.AuthenticatorException;
import android.accounts.OnAccountsUpdateListener; import android.accounts.OnAccountsUpdateListener;
import android.accounts.OperationCanceledException; import android.accounts.OperationCanceledException;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -52,16 +45,12 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SyncStatusObserver; import android.content.SyncStatusObserver;
import android.database.Cursor; import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.AudioManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.os.SystemClock; import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Config; import android.util.Config;
import android.util.Log; import android.util.Log;
@ -96,12 +85,18 @@ public class MailService extends Service {
private static final int WATCHDOG_DELAY = 10 * 60 * 1000; // 10 minutes private static final int WATCHDOG_DELAY = 10 * 60 * 1000; // 10 minutes
// Sentinel value asking to update mSyncReports if it's currently empty
/*package*/ static final int SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY = -1;
// Sentinel value asking that mSyncReports be rebuilt
/*package*/ static final int SYNC_REPORTS_RESET = -2;
private static final String[] NEW_MESSAGE_COUNT_PROJECTION = private static final String[] NEW_MESSAGE_COUNT_PROJECTION =
new String[] {AccountColumns.NEW_MESSAGE_COUNT}; new String[] {AccountColumns.NEW_MESSAGE_COUNT};
/*package*/ Controller mController; /*package*/ Controller mController;
private final Controller.Result mControllerCallback = new ControllerResults(); private final Controller.Result mControllerCallback = new ControllerResults();
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private Context mContext;
private AccountsUpdatedListener mAccountsUpdatedListener; private AccountsUpdatedListener mAccountsUpdatedListener;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
@ -204,6 +199,7 @@ public class MailService extends Service {
mController = Controller.getInstance(this); mController = Controller.getInstance(this);
mController.addResultCallback(mControllerCallback); mController.addResultCallback(mControllerCallback);
mContentResolver = getContentResolver(); mContentResolver = getContentResolver();
mContext = this;
if (ACTION_CHECK_MAIL.equals(action)) { if (ACTION_CHECK_MAIL.equals(action)) {
// If we have the data, restore the last-sync-times for each account // If we have the data, restore the last-sync-times for each account
@ -345,15 +341,14 @@ public class MailService extends Service {
/** /**
* Refresh the sync reports, to pick up any changes in the account list or account settings. * Refresh the sync reports, to pick up any changes in the account list or account settings.
*/ */
private void refreshSyncReports() { /*package*/ void refreshSyncReports() {
synchronized (mSyncReports) { synchronized (mSyncReports) {
// Make shallow copy of sync reports so we can recover the prev sync times // Make shallow copy of sync reports so we can recover the prev sync times
HashMap<Long,AccountSyncReport> oldSyncReports = HashMap<Long,AccountSyncReport> oldSyncReports =
new HashMap<Long,AccountSyncReport>(mSyncReports); new HashMap<Long,AccountSyncReport>(mSyncReports);
// Delete the sync reports to force a refresh from live account db data // Delete the sync reports to force a refresh from live account db data
mSyncReports.clear(); setupSyncReportsLocked(SYNC_REPORTS_RESET, this);
setupSyncReportsLocked(-1, mContentResolver);
// Restore prev-sync & next-sync times for any reports in the new list // Restore prev-sync & next-sync times for any reports in the new list
for (AccountSyncReport newReport : mSyncReports.values()) { for (AccountSyncReport newReport : mSyncReports.values()) {
@ -377,7 +372,7 @@ public class MailService extends Service {
*/ */
/* package */ void reschedule(AlarmManager alarmMgr) { /* package */ void reschedule(AlarmManager alarmMgr) {
// restore the reports if lost // restore the reports if lost
setupSyncReports(-1); setupSyncReports(SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY);
synchronized (mSyncReports) { synchronized (mSyncReports) {
int numAccounts = mSyncReports.size(); int numAccounts = mSyncReports.size();
long[] accountInfo = new long[numAccounts * 2]; // pairs of { accountId, lastSync } long[] accountInfo = new long[numAccounts * 2]; // pairs of { accountId, lastSync }
@ -531,15 +526,20 @@ public class MailService extends Service {
*/ */
/* package */ void setupSyncReports(long accountId) { /* package */ void setupSyncReports(long accountId) {
synchronized (mSyncReports) { synchronized (mSyncReports) {
setupSyncReportsLocked(accountId, mContentResolver); setupSyncReportsLocked(accountId, mContext);
} }
} }
/** /**
* Handle the work of setupSyncReports. Must be synchronized on mSyncReports. * Handle the work of setupSyncReports. Must be synchronized on mSyncReports.
*/ */
/*package*/ void setupSyncReportsLocked(long accountId, ContentResolver resolver) { /*package*/ void setupSyncReportsLocked(long accountId, Context context) {
if (accountId == -1) { ContentResolver resolver = context.getContentResolver();
if (accountId == SYNC_REPORTS_RESET) {
// For test purposes, force refresh of mSyncReports
mSyncReports.clear();
accountId = SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY;
} else if (accountId == SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY) {
// -1 == reload the list if empty, otherwise exit immediately // -1 == reload the list if empty, otherwise exit immediately
if (mSyncReports.size() > 0) { if (mSyncReports.size() > 0) {
return; return;
@ -553,7 +553,7 @@ public class MailService extends Service {
// setup to add a single account or all accounts // setup to add a single account or all accounts
Uri uri; Uri uri;
if (accountId == -1) { if (accountId == SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY) {
uri = Account.CONTENT_URI; uri = Account.CONTENT_URI;
} else { } else {
uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId); uri = ContentUris.withAppendedId(Account.CONTENT_URI, accountId);
@ -584,7 +584,7 @@ public class MailService extends Service {
report.notify = (flags & Account.FLAGS_NOTIFY_NEW_MAIL) != 0; report.notify = (flags & Account.FLAGS_NOTIFY_NEW_MAIL) != 0;
// See if the account is enabled for sync in AccountManager // See if the account is enabled for sync in AccountManager
Account providerAccount = Account.restoreAccountWithId(this, id); Account providerAccount = Account.restoreAccountWithId(context, id);
android.accounts.Account accountManagerAccount = android.accounts.Account accountManagerAccount =
new android.accounts.Account(providerAccount.mEmailAddress, new android.accounts.Account(providerAccount.mEmailAddress,
Email.POP_IMAP_ACCOUNT_MANAGER_TYPE); Email.POP_IMAP_ACCOUNT_MANAGER_TYPE);
@ -641,7 +641,7 @@ public class MailService extends Service {
*/ */
/* package */ void restoreSyncReports(Intent restoreIntent) { /* package */ void restoreSyncReports(Intent restoreIntent) {
// restore the reports if lost // restore the reports if lost
setupSyncReports(-1); setupSyncReports(SYNC_REPORTS_ALL_ACCOUNTS_IF_EMPTY);
synchronized (mSyncReports) { synchronized (mSyncReports) {
long[] accountInfo = restoreIntent.getLongArrayExtra(EXTRA_ACCOUNT_INFO); long[] accountInfo = restoreIntent.getLongArrayExtra(EXTRA_ACCOUNT_INFO);
if (accountInfo == null) { if (accountInfo == null) {

View File

@ -200,32 +200,33 @@ public class MailServiceTests extends AccountTestCase {
MailService mailService = new MailService(); MailService mailService = new MailService();
mailService.mController = new TestController(mMockContext, getContext()); mailService.mController = new TestController(mMockContext, getContext());
try { try {
mailService.setupSyncReportsLocked(-1, mMockContext.getContentResolver()); mailService.setupSyncReportsLocked(MailService.SYNC_REPORTS_RESET, mMockContext);
// Get back the map created by MailService // Get back the map created by MailService
HashMap<Long, AccountSyncReport> syncReportMap = MailService.mSyncReports; HashMap<Long, AccountSyncReport> syncReportMap = MailService.mSyncReports;
// Check the SyncReport's for correctness of sync interval synchronized (syncReportMap) {
AccountSyncReport syncReport = syncReportMap.get(easAccount.mId); // Check the SyncReport's for correctness of sync interval
assertNotNull(syncReport); AccountSyncReport syncReport = syncReportMap.get(easAccount.mId);
// EAS sync interval should have been changed to "never" assertNotNull(syncReport);
assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval); // EAS sync interval should have been changed to "never"
syncReport = syncReportMap.get(imapAccount.mId); assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval);
assertNotNull(syncReport); syncReport = syncReportMap.get(imapAccount.mId);
assertEquals(60, syncReport.syncInterval); assertNotNull(syncReport);
syncReport = syncReportMap.get(pop3Account.mId); assertEquals(60, syncReport.syncInterval);
assertNotNull(syncReport); syncReport = syncReportMap.get(pop3Account.mId);
assertEquals(90, syncReport.syncInterval); assertNotNull(syncReport);
assertEquals(90, syncReport.syncInterval);
// Change the EAS account to push // Change the EAS account to push
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.put(Account.SYNC_INTERVAL, Account.CHECK_INTERVAL_PUSH); cv.put(Account.SYNC_INTERVAL, Account.CHECK_INTERVAL_PUSH);
easAccount.update(mMockContext, cv); easAccount.update(mMockContext, cv);
syncReportMap.clear(); syncReportMap.clear();
mailService.setupSyncReportsLocked(easAccount.mId, mMockContext.getContentResolver()); mailService.setupSyncReportsLocked(easAccount.mId, mMockContext);
syncReport = syncReportMap.get(easAccount.mId); syncReport = syncReportMap.get(easAccount.mId);
assertNotNull(syncReport); assertNotNull(syncReport);
// EAS sync interval should be "never" in this case as well // EAS sync interval should be "never" in this case as well
assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval); assertEquals(Account.CHECK_INTERVAL_NEVER, syncReport.syncInterval);
}
} finally { } finally {
mailService.mController.cleanupForTest(); mailService.mController.cleanupForTest();
} }