Improve message requests, add megaphone.

This commit is contained in:
Alex Hart
2020-02-19 18:08:34 -04:00
committed by Greyson Parrelli
parent dc689d325b
commit 9e5f64c431
83 changed files with 2406 additions and 735 deletions

View File

@@ -0,0 +1,253 @@
package org.thoughtcrime.securesms.recipients;
import android.content.Context;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.ThreadDatabase;
import org.thoughtcrime.securesms.util.FeatureFlags;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
@RunWith(PowerMockRunner.class)
@PrepareForTest({DatabaseFactory.class, FeatureFlags.class})
public class RecipientUtilTest {
private Context context = mock(Context.class);
private Recipient recipient = mock(Recipient.class);
private ThreadDatabase mockThreadDatabase = mock(ThreadDatabase.class);
private MmsSmsDatabase mockMmsSmsDatabase = mock(MmsSmsDatabase.class);
private RecipientDatabase mockRecipientDatabase = mock(RecipientDatabase.class);
@Before
public void setUp() {
mockStatic(DatabaseFactory.class);
when(DatabaseFactory.getThreadDatabase(any())).thenReturn(mockThreadDatabase);
when(DatabaseFactory.getMmsSmsDatabase(any())).thenReturn(mockMmsSmsDatabase);
when(DatabaseFactory.getRecipientDatabase(any())).thenReturn(mockRecipientDatabase);
mockStatic(FeatureFlags.class);
when(FeatureFlags.messageRequests()).thenReturn(true);
when(recipient.getId()).thenReturn(RecipientId.from(5));
when(recipient.resolve()).thenReturn(recipient);
}
@Test
public void givenMessageRequestsFlagDisabled_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(FeatureFlags.messageRequests()).thenReturn(false);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1);
// THEN
assertTrue(result);
}
@Test
public void givenThreadIsNegativeOne_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, -1L);
// THEN
assertTrue(result);
}
@Test
public void givenRecipientIsNullForThreadId_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertTrue(result);
}
@Test
public void givenIHaveSentASecureMessageInThisThread_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(mockThreadDatabase.getRecipientForThreadId(anyLong())).thenReturn(recipient);
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(1L)).thenReturn(5);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertTrue(result);
}
@Test
public void givenIHaveNotSentASecureMessageInThisThreadAndIAmProfileSharing_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(recipient.isProfileSharing()).thenReturn(true);
when(mockThreadDatabase.getRecipientForThreadId(anyLong())).thenReturn(recipient);
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(1L)).thenReturn(0);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertTrue(result);
}
@Test
public void givenIHaveNotSentASecureMessageInThisThreadAndRecipientIsSystemContact_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(recipient.isSystemContact()).thenReturn(true);
when(mockThreadDatabase.getRecipientForThreadId(anyLong())).thenReturn(recipient);
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(1L)).thenReturn(0);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertTrue(result);
}
@Test
public void givenIHaveReceivedASecureMessageIHaveNotSentASecureMessageAndRecipientIsNotSystemContactAndNotProfileSharing_whenIsThreadMessageRequestAccepted_thenIExpectFalse() {
// GIVEN
when(mockThreadDatabase.getRecipientForThreadId(anyLong())).thenReturn(recipient);
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(1L)).thenReturn(0);
when(mockMmsSmsDatabase.getSecureConversationCount(1L)).thenReturn(5);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertFalse(result);
}
@Test
public void givenIHaveNotReceivedASecureMessageIHaveNotSentASecureMessageAndRecipientIsNotSystemContactAndNotProfileSharing_whenIsThreadMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(mockThreadDatabase.getRecipientForThreadId(anyLong())).thenReturn(recipient);
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(1L)).thenReturn(0);
when(mockMmsSmsDatabase.getSecureConversationCount(1L)).thenReturn(0);
// WHEN
boolean result = RecipientUtil.isThreadMessageRequestAccepted(context, 1L);
// THEN
assertTrue(result);
}
@Test
public void givenRecipientIsNull_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, null);
// THEN
assertTrue(result);
}
@Test
public void givenMessageRequestsFlagIsOff_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(FeatureFlags.messageRequests()).thenReturn(false);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertTrue(result);
}
@Test
public void givenNonZeroOutgoingSecureMessageCount_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(anyLong())).thenReturn(1);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertTrue(result);
}
@Test
public void givenIAmProfileSharing_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(recipient.isProfileSharing()).thenReturn(true);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertTrue(result);
}
@Test
public void givenRecipientIsASystemContact_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(recipient.isSystemContact()).thenReturn(true);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertTrue(result);
}
@Test
public void givenNoSecureMessagesSentSomeSecureMessagesReceivedNotSharingAndNotSystemContact_whenIsRecipientMessageRequestAccepted_thenIExpectFalse() {
// GIVEN
when(mockMmsSmsDatabase.getSecureConversationCount(anyLong())).thenReturn(5);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertFalse(result);
}
@Test
public void givenNoSecureMessagesSentNoSecureMessagesReceivedNotSharingAndNotSystemContact_whenIsRecipientMessageRequestAccepted_thenIExpectTrue() {
// GIVEN
when(mockMmsSmsDatabase.getSecureConversationCount(anyLong())).thenReturn(0);
// WHEN
boolean result = RecipientUtil.isRecipientMessageRequestAccepted(context, recipient);
// THEN
assertTrue(result);
}
@Test
public void givenNoSecureMessagesSent_whenIShareProfileIfFirstSecureMessage_thenIShareProfile() {
// GIVEN
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(anyLong())).thenReturn(0);
// WHEN
RecipientUtil.shareProfileIfFirstSecureMessage(context, recipient);
// THEN
verify(mockRecipientDatabase).setProfileSharing(recipient.getId(), true);
}
@Test
public void givenSecureMessagesSent_whenIShareProfileIfFirstSecureMessage_thenIShareProfile() {
// GIVEN
when(mockMmsSmsDatabase.getOutgoingSecureConversationCount(anyLong())).thenReturn(5);
// WHEN
RecipientUtil.shareProfileIfFirstSecureMessage(context, recipient);
// THEN
verify(mockRecipientDatabase, never()).setProfileSharing(recipient.getId(), true);
}
}