Don't pass Folder instance to Backend

This commit is contained in:
cketti 2019-12-18 01:13:57 +01:00
parent aa13a46e3e
commit 84327e085d
10 changed files with 65 additions and 104 deletions

View file

@ -667,7 +667,7 @@ public class MessagingController {
if (localFolder.getVisibleLimit() > 0) { if (localFolder.getVisibleLimit() > 0) {
localFolder.setVisibleLimit(localFolder.getVisibleLimit() + account.getDisplayCount()); localFolder.setVisibleLimit(localFolder.getVisibleLimit() + account.getDisplayCount());
} }
synchronizeMailbox(account, folder, listener, null); synchronizeMailbox(account, folder, listener);
} catch (MessagingException me) { } catch (MessagingException me) {
throw new RuntimeException("Unable to set visible limit on folder", me); throw new RuntimeException("Unable to set visible limit on folder", me);
} }
@ -676,12 +676,11 @@ public class MessagingController {
/** /**
* Start background synchronization of the specified folder. * Start background synchronization of the specified folder.
*/ */
public void synchronizeMailbox(final Account account, final String folder, final MessagingListener listener, public void synchronizeMailbox(final Account account, final String folder, final MessagingListener listener) {
final Folder providedRemoteFolder) {
putBackground("synchronizeMailbox", listener, new Runnable() { putBackground("synchronizeMailbox", listener, new Runnable() {
@Override @Override
public void run() { public void run() {
synchronizeMailboxSynchronous(account, folder, listener, providedRemoteFolder); synchronizeMailboxSynchronous(account, folder, listener);
} }
}); });
} }
@ -693,15 +692,12 @@ public class MessagingController {
* TODO Break this method up into smaller chunks. * TODO Break this method up into smaller chunks.
*/ */
@VisibleForTesting @VisibleForTesting
void synchronizeMailboxSynchronous(final Account account, final String folder, final MessagingListener listener, void synchronizeMailboxSynchronous(final Account account, final String folder, final MessagingListener listener) {
Folder providedRemoteFolder) {
Backend remoteMessageStore = getBackend(account); Backend remoteMessageStore = getBackend(account);
syncFolder(account, folder, listener, providedRemoteFolder, remoteMessageStore); syncFolder(account, folder, listener, remoteMessageStore);
} }
private void syncFolder(Account account, String folder, MessagingListener listener, Folder providedRemoteFolder, private void syncFolder(Account account, String folder, MessagingListener listener, Backend remoteMessageStore) {
Backend remoteMessageStore) {
Exception commandException = null; Exception commandException = null;
try { try {
processPendingCommandsSynchronous(account); processPendingCommandsSynchronous(account);
@ -718,7 +714,7 @@ public class MessagingController {
SyncConfig syncConfig = createSyncConfig(account); SyncConfig syncConfig = createSyncConfig(account);
ControllerSyncListener syncListener = new ControllerSyncListener(account, listener); ControllerSyncListener syncListener = new ControllerSyncListener(account, listener);
remoteMessageStore.sync(folder, syncConfig, syncListener, providedRemoteFolder); remoteMessageStore.sync(folder, syncConfig, syncListener);
if (commandException != null && !syncListener.syncFailed) { if (commandException != null && !syncListener.syncFailed) {
String rootMessage = getRootCauseMessage(commandException); String rootMessage = getRootCauseMessage(commandException);
@ -2582,7 +2578,7 @@ public class MessagingController {
} }
showFetchingMailNotificationIfNecessary(account, folder); showFetchingMailNotificationIfNecessary(account, folder);
try { try {
synchronizeMailboxSynchronous(account, folder.getServerId(), listener, null); synchronizeMailboxSynchronous(account, folder.getServerId(), listener);
} finally { } finally {
clearFetchingMailNotificationIfNecessary(account); clearFetchingMailNotificationIfNecessary(account);
} }

View file

@ -58,7 +58,7 @@ public class MessagingControllerPushReceiver implements PushReceiver {
String message) { String message) {
latch.countDown(); latch.countDown();
} }
}, null); });
Timber.v("syncFolder(%s) about to await latch release", folderServerId); Timber.v("syncFolder(%s) about to await latch release", folderServerId);

View file

@ -505,7 +505,7 @@ public class AccountSetupCheckSettings extends K9Activity implements OnClickList
} }
MessagingController.getInstance(getApplication()).listFoldersSynchronous(account, true, null); MessagingController.getInstance(getApplication()).listFoldersSynchronous(account, true, null);
MessagingController.getInstance(getApplication()) MessagingController.getInstance(getApplication())
.synchronizeMailbox(account, account.getInboxFolder(), null, null); .synchronizeMailbox(account, account.getInboxFolder(), null);
} }
private boolean isWebDavAccount() { private boolean isWebDavAccount() {

View file

@ -2132,7 +2132,7 @@ public class MessageListFragment extends Fragment implements OnItemClickListener
public void checkMail() { public void checkMail() {
if (isSingleAccountMode() && isSingleFolderMode()) { if (isSingleAccountMode() && isSingleFolderMode()) {
messagingController.synchronizeMailbox(account, folderServerId, activityListener, null); messagingController.synchronizeMailbox(account, folderServerId, activityListener);
messagingController.sendPendingMessages(account, activityListener); messagingController.sendPendingMessages(account, activityListener);
} else if (allAccounts) { } else if (allAccounts) {
messagingController.checkMail(context, null, true, true, activityListener); messagingController.checkMail(context, null, true, true, activityListener);

View file

@ -3,7 +3,6 @@ package com.fsck.k9.backend.api
import com.fsck.k9.mail.BodyFactory import com.fsck.k9.mail.BodyFactory
import com.fsck.k9.mail.FetchProfile import com.fsck.k9.mail.FetchProfile
import com.fsck.k9.mail.Flag import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.Folder
import com.fsck.k9.mail.Message import com.fsck.k9.mail.Message
import com.fsck.k9.mail.MessagingException import com.fsck.k9.mail.MessagingException
import com.fsck.k9.mail.Part import com.fsck.k9.mail.Part
@ -25,7 +24,7 @@ interface Backend {
fun refreshFolderList() fun refreshFolderList()
// TODO: Add a way to cancel the sync process // TODO: Add a way to cancel the sync process
fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener)
@Throws(MessagingException::class) @Throws(MessagingException::class)
fun downloadMessage(syncConfig: SyncConfig, folderServerId: String, messageServerId: String) fun downloadMessage(syncConfig: SyncConfig, folderServerId: String, messageServerId: String)

View file

@ -12,7 +12,6 @@ import com.fsck.k9.backend.api.SyncListener;
import com.fsck.k9.mail.BodyFactory; import com.fsck.k9.mail.BodyFactory;
import com.fsck.k9.mail.FetchProfile; import com.fsck.k9.mail.FetchProfile;
import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Folder;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Part; import com.fsck.k9.mail.Part;
@ -113,9 +112,8 @@ public class ImapBackend implements Backend {
} }
@Override @Override
public void sync(@NotNull String folder, @NotNull SyncConfig syncConfig, @NotNull SyncListener listener, public void sync(@NotNull String folder, @NotNull SyncConfig syncConfig, @NotNull SyncListener listener) {
Folder providedRemoteFolder) { imapSync.sync(folder, syncConfig, listener);
imapSync.sync(folder, syncConfig, listener, providedRemoteFolder);
} }
@Override @Override

View file

@ -47,16 +47,14 @@ class ImapSync {
this.imapStore = imapStore; this.imapStore = imapStore;
} }
void sync(String folder, SyncConfig syncConfig, SyncListener listener, Folder providedRemoteFolder) { void sync(String folder, SyncConfig syncConfig, SyncListener listener) {
synchronizeMailboxSynchronous(folder, syncConfig, listener, providedRemoteFolder); synchronizeMailboxSynchronous(folder, syncConfig, listener);
} }
void synchronizeMailboxSynchronous(final String folder, SyncConfig syncConfig, final SyncListener listener, void synchronizeMailboxSynchronous(final String folder, SyncConfig syncConfig, final SyncListener listener) {
Folder providedRemoteFolder) {
Folder remoteFolder = null;
Timber.i("Synchronizing folder %s:%s", accountName, folder); Timber.i("Synchronizing folder %s:%s", accountName, folder);
ImapFolder remoteFolder = null;
BackendFolder backendFolder = null; BackendFolder backendFolder = null;
try { try {
Timber.v("SYNC: About to get local folder %s", folder); Timber.v("SYNC: About to get local folder %s", folder);
@ -74,10 +72,6 @@ class ImapSync {
Map<String, Long> localUidMap = backendFolder.getAllMessagesAndEffectiveDates(); Map<String, Long> localUidMap = backendFolder.getAllMessagesAndEffectiveDates();
if (providedRemoteFolder != null) {
Timber.v("SYNC: using providedRemoteFolder %s", folder);
remoteFolder = providedRemoteFolder;
} else {
Timber.v("SYNC: About to get remote folder %s", folder); Timber.v("SYNC: About to get remote folder %s", folder);
remoteFolder = imapStore.getFolder(folder); remoteFolder = imapStore.getFolder(folder);
@ -110,8 +104,6 @@ class ImapSync {
} }
remoteFolder.open(Folder.OPEN_MODE_RO); remoteFolder.open(Folder.OPEN_MODE_RO);
}
listener.syncAuthenticationSuccess(); listener.syncAuthenticationSuccess();
/* /*
@ -249,10 +241,8 @@ class ImapSync {
System.currentTimeMillis()); System.currentTimeMillis());
} finally { } finally {
if (providedRemoteFolder == null) {
closeFolder(remoteFolder); closeFolder(remoteFolder);
} }
}
} }

View file

@ -81,13 +81,14 @@ public class ImapSyncTest {
configureSyncConfig(); configureSyncConfig();
configureBackendStorage(); configureBackendStorage();
configureRemoteStoreWithFolder();
} }
@Test @Test
public void sync_withOneMessageInRemoteFolder_shouldFinishWithoutError() { public void sync_withOneMessageInRemoteFolder_shouldFinishWithoutError() {
messageCountInRemoteFolder(1); messageCountInRemoteFolder(1);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(listener).syncFinished(FOLDER_NAME, 1, 0); verify(listener).syncFinished(FOLDER_NAME, 1, 0);
} }
@ -96,7 +97,7 @@ public class ImapSyncTest {
public void sync_withEmptyRemoteFolder_shouldFinishWithoutError() { public void sync_withEmptyRemoteFolder_shouldFinishWithoutError() {
messageCountInRemoteFolder(0); messageCountInRemoteFolder(0);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(listener).syncFinished(FOLDER_NAME, 0, 0); verify(listener).syncFinished(FOLDER_NAME, 0, 0);
} }
@ -105,46 +106,26 @@ public class ImapSyncTest {
public void sync_withNegativeMessageCountInRemoteFolder_shouldFinishWithError() { public void sync_withNegativeMessageCountInRemoteFolder_shouldFinishWithError() {
messageCountInRemoteFolder(-1); messageCountInRemoteFolder(-1);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(listener).syncFailed(eq(FOLDER_NAME), eq("Exception: Message count -1 for folder Folder"), verify(listener).syncFailed(eq(FOLDER_NAME), eq("Exception: Message count -1 for folder Folder"),
any(Exception.class)); any(Exception.class));
} }
@Test @Test
public void sync_withRemoteFolderProvided_shouldNotOpenRemoteFolder() throws Exception { public void sync_shouldOpenRemoteFolder() throws Exception {
messageCountInRemoteFolder(1); messageCountInRemoteFolder(1);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder, never()).open(Folder.OPEN_MODE_RW);
}
@Test
public void sync_withNoRemoteFolderProvided_shouldOpenRemoteFolderFromStore() throws Exception {
messageCountInRemoteFolder(1);
configureRemoteStoreWithFolder();
imapSync.sync(FOLDER_NAME, syncConfig, listener, null);
verify(remoteFolder).open(Folder.OPEN_MODE_RO); verify(remoteFolder).open(Folder.OPEN_MODE_RO);
} }
@Test @Test
public void sync_withRemoteFolderProvided_shouldNotCloseRemoteFolder() { public void sync_shouldCloseRemoteFolder() {
messageCountInRemoteFolder(1); messageCountInRemoteFolder(1);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder, never()).close();
}
@Test
public void sync_withNoRemoteFolderProvided_shouldCloseRemoteFolderFromStore() {
messageCountInRemoteFolder(1);
configureRemoteStoreWithFolder();
imapSync.sync(FOLDER_NAME, syncConfig, listener, null);
verify(remoteFolder).close(); verify(remoteFolder).close();
} }
@ -153,9 +134,8 @@ public class ImapSyncTest {
public void sync_withAccountPolicySetToExpungeOnPoll_shouldExpungeRemoteFolder() throws Exception { public void sync_withAccountPolicySetToExpungeOnPoll_shouldExpungeRemoteFolder() throws Exception {
messageCountInRemoteFolder(1); messageCountInRemoteFolder(1);
configureSyncConfigWithExpungePolicy(ExpungePolicy.ON_POLL); configureSyncConfigWithExpungePolicy(ExpungePolicy.ON_POLL);
configureRemoteStoreWithFolder();
imapSync.sync(FOLDER_NAME, syncConfig, listener, null); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder).expunge(); verify(remoteFolder).expunge();
} }
@ -165,7 +145,7 @@ public class ImapSyncTest {
messageCountInRemoteFolder(1); messageCountInRemoteFolder(1);
configureSyncConfigWithExpungePolicy(ExpungePolicy.MANUALLY); configureSyncConfigWithExpungePolicy(ExpungePolicy.MANUALLY);
imapSync.sync(FOLDER_NAME, syncConfig, listener, null); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder, never()).expunge(); verify(remoteFolder, never()).expunge();
} }
@ -176,7 +156,7 @@ public class ImapSyncTest {
configureSyncConfigWithSyncRemoteDeletions(true); configureSyncConfigWithSyncRemoteDeletions(true);
when(backendFolder.getAllMessagesAndEffectiveDates()).thenReturn(Collections.singletonMap(MESSAGE_UID1, 0L)); when(backendFolder.getAllMessagesAndEffectiveDates()).thenReturn(Collections.singletonMap(MESSAGE_UID1, 0L));
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(backendFolder).destroyMessages(messageListCaptor.capture()); verify(backendFolder).destroyMessages(messageListCaptor.capture());
assertEquals(MESSAGE_UID1, messageListCaptor.getValue().get(0)); assertEquals(MESSAGE_UID1, messageListCaptor.getValue().get(0));
@ -191,7 +171,7 @@ public class ImapSyncTest {
configureSyncConfigWithSyncRemoteDeletionsAndEarliestPollDate(dateOfEarliestPoll); configureSyncConfigWithSyncRemoteDeletionsAndEarliestPollDate(dateOfEarliestPoll);
when(remoteMessage.olderThan(dateOfEarliestPoll)).thenReturn(false); when(remoteMessage.olderThan(dateOfEarliestPoll)).thenReturn(false);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(backendFolder, never()).destroyMessages(messageListCaptor.capture()); verify(backendFolder, never()).destroyMessages(messageListCaptor.capture());
} }
@ -206,7 +186,7 @@ public class ImapSyncTest {
when(remoteMessage.olderThan(dateOfEarliestPoll)).thenReturn(true); when(remoteMessage.olderThan(dateOfEarliestPoll)).thenReturn(true);
when(backendFolder.getAllMessagesAndEffectiveDates()).thenReturn(Collections.singletonMap(MESSAGE_UID1, 0L)); when(backendFolder.getAllMessagesAndEffectiveDates()).thenReturn(Collections.singletonMap(MESSAGE_UID1, 0L));
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(backendFolder).destroyMessages(messageListCaptor.capture()); verify(backendFolder).destroyMessages(messageListCaptor.capture());
assertEquals(MESSAGE_UID1, messageListCaptor.getValue().get(0)); assertEquals(MESSAGE_UID1, messageListCaptor.getValue().get(0));
@ -217,7 +197,7 @@ public class ImapSyncTest {
messageCountInRemoteFolder(0); messageCountInRemoteFolder(0);
configureSyncConfigWithSyncRemoteDeletions(false); configureSyncConfigWithSyncRemoteDeletions(false);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(backendFolder, never()).destroyMessages(messageListCaptor.capture()); verify(backendFolder, never()).destroyMessages(messageListCaptor.capture());
} }
@ -228,7 +208,7 @@ public class ImapSyncTest {
hasUnsyncedRemoteMessage(); hasUnsyncedRemoteMessage();
when(remoteFolder.supportsFetchingFlags()).thenReturn(true); when(remoteFolder.supportsFetchingFlags()).thenReturn(true);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder, atLeastOnce()).fetch(any(List.class), fetchProfileCaptor.capture(), verify(remoteFolder, atLeastOnce()).fetch(any(List.class), fetchProfileCaptor.capture(),
nullable(MessageRetrievalListener.class)); nullable(MessageRetrievalListener.class));
@ -245,7 +225,7 @@ public class ImapSyncTest {
when(remoteFolder.supportsFetchingFlags()).thenReturn(false); when(remoteFolder.supportsFetchingFlags()).thenReturn(false);
respondToFetchEnvelopesWithMessage(smallMessage); respondToFetchEnvelopesWithMessage(smallMessage);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
verify(remoteFolder, atLeast(2)).fetch(any(List.class), fetchProfileCaptor.capture(), verify(remoteFolder, atLeast(2)).fetch(any(List.class), fetchProfileCaptor.capture(),
nullable(MessageRetrievalListener.class)); nullable(MessageRetrievalListener.class));
@ -261,7 +241,7 @@ public class ImapSyncTest {
when(remoteFolder.supportsFetchingFlags()).thenReturn(false); when(remoteFolder.supportsFetchingFlags()).thenReturn(false);
respondToFetchEnvelopesWithMessage(largeMessage); respondToFetchEnvelopesWithMessage(largeMessage);
imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); imapSync.sync(FOLDER_NAME, syncConfig, listener);
//TODO: Don't bother fetching messages of a size we don't have //TODO: Don't bother fetching messages of a size we don't have
verify(remoteFolder, atLeast(4)).fetch(any(List.class), fetchProfileCaptor.capture(), verify(remoteFolder, atLeast(4)).fetch(any(List.class), fetchProfileCaptor.capture(),

View file

@ -7,7 +7,6 @@ import com.fsck.k9.backend.api.SyncListener
import com.fsck.k9.mail.BodyFactory import com.fsck.k9.mail.BodyFactory
import com.fsck.k9.mail.FetchProfile import com.fsck.k9.mail.FetchProfile
import com.fsck.k9.mail.Flag import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.Folder
import com.fsck.k9.mail.Message import com.fsck.k9.mail.Message
import com.fsck.k9.mail.Part import com.fsck.k9.mail.Part
import com.fsck.k9.mail.PushReceiver import com.fsck.k9.mail.PushReceiver
@ -41,7 +40,7 @@ class Pop3Backend(
commandRefreshFolderList.refreshFolderList() commandRefreshFolderList.refreshFolderList()
} }
override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) { override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener) {
pop3Sync.sync(folder, syncConfig, listener) pop3Sync.sync(folder, syncConfig, listener)
} }

View file

@ -7,7 +7,6 @@ import com.fsck.k9.backend.api.SyncListener
import com.fsck.k9.mail.BodyFactory import com.fsck.k9.mail.BodyFactory
import com.fsck.k9.mail.FetchProfile import com.fsck.k9.mail.FetchProfile
import com.fsck.k9.mail.Flag import com.fsck.k9.mail.Flag
import com.fsck.k9.mail.Folder
import com.fsck.k9.mail.Message import com.fsck.k9.mail.Message
import com.fsck.k9.mail.MessagingException import com.fsck.k9.mail.MessagingException
import com.fsck.k9.mail.Part import com.fsck.k9.mail.Part
@ -45,7 +44,7 @@ class WebDavBackend(
commandGetFolders.refreshFolderList() commandGetFolders.refreshFolderList()
} }
override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener, providedRemoteFolder: Folder<*>?) { override fun sync(folder: String, syncConfig: SyncConfig, listener: SyncListener) {
webDavSync.sync(folder, syncConfig, listener) webDavSync.sync(folder, syncConfig, listener)
} }