Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.text.util;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 
     23 import android.content.Context;
     24 import android.content.res.Configuration;
     25 import android.os.LocaleList;
     26 import android.support.test.InstrumentationRegistry;
     27 import android.support.test.filters.SmallTest;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.text.Spannable;
     30 import android.text.SpannableString;
     31 import android.text.method.LinkMovementMethod;
     32 import android.text.style.URLSpan;
     33 import android.util.Patterns;
     34 import android.widget.TextView;
     35 
     36 import org.junit.After;
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 import java.util.Locale;
     42 
     43 /**
     44  * LinkifyTest tests {@link Linkify}.
     45  */
     46 @SmallTest
     47 @RunWith(AndroidJUnit4.class)
     48 public class LinkifyTest {
     49 
     50     private static final LocaleList LOCALE_LIST_US = new LocaleList(Locale.US);
     51     private LocaleList mOriginalLocales;
     52     private Context mContext;
     53 
     54     @Before
     55     public void setup() {
     56         mContext = InstrumentationRegistry.getContext();
     57         mOriginalLocales = LocaleList.getDefault();
     58         LocaleList.setDefault(LOCALE_LIST_US);
     59     }
     60 
     61     @After
     62     public void teardown() {
     63         LocaleList.setDefault(mOriginalLocales);
     64     }
     65 
     66     @Test
     67     public void testNothing() {
     68         TextView tv;
     69 
     70         tv = new TextView(createUsEnglishContext());
     71         tv.setText("Hey, foo (at) google.com, call 415-555-1212.");
     72 
     73         assertFalse(tv.getMovementMethod() instanceof LinkMovementMethod);
     74         assertTrue(tv.getUrls().length == 0);
     75     }
     76 
     77     @Test
     78     public void testNormal() {
     79         TextView tv;
     80 
     81         tv = new TextView(createUsEnglishContext());
     82         tv.setAutoLinkMask(Linkify.ALL);
     83         tv.setText("Hey, foo (at) google.com, call +1-415-555-1212.");
     84 
     85         assertTrue(tv.getMovementMethod() instanceof LinkMovementMethod);
     86         assertTrue(tv.getUrls().length == 2);
     87     }
     88 
     89     @Test
     90     public void testUnclickable() {
     91         TextView tv;
     92 
     93         tv = new TextView(createUsEnglishContext());
     94         tv.setAutoLinkMask(Linkify.ALL);
     95         tv.setLinksClickable(false);
     96         tv.setText("Hey, foo (at) google.com, call +1-415-555-1212.");
     97 
     98         assertFalse(tv.getMovementMethod() instanceof LinkMovementMethod);
     99         assertTrue(tv.getUrls().length == 2);
    100     }
    101 
    102     private Context createUsEnglishContext() {
    103         final Configuration overrideConfig = new Configuration();
    104         overrideConfig.setLocales(LOCALE_LIST_US);
    105         return mContext.createConfigurationContext(overrideConfig);
    106     }
    107 
    108     @Test
    109     public void testAddLinks_addsLinksWhenDefaultSchemeIsNull() {
    110         Spannable spannable = new SpannableString("any https://android.com any android.com any");
    111         Linkify.addLinks(spannable, Patterns.AUTOLINK_WEB_URL, null, null, null);
    112 
    113         URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    114         assertEquals("android.com and https://android.com should be linkified", 2, spans.length);
    115         assertEquals("https://android.com", spans[0].getURL());
    116         assertEquals("android.com", spans[1].getURL());
    117     }
    118 
    119     @Test
    120     public void testAddLinks_addsLinksWhenSchemesArrayIsNull() {
    121         Spannable spannable = new SpannableString("any https://android.com any android.com any");
    122         Linkify.addLinks(spannable, Patterns.AUTOLINK_WEB_URL, "http://", null, null);
    123 
    124         URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    125         assertEquals("android.com and https://android.com should be linkified", 2, spans.length);
    126         // expected behavior, passing null schemes array means: prepend defaultScheme to all links.
    127         assertEquals("http://https://android.com", spans[0].getURL());
    128         assertEquals("http://android.com", spans[1].getURL());
    129     }
    130 
    131     @Test
    132     public void testAddLinks_prependsDefaultSchemeToBeginingOfLink() {
    133         Spannable spannable = new SpannableString("any android.com any");
    134         Linkify.addLinks(spannable, Patterns.AUTOLINK_WEB_URL, "http://",
    135                 new String[] { "http://", "https://"}, null, null);
    136 
    137         URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    138         assertEquals("android.com should be linkified", 1, spans.length);
    139         assertEquals("http://android.com", spans[0].getURL());
    140     }
    141 
    142     @Test
    143     public void testAddLinks_doesNotPrependSchemeIfSchemeExists() {
    144         Spannable spannable = new SpannableString("any https://android.com any");
    145         Linkify.addLinks(spannable, Patterns.AUTOLINK_WEB_URL, "http://",
    146                 new String[] { "http://", "https://"}, null, null);
    147 
    148         URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
    149         assertEquals("android.com should be linkified", 1, spans.length);
    150         assertEquals("https://android.com", spans[0].getURL());
    151     }
    152 }
    153