Add conformance test for preference controller constructor
BasePreferenceController's constructor must be in a certain format in order to work with search and slice. This test enforces that. Change-Id: I236b11248b09ce71f052c449d39e994c84981a06 Fixes: 77216595 Test: robotests
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
com.android.settings.applications.appinfo.AppActionButtonPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppBatteryPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppDataUsagePreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppHeaderViewPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppInstallerInfoPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppMemoryPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppNotificationPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppOpenByDefaultPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppPermissionPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppStoragePreferenceController
|
||||||
|
com.android.settings.applications.appinfo.AppVersionPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.DrawOverlayDetailPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.ExternalSourceDetailPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.InstantAppButtonsPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.InstantAppDomainsPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.PictureInPictureDetailPreferenceController
|
||||||
|
com.android.settings.applications.appinfo.WriteSystemSettingsPreferenceController
|
||||||
|
com.android.settings.bluetooth.BluetoothDeviceNamePreferenceController
|
||||||
|
com.android.settings.bluetooth.BluetoothDeviceRenamePreferenceController
|
||||||
|
com.android.settings.bluetooth.BluetoothSwitchPreferenceController
|
||||||
|
com.android.settings.connecteddevice.ConnectedDeviceGroupController
|
||||||
|
com.android.settings.connecteddevice.SavedDeviceGroupController
|
||||||
|
com.android.settings.datausage.DataUsageSummaryPreferenceController
|
||||||
|
com.android.settings.datetime.timezone.TimeZoneInfoPreferenceController
|
||||||
|
com.android.settings.fuelgauge.RestrictAppPreferenceController
|
||||||
|
com.android.settings.fuelgauge.batterysaver.AutoBatterySeekBarPreferenceController
|
||||||
|
com.android.settings.fuelgauge.batterysaver.BatterySaverButtonPreferenceController
|
||||||
|
com.android.settings.fuelgauge.batterytip.BatteryTipPreferenceController
|
||||||
|
com.android.settings.security.VisiblePatternProfilePreferenceController
|
||||||
|
com.android.settings.security.screenlock.LockScreenPreferenceController
|
||||||
|
com.android.settings.wifi.details.WifiMeteredPreferenceController
|
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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.settings.core;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertWithMessage;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.android.settings.core.codeinspection.CodeInspector;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class BasePreferenceControllerSignatureInspector extends CodeInspector {
|
||||||
|
|
||||||
|
private final List<String> grandfather;
|
||||||
|
|
||||||
|
public BasePreferenceControllerSignatureInspector(List<Class<?>> classes) {
|
||||||
|
super(classes);
|
||||||
|
grandfather = new ArrayList<>();
|
||||||
|
initializeGrandfatherList(grandfather,
|
||||||
|
"grandfather_invalid_base_preference_controller_constructor");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
StringBuilder badClasses = new StringBuilder();
|
||||||
|
|
||||||
|
for (Class c : mClasses) {
|
||||||
|
if (!isConcreteSettingsClass(c)) {
|
||||||
|
// Not a Settings class, or is abstract, don't care.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!BasePreferenceController.class.isAssignableFrom(c)) {
|
||||||
|
// Not a BasePreferenceController, don't care.
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final String className = c.getName();
|
||||||
|
if (grandfather.remove(className)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final Constructor[] constructors = c.getDeclaredConstructors();
|
||||||
|
if (constructors == null || constructors.length == 0) {
|
||||||
|
badClasses.append(c.getName()).append(",");
|
||||||
|
}
|
||||||
|
for (Constructor constructor : constructors) {
|
||||||
|
if (!hasValidConstructorSignature(constructor)) {
|
||||||
|
badClasses.append(className).append(",");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertWithMessage("All BasePreferenceController (and subclasses) constructor must either"
|
||||||
|
+ "only take Context, or (Context, String). No other types are allowed")
|
||||||
|
.that(badClasses.toString())
|
||||||
|
.isEmpty();
|
||||||
|
|
||||||
|
assertWithMessage("Something in the grandfather list is no longer relevant. Please remove")
|
||||||
|
.that(grandfather)
|
||||||
|
.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean hasValidConstructorSignature(Constructor constructor) {
|
||||||
|
final Class[] parameterTypes = constructor.getParameterTypes();
|
||||||
|
if (parameterTypes.length == 1) {
|
||||||
|
return Context.class.isAssignableFrom(parameterTypes[0]);
|
||||||
|
} else if (parameterTypes.length == 2) {
|
||||||
|
return Context.class.isAssignableFrom(parameterTypes[0])
|
||||||
|
&& String.class.isAssignableFrom(parameterTypes[1]);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@@ -18,6 +18,7 @@ package com.android.settings.core.codeinspection;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import com.android.settings.core.BasePreferenceControllerSignatureInspector;
|
||||||
import com.android.settings.core.instrumentation.InstrumentableFragmentCodeInspector;
|
import com.android.settings.core.instrumentation.InstrumentableFragmentCodeInspector;
|
||||||
import com.android.settings.search.SearchIndexProviderCodeInspector;
|
import com.android.settings.search.SearchIndexProviderCodeInspector;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
@@ -49,6 +50,11 @@ public class CodeInspectionTest {
|
|||||||
new InstrumentableFragmentCodeInspector(mClasses).run();
|
new InstrumentableFragmentCodeInspector(mClasses).run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void runBasePreferenceControllerConstructorSignatureInspection() {
|
||||||
|
new BasePreferenceControllerSignatureInspector(mClasses).run();
|
||||||
|
}
|
||||||
|
|
||||||
@Ignore("b/73960706")
|
@Ignore("b/73960706")
|
||||||
@Test
|
@Test
|
||||||
public void runSearchIndexProviderCodeInspection() {
|
public void runSearchIndexProviderCodeInspection() {
|
||||||
|
Reference in New Issue
Block a user