1 /*
2  * Copyright (C) 2021 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.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.TextView;
27 
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.PreferenceViewHolder;
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.settings.R;
33 
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.Spy;
40 import org.mockito.junit.MockitoJUnit;
41 import org.mockito.junit.MockitoRule;
42 import org.robolectric.RobolectricTestRunner;
43 
44 /**
45  * Tests for {@link AccessibilityFooterPreferenceController}.
46  */
47 @RunWith(RobolectricTestRunner.class)
48 public class AccessibilityFooterPreferenceControllerTest {
49 
50     private static final String TEST_KEY = "test_pref_key";
51     private static final String TEST_TITLE = "test_title";
52     private static final String TEST_INTRODUCTION_TITLE = "test_introduction_title";
53     private static final String TEST_CONTENT_DESCRIPTION = "test_content_description";
54     private static final int TEST_HELP_ID = 12345;
55 
56     @Rule
57     public final MockitoRule mockito = MockitoJUnit.rule();
58 
59     @Spy
60     private final Context mContext = ApplicationProvider.getApplicationContext();
61     @Mock
62     private PreferenceScreen mScreen;
63     private AccessibilityFooterPreferenceController mController;
64     private AccessibilityFooterPreference mPreference;
65     private PreferenceViewHolder mPreferenceViewHolder;
66 
67     @Before
setUp()68     public void setUp() {
69         mController = new AccessibilityFooterPreferenceController(mContext, TEST_KEY);
70         mPreference = new AccessibilityFooterPreference(mContext);
71         mPreference.setKey(TEST_KEY);
72         mPreference.setTitle(TEST_TITLE);
73 
74         final LayoutInflater inflater = LayoutInflater.from(mContext);
75         final View view = inflater.inflate(
76                 com.android.settingslib.widget.preference.footer.R.layout.preference_footer, null);
77         mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(view);
78         mPreference.onBindViewHolder(mPreferenceViewHolder);
79 
80         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
81     }
82 
83     @Test
setIntroductionTitle_setCorrectIntroductionTitle()84     public void setIntroductionTitle_setCorrectIntroductionTitle() {
85         mController.setIntroductionTitle(TEST_INTRODUCTION_TITLE);
86 
87         assertThat(mController.getIntroductionTitle()).isEqualTo(TEST_INTRODUCTION_TITLE);
88     }
89 
90     @Test
onBindViewHolder_setIntroductionTitle_setCorrectIntroductionTitle()91     public void onBindViewHolder_setIntroductionTitle_setCorrectIntroductionTitle() {
92         mController.setIntroductionTitle(TEST_INTRODUCTION_TITLE);
93         mController.displayPreference(mScreen);
94 
95         mPreference.onBindViewHolder(mPreferenceViewHolder);
96 
97         final TextView summaryView = (TextView) mPreferenceViewHolder
98                 .findViewById(android.R.id.title);
99         assertThat(summaryView.getContentDescription().toString())
100                 .contains(TEST_INTRODUCTION_TITLE);
101     }
102 
103     @Test
setupHelpLink_setCorrectHelpLinkAndLearnMoreText()104     public void setupHelpLink_setCorrectHelpLinkAndLearnMoreText() {
105         mController.setupHelpLink(TEST_HELP_ID, TEST_CONTENT_DESCRIPTION);
106 
107         assertThat(mController.getHelpResource()).isEqualTo(TEST_HELP_ID);
108         assertThat(mController.getLearnMoreText())
109                 .isEqualTo(TEST_CONTENT_DESCRIPTION);
110     }
111 
112     @Test
onBindViewHolder_setHelpResource_emptyString_notVisible()113     public void onBindViewHolder_setHelpResource_emptyString_notVisible() {
114         mController.setupHelpLink(R.string.help_url_timeout, TEST_CONTENT_DESCRIPTION);
115         mController.displayPreference(mScreen);
116 
117         mPreference.onBindViewHolder(mPreferenceViewHolder);
118 
119         final TextView learnMoreView = (TextView) mPreferenceViewHolder
120                 .findViewById(com.android.settingslib.widget.preference.footer.R.id.settingslib_learn_more);
121         assertThat(learnMoreView.getContentDescription()).isNull();
122         assertThat(learnMoreView.getVisibility()).isEqualTo(View.GONE);
123         assertThat(mPreference.isLinkEnabled()).isFalse();
124     }
125 
126     @Test
onBindViewHolder_setHelpResource_expectSummaryViewIsNonFocusable()127     public void onBindViewHolder_setHelpResource_expectSummaryViewIsNonFocusable() {
128         mController.setupHelpLink(R.string.help_url_timeout, TEST_CONTENT_DESCRIPTION);
129         mController.displayPreference(mScreen);
130 
131         mPreference.onBindViewHolder(mPreferenceViewHolder);
132 
133         final TextView summaryView = (TextView) mPreferenceViewHolder
134                 .findViewById(android.R.id.title);
135         assertThat(summaryView.isFocusable()).isFalse();
136     }
137 }
138