TweetTextLinkifier { static CharSequence linkifyUrls(FormattedTweetText tweetText, final LinkClickListener linkListener, final int linkColor, final int linkHighlightColor, boolean stripQuoteTweet, boolean stripVineCard) { if (tweetText == null) return null; if (TextUtils.isEmpty(tweetText.text)) { return tweetText.text; } final SpannableStringBuilder spannable = new SpannableStringBuilder(tweetText.text); final List<FormattedUrlEntity> urls = ModelUtils.getSafeList(tweetText.urlEntities); final List<FormattedMediaEntity> media = ModelUtils.getSafeList(tweetText.mediaEntities); final List<FormattedUrlEntity> hashtags = ModelUtils.getSafeList(tweetText.hashtagEntities); final List<FormattedUrlEntity> mentions = ModelUtils.getSafeList(tweetText.mentionEntities); final List<FormattedUrlEntity> symbols = ModelUtils.getSafeList(tweetText.symbolEntities); final List<FormattedUrlEntity> combined = mergeAndSortEntities(urls, media, hashtags, mentions, symbols); final FormattedUrlEntity strippedEntity = getEntityToStrip(tweetText.text, combined, stripQuoteTweet, stripVineCard); addUrlEntities(spannable, combined, strippedEntity, linkListener, linkColor, linkHighlightColor); return trimEnd(spannable); } private  TweetTextLinkifier();   }
@Test public void testLinkifyUrls_nullFormattedTweetText() { try { TweetTextLinkifier.linkifyUrls(null, null, 0, 0, true, true); } catch (Exception e) { fail("threw unexpected exception"); } }
@Test public void testLinkifyUrls_newFormattedTweetText() { try { TweetTextLinkifier.linkifyUrls(new FormattedTweetText(), null, 0, 0, true, true); } catch (Exception e) { fail("threw unexpected exception"); } }
@Test public void testLinkifyUrls_oneUrlEntity() { final String url = "http: final String displayUrl = "dev.twitter.com"; final String fullText = BASE_TEXT + " " + "http: final UrlEntity urlEntity = EntityFactory.newUrlEntity(fullText, url, displayUrl); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.urlEntities.add(FormattedUrlEntity.createFormattedUrlEntity(urlEntity)); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); final String displayUrlFromEntity = linkifiedText.subSequence(urlEntity.getStart(), urlEntity.getEnd()).toString(); assertEquals(urlEntity.displayUrl, displayUrlFromEntity); }
@Test public void testLinkifyUrls_oneInvalidUrlEntity() { final String fullText = ""; final UrlEntity urlEntity = new UrlEntity("x z", "y", "z", -1, 30); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.urlEntities.add(FormattedUrlEntity.createFormattedUrlEntity(urlEntity)); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); assertEquals("", linkifiedText.toString()); }
@Test public void testLinkifyUrls_linkClickListener() { final String url = "http: final String displayUrl = "dev.twitter.com"; final String fullText = BASE_TEXT + " " + "http: final LinkClickListener mockClickListener = mock(LinkClickListener.class); final UrlEntity urlEntity = EntityFactory.newUrlEntity(fullText, url, displayUrl); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.urlEntities.add(FormattedUrlEntity.createFormattedUrlEntity(urlEntity)); final SpannableStringBuilder linkifiedText = (SpannableStringBuilder) TweetTextLinkifier.linkifyUrls(formattedText, mockClickListener, 0, 0, true, true); final ClickableSpan[] clickables = linkifiedText.getSpans(urlEntity.getStart(), urlEntity.getEnd(), ClickableSpan.class); assertEquals(1, clickables.length); }
@Test public void testLinkifyHashtags_oneHashtagEntity() { final String hashtag = "TwitterForGood"; final String fullHashtag = "#" + hashtag; final String fullText = BASE_TEXT + " " + fullHashtag; final HashtagEntity hashtagEntity = EntityFactory.newHashtagEntity(fullText, hashtag); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.hashtagEntities.add(FormattedUrlEntity.createFormattedUrlEntity( hashtagEntity)); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); final String displayUrlFromEntity = linkifiedText.subSequence(hashtagEntity.getStart(), hashtagEntity.getEnd()).toString(); assertEquals(fullHashtag, displayUrlFromEntity); }
@Test public void testLinkifyHashtags_linkClickListener() { final String hashtag = "TwitterForGood"; final String fullText = BASE_TEXT + " #" + hashtag; final LinkClickListener mockClickListener = mock(LinkClickListener.class); final HashtagEntity hashtagEntity = EntityFactory.newHashtagEntity(fullText, hashtag); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.hashtagEntities.add(FormattedUrlEntity.createFormattedUrlEntity( hashtagEntity)); final SpannableStringBuilder linkifiedText = (SpannableStringBuilder) TweetTextLinkifier.linkifyUrls(formattedText, mockClickListener, 0, 0, true, true); final ClickableSpan[] clickables = linkifiedText.getSpans(hashtagEntity.getStart(), hashtagEntity.getEnd(), ClickableSpan.class); assertEquals(1, clickables.length); }
@Test public void testLinkifyMentions_oneMentionEntity() { final String mention = "TwitterDev"; final String fullMention = "@" + mention; final String fullText = BASE_TEXT + " " + fullMention; final MentionEntity mentionEntity = EntityFactory.newMentionEntity(fullText, mention); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.mentionEntities.add(FormattedUrlEntity.createFormattedUrlEntity( mentionEntity)); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); final String displayUrlFromEntity = linkifiedText.subSequence(mentionEntity.getStart(), mentionEntity.getEnd()).toString(); assertEquals(fullMention, displayUrlFromEntity); }
@Test public void testLinkifyMentions_linkClickListener() { final String mention = "TwitterDev"; final String fullText = BASE_TEXT + " @" + mention; final LinkClickListener mockClickListener = mock(LinkClickListener.class); final MentionEntity mentionEntity = EntityFactory.newMentionEntity(fullText, mention); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.mentionEntities.add(FormattedUrlEntity.createFormattedUrlEntity( mentionEntity)); final SpannableStringBuilder linkifiedText = (SpannableStringBuilder) TweetTextLinkifier.linkifyUrls(formattedText, mockClickListener, 0, 0, true, true); final ClickableSpan[] clickables = linkifiedText.getSpans(mentionEntity.getStart(), mentionEntity.getEnd(), ClickableSpan.class); assertEquals(1, clickables.length); }
@Test public void testLinkifySymbols_oneSymbolEntity() { final String symbol = "TWTR"; final String fullSymbol = "$" + symbol; final String fullText = BASE_TEXT + " " + fullSymbol; final SymbolEntity symbolEntity = EntityFactory.newSymbolEntity(fullText, symbol); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.symbolEntities.add(FormattedUrlEntity.createFormattedUrlEntity( symbolEntity)); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); final String displayUrlFromEntity = linkifiedText.subSequence(symbolEntity.getStart(), symbolEntity.getEnd()).toString(); assertEquals(fullSymbol, displayUrlFromEntity); }
@Test public void testLinkifySymbols_linkClickListener() { final String symbol = "TWTR"; final String fullText = BASE_TEXT + " $" + symbol; final LinkClickListener mockClickListener = mock(LinkClickListener.class); final SymbolEntity symbolEntity = EntityFactory.newSymbolEntity(fullText, symbol); final FormattedTweetText formattedText = new FormattedTweetText(); formattedText.text = fullText; formattedText.symbolEntities.add(FormattedUrlEntity.createFormattedUrlEntity( symbolEntity)); final SpannableStringBuilder linkifiedText = (SpannableStringBuilder) TweetTextLinkifier.linkifyUrls(formattedText, mockClickListener, 0, 0, true, true); final ClickableSpan[] clickables = linkifiedText.getSpans(symbolEntity.getStart(), symbolEntity.getEnd(), ClickableSpan.class); assertEquals(1, clickables.length); }
@Test public void testLinkifyUrls_verifyPhotoOnlyStrippedFromEnd() { final FormattedTweetText formattedText = setupPicTwitterEntities(); final FormattedMediaEntity lastPhotoUrl = formattedText.mediaEntities.get(0); final CharSequence linkifiedText = TweetTextLinkifier.linkifyUrls(formattedText, null, 0, 0, true, true); assertEquals("photo", lastPhotoUrl.type); assertTrue(linkifiedText.toString().contains(lastPhotoUrl.displayUrl)); }