From 436873f156810c4777996c0be7cc495a6b431125 Mon Sep 17 00:00:00 2001 From: Bartosz Fabianowski Date: Fri, 10 Mar 2017 23:23:17 +0100 Subject: [PATCH] Add DynamicAvailabilityPreferenceController This is a PreferenceController that correctly handles the case of isAvailable() changing dynamically, while the Settings page containing the preference in question is open. Bug: 32692748 Test: m RunSettingsRoboTests Change-Id: I95e22bbc7bdc17bacd2bf6b82fa5b535494d5bce --- ...namicAvailabilityPreferenceController.java | 59 ++++++++ ...cAvailabilityPreferenceControllerTest.java | 140 ++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 src/com/android/settings/core/DynamicAvailabilityPreferenceController.java create mode 100644 tests/robotests/src/com/android/settings/core/DynamicAvailabilityPreferenceControllerTest.java diff --git a/src/com/android/settings/core/DynamicAvailabilityPreferenceController.java b/src/com/android/settings/core/DynamicAvailabilityPreferenceController.java new file mode 100644 index 00000000000..9323aa34821 --- /dev/null +++ b/src/com/android/settings/core/DynamicAvailabilityPreferenceController.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.core; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.core.lifecycle.Lifecycle; +import com.android.settings.core.lifecycle.LifecycleObserver; +import com.android.settings.core.lifecycle.events.OnResume; + +public abstract class DynamicAvailabilityPreferenceController extends PreferenceController + implements LifecycleObserver, OnResume { + + private Preference mPreference; + private PreferenceScreen mScreen; + + public DynamicAvailabilityPreferenceController(Context context, Lifecycle lifecycle) { + super(context); + if (lifecycle != null) { + lifecycle.addObserver(this); + } + } + + @Override + public void displayPreference(PreferenceScreen screen) { + mScreen = screen; + mPreference = screen.findPreference(getPreferenceKey()); + super.displayPreference(screen); + } + + @Override + public void onResume() { + if (!isAvailable()) { + removePreference(mScreen, getPreferenceKey()); + return; + } + + updateState(mPreference); + if (mScreen.findPreference(getPreferenceKey()) == null) { + mScreen.addPreference(mPreference); + } + } +} diff --git a/tests/robotests/src/com/android/settings/core/DynamicAvailabilityPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/core/DynamicAvailabilityPreferenceControllerTest.java new file mode 100644 index 00000000000..38a8356aef2 --- /dev/null +++ b/tests/robotests/src/com/android/settings/core/DynamicAvailabilityPreferenceControllerTest.java @@ -0,0 +1,140 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.core; + +import android.content.Context; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; + +import com.android.settings.SettingsRobolectricTestRunner; +import com.android.settings.TestConfig; +import com.android.settings.core.lifecycle.Lifecycle; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link DynamicAvailabilityPreferenceController}. + */ +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public final class DynamicAvailabilityPreferenceControllerTest { + + private final String PREFERENCE_KEY = "preference_key"; + + private @Mock Context mContext; + private @Mock Preference mPreference; + private @Mock PreferenceScreen mScreen; + private @Mock Lifecycle mLifecycle; + + private boolean mIsAvailable; + private Preference mUpdatedPreference = null; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mPreference.getKey()).thenReturn(PREFERENCE_KEY); + when(mScreen.findPreference(PREFERENCE_KEY)).thenReturn(mPreference); + when(mScreen.getPreferenceCount()).thenReturn(1); + when(mScreen.getPreference(0)).thenReturn(mPreference); + } + + @Test + public void testAvailableToUnavailable() { + mIsAvailable = true; + + final DynamicAvailabilityPreferenceController controller + = new DynamicAvailabilityPreferenceControllerTestable(mLifecycle); + verify(mLifecycle).addObserver(controller); + + controller.displayPreference(mScreen); + verify(mScreen, never()).removePreference(mPreference); + verify(mScreen, never()).addPreference(mPreference); + assertThat(mUpdatedPreference).isNull(); + + controller.onResume(); + verify(mScreen, never()).removePreference(mPreference); + verify(mScreen, never()).addPreference(mPreference); + assertThat(mUpdatedPreference).isEqualTo(mPreference); + + mUpdatedPreference = null; + mIsAvailable = false; + controller.onResume(); + verify(mScreen).removePreference(mPreference); + verify(mScreen, never()).addPreference(mPreference); + assertThat(mUpdatedPreference).isNull(); + } + + @Test + public void testUnavailableToAvailable() { + mIsAvailable = false; + + final DynamicAvailabilityPreferenceController controller + = new DynamicAvailabilityPreferenceControllerTestable(mLifecycle); + verify(mLifecycle).addObserver(controller); + + controller.displayPreference(mScreen); + verify(mScreen).removePreference(mPreference); + verify(mScreen, never()).addPreference(mPreference); + assertThat(mUpdatedPreference).isNull(); + + reset(mScreen); + controller.onResume(); + verify(mScreen, never()).removePreference(mPreference); + verify(mScreen, never()).addPreference(mPreference); + assertThat(mUpdatedPreference).isNull(); + + mIsAvailable = true; + controller.onResume(); + verify(mScreen, never()).removePreference(mPreference); + verify(mScreen).addPreference(mPreference); + assertThat(mUpdatedPreference).isEqualTo(mPreference); + } + + + private class DynamicAvailabilityPreferenceControllerTestable + extends DynamicAvailabilityPreferenceController { + public DynamicAvailabilityPreferenceControllerTestable(Lifecycle lifecycle) { + super(DynamicAvailabilityPreferenceControllerTest.this.mContext, lifecycle); + } + + @Override + public boolean isAvailable() { + return mIsAvailable; + } + + @Override + public void updateState(Preference preference) { + mUpdatedPreference = preference; + } + + @Override + public String getPreferenceKey() { + return PREFERENCE_KEY; + } + } +}