checkAvailablePrefs(getPreferenceScreen());
}
- private void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
+ @VisibleForTesting
+ void checkAvailablePrefs(PreferenceGroup preferenceGroup) {
if (preferenceGroup == null) return;
for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
Preference pref = preferenceGroup.getPreference(i);
if (pref instanceof SelfAvailablePreference
&& !((SelfAvailablePreference) pref).isAvailable(getContext())) {
- preferenceGroup.removePreference(pref);
+ pref.setVisible(false);
} else if (pref instanceof PreferenceGroup) {
checkAvailablePrefs((PreferenceGroup) pref);
}
import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
+import com.android.settings.widget.WorkOnlyCategory;
import org.junit.Before;
import org.junit.Test;
// no crash
}
+ @Test
+ public void checkAvailablePrefs_selfAvialbalePreferenceNotAvailable_shouldHidePreference() {
+ doReturn(mPreferenceScreen).when(mFragment).getPreferenceScreen();
+ final WorkOnlyCategory workOnlyCategory = mock(WorkOnlyCategory.class);
+ when(mPreferenceScreen.getPreferenceCount()).thenReturn(1);
+ when(mPreferenceScreen.getPreference(0)).thenReturn(workOnlyCategory);
+ when(workOnlyCategory.isAvailable(any(Context.class))).thenReturn(false);
+
+ mFragment.checkAvailablePrefs(mPreferenceScreen);
+
+ verify(mPreferenceScreen, never()).removePreference(workOnlyCategory);
+ verify(workOnlyCategory).setVisible(false);
+ }
+
public static class TestFragment extends SettingsPreferenceFragment {
@Override