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