LinkifyTest.java

 1package de.gultsch.common;
 2
 3import android.os.Build;
 4import android.text.SpannableStringBuilder;
 5import android.text.style.URLSpan;
 6
 7import eu.siacs.conversations.Conversations;
 8
 9import org.junit.Assert;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12import org.robolectric.RobolectricTestRunner;
13import org.robolectric.annotation.Config;
14import org.robolectric.annotation.ConscryptMode;
15
16@RunWith(RobolectricTestRunner.class)
17@Config(sdk = Build.VERSION_CODES.TIRAMISU, application = Conversations.class)
18@ConscryptMode(ConscryptMode.Mode.OFF)
19public class LinkifyTest {
20    @Test
21    public void addLinksDoesNotLinkifyInvalidUris() {
22        final var text = new SpannableStringBuilder("https://example.com?q=%s");
23        Linkify.addLinks(text);
24        Assert.assertEquals(0, text.getSpans(0, text.length(), URLSpan.class).length);
25    }
26
27    @Test
28    public void malformedEscapeIsRejected() {
29        final var links = Linkify.getLinks("https://example.com/?x=%");
30
31        Assert.assertTrue(links.isEmpty());
32    }
33
34    @Test
35    public void validEscapesStillProduceLinks() {
36        final var links = Linkify.getLinks("https://example.com/?x=%20");
37
38        Assert.assertEquals(1, links.size());
39        Assert.assertEquals("https://example.com/?x=%20", links.get(0).getRaw());
40    }
41
42    @Test
43    public void ipv6HostsStillProduceLinks() {
44        final var links = Linkify.getLinks("http://[::1]/foo");
45
46        Assert.assertEquals(1, links.size());
47        Assert.assertEquals("http://[::1]/foo", links.get(0).getRaw());
48    }
49
50    @Test
51    public void bracketsInPathAreRejected() {
52        final var links = Linkify.getLinks("http://example.com/foo[bar]");
53
54        Assert.assertTrue(links.isEmpty());
55    }
56}