Remove duplicate preferences in Accessibility settings.

- addPreferencesFromResource() call has been added to
InstrumentedPreferenceFragment, and hence sub-class should not call it
again.

Change-Id: Ia016d9d407ac3f838c962d1ced585647382a051a
Fixes: 68820835
Test: make RunSettingsRoboTests
This commit is contained in:
Doris Ling
2017-11-02 16:44:50 -07:00
parent 910d452874
commit 024668a2c5
5 changed files with 120 additions and 10 deletions

View File

@@ -28,8 +28,7 @@ import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.ToggleSwitch;
public abstract class ToggleFeaturePreferenceFragment
extends SettingsPreferenceFragment {
public abstract class ToggleFeaturePreferenceFragment extends SettingsPreferenceFragment {
protected SwitchBar mSwitchBar;
protected ToggleSwitch mToggleSwitch;
@@ -43,9 +42,7 @@ public abstract class ToggleFeaturePreferenceFragment
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final int resId = getPreferenceScreenResId();
if (usePreferenceScreenTitle() && resId > 0) {
addPreferencesFromResource(resId);
} else {
if (!usePreferenceScreenTitle() || resId <= 0) {
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(
getActivity());
setPreferenceScreen(preferenceScreen);

View File

@@ -63,9 +63,7 @@ public class ManageDomainUrls extends SettingsPreferenceFragment
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setAnimationAllowed(true);
if (usePreferenceScreenTitle()) {
addPreferencesFromResource(R.xml.manage_domain_url_settings);
} else {
if (!usePreferenceScreenTitle()) {
setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getContext()));
}
mApplicationsState = ApplicationsState.getInstance(
@@ -74,6 +72,11 @@ public class ManageDomainUrls extends SettingsPreferenceFragment
setHasOptionsMenu(true);
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.manage_domain_url_settings;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

View File

@@ -59,7 +59,9 @@ public class UnrestrictedDataAccess extends SettingsPreferenceFragment
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setAnimationAllowed(true);
addPreferencesFromResource(R.xml.unrestricted_data_access_settings);
if (!usePreferenceScreenTitle()) {
addPreferencesFromResource(R.xml.unrestricted_data_access_settings);
}
mApplicationsState = ApplicationsState.getInstance(
(Application) getContext().getApplicationContext());
mDataSaverBackend = new DataSaverBackend(getContext());
@@ -208,6 +210,11 @@ public class UnrestrictedDataAccess extends SettingsPreferenceFragment
return MetricsEvent.DATA_USAGE_UNRESTRICTED_ACCESS;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.unrestricted_data_access_settings;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof AccessPreference) {

View File

@@ -51,6 +51,11 @@ public class PaymentSettings extends SettingsPreferenceFragment implements Index
return MetricsEvent.NFC_PAYMENT;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.nfc_payment_settings;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
@@ -60,7 +65,6 @@ public class PaymentSettings extends SettingsPreferenceFragment implements Index
final PreferenceScreen screen;
if (usePreferenceScreenTitle()) {
addPreferencesFromResource(R.xml.nfc_payment_settings);
screen = getPreferenceScreen();
} else {
PreferenceManager manager = getPreferenceManager();

View File

@@ -0,0 +1,99 @@
/*
* 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.accessibility;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.os.Bundle;
import android.support.annotation.XmlRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.widget.SwitchBar;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.util.FragmentTestUtil;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
shadows = {
SettingsShadowResources.class,
SettingsShadowResources.SettingsShadowTheme.class,
})
public class ToggleFeaturePreferenceFragmentTest {
private ToggleFeaturePreferenceFragmentTestable mFragment;
@Test
public void createFragment_shouldOnlyAddPreferencesOnce() {
mFragment = spy(new ToggleFeaturePreferenceFragmentTestable());
FragmentTestUtil.startFragment(mFragment);
// execute exactly once
verify(mFragment).addPreferencesFromResource(R.xml.placeholder_prefs);
}
public static class ToggleFeaturePreferenceFragmentTestable
extends ToggleFeaturePreferenceFragment {
@Override
protected void onPreferenceToggled(String preferenceKey, boolean enabled) {}
@Override
public int getMetricsCategory() {
return 0;
}
@Override
public int getPreferenceScreenResId() {
return R.xml.placeholder_prefs;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return mock(View.class);
}
@Override
public void addPreferencesFromResource(@XmlRes int preferencesResId) {
// do nothing
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// do nothing
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
mSwitchBar = mock(SwitchBar.class);
super.onActivityCreated(savedInstanceState);
}
}
}