Add integration test for auto-delete timer in private messages.

This commit is contained in:
akwizgran
2020-11-19 13:12:02 +00:00
committed by Torsten Grote
parent dba85debfa
commit d67cbd40bd
2 changed files with 50 additions and 14 deletions

View File

@@ -58,7 +58,6 @@ import static androidx.lifecycle.Transformations.map;
import static java.util.Objects.requireNonNull;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
import static org.briarproject.bramble.util.LogUtils.logDuration;
import static org.briarproject.bramble.util.LogUtils.logException;
import static org.briarproject.bramble.util.LogUtils.now;
@@ -245,11 +244,8 @@ public class ConversationViewModel extends DbViewModel
// messagingGroupId is loaded with the contact
observeForeverOnce(messagingGroupId, groupId -> {
requireNonNull(groupId);
// TODO: Use the timer duration that was fetched when checking the
// message format
observeForeverOnce(privateMessageFormat, format ->
createMessage(groupId, text, headers, timestamp,
format));
createMessage(groupId, text, headers, timestamp, format));
});
}
@@ -313,8 +309,6 @@ public class ConversationViewModel extends DbViewModel
private void createMessage(GroupId groupId, @Nullable String text,
List<AttachmentHeader> headers, long timestamp,
PrivateMessageFormat format) {
// TODO: Move this inside the DB transaction that stores the message
// so we can look up the timer duration (if needed) in the same txn
try {
PrivateMessage pm;
if (format == TEXT) {
@@ -324,8 +318,9 @@ public class ConversationViewModel extends DbViewModel
pm = privateMessageFactory.createPrivateMessage(groupId,
timestamp, text, headers);
} else {
// TODO: Look up auto-delete timer
pm = privateMessageFactory.createPrivateMessage(groupId,
timestamp, text, headers, MIN_AUTO_DELETE_TIMER_MS);
timestamp, text, headers, -1);
}
storeMessage(pm);
} catch (FormatException e) {

View File

@@ -32,6 +32,7 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.briarproject.bramble.api.autodelete.AutoDeleteConstants.MIN_AUTO_DELETE_TIMER_MS;
import static org.briarproject.bramble.api.sync.validation.MessageState.DELIVERED;
import static org.briarproject.bramble.api.sync.validation.MessageState.PENDING;
import static org.briarproject.bramble.test.TestUtils.getRandomBytes;
@@ -89,7 +90,7 @@ public class MessagingManagerIntegrationTest
@Test
public void testSimpleConversation() throws Exception {
// conversation start out empty
// conversation starts out empty
Collection<ConversationMessageHeader> messages0 = getMessages(c0);
Collection<ConversationMessageHeader> messages1 = getMessages(c1);
assertEquals(0, messages0.size());
@@ -108,6 +109,10 @@ public class MessagingManagerIntegrationTest
(PrivateMessageHeader) messages1.iterator().next();
assertTrue(m0.hasText());
assertTrue(m1.hasText());
assertEquals(0, m0.getAttachmentHeaders().size());
assertEquals(0, m1.getAttachmentHeaders().size());
assertEquals(-1, m0.getAutoDeleteTimer());
assertEquals(-1, m1.getAutoDeleteTimer());
assertTrue(m0.isRead());
assertFalse(m1.isRead());
assertGroupCounts(c0, 1, 0);
@@ -143,13 +148,44 @@ public class MessagingManagerIntegrationTest
assertFalse(m1.hasText());
assertEquals(1, m0.getAttachmentHeaders().size());
assertEquals(1, m1.getAttachmentHeaders().size());
assertEquals(-1, m0.getAutoDeleteTimer());
assertEquals(-1, m1.getAutoDeleteTimer());
assertTrue(m0.isRead());
assertFalse(m1.isRead());
assertGroupCounts(c0, 1, 0);
assertGroupCounts(c1, 1, 1);
}
@Test
public void testAutoDeleteTimer() throws Exception {
// send message with auto-delete timer
sendMessage(c0, c1, getRandomString(123), emptyList(),
MIN_AUTO_DELETE_TIMER_MS);
// message with timer is sent/displayed properly
Collection<ConversationMessageHeader> messages0 = getMessages(c0);
Collection<ConversationMessageHeader> messages1 = getMessages(c1);
assertEquals(1, messages0.size());
assertEquals(1, messages1.size());
PrivateMessageHeader m0 =
(PrivateMessageHeader) messages0.iterator().next();
PrivateMessageHeader m1 =
(PrivateMessageHeader) messages1.iterator().next();
assertTrue(m0.hasText());
assertTrue(m1.hasText());
assertEquals(0, m0.getAttachmentHeaders().size());
assertEquals(0, m1.getAttachmentHeaders().size());
assertEquals(MIN_AUTO_DELETE_TIMER_MS, m0.getAutoDeleteTimer());
assertEquals(MIN_AUTO_DELETE_TIMER_MS, m1.getAutoDeleteTimer());
assertTrue(m0.isRead());
assertFalse(m1.isRead());
assertGroupCounts(c0, 1, 0);
assertGroupCounts(c1, 1, 1);
}
@Test
public void testDeleteAll() throws Exception {
// send 3 message (1 with attachment)
// send 3 messages (1 with attachment)
sendMessage(c0, c1, getRandomString(42));
sendMessage(c0, c1, getRandomString(23));
sendMessage(c0, c1, null, singletonList(addAttachment(c0)));
@@ -331,18 +367,23 @@ public class MessagingManagerIntegrationTest
}
private PrivateMessage sendMessage(BriarIntegrationTestComponent from,
BriarIntegrationTestComponent to, String text)
throws Exception {
BriarIntegrationTestComponent to, String text) throws Exception {
return sendMessage(from, to, text, emptyList());
}
private PrivateMessage sendMessage(BriarIntegrationTestComponent from,
BriarIntegrationTestComponent to, @Nullable String text,
List<AttachmentHeader> attachments) throws Exception {
return sendMessage(from, to, text, attachments, -1);
}
private PrivateMessage sendMessage(BriarIntegrationTestComponent from,
BriarIntegrationTestComponent to, @Nullable String text,
List<AttachmentHeader> attachments, long autoDeleteTimer)
throws Exception {
GroupId g = from.getMessagingManager().getConversationId(contactId);
// TODO: Add tests for auto-deletion timer
PrivateMessage m = messageFactory.createPrivateMessage(g,
clock.currentTimeMillis(), text, attachments, -1);
clock.currentTimeMillis(), text, attachments, autoDeleteTimer);
from.getMessagingManager().addLocalMessage(m);
syncMessage(from, to, contactId, 1 + attachments.size(), true);
return m;