1 /*
2  * Copyright (C) 2020 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 package com.android.settings;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.text.Spannable;
21 import android.text.style.ClickableSpan;
22 import android.widget.TextView;
23 
24 import androidx.test.core.app.ApplicationProvider;
25 import androidx.test.ext.junit.runners.AndroidJUnit4;
26 
27 import org.junit.Before;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(AndroidJUnit4.class)
33 @Ignore("b/340657656")
34 public class LinkifyUtilsTest {
35     private static final String TEST_STRING = "to LINK_BEGINscanning settingsLINK_END.";
36     private static final String WRONG_STRING = "to scanning settingsLINK_END.";
37     private final LinkifyUtils.OnClickListener mClickListener = () -> { /* Do nothing */ };
38 
39     private StringBuilder mSpanStringBuilder;
40     private StringBuilder mWrongSpanStringBuilder;
41     TextView mTextView;
42 
43     @Before
setUp()44     public void setUp() throws Exception {
45         mSpanStringBuilder = new StringBuilder(TEST_STRING);
46         mWrongSpanStringBuilder = new StringBuilder(WRONG_STRING);
47         mTextView = new TextView(ApplicationProvider.getApplicationContext());
48     }
49 
50     @Test
linkify_whenSpanStringCorrect_shouldReturnTrue()51     public void linkify_whenSpanStringCorrect_shouldReturnTrue() {
52         final boolean linkifyResult = LinkifyUtils.linkify(mTextView, mSpanStringBuilder,
53                 mClickListener);
54 
55         assertThat(linkifyResult).isTrue();
56     }
57 
58     @Test
linkify_whenSpanStringWrong_shouldReturnFalse()59     public void linkify_whenSpanStringWrong_shouldReturnFalse() {
60         final boolean linkifyResult = LinkifyUtils.linkify(mTextView, mWrongSpanStringBuilder,
61                 mClickListener);
62 
63         assertThat(linkifyResult).isFalse();
64     }
65 
66     @Test
linkify_whenSpanStringCorrect_shouldContainClickableSpan()67     public void linkify_whenSpanStringCorrect_shouldContainClickableSpan() {
68         LinkifyUtils.linkify(mTextView, mSpanStringBuilder, mClickListener);
69         final Spannable spannableContent = (Spannable) mTextView.getText();
70         final int len = spannableContent.length();
71         final Object[] spans = spannableContent.getSpans(0, len, Object.class);
72 
73         assertThat(spans[1] instanceof ClickableSpan).isTrue();
74     }
75 }
76