diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java index d768473b5..164a9a5e6 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/webdav/WebDavFolder.java @@ -497,19 +497,12 @@ class WebDavFolder extends Folder { } WebDavMessage wdMessage = (WebDavMessage) messages.get(i); - if (listener != null) { - listener.messageStarted(wdMessage.getUid(), i, count); - } - try { wdMessage.setFlagInternal(Flag.SEEN, uidToReadStatus.get(wdMessage.getUid())); } catch (NullPointerException e) { Log.v(LOG_TAG, "Under some weird circumstances, setting the read status when syncing from webdav threw an NPE. Skipping."); } - if (listener != null) { - listener.messageFinished(wdMessage, i, count); - } } } diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java index 22f85b67a..c9e9a3b2b 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/store/webdav/WebDavFolderTest.java @@ -7,15 +7,11 @@ import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.BinaryTempFileBody; import com.fsck.k9.mail.store.StoreConfig; -import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; -import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.entity.BasicHttpEntity; import org.apache.http.protocol.HttpContext; -import org.apache.tools.ant.taskdefs.condition.Http; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -35,18 +31,15 @@ import static com.fsck.k9.mail.Folder.OPEN_MODE_RW; import static com.fsck.k9.mail.Folder.OPEN_MODE_RO; import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyMapOf; import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.anyMap; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -57,7 +50,6 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Date; @@ -65,6 +57,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +@SuppressWarnings("deprecation") @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE, sdk = 21) public class WebDavFolderTest { @@ -142,14 +135,14 @@ public class WebDavFolderTest { headers.put("Brief", "t"); when(mockStore.processRequest("https://localhost/webDavStoreUrl/testFolder", "SEARCH", getUrlsXml, headers)) .thenReturn(mockDataSet); - Map urlUids = new HashMap(); + Map urlUids = new HashMap<>(); urlUids.put(uid, url); when(mockDataSet.getUidToUrl()).thenReturn(urlUids); } @Test public void folder_can_fetch_less_than_10_envelopes() throws MessagingException { - when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())) + when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List messages = new ArrayList<>(); @@ -165,7 +158,8 @@ public class WebDavFolderTest { @Test public void folder_can_fetch_more_than_10_envelopes() throws MessagingException { - when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())) + when(mockStore.processRequest(anyString(), anyString(), anyString(), + anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List messages = new ArrayList<>(); @@ -180,7 +174,8 @@ public class WebDavFolderTest { @Test public void folder_can_fetch_less_than_20_flags() throws MessagingException { - when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())) + when(mockStore.processRequest(anyString(), anyString(), anyString(), + anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List messages = new ArrayList<>(); @@ -195,7 +190,8 @@ public class WebDavFolderTest { @Test public void folder_can_fetch_more_than_20_flags() throws MessagingException { - when(mockStore.processRequest(anyString(), anyString(), anyString(), anyMap())) + when(mockStore.processRequest(anyString(), anyString(), anyString(), + anyMapOf(String.class, String.class))) .thenReturn(mockDataSet); List messages = new ArrayList<>(); @@ -239,6 +235,39 @@ public class WebDavFolderTest { verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25)); } + @Test + public void folder_does_not_notify_listener_twice_when_fetching_flags_and_bodies() + throws MessagingException, IOException, URISyntaxException { + setupStoreForMessageFetching(); + when(mockStore.processRequest(anyString(), anyString(), anyString(), + anyMapOf(String.class, String.class))) + .thenReturn(mockDataSet); + List messages = setup25MessagesToFetch(); + when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer( + new Answer() { + @Override + public HttpResponse answer(InvocationOnMock invocation) throws Throwable { + HttpResponse httpResponse = mock(HttpResponse.class); + StatusLine statusLine = mock(StatusLine.class); + when(httpResponse.getStatusLine()).thenReturn(statusLine); + when(statusLine.getStatusCode()).thenReturn(200); + + BasicHttpEntity httpEntity = new BasicHttpEntity(); + String body = ""; + httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8"))); + when(httpResponse.getEntity()).thenReturn(httpEntity); + return httpResponse; + } + }); + + FetchProfile profile = new FetchProfile(); + profile.add(FetchProfile.Item.FLAGS); + profile.add(FetchProfile.Item.BODY); + folder.fetch(messages, profile, listener); + verify(listener, times(25)).messageStarted(any(String.class), anyInt(), anyInt()); + verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), anyInt()); + } + private void setupStoreForMessageFetching() { String authString = "authString"; when(mockStoreConfig.getMaximumAutoDownloadMessageSize()).thenReturn(1900); @@ -385,7 +414,7 @@ public class WebDavFolderTest { when(mockStore.processRequest(eq("https://localhost/webDavStoreUrl/testFolder"), eq("SEARCH"), eq(messagesXml), Matchers.>any())).thenReturn(mockDataSet); - folder.getMessages(1, 11, new Date(), listener); + folder.getMessages(messageStart, messageEnd, new Date(), listener); verify(listener, times(5)).messageStarted(anyString(), anyInt(), eq(5)); verify(listener, times(5)).messageFinished(any(WebDavMessage.class), anyInt(), eq(5)); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index a2ec40997..180bc1627 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -525,7 +525,6 @@ public class MessagingController implements Runnable { @Override public void messagesFinished(int number) {} @Override - public void messageFinished(LocalMessage message, int number, int ofTotal) { if (!isMessageSuppressed(message)) { List messages = new ArrayList(); @@ -1396,7 +1395,7 @@ public class MessagingController implements Runnable { final Date earliestDate = account.getEarliestPollDate(); if (K9.DEBUG) - Log.d(K9.LOG_TAG, "SYNC: Fetching small messages for folder " + folder); + Log.d(K9.LOG_TAG, "SYNC: Fetching " + smallMessages.size() + " small messages for folder " + folder); remoteFolder.fetch(smallMessages, fp, new MessageRetrievalListener() {