@Test public void getTextFromPart_withNoBody_shouldReturnNull() throws Exception { part.setBody(null); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@Test public void getTextFromPart_withExceptionThrownGettingInputStream_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); Body body = mock(Body.class); when(body.getInputStream()).thenThrow(new MessagingException("Test")); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@Test public void getTextFromPart_withUnknownEncoding_shouldReturnUnmodifiedBodyContents() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain"); String bodyText = "Sample text body"; BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), "unknown encoding"); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals(bodyText, result); }
@Test public void getTextFromPart_withPlainTextWithCharsetInContentTypeRawDataBody_shouldReturnText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; charset=UTF-8"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("Sample text body", result); }
@Test public void getTextFromPart_withTextBody_shouldReturnText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain; charset=utf-8"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("Sample text body", result); }
@Test public void getTextFromPart_withRawDataBodyWithNonText_shouldReturnNull() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "image/jpeg"); BinaryMemoryBody body = new BinaryMemoryBody("Sample text body".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNull(result); }
@Test public void getTextFromPart_withHtmlWithCharsetInContentTypeRawDataBody_shouldReturnHtmlText() throws Exception { part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html; charset=UTF-8"); BinaryMemoryBody body = new BinaryMemoryBody( "<html><body>Sample text body</body></html>".getBytes(), MimeUtil.ENC_8BIT); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertEquals("<html><body>Sample text body</body></html>", result); }
@Test public void getTextFromPart_withHtmlWithCharsetInHtmlRawDataBody_shouldReturnHtmlText() throws Exception { String bodyText = "<html><head>" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" + "</head><body>Sample text body</body></html>"; BinaryMemoryBody body = new BinaryMemoryBody(bodyText.getBytes(), MimeUtil.ENC_8BIT); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/html"); part.setBody(body); String result = MessageExtractor.getTextFromPart(part); assertNotNull(result); assertEquals(bodyText, result); } }
@Test public void extractInfoForDb__withNoBody__shouldReturnContentAvailable() throws Exception { MimeBodyPart part = new MimeBodyPart(); part.setBody(new TextBody("data")); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfoForDatabase(part); assertTrue(attachmentViewInfo.isContentAvailable()); }
@Test public void extractInfo__withDeferredFileBody() throws Exception { attachmentInfoExtractor = new AttachmentInfoExtractor(context) { @Nullable @Override protected Uri getDecryptedFileProviderUri(DeferredFileBody decryptedTempFileBody, String mimeType) { return TEST_URI; } }; DeferredFileBody body = mock(DeferredFileBody.class); when(body.getSize()).thenReturn(TEST_SIZE); MimeBodyPart part = new MimeBodyPart(); part.setBody(body); part.setHeader(MimeHeader.HEADER_CONTENT_TYPE, TEST_MIME_TYPE); AttachmentViewInfo attachmentViewInfo = attachmentInfoExtractor.extractAttachmentInfo(part); assertEquals(TEST_URI, attachmentViewInfo.internalUri); assertEquals(TEST_SIZE, attachmentViewInfo.size); assertEquals(TEST_MIME_TYPE, attachmentViewInfo.mimeType); assertFalse(attachmentViewInfo.inlineAttachment); assertTrue(attachmentViewInfo.isContentAvailable()); } }