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 17 package com.android.settings.widget; 18 19 import static org.mockito.ArgumentMatchers.any; 20 import static org.mockito.Mockito.doReturn; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.text.SpannableStringBuilder; 28 import android.text.Spanned; 29 import android.text.method.LinkMovementMethod; 30 import android.text.style.URLSpan; 31 import android.view.View; 32 import android.widget.TextView; 33 34 import androidx.preference.PreferenceViewHolder; 35 import androidx.test.core.app.ApplicationProvider; 36 37 import org.junit.Before; 38 import org.junit.Rule; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Mock; 42 import org.mockito.junit.MockitoJUnit; 43 import org.mockito.junit.MockitoRule; 44 import org.robolectric.RobolectricTestRunner; 45 46 @RunWith(RobolectricTestRunner.class) 47 public class LinkifySummaryPreferenceTest { 48 @Rule 49 public final MockitoRule mMockitoRule = MockitoJUnit.rule(); 50 51 private PreferenceViewHolder mViewHolder; 52 @Mock 53 private TextView mSummaryTextView; 54 private LinkifySummaryPreference mPreference; 55 56 @Before setUp()57 public void setUp() { 58 final Context context = ApplicationProvider.getApplicationContext(); 59 mPreference = new LinkifySummaryPreference(context, null /* attrs */); 60 61 final View view = spy(View.inflate(context, mPreference.getLayoutResource(), 62 null /* root */)); 63 mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(view)); 64 doReturn(mSummaryTextView).when(mViewHolder).findViewById(android.R.id.summary); 65 } 66 67 @Test onBindViewHolder_summaryTextViewGone_shouldNotSetMovementMethod()68 public void onBindViewHolder_summaryTextViewGone_shouldNotSetMovementMethod() { 69 when(mSummaryTextView.getVisibility()).thenReturn(View.GONE); 70 71 mPreference.onBindViewHolder(mViewHolder); 72 73 verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance()); 74 } 75 76 @Test onBindViewHolder_noLinkSummary_shouldNotSetMovementMethod()77 public void onBindViewHolder_noLinkSummary_shouldNotSetMovementMethod() { 78 when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE); 79 final CharSequence seondSummary = "secondSummary"; 80 mPreference.setSummary(seondSummary); 81 82 mPreference.onBindViewHolder(mViewHolder); 83 84 verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance()); 85 } 86 87 @Test onBindViewHolder_linkedSummary_shouldSetMovementMethod()88 public void onBindViewHolder_linkedSummary_shouldSetMovementMethod() { 89 when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE); 90 final CharSequence seondSummary = "secondSummary"; 91 final SpannableStringBuilder summaryBuilder = new SpannableStringBuilder(); 92 summaryBuilder.append(seondSummary, new URLSpan("" /* url */), 93 Spanned.SPAN_INCLUSIVE_INCLUSIVE); 94 mPreference.setSummary(summaryBuilder); 95 96 mPreference.onBindViewHolder(mViewHolder); 97 98 verify(mSummaryTextView).setMovementMethod(any(LinkMovementMethod.class)); 99 } 100 } 101