public ReplyToAddresses getRecipientsToReplyAllTo(Message message, Account account) { List<Address> replyToAddresses = Arrays.asList(getRecipientsToReplyTo(message, account).to); HashSet<Address> alreadyAddedAddresses = new HashSet<>(replyToAddresses); ArrayList<Address> toAddresses = new ArrayList<>(replyToAddresses); ArrayList<Address> ccAddresses = new ArrayList<>(); for (Address address : message.getFrom()) { if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { toAddresses.add(address); alreadyAddedAddresses.add(address); } } for (Address address : message.getRecipients(RecipientType.TO)) { if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { toAddresses.add(address); alreadyAddedAddresses.add(address); } } for (Address address : message.getRecipients(RecipientType.CC)) { if (!alreadyAddedAddresses.contains(address) && !account.isAnIdentity(address)) { ccAddresses.add(address); alreadyAddedAddresses.add(address); } } return new ReplyToAddresses(toAddresses, ccAddresses); }
public void initFromReplyToMessage(Message message, boolean isReplyAll) { ReplyToAddresses replyToAddresses = isReplyAll ? replyToParser.getRecipientsToReplyAllTo(message, account) : replyToParser.getRecipientsToReplyTo(message, account); addToAddresses(replyToAddresses.to); addCcAddresses(replyToAddresses.cc); boolean shouldSendAsPgpInline = composePgpInlineDecider.shouldReplyInline(message); if (shouldSendAsPgpInline) { cryptoEnablePgpInline = true; } isReplyToEncryptedMessage = composePgpEnableByDefaultDecider.shouldEncryptByDefault(message); }
@Before public void setUp() throws Exception { message = mock(Message.class); account = mock(Account.class); replyToParser = new ReplyToParser(); }
@Test public void getRecipientsToReplyAllTo_should_returnFromAndToAndCcRecipients() throws Exception { when(message.getReplyTo()).thenReturn(EMPTY_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(new String[0]); when(message.getFrom()).thenReturn(FROM_ADDRESSES); when(message.getRecipients(RecipientType.TO)).thenReturn(TO_ADDRESSES); when(message.getRecipients(RecipientType.CC)).thenReturn(CC_ADDRESSES); ReplyToAddresses recipientsToReplyAllTo = replyToParser.getRecipientsToReplyAllTo(message, account); assertArrayEquals(arrayConcatenate(FROM_ADDRESSES, TO_ADDRESSES, Address.class), recipientsToReplyAllTo.to); assertArrayEquals(CC_ADDRESSES, recipientsToReplyAllTo.cc); }
@Test public void getRecipientsToReplyAllTo_should_excludeIdentityAddresses() throws Exception { when(message.getReplyTo()).thenReturn(EMPTY_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(new String[0]); when(message.getFrom()).thenReturn(EMPTY_ADDRESSES); when(message.getRecipients(RecipientType.TO)).thenReturn(TO_ADDRESSES); when(message.getRecipients(RecipientType.CC)).thenReturn(CC_ADDRESSES); Address excludedCcAddress = CC_ADDRESSES[1]; Address excludedToAddress = TO_ADDRESSES[0]; when(account.isAnIdentity(eq(excludedToAddress))).thenReturn(true); when(account.isAnIdentity(eq(excludedCcAddress))).thenReturn(true); ReplyToAddresses recipientsToReplyAllTo = replyToParser.getRecipientsToReplyAllTo(message, account); assertArrayEquals(arrayExcept(TO_ADDRESSES, excludedToAddress), recipientsToReplyAllTo.to); assertArrayEquals(arrayExcept(CC_ADDRESSES, excludedCcAddress), recipientsToReplyAllTo.cc); }
@Test public void initFromReplyToMessage_shouldCallComposePgpInlineDecider() throws Exception { Message message = mock(Message.class); when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); recipientPresenter.initFromReplyToMessage(message, false); verify(composePgpInlineDecider).shouldReplyInline(message); }
@Test @Ignore("It looks like the support version of AsyncTaskLoader handles background tasks differently") public void testInitFromReplyToAllMessage() throws Exception { Message message = mock(Message.class); when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); ReplyToAddresses replyToAddresses = new ReplyToAddresses(ALL_TO_ADDRESSES, ALL_CC_ADDRESSES); when(replyToParser.getRecipientsToReplyAllTo(message, account)).thenReturn(replyToAddresses); recipientPresenter.initFromReplyToMessage(message, true); // one for To, one for Cc runBackgroundTask(); runBackgroundTask(); verify(recipientMvpView).addRecipients(eq(RecipientType.TO), any(Recipient.class)); verify(recipientMvpView).addRecipients(eq(RecipientType.CC), any(Recipient.class)); }
@Test public void getRecipientsToReplyAllTo_should_excludeDuplicates() throws Exception { when(message.getReplyTo()).thenReturn(REPLY_TO_ADDRESSES); when(message.getFrom()).thenReturn(arrayConcatenate(FROM_ADDRESSES, REPLY_TO_ADDRESSES, Address.class)); when(message.getRecipients(RecipientType.TO)).thenReturn(arrayConcatenate(FROM_ADDRESSES, TO_ADDRESSES, Address.class)); when(message.getRecipients(RecipientType.CC)).thenReturn(arrayConcatenate(CC_ADDRESSES, TO_ADDRESSES, Address.class)); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(new String[0]); ReplyToAddresses recipientsToReplyAllTo = replyToParser.getRecipientsToReplyAllTo(message, account); assertArrayContainsAll(REPLY_TO_ADDRESSES, recipientsToReplyAllTo.to); assertArrayContainsAll(FROM_ADDRESSES, recipientsToReplyAllTo.to); assertArrayContainsAll(TO_ADDRESSES, recipientsToReplyAllTo.to); int totalExpectedAddresses = REPLY_TO_ADDRESSES.length + FROM_ADDRESSES.length + TO_ADDRESSES.length; assertEquals(totalExpectedAddresses, recipientsToReplyAllTo.to.length); assertArrayEquals(CC_ADDRESSES, recipientsToReplyAllTo.cc); }
recipientPresenter = new RecipientPresenter(getApplicationContext(), getSupportLoaderManager(), openPgpApiManager, recipientMvpView, account, composePgpInlineDecider, composePgpEnableByDefaultDecider, AutocryptStatusInteractor.getInstance(), new ReplyToParser(), this, DI.get(AutocryptDraftStateHeaderParser.class)); recipientPresenter.asyncUpdateCryptoStatus();
@Test public void getRecipientsToReplyTo_should_prefer_from_ifOtherIsIdentity() throws Exception { when(message.getReplyTo()).thenReturn(REPLY_TO_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(LIST_POST_HEADER_VALUES); when(message.getFrom()).thenReturn(FROM_ADDRESSES); when(message.getRecipients(RecipientType.TO)).thenReturn(TO_ADDRESSES); when(account.isAnIdentity(any(Address[].class))).thenReturn(true); ReplyToAddresses result = replyToParser.getRecipientsToReplyTo(message, account); assertArrayEquals(TO_ADDRESSES, result.to); assertArrayEquals(EMPTY_ADDRESSES, result.cc); }
@Test @Ignore("It looks like the support version of AsyncTaskLoader handles background tasks differently") public void testInitFromReplyToMessage() throws Exception { Message message = mock(Message.class); when(replyToParser.getRecipientsToReplyTo(message, account)).thenReturn(TO_ADDRESSES); recipientPresenter.initFromReplyToMessage(message, false); runBackgroundTask(); Recipient toRecipient = new Recipient(TO_ADDRESSES.to[0]); verify(recipientMvpView).addRecipients(eq(RecipientType.TO), eq(toRecipient)); }
@Test public void getRecipientsToReplyTo_should_prefer_replyTo_over_any_other_field() throws Exception { when(message.getReplyTo()).thenReturn(REPLY_TO_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(LIST_POST_HEADER_VALUES); when(message.getFrom()).thenReturn(FROM_ADDRESSES); ReplyToAddresses result = replyToParser.getRecipientsToReplyTo(message, account); assertArrayEquals(REPLY_TO_ADDRESSES, result.to); assertArrayEquals(EMPTY_ADDRESSES, result.cc); verify(account).isAnIdentity(result.to); }
@Test public void getRecipientsToReplyTo_should_prefer_listPost_over_from_field() throws Exception { when(message.getReplyTo()).thenReturn(EMPTY_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(LIST_POST_HEADER_VALUES); when(message.getFrom()).thenReturn(FROM_ADDRESSES); ReplyToAddresses result = replyToParser.getRecipientsToReplyTo(message, account); assertArrayEquals(LIST_POST_ADDRESSES, result.to); assertArrayEquals(EMPTY_ADDRESSES, result.cc); verify(account).isAnIdentity(result.to); }
@Test public void getRecipientsToReplyTo_should_return_from_otherwise() throws Exception { when(message.getReplyTo()).thenReturn(EMPTY_ADDRESSES); when(message.getHeader(ListHeaders.LIST_POST_HEADER)).thenReturn(new String[0]); when(message.getFrom()).thenReturn(FROM_ADDRESSES); ReplyToAddresses result = replyToParser.getRecipientsToReplyTo(message, account); assertArrayEquals(FROM_ADDRESSES, result.to); assertArrayEquals(EMPTY_ADDRESSES, result.cc); verify(account).isAnIdentity(result.to); }