Fix crash for UiBlockerController

1. Don't create UiBlockerController if there is no related controller
2. Don't update visibility if UiBlockerController is null
3. Use findPreference() from DashboardFragment, which already has null
check.

Change-Id: Iee24c64317fb9d5a1cf2076d25728af485d390c5
Fixes: 122807414
Fixes: 122805831
Test: RunSettingsRoboTests
This commit is contained in:
jackqdyulei
2019-01-14 10:55:39 -08:00
committed by Tsung-Mao Fang
parent 8b9529e533
commit a5f1b5c629
3 changed files with 55 additions and 10 deletions

View File

@@ -38,6 +38,7 @@ import androidx.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.slices.BlockingSlicePrefController;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
@@ -55,7 +56,10 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(RobolectricTestRunner.class)
public class DashboardFragmentTest {
@@ -66,6 +70,7 @@ public class DashboardFragmentTest {
private DashboardCategory mDashboardCategory;
private Context mContext;
private TestFragment mTestFragment;
private List<AbstractPreferenceController> mControllers;
@Before
public void setUp() {
@@ -83,6 +88,7 @@ public class DashboardFragmentTest {
.thenReturn(mDashboardCategory);
mTestFragment.onAttach(RuntimeEnvironment.application);
when(mContext.getPackageName()).thenReturn("TestPackage");
mControllers = new ArrayList<>();
}
@Test
@@ -193,6 +199,39 @@ public class DashboardFragmentTest {
DASHBOARD_CONTAINER, null, 0);
}
@Test
public void updatePreferenceVisibility_prefKeyNull_shouldNotCrash() {
final Map<Class, List<AbstractPreferenceController>> prefControllers = new HashMap<>();
final List<AbstractPreferenceController> controllerList = new ArrayList<>();
controllerList.add(new TestPreferenceController(mContext));
prefControllers.put(TestPreferenceController.class, controllerList);
mTestFragment.mBlockerController = new UiBlockerController(Arrays.asList("pref_key"));
// Should not crash
mTestFragment.updatePreferenceVisibility(prefControllers);
}
@Test
public void checkUiBlocker_noUiBlocker_controllerIsNull() {
mTestFragment.mBlockerController = null;
mControllers.add(new TestPreferenceController(mContext));
mTestFragment.checkUiBlocker(mControllers);
assertThat(mTestFragment.mBlockerController).isNull();
}
@Test
public void checkUiBlocker_hasUiBlocker_controllerNotNull() {
mTestFragment.mBlockerController = null;
mControllers.add(new TestPreferenceController(mContext));
mControllers.add(new BlockingSlicePrefController(mContext, "pref_key"));
mTestFragment.checkUiBlocker(mControllers);
assertThat(mTestFragment.mBlockerController).isNotNull();
}
public static class TestPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin {