MessageGenerator { public @Nullable Message messageForDataType(Map<String, String> msgMap, DataType dataType) throws MessagingException { switch (dataType) { case SMS: return messageFromMapSms(msgMap); case MMS: return messageFromMapMms(msgMap); case CALLLOG: return messageFromMapCallLog(msgMap); default: return null; } } MessageGenerator(Context context,
                     Address userAddress,
                     AddressStyle addressStyle,
                     HeaderGenerator headerGenerator,
                     PersonLookup personLookup,
                     boolean mailSubjectPrefix,
                     @Nullable ContactGroupIds contactsToBackup,
                     MmsSupport mmsSupport,
                     CallLogTypes callLogTypes,
                     DataTypePreferences dataTypePreferences); @Nullable Message messageForDataType(Map<String, String> msgMap, DataType dataType);  }
@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 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 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 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 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 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 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 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 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 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 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(); }