• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.mockito.Spy;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class LinkifySummaryPreferenceTest {
47     @Spy
48     private PreferenceViewHolder mViewHolder;
49     @Mock
50     private TextView mSummaryTextView;
51     private LinkifySummaryPreference mPreference;
52 
53     @Before
setUp()54     public void setUp() {
55         MockitoAnnotations.initMocks(this);
56 
57         final Context context = RuntimeEnvironment.application;
58         mPreference = new LinkifySummaryPreference(context, null /* attrs */);
59 
60         final View view = spy(View.inflate(context, mPreference.getLayoutResource(),
61                 null /* root */));
62         mViewHolder = spy(PreferenceViewHolder.createInstanceForTests(view));
63         doReturn(mSummaryTextView).when(mViewHolder).findViewById(android.R.id.summary);
64     }
65 
66     @Test
onBindViewHolder_summaryTextViewGone_shouldNotSetMovementMethod()67     public void onBindViewHolder_summaryTextViewGone_shouldNotSetMovementMethod() {
68         when(mSummaryTextView.getVisibility()).thenReturn(View.GONE);
69 
70         mPreference.onBindViewHolder(mViewHolder);
71 
72         verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance());
73     }
74 
75     @Test
onBindViewHolder_noLinkSummary_shouldNotSetMovementMethod()76     public void onBindViewHolder_noLinkSummary_shouldNotSetMovementMethod() {
77         when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE);
78         final CharSequence seondSummary = "secondSummary";
79         mPreference.setSummary(seondSummary);
80 
81         mPreference.onBindViewHolder(mViewHolder);
82 
83         verify(mSummaryTextView, never()).setMovementMethod(LinkMovementMethod.getInstance());
84     }
85 
86     @Test
onBindViewHolder_linkedSummary_shouldSetMovementMethod()87     public void onBindViewHolder_linkedSummary_shouldSetMovementMethod() {
88         when(mSummaryTextView.getVisibility()).thenReturn(View.VISIBLE);
89         final CharSequence seondSummary = "secondSummary";
90         final SpannableStringBuilder summaryBuilder = new SpannableStringBuilder();
91         summaryBuilder.append(seondSummary, new URLSpan("" /* url */),
92                 Spanned.SPAN_INCLUSIVE_INCLUSIVE);
93         mPreference.setSummary(summaryBuilder);
94 
95         mPreference.onBindViewHolder(mViewHolder);
96 
97         verify(mSummaryTextView).setMovementMethod(any(LinkMovementMethod.class));
98     }
99 }
100