Refactor CaptionAppearanceFragment to improve maintainability (5/n)
Root cause: There is a bunch of different logic of preferences in CaptionAppearanceFragment. It’s hard to implement new features and hard to maintain and hard to be testable. Solution: Move out custom perference visiblity logic of CaptionAppearanceFragment into controllers to reduce the complexity of the relationship between preference and fragment. Bug: 197695932 Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings.accessibility Change-Id: I53d4ca3809031587f8d6853b427774fd322a71d8
This commit is contained in:
@@ -18,37 +18,18 @@ package com.android.settings.accessibility;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.provider.Settings;
|
||||
import android.view.accessibility.CaptioningManager.CaptionStyle;
|
||||
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.testutils.XmlTestUtils;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -56,76 +37,12 @@ import java.util.List;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class CaptionAppearanceFragmentTest {
|
||||
|
||||
@Rule
|
||||
public MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
@Mock
|
||||
private SettingsActivity mActivity;
|
||||
@Mock
|
||||
private PreferenceScreen mScreen;
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@Mock
|
||||
private ContentResolver mContentResolver;
|
||||
@Mock
|
||||
private PreferenceCategory mCustomPref;
|
||||
@Spy
|
||||
private Context mContext = ApplicationProvider.getApplicationContext();
|
||||
private TestCaptionAppearanceFragment mFragment;
|
||||
private final Context mContext = ApplicationProvider.getApplicationContext();
|
||||
private CaptionAppearanceFragment mFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mFragment = spy(new TestCaptionAppearanceFragment());
|
||||
doReturn(mActivity).when(mFragment).getActivity();
|
||||
doReturn(mContext).when(mFragment).getContext();
|
||||
doReturn(mCustomPref).when(mFragment).findPreference(mFragment.PREF_CUSTOM);
|
||||
when(mPreferenceManager.getPreferenceScreen()).thenReturn(mScreen);
|
||||
ReflectionHelpers.setField(mFragment, "mPreferenceManager", mPreferenceManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreatePreferences_shouldPreferenceIsInvisible() {
|
||||
mFragment.onAttach(mContext);
|
||||
|
||||
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
|
||||
|
||||
verify(mCustomPref).setVisible(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreatePreferences_customValue_shouldPreferenceIsVisible() {
|
||||
Settings.Secure.putInt(mContext.getContentResolver(),
|
||||
Settings.Secure.ACCESSIBILITY_CAPTIONING_PRESET, CaptionStyle.PRESET_CUSTOM);
|
||||
mFragment.onAttach(mContext);
|
||||
|
||||
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
|
||||
|
||||
verify(mCustomPref).setVisible(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStart_registerSpecificContentObserverForSpecificKeys() {
|
||||
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
||||
mFragment.onAttach(mContext);
|
||||
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
|
||||
|
||||
mFragment.onStart();
|
||||
|
||||
for (String key : mFragment.CAPTIONING_FEATURE_KEYS) {
|
||||
verify(mContentResolver).registerContentObserver(Settings.Secure.getUriFor(key),
|
||||
/* notifyForDescendants= */ false, mFragment.mSettingsContentObserver);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStop_unregisterContentObserver() {
|
||||
when(mContext.getContentResolver()).thenReturn(mContentResolver);
|
||||
mFragment.onAttach(mContext);
|
||||
mFragment.onCreatePreferences(Bundle.EMPTY, /* rootKey */ null);
|
||||
mFragment.onStart();
|
||||
|
||||
mFragment.onStop();
|
||||
|
||||
verify(mContentResolver).unregisterContentObserver(mFragment.mSettingsContentObserver);
|
||||
mFragment = new CaptionAppearanceFragment();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,11 +51,21 @@ public class CaptionAppearanceFragmentTest {
|
||||
SettingsEnums.ACCESSIBILITY_CAPTION_APPEARANCE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getPreferenceScreenResId_returnsCorrectXml() {
|
||||
assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(R.xml.captioning_appearance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLogTag_returnsCorrectTag() {
|
||||
assertThat(mFragment.getLogTag()).isEqualTo("CaptionAppearanceFragment");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHelpResource_returnsCorrectHelpResource() {
|
||||
assertThat(mFragment.getHelpResource()).isEqualTo(R.string.help_url_caption);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getNonIndexableKeys_existInXmlLayout() {
|
||||
final List<String> niks = CaptionAppearanceFragment.SEARCH_INDEX_DATA_PROVIDER
|
||||
@@ -148,12 +75,4 @@ public class CaptionAppearanceFragmentTest {
|
||||
|
||||
assertThat(keys).containsAtLeastElementsIn(niks);
|
||||
}
|
||||
|
||||
private static class TestCaptionAppearanceFragment extends CaptionAppearanceFragment {
|
||||
|
||||
@Override
|
||||
public int getPreferenceScreenResId() {
|
||||
return R.xml.placeholder_prefs;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user