From 82e81af8321526acb3c6cb5db07487eec3a77b45 Mon Sep 17 00:00:00 2001 From: Sunny Shao Date: Thu, 29 Aug 2019 14:30:07 +0800 Subject: [PATCH] Clean FooterPreferenceMixin and FooterPreferenceMixinCompat Fixes: 124129485 Test: make RunSettingsLibRoboTests Change-Id: Id0940329bd0c20e32f2cec3b0b05cc4e98357944 --- .../widget/FooterPreferenceMixin.java | 78 -------------- .../widget/FooterPreferenceMixinCompat.java | 72 ------------- .../FooterPreferenceMixinCompatTest.java | 100 ----------------- .../widget/FooterPreferenceMixinTest.java | 101 ------------------ 4 files changed, 351 deletions(-) delete mode 100644 packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixin.java delete mode 100644 packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixinCompat.java delete mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java delete mode 100644 packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java diff --git a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixin.java b/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixin.java deleted file mode 100644 index fcf2363377030..0000000000000 --- a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixin.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (C) 2016 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.settingslib.widget; - -import android.content.Context; - -import androidx.preference.PreferenceFragment; -import androidx.preference.PreferenceScreen; - -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.core.lifecycle.LifecycleObserver; -import com.android.settingslib.core.lifecycle.events.SetPreferenceScreen; - -/** - * Framework mixin is deprecated, use the compat version instead. - * - * @deprecated - */ -@Deprecated -public class FooterPreferenceMixin implements LifecycleObserver, SetPreferenceScreen { - - private final PreferenceFragment mFragment; - private FooterPreference mFooterPreference; - - public FooterPreferenceMixin(PreferenceFragment fragment, Lifecycle lifecycle) { - mFragment = fragment; - lifecycle.addObserver(this); - } - - @Override - public void setPreferenceScreen(PreferenceScreen preferenceScreen) { - if (mFooterPreference != null) { - preferenceScreen.addPreference(mFooterPreference); - } - } - - /** - * Creates a new {@link FooterPreference}. - */ - public FooterPreference createFooterPreference() { - final PreferenceScreen screen = mFragment.getPreferenceScreen(); - if (mFooterPreference != null && screen != null) { - screen.removePreference(mFooterPreference); - } - mFooterPreference = new FooterPreference(getPrefContext()); - - if (screen != null) { - screen.addPreference(mFooterPreference); - } - return mFooterPreference; - } - - /** - * Returns an UI context with theme properly set for new Preference objects. - */ - private Context getPrefContext() { - return mFragment.getPreferenceManager().getContext(); - } - - public boolean hasFooter() { - return mFooterPreference != null; - } -} - diff --git a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixinCompat.java b/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixinCompat.java deleted file mode 100644 index d45e56d486aec..0000000000000 --- a/packages/SettingsLib/src/com/android/settingslib/widget/FooterPreferenceMixinCompat.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2018 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.settingslib.widget; - -import android.content.Context; - -import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceScreen; - -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.core.lifecycle.LifecycleObserver; -import com.android.settingslib.core.lifecycle.events.SetPreferenceScreen; - -public class FooterPreferenceMixinCompat implements LifecycleObserver, SetPreferenceScreen { - - private final PreferenceFragmentCompat mFragment; - private FooterPreference mFooterPreference; - - public FooterPreferenceMixinCompat(PreferenceFragmentCompat fragment, Lifecycle lifecycle) { - mFragment = fragment; - lifecycle.addObserver(this); - } - - @Override - public void setPreferenceScreen(PreferenceScreen preferenceScreen) { - if (mFooterPreference != null) { - preferenceScreen.addPreference(mFooterPreference); - } - } - - /** - * Creates a new {@link FooterPreference}. - */ - public FooterPreference createFooterPreference() { - final PreferenceScreen screen = mFragment.getPreferenceScreen(); - if (mFooterPreference != null && screen != null) { - screen.removePreference(mFooterPreference); - } - mFooterPreference = new FooterPreference(getPrefContext()); - - if (screen != null) { - screen.addPreference(mFooterPreference); - } - return mFooterPreference; - } - - /** - * Returns an UI context with theme properly set for new Preference objects. - */ - private Context getPrefContext() { - return mFragment.getPreferenceManager().getContext(); - } - - public boolean hasFooter() { - return mFooterPreference != null; - } -} - diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java deleted file mode 100644 index 601da0512c7c5..0000000000000 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinCompatTest.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright (C) 2016 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.settingslib.widget; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.PreferenceFragmentCompat; -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; - -import com.android.settingslib.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.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class FooterPreferenceMixinCompatTest { - - @Mock - private PreferenceFragmentCompat mFragment; - @Mock - private PreferenceScreen mScreen; - - private LifecycleOwner mLifecycleOwner; - private Lifecycle mLifecycle; - private FooterPreferenceMixinCompat mMixin; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mLifecycleOwner = () -> mLifecycle; - mLifecycle = new Lifecycle(mLifecycleOwner); - when(mFragment.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); - when(mFragment.getPreferenceManager().getContext()) - .thenReturn(RuntimeEnvironment.application); - mMixin = new FooterPreferenceMixinCompat(mFragment, mLifecycle); - } - - @Test - public void createFooter_screenNotAvailable_noCrash() { - assertThat(mMixin.createFooterPreference()).isNotNull(); - } - - @Test - public void createFooter_screenAvailable_canAttachToScreen() { - when(mFragment.getPreferenceScreen()).thenReturn(mScreen); - - final FooterPreference preference = mMixin.createFooterPreference(); - - assertThat(preference).isNotNull(); - verify(mScreen).addPreference(preference); - } - - @Test - public void createFooter_screenAvailableDelayed_canAttachToScreen() { - final FooterPreference preference = mMixin.createFooterPreference(); - - mLifecycle.setPreferenceScreen(mScreen); - - assertThat(preference).isNotNull(); - verify(mScreen).addPreference(preference); - } - - @Test - public void createFooterTwice_screenAvailable_replaceOldFooter() { - when(mFragment.getPreferenceScreen()).thenReturn(mScreen); - - mMixin.createFooterPreference(); - mMixin.createFooterPreference(); - - verify(mScreen).removePreference(any(FooterPreference.class)); - verify(mScreen, times(2)).addPreference(any(FooterPreference.class)); - } -} diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java deleted file mode 100644 index 7ae5d2d97cb89..0000000000000 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/widget/FooterPreferenceMixinTest.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright (C) 2016 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.settingslib.widget; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.PreferenceFragment; -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; - -import com.android.settingslib.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.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -@RunWith(RobolectricTestRunner.class) -public class FooterPreferenceMixinTest { - - @Mock - private PreferenceFragment mFragment; - @Mock - private PreferenceScreen mScreen; - - private LifecycleOwner mLifecycleOwner; - private Lifecycle mLifecycle; - private FooterPreferenceMixin mMixin; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - mLifecycleOwner = () -> mLifecycle; - mLifecycle = new Lifecycle(mLifecycleOwner); - when(mFragment.getPreferenceManager()).thenReturn(mock(PreferenceManager.class)); - when(mFragment.getPreferenceManager().getContext()) - .thenReturn(RuntimeEnvironment.application); - mMixin = new FooterPreferenceMixin(mFragment, mLifecycle); - } - - @Test - public void createFooter_screenNotAvailable_noCrash() { - assertThat(mMixin.createFooterPreference()).isNotNull(); - } - - @Test - public void createFooter_screenAvailable_canAttachToScreen() { - when(mFragment.getPreferenceScreen()).thenReturn(mScreen); - - final FooterPreference preference = mMixin.createFooterPreference(); - - assertThat(preference).isNotNull(); - verify(mScreen).addPreference(preference); - } - - @Test - public void createFooter_screenAvailableDelayed_canAttachToScreen() { - final FooterPreference preference = mMixin.createFooterPreference(); - - mLifecycle.setPreferenceScreen(mScreen); - - assertThat(preference).isNotNull(); - verify(mScreen).addPreference(preference); - } - - @Test - public void createFooterTwice_screenAvailable_replaceOldFooter() { - when(mFragment.getPreferenceScreen()).thenReturn(mScreen); - - mMixin.createFooterPreference(); - mMixin.createFooterPreference(); - - verify(mScreen).removePreference(any(FooterPreference.class)); - verify(mScreen, times(2)).addPreference(any(FooterPreference.class)); - } - -}