1 /* 2 * Copyright (C) 2022 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.android.settings.accessibility.FontWeightAdjustmentPreferenceController.BOLD_TEXT_ADJUSTMENT; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.mock; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.app.settings.SettingsEnums; 30 import android.content.Context; 31 import android.content.DialogInterface; 32 import android.provider.Settings; 33 34 import androidx.appcompat.app.AlertDialog; 35 import androidx.fragment.app.FragmentActivity; 36 import androidx.preference.PreferenceManager; 37 import androidx.test.core.app.ApplicationProvider; 38 39 import com.android.settings.R; 40 import com.android.settings.accessibility.AccessibilityDialogUtils.DialogEnums; 41 import com.android.settings.accessibility.TextReadingResetController.ResetStateListener; 42 import com.android.settings.testutils.XmlTestUtils; 43 44 import org.junit.Before; 45 import org.junit.Rule; 46 import org.junit.Test; 47 import org.junit.runner.RunWith; 48 import org.mockito.Answers; 49 import org.mockito.Mock; 50 import org.mockito.junit.MockitoJUnit; 51 import org.mockito.junit.MockitoRule; 52 import org.robolectric.Robolectric; 53 import org.robolectric.RobolectricTestRunner; 54 import org.robolectric.annotation.Config; 55 import org.robolectric.shadows.ShadowLooper; 56 import org.robolectric.shadows.ShadowToast; 57 58 import java.util.ArrayList; 59 import java.util.Arrays; 60 import java.util.List; 61 62 /** Tests for {@link TextReadingPreferenceFragment}. */ 63 @RunWith(RobolectricTestRunner.class) 64 @Config(shadows = { 65 com.android.settings.testutils.shadow.ShadowFragment.class, 66 }) 67 public class TextReadingPreferenceFragmentTest { 68 69 @Rule 70 public final MockitoRule mMockito = MockitoJUnit.rule(); 71 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 72 private PreferenceManager mPreferenceManager; 73 private Context mContext = ApplicationProvider.getApplicationContext(); 74 private TextReadingPreferenceFragment mFragment; 75 76 @Before setUp()77 public void setUp() { 78 mContext.setTheme(androidx.appcompat.R.style.Theme_AppCompat); 79 80 mFragment = spy(new TextReadingPreferenceFragment()); 81 when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager); 82 when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext); 83 when(mFragment.getContext()).thenReturn(mContext); 84 when(mFragment.getActivity()).thenReturn(Robolectric.setupActivity(FragmentActivity.class)); 85 86 // Avoid a NPE is happened in ShadowWindowManagerGlobal 87 doReturn(mock(DisplaySizeData.class)).when(mFragment).createDisplaySizeData(mContext); 88 mFragment.createPreferenceControllers(mContext); 89 } 90 91 @Test onDialogPositiveButtonClicked_boldTextEnabled_needResetSettings()92 public void onDialogPositiveButtonClicked_boldTextEnabled_needResetSettings() { 93 Settings.Secure.putInt(mContext.getContentResolver(), 94 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, BOLD_TEXT_ADJUSTMENT); 95 final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog( 96 DialogEnums.DIALOG_RESET_SETTINGS); 97 dialog.show(); 98 99 dialog.getButton(DialogInterface.BUTTON_POSITIVE).callOnClick(); 100 ShadowLooper.idleMainLooper(); 101 102 assertThat(mFragment.mNeedResetSettings).isTrue(); 103 } 104 105 @Test onDialogPositiveButtonClicked_boldTextDisabled_resetAllListeners()106 public void onDialogPositiveButtonClicked_boldTextDisabled_resetAllListeners() { 107 final ResetStateListener listener1 = mock(ResetStateListener.class); 108 final ResetStateListener listener2 = mock(ResetStateListener.class); 109 mFragment.mResetStateListeners = new ArrayList<>(Arrays.asList(listener1, listener2)); 110 final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog( 111 DialogEnums.DIALOG_RESET_SETTINGS); 112 dialog.show(); 113 114 dialog.getButton(DialogInterface.BUTTON_POSITIVE).callOnClick(); 115 ShadowLooper.idleMainLooper(); 116 117 verify(listener1).resetState(); 118 verify(listener2).resetState(); 119 } 120 121 @Test onDialogPositiveButtonClicked_boldTextEnabled_showToast()122 public void onDialogPositiveButtonClicked_boldTextEnabled_showToast() { 123 Settings.Secure.putInt(mContext.getContentResolver(), 124 Settings.Secure.FONT_WEIGHT_ADJUSTMENT, BOLD_TEXT_ADJUSTMENT); 125 final AlertDialog dialog = (AlertDialog) mFragment.onCreateDialog( 126 DialogEnums.DIALOG_RESET_SETTINGS); 127 dialog.show(); 128 129 dialog.getButton(DialogInterface.BUTTON_POSITIVE).callOnClick(); 130 ShadowLooper.idleMainLooper(); 131 132 assertThat(ShadowToast.getTextOfLatestToast()) 133 .isEqualTo(mContext.getString(R.string.accessibility_text_reading_reset_message)); 134 } 135 136 @Test getMetricsCategory_returnsCorrectCategory()137 public void getMetricsCategory_returnsCorrectCategory() { 138 assertThat(mFragment.getMetricsCategory()).isEqualTo( 139 SettingsEnums.ACCESSIBILITY_TEXT_READING_OPTIONS); 140 } 141 142 @Test getPreferenceScreenResId_returnsCorrectXml()143 public void getPreferenceScreenResId_returnsCorrectXml() { 144 assertThat(mFragment.getPreferenceScreenResId()).isEqualTo( 145 R.xml.accessibility_text_reading_options); 146 } 147 148 @Test getLogTag_returnsCorrectTag()149 public void getLogTag_returnsCorrectTag() { 150 assertThat(mFragment.getLogTag()).isEqualTo("TextReadingPreferenceFragment"); 151 } 152 153 @Test getNonIndexableKeys_existInXmlLayout()154 public void getNonIndexableKeys_existInXmlLayout() { 155 final List<String> niks = TextReadingPreferenceFragment.SEARCH_INDEX_DATA_PROVIDER 156 .getNonIndexableKeys(mContext); 157 final List<String> keys = 158 XmlTestUtils.getKeysFromPreferenceXml(mContext, 159 R.xml.accessibility_text_reading_options); 160 161 assertThat(keys).containsAtLeastElementsIn(niks); 162 } 163 } 164