test: added unit tests for the TextMatchers utility class.

- Replaced Patterns with PatternsCompat equivalent for unit testing compatibility
- Removed ExampleUnitTest placeholder test class

Signed-off-by: eho <edvard.holst@gmail.com>
This commit is contained in:
eho 2019-04-10 22:07:50 +02:00
parent f35c4b486c
commit d2aa3a7f50
No known key found for this signature in database
GPG Key ID: C30729EE3C9A3CCE
3 changed files with 92 additions and 20 deletions

View File

@ -22,12 +22,13 @@
package com.nextcloud.talk.utils;
import android.util.Patterns;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import com.vanniktech.emoji.EmojiInformation;
import com.vanniktech.emoji.EmojiUtils;
import androidx.core.util.PatternsCompat;
import eu.medsea.mimeutil.MimeUtil;
import eu.medsea.mimeutil.detector.ExtensionMimeDetector;
import eu.medsea.mimeutil.detector.MagicMimeMimeDetector;
@ -44,7 +45,7 @@ public final class TextMatchers {
public static ChatMessage.MessageType getMessageTypeFromString(@NonNull final String text) {
List<String> links = new ArrayList<>();
Matcher m = Patterns.WEB_URL.matcher(text);
Matcher m = PatternsCompat.WEB_URL.matcher(text);
while (m.find()) {
String url = m.group();
links.add(url);

View File

@ -1,18 +0,0 @@
package com.nextcloud.talk;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void additionIsCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}

View File

@ -0,0 +1,89 @@
package com.nextcloud.talk.utils;
import com.nextcloud.talk.models.json.chat.ChatMessage;
import org.junit.Test;
import static org.junit.Assert.*;
public class TextMatchersTest {
@Test
public void getMessageTypeFromString_assertRegularTextMessageParsing() {
String simpleMessageText = "Hello world! Have a cookie!";
String messageContainingLink = "Hello https://nextcloud.com! Have a good day";
assertEquals(ChatMessage.MessageType.REGULAR_TEXT_MESSAGE,
TextMatchers.getMessageTypeFromString(simpleMessageText));
assertEquals(ChatMessage.MessageType.REGULAR_TEXT_MESSAGE,
TextMatchers.getMessageTypeFromString(messageContainingLink));
}
@Test
public void getMessageTypeFromString_assertSingleLinkMessageParsing() {
String simpleUrlText = "https://nextcloud.com/";
String complexUrlText = "https://docs.nextcloud.com/server/15/admin_manual/#target-audience";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_MESSAGE,
TextMatchers.getMessageTypeFromString(simpleUrlText));
assertEquals(ChatMessage.MessageType.SINGLE_LINK_MESSAGE,
TextMatchers.getMessageTypeFromString(complexUrlText));
}
@Test
public void getMessageTypeFromString_assertSingleLinkImageMessage() {
String simpleImageText = "https://nextcloud.com/image.jpg";
String complexImageUrlText = "https://nextcloud.com/wp-content/themes/next/assets/img/features/mobileDesktop.png?x22777";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE,
TextMatchers.getMessageTypeFromString(simpleImageText));
assertEquals(ChatMessage.MessageType.SINGLE_LINK_IMAGE_MESSAGE,
TextMatchers.getMessageTypeFromString(complexImageUrlText));
}
@Test
public void getMessageTypeFromString_assertSingleLinkGifMessage() {
String gifImageText = "https://nextcloud.com/funny.gif";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_GIF_MESSAGE,
TextMatchers.getMessageTypeFromString(gifImageText));
}
@Test
public void getMessageTypeFromString_assertSingleLinkAudioMessage() {
String wavLink = "https://nextcloud.com/message.wav";
String mp3Link = "https://nextcloud.com/message.mp3";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE,
TextMatchers.getMessageTypeFromString(wavLink));
assertEquals(ChatMessage.MessageType.SINGLE_LINK_AUDIO_MESSAGE,
TextMatchers.getMessageTypeFromString(mp3Link));
}
@Test
public void getMessageTypeFromString_assertSingleLinkVideoMessage() {
String mp4Link = "https://nextcloud.com/message.mp4";
String flvLink = "https://nextcloud.com/message.flv";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE,
TextMatchers.getMessageTypeFromString(mp4Link));
assertEquals(ChatMessage.MessageType.SINGLE_LINK_VIDEO_MESSAGE,
TextMatchers.getMessageTypeFromString(flvLink));
}
@Test
public void getMessageTypeFromString_assertSingleGiphyMessage() {
String giphyLink = "https://media.giphy.com/media/11fucLQCTOdvBS/giphy.gif";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE,
TextMatchers.getMessageTypeFromString(giphyLink));
}
@Test
public void getMessageTypeFromString_assertSingleTenorMessage() {
String tenorLink = "https://media.tenor.com/images/d98e76e3930cf171cc39e301c9e974af/tenor.gif";
assertEquals(ChatMessage.MessageType.SINGLE_LINK_TENOR_MESSAGE,
TextMatchers.getMessageTypeFromString(tenorLink));
}
}