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 org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.robolectric.RobolectricTestRunner;
28 import org.robolectric.RuntimeEnvironment;
29 
30 @RunWith(RobolectricTestRunner.class)
31 public class LinkifyUtilsTest {
32     private static final String TEST_STRING = "to LINK_BEGINscanning settingsLINK_END.";
33     private static final String WRONG_STRING = "to scanning settingsLINK_END.";
34     private final LinkifyUtils.OnClickListener mClickListener = () -> { /* Do nothing */ };
35 
36     private StringBuilder mSpanStringBuilder;
37     private StringBuilder mWrongSpanStringBuilder;
38     TextView mTextView;
39 
40     @Before
setUp()41     public void setUp() throws Exception {
42         mSpanStringBuilder = new StringBuilder(TEST_STRING);
43         mWrongSpanStringBuilder = new StringBuilder(WRONG_STRING);
44         mTextView = new TextView(RuntimeEnvironment.application);
45     }
46 
47     @Test
linkify_whenSpanStringCorrect_shouldReturnTrue()48     public void linkify_whenSpanStringCorrect_shouldReturnTrue() {
49         final boolean linkifyResult = LinkifyUtils.linkify(mTextView, mSpanStringBuilder,
50                 mClickListener);
51 
52         assertThat(linkifyResult).isTrue();
53     }
54 
55     @Test
linkify_whenSpanStringWrong_shouldReturnFalse()56     public void linkify_whenSpanStringWrong_shouldReturnFalse() {
57         final boolean linkifyResult = LinkifyUtils.linkify(mTextView, mWrongSpanStringBuilder,
58                 mClickListener);
59 
60         assertThat(linkifyResult).isFalse();
61     }
62 
63     @Test
linkify_whenSpanStringCorrect_shouldContainClickableSpan()64     public void linkify_whenSpanStringCorrect_shouldContainClickableSpan() {
65         LinkifyUtils.linkify(mTextView, mSpanStringBuilder, mClickListener);
66         final Spannable spannableContent = (Spannable) mTextView.getText();
67         final int len = spannableContent.length();
68         final Object[] spans = spannableContent.getSpans(0, len, Object.class);
69 
70         assertThat(spans[1] instanceof ClickableSpan).isTrue();
71     }
72 }
73