public @NonNull ConversionResult convertMessages(final Cursor cursor, DataType dataType) throws MessagingException { final Map<String, String> msgMap = getMessageMap(cursor); final Message m = messageGenerator.messageForDataType(msgMap, dataType); final ConversionResult result = new ConversionResult(dataType); if (m != null) { m.setFlag(Flag.SEEN, markAsSeen(dataType, msgMap)); result.add(m, msgMap); } return result; }
@Test public void testShouldReturnNullIfMessageHasNoAddress() throws Exception { Map<String, String> map = new HashMap<String, String>(); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNull(); }
@Test public void shouldGenerateCorrectHeaders() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); Date date = new Date(); map.put(Telephony.TextBasedSmsColumns.DATE, String.valueOf(date.getTime())); map.put(Telephony.TextBasedSmsColumns.TYPE, "0"); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNotNull(); verify(headerGenerator).setHeaders(any(Message.class), any(Map.class), eq(DataType.SMS), anyString(), eq(record), eq(date), eq(0)); }
@Test public void shouldOnlyIncludePeopleFromContactIdsIfSpecified() throws Exception { MessageGenerator generator = new MessageGenerator(RuntimeEnvironment.application, me, AddressStyle.NAME, headerGenerator, personLookup, false, groupIds, mmsSupport, CallLogTypes.EVERYTHING, dataTypePreferences ); PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); map.put(Telephony.TextBasedSmsColumns.TYPE, "1"); when(groupIds.contains(record)).thenReturn(false); assertThat(generator.messageForDataType(map, DataType.SMS)).isNull(); when(groupIds.contains(record)).thenReturn(true); assertThat(generator.messageForDataType(map, DataType.SMS)).isNotNull(); }
@Test public void testShouldGenerateMMSMessageWithCorrectEncoding() throws Exception { PersonRecord personRecord = new PersonRecord(1, "Foo Bar", "foo@bar.com", "1234"); MmsSupport.MmsDetails details = new MmsSupport.MmsDetails(true, "foo", personRecord, new Address("foo@bar.com")); when(mmsSupport.getDetails(any(Uri.class), any(AddressStyle.class))).thenReturn(details); Message msg = generator.messageForDataType(mockMessage("1234", personRecord), DataType.MMS); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_7BIT }); }
@Test public void testShouldGenerateSubjectWithNameForMMS() throws Exception { PersonRecord personRecord = new PersonRecord(1, "Foo Bar", "foo@bar.com", "1234"); MmsSupport.MmsDetails details = new MmsSupport.MmsDetails(true, "foo", personRecord, new Address("foo@bar.com")); when(mmsSupport.getDetails(any(Uri.class), any(AddressStyle.class))).thenReturn(details); Message msg = generator.messageForDataType(mockMessage("1234", personRecord), DataType.MMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Foo Bar"); }
@Test public void testShouldGenerateSubjectWithNameForSMS() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Test Testor"); }
@Test public void testShouldUseNumberIfNameIsUnknown() throws Exception { PersonRecord record = new PersonRecord(-1, null, null, "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with 1234"); }
@Test public void testShouldGenerateSubjectWithNameAndNumberForSMS() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("SMS with Test Testor"); }
@Test public void shouldGenerateCorrectFromHeaderWithUsersEmailAddress() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getFrom()[0]).isEqualTo(me); }
@Test public void testShouldGenerateCallLogMessageWithCorrectEncoding() throws Exception { PersonRecord record = new PersonRecord(-1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockCalllogMessage("1234", OUTGOING_TYPE, record), CALLLOG); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_QUOTED_PRINTABLE }); }
@Test public void shouldGenerateCorrectToHeaderWhenUserisRecipient() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Map<String, String> map = mockMessage("1234", record); map.put(Telephony.TextBasedSmsColumns.TYPE, "1"); Message msg = generator.messageForDataType(map, DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getFrom()[0].toString()) .isEqualTo("Test Testor <test@test.com>"); assertThat(msg.getRecipients(Message.RecipientType.TO)[0]).isEqualTo(me); }
@Test public void testShouldGenerateMessageForCallLogIncoming() throws Exception { PersonRecord record = new PersonRecord(-1, "Test Testor", null, null); Message message = generator.messageForDataType(mockCalllogMessage("1234", INCOMING_TYPE, record), CALLLOG); assertMessage(message); }
@Test public void testShouldGenerateSMSMessageWithCorrectEncoding() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg.getHeader(MimeHeader.HEADER_CONTENT_TRANSFER_ENCODING)).isEqualTo(new String[] { MimeUtil.ENC_QUOTED_PRINTABLE }); }
@Test public void testShouldGenerateMessageForCallLogOutgoing() throws Exception { PersonRecord record = new PersonRecord(-1, "Test Testor", null, null); Message msg = generator.messageForDataType(mockCalllogMessage("1234", OUTGOING_TYPE, record), CALLLOG); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("Call with Test Testor"); assertThat(msg.getFrom()[0]).isEqualTo(me); assertThat(msg.getRecipients(Message.RecipientType.TO)[0].toString()).isEqualTo("Test Testor <unknown.number@unknown.email>"); }
@Test public void testShouldGenerateMessageForCallLogMissed() throws Exception { PersonRecord record = new PersonRecord(-1, "Test Testor", null, null); Message message = generator.messageForDataType(mockCalllogMessage("1234", MISSED_TYPE, record), CALLLOG); assertMessage(message); }
@Test public void shouldGenerateCorrectToHeader() throws Exception { PersonRecord record = new PersonRecord(1, "Test Testor", "test@test.com", "1234"); Message msg = generator.messageForDataType(mockMessage("1234", record), DataType.SMS); assertThat(msg).isNotNull(); assertThat(msg.getRecipients(Message.RecipientType.TO)[0].toString()) .isEqualTo("Test Testor <test@test.com>"); }
@Test public void testShouldGenerateMessageForCallLogIncomingUnknown() throws Exception { PersonRecord record = new PersonRecord(0, null, null, "-1"); Message msg = generator.messageForDataType(mockCalllogMessage("", INCOMING_TYPE, record), CALLLOG); assertThat(msg).isNotNull(); assertThat(msg.getSubject()).isEqualTo("Call with Unknown"); assertThat(msg.getFrom()[0].toString()).isEqualTo("Unknown <unknown.number@unknown.email>"); assertThat(msg.getRecipients(Message.RecipientType.TO)[0]).isEqualTo(me); }