fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,90 @@
/*
* Copyright (C) 2023 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.testutils;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.provider.DeviceConfig;
import android.provider.Settings;
import com.android.settings.biometrics.activeunlock.ActiveUnlockStatusUtils;
import java.util.ArrayList;
/** Utilities class to enable or disable the Active Unlock flag in tests. */
public final class ActiveUnlockTestUtils {
public static final String TARGET = "com.active.unlock.target";
public static final String PROVIDER = "com.active.unlock.provider";
public static final String TARGET_SETTING = "active_unlock_target";
public static final String PROVIDER_SETTING = "active_unlock_provider";
public static void enable(Context context) {
ActiveUnlockTestUtils.enable(context, ActiveUnlockStatusUtils.UNLOCK_INTENT_LAYOUT);
}
public static void enable(Context context, String flagValue) {
Settings.Secure.putString(
context.getContentResolver(), TARGET_SETTING, TARGET);
Settings.Secure.putString(
context.getContentResolver(), PROVIDER_SETTING, PROVIDER);
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo = new ApplicationInfo();
applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = new ActivityInfo();
resolveInfo.activityInfo.applicationInfo = applicationInfo;
when(packageManager.resolveActivity(any(), anyInt())).thenReturn(resolveInfo);
PackageInfo packageInfo = new PackageInfo();
packageInfo.applicationInfo = applicationInfo;
ProviderInfo providerInfo = new ProviderInfo();
providerInfo.authority = PROVIDER;
providerInfo.applicationInfo = applicationInfo;
packageInfo.providers = new ProviderInfo[] { providerInfo };
ArrayList<PackageInfo> packageInfos = new ArrayList<>();
packageInfos.add(packageInfo);
when(packageManager.getInstalledPackages(any())).thenReturn(packageInfos);
DeviceConfig.setProperty(
DeviceConfig.NAMESPACE_REMOTE_AUTH,
ActiveUnlockStatusUtils.CONFIG_FLAG_NAME,
flagValue,
false /* makeDefault */);
}
public static void disable(Context context) {
DeviceConfig.setProperty(
DeviceConfig.NAMESPACE_REMOTE_AUTH,
ActiveUnlockStatusUtils.CONFIG_FLAG_NAME,
null /* value */,
false /* makeDefault */);
Settings.Secure.putString(context.getContentResolver(), TARGET_SETTING, null);
Settings.Secure.putString(context.getContentResolver(), PROVIDER_SETTING, null);
}
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.pm.ApplicationInfo;
/**
* Helper for mocking installed applications.
*/
public class ApplicationTestUtils {
/**
* Create and populate an {@link android.content.pm.ApplicationInfo} object that describes an
* installed app.
*
* @param uid The app's uid
* @param packageName The app's package name.
* @param flags Flags describing the app. See {@link android.content.pm.ApplicationInfo#flags}
* for possible values.
* @param targetSdkVersion The app's target SDK version
*
* @see android.content.pm.ApplicationInfo
*/
public static ApplicationInfo buildInfo(int uid, String packageName, int flags,
int targetSdkVersion) {
final ApplicationInfo info = new ApplicationInfo();
info.uid = uid;
info.packageName = packageName;
info.flags = flags;
info.targetSdkVersion = targetSdkVersion;
return info;
}
}

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Intent;
import android.os.BatteryManager;
public class BatteryTestUtils {
public static Intent getChargingIntent() {
return getCustomBatteryIntent(
BatteryManager.BATTERY_PLUGGED_AC,
50 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_CHARGING);
}
public static Intent getDischargingIntent() {
return getCustomBatteryIntent(
0 /* plugged */,
10 /* level */,
100 /* scale */,
BatteryManager.BATTERY_STATUS_DISCHARGING);
}
private static Intent getCustomBatteryIntent(int plugged, int level, int scale, int status) {
Intent intent = new Intent();
intent.putExtra(BatteryManager.EXTRA_PLUGGED, plugged);
intent.putExtra(BatteryManager.EXTRA_LEVEL, level);
intent.putExtra(BatteryManager.EXTRA_SCALE, scale);
intent.putExtra(BatteryManager.EXTRA_STATUS, status);
return intent;
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright (C) 2020 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.testutils;
import android.app.Activity;
import android.content.Intent;
import android.os.UserHandle;
public class CustomActivity extends Activity {
@Override
public void startActivityAsUser(Intent intent, UserHandle user) {}
}

View File

@@ -0,0 +1,351 @@
/*
* Copyright (C) 2020 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.testutils;
import static org.mockito.Mockito.mock;
import android.content.Context;
import com.android.settings.accessibility.AccessibilityMetricsFeatureProvider;
import com.android.settings.accessibility.AccessibilitySearchFeatureProvider;
import com.android.settings.accounts.AccountFeatureProvider;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.biometrics.face.FaceFeatureProvider;
import com.android.settings.biometrics.fingerprint.FingerprintFeatureProvider;
import com.android.settings.biometrics2.factory.BiometricsRepositoryProvider;
import com.android.settings.bluetooth.BluetoothFeatureProvider;
import com.android.settings.connecteddevice.audiosharing.AudioSharingFeatureProvider;
import com.android.settings.connecteddevice.fastpair.FastPairFeatureProvider;
import com.android.settings.connecteddevice.stylus.StylusFeatureProvider;
import com.android.settings.dashboard.DashboardFeatureProvider;
import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider;
import com.android.settings.deviceinfo.hardwareinfo.HardwareInfoFeatureProvider;
import com.android.settings.deviceinfo.hardwareinfo.HardwareInfoFeatureProviderImpl;
import com.android.settings.display.DisplayFeatureProvider;
import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider;
import com.android.settings.fuelgauge.BatterySettingsFeatureProvider;
import com.android.settings.fuelgauge.BatteryStatusFeatureProvider;
import com.android.settings.fuelgauge.PowerUsageFeatureProvider;
import com.android.settings.homepage.contextualcards.ContextualCardFeatureProvider;
import com.android.settings.inputmethod.KeyboardSettingsFeatureProvider;
import com.android.settings.localepicker.LocaleFeatureProvider;
import com.android.settings.onboarding.OnboardingFeatureProvider;
import com.android.settings.overlay.DockUpdaterFeatureProvider;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.overlay.SupportFeatureProvider;
import com.android.settings.overlay.SurveyFeatureProvider;
import com.android.settings.panel.PanelFeatureProvider;
import com.android.settings.privatespace.PrivateSpaceLoginFeatureProvider;
import com.android.settings.search.SearchFeatureProvider;
import com.android.settings.security.SecurityFeatureProvider;
import com.android.settings.security.SecuritySettingsFeatureProvider;
import com.android.settings.slices.SlicesFeatureProvider;
import com.android.settings.users.UserFeatureProvider;
import com.android.settings.vpn2.AdvancedVpnFeatureProvider;
import com.android.settings.wifi.WifiTrackerLibProvider;
import com.android.settings.wifi.factory.WifiFeatureProvider;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.jetbrains.annotations.NotNull;
/**
* Test util to provide fake FeatureFactory. To use this factory, call {@code setupForTest} in
* {@code @Before} method of the test class.
*/
public class FakeFeatureFactory extends FeatureFactory {
public final SupportFeatureProvider supportFeatureProvider;
public final MetricsFeatureProvider metricsFeatureProvider;
public final BatteryStatusFeatureProvider batteryStatusFeatureProvider;
public final BatterySettingsFeatureProvider batterySettingsFeatureProvider;
public final PowerUsageFeatureProvider powerUsageFeatureProvider;
public final DashboardFeatureProvider dashboardFeatureProvider;
public final DockUpdaterFeatureProvider dockUpdaterFeatureProvider;
public final LocaleFeatureProvider localeFeatureProvider;
public final ApplicationFeatureProvider applicationFeatureProvider;
public final EnterprisePrivacyFeatureProvider enterprisePrivacyFeatureProvider;
public final SurveyFeatureProvider surveyFeatureProvider;
public final SecurityFeatureProvider securityFeatureProvider;
public final SuggestionFeatureProvider suggestionsFeatureProvider;
public final UserFeatureProvider userFeatureProvider;
public final AccountFeatureProvider mAccountFeatureProvider;
public final BluetoothFeatureProvider mBluetoothFeatureProvider;
public final FaceFeatureProvider mFaceFeatureProvider;
public final FingerprintFeatureProvider mFingerprintFeatureProvider;
public final BiometricsRepositoryProvider mBiometricsRepositoryProvider;
public PanelFeatureProvider panelFeatureProvider;
public SlicesFeatureProvider slicesFeatureProvider;
public SearchFeatureProvider searchFeatureProvider;
public ContextualCardFeatureProvider mContextualCardFeatureProvider;
public WifiTrackerLibProvider wifiTrackerLibProvider;
public SecuritySettingsFeatureProvider securitySettingsFeatureProvider;
public AccessibilitySearchFeatureProvider mAccessibilitySearchFeatureProvider;
public AccessibilityMetricsFeatureProvider mAccessibilityMetricsFeatureProvider;
public AdvancedVpnFeatureProvider mAdvancedVpnFeatureProvider;
public WifiFeatureProvider mWifiFeatureProvider;
public KeyboardSettingsFeatureProvider mKeyboardSettingsFeatureProvider;
public StylusFeatureProvider mStylusFeatureProvider;
public OnboardingFeatureProvider mOnboardingFeatureProvider;
public FastPairFeatureProvider mFastPairFeatureProvider;
public PrivateSpaceLoginFeatureProvider mPrivateSpaceLoginFeatureProvider;
public DisplayFeatureProvider mDisplayFeatureProvider;
public AudioSharingFeatureProvider mAudioSharingFeatureProvider;
/** Call this in {@code @Before} method of the test class to use fake factory. */
public static FakeFeatureFactory setupForTest() {
FakeFeatureFactory factory = new FakeFeatureFactory();
try {
setFactory(getAppContext(), factory);
} catch (NoSuchMethodError ex) {
// The getAppContext() @JvmStatic method doesn't appear to generated in AOSP. Falling
// back to using the companion object method instead.
setFactory(FeatureFactory.Companion.getAppContext(), factory);
}
return factory;
}
/** FeatureFactory constructor. */
public FakeFeatureFactory() {
supportFeatureProvider = mock(SupportFeatureProvider.class);
metricsFeatureProvider = mock(MetricsFeatureProvider.class);
batteryStatusFeatureProvider = mock(BatteryStatusFeatureProvider.class);
batterySettingsFeatureProvider = mock(BatterySettingsFeatureProvider.class);
powerUsageFeatureProvider = mock(PowerUsageFeatureProvider.class);
dashboardFeatureProvider = mock(DashboardFeatureProvider.class);
dockUpdaterFeatureProvider = mock(DockUpdaterFeatureProvider.class);
localeFeatureProvider = mock(LocaleFeatureProvider.class);
applicationFeatureProvider = mock(ApplicationFeatureProvider.class);
enterprisePrivacyFeatureProvider = mock(EnterprisePrivacyFeatureProvider.class);
searchFeatureProvider = mock(SearchFeatureProvider.class);
surveyFeatureProvider = mock(SurveyFeatureProvider.class);
securityFeatureProvider = mock(SecurityFeatureProvider.class);
suggestionsFeatureProvider = mock(SuggestionFeatureProvider.class);
userFeatureProvider = mock(UserFeatureProvider.class);
slicesFeatureProvider = mock(SlicesFeatureProvider.class);
mAccountFeatureProvider = mock(AccountFeatureProvider.class);
mContextualCardFeatureProvider = mock(ContextualCardFeatureProvider.class);
panelFeatureProvider = mock(PanelFeatureProvider.class);
mBluetoothFeatureProvider = mock(BluetoothFeatureProvider.class);
mFaceFeatureProvider = mock(FaceFeatureProvider.class);
mFingerprintFeatureProvider = mock(FingerprintFeatureProvider.class);
mBiometricsRepositoryProvider = mock(BiometricsRepositoryProvider.class);
wifiTrackerLibProvider = mock(WifiTrackerLibProvider.class);
securitySettingsFeatureProvider = mock(SecuritySettingsFeatureProvider.class);
mAccessibilitySearchFeatureProvider = mock(AccessibilitySearchFeatureProvider.class);
mAccessibilityMetricsFeatureProvider = mock(AccessibilityMetricsFeatureProvider.class);
mAdvancedVpnFeatureProvider = mock(AdvancedVpnFeatureProvider.class);
mWifiFeatureProvider = mock(WifiFeatureProvider.class);
mKeyboardSettingsFeatureProvider = mock(KeyboardSettingsFeatureProvider.class);
mStylusFeatureProvider = mock(StylusFeatureProvider.class);
mOnboardingFeatureProvider = mock(OnboardingFeatureProvider.class);
mFastPairFeatureProvider = mock(FastPairFeatureProvider.class);
mPrivateSpaceLoginFeatureProvider = mock(PrivateSpaceLoginFeatureProvider.class);
mDisplayFeatureProvider = mock(DisplayFeatureProvider.class);
mAudioSharingFeatureProvider = mock(AudioSharingFeatureProvider.class);
}
@Override
public SuggestionFeatureProvider getSuggestionFeatureProvider() {
return suggestionsFeatureProvider;
}
@Override
public SupportFeatureProvider getSupportFeatureProvider() {
return supportFeatureProvider;
}
@Override
public MetricsFeatureProvider getMetricsFeatureProvider() {
return metricsFeatureProvider;
}
@NotNull
@Override
public BatteryStatusFeatureProvider getBatteryStatusFeatureProvider() {
return batteryStatusFeatureProvider;
}
@Override
public BatterySettingsFeatureProvider getBatterySettingsFeatureProvider() {
return batterySettingsFeatureProvider;
}
@NotNull
@Override
public PowerUsageFeatureProvider getPowerUsageFeatureProvider() {
return powerUsageFeatureProvider;
}
@NotNull
@Override
public DashboardFeatureProvider getDashboardFeatureProvider() {
return dashboardFeatureProvider;
}
@Override
public DockUpdaterFeatureProvider getDockUpdaterFeatureProvider() {
return dockUpdaterFeatureProvider;
}
@NotNull
@Override
public ApplicationFeatureProvider getApplicationFeatureProvider() {
return applicationFeatureProvider;
}
@Override
public LocaleFeatureProvider getLocaleFeatureProvider() {
return localeFeatureProvider;
}
@NotNull
@Override
public EnterprisePrivacyFeatureProvider getEnterprisePrivacyFeatureProvider() {
return enterprisePrivacyFeatureProvider;
}
@Override
public SearchFeatureProvider getSearchFeatureProvider() {
return searchFeatureProvider;
}
@Override
public SurveyFeatureProvider getSurveyFeatureProvider(Context context) {
return surveyFeatureProvider;
}
@Override
public SecurityFeatureProvider getSecurityFeatureProvider() {
return securityFeatureProvider;
}
@NotNull
@Override
public UserFeatureProvider getUserFeatureProvider() {
return userFeatureProvider;
}
@Override
public SlicesFeatureProvider getSlicesFeatureProvider() {
return slicesFeatureProvider;
}
@Override
public AccountFeatureProvider getAccountFeatureProvider() {
return mAccountFeatureProvider;
}
@Override
public PanelFeatureProvider getPanelFeatureProvider() {
return panelFeatureProvider;
}
@Override
public ContextualCardFeatureProvider getContextualCardFeatureProvider(Context context) {
return mContextualCardFeatureProvider;
}
@Override
public BluetoothFeatureProvider getBluetoothFeatureProvider() {
return mBluetoothFeatureProvider;
}
@Override
public FaceFeatureProvider getFaceFeatureProvider() {
return mFaceFeatureProvider;
}
@Override
public FingerprintFeatureProvider getFingerprintFeatureProvider() {
return mFingerprintFeatureProvider;
}
@Override
public BiometricsRepositoryProvider getBiometricsRepositoryProvider() {
return mBiometricsRepositoryProvider;
}
@Override
public WifiTrackerLibProvider getWifiTrackerLibProvider() {
return wifiTrackerLibProvider;
}
@Override
public SecuritySettingsFeatureProvider getSecuritySettingsFeatureProvider() {
return securitySettingsFeatureProvider;
}
@Override
public AccessibilitySearchFeatureProvider getAccessibilitySearchFeatureProvider() {
return mAccessibilitySearchFeatureProvider;
}
@Override
public AccessibilityMetricsFeatureProvider getAccessibilityMetricsFeatureProvider() {
return mAccessibilityMetricsFeatureProvider;
}
@Override
public HardwareInfoFeatureProvider getHardwareInfoFeatureProvider() {
return HardwareInfoFeatureProviderImpl.INSTANCE;
}
@Override
public AdvancedVpnFeatureProvider getAdvancedVpnFeatureProvider() {
return mAdvancedVpnFeatureProvider;
}
@Override
public WifiFeatureProvider getWifiFeatureProvider() {
return mWifiFeatureProvider;
}
@Override
public KeyboardSettingsFeatureProvider getKeyboardSettingsFeatureProvider() {
return mKeyboardSettingsFeatureProvider;
}
@Override
public StylusFeatureProvider getStylusFeatureProvider() {
return mStylusFeatureProvider;
}
@Override
public OnboardingFeatureProvider getOnboardingFeatureProvider() {
return mOnboardingFeatureProvider;
}
@Override
public FastPairFeatureProvider getFastPairFeatureProvider() {
return mFastPairFeatureProvider;
}
@Override
public PrivateSpaceLoginFeatureProvider getPrivateSpaceLoginFeatureProvider() {
return mPrivateSpaceLoginFeatureProvider;
}
@Override
public DisplayFeatureProvider getDisplayFeatureProvider() {
return mDisplayFeatureProvider;
}
@Override
public AudioSharingFeatureProvider getAudioSharingFeatureProvider() {
return mAudioSharingFeatureProvider;
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
public class FakeInvalidSliderController extends FakeSliderController {
public FakeInvalidSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getMax() {
// Return 0 to make it invalid
return 0;
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import android.provider.Settings;
import com.android.settings.core.SliderPreferenceController;
public class FakeSliderController extends SliderPreferenceController {
public static final String AVAILABILITY_KEY = "fake_slider_availability_key";
public static final int MAX_VALUE = 9;
private static final String SETTING_KEY = "fake_slider_key";
public FakeSliderController(Context context, String key) {
super(context, key);
}
@Override
public int getSliderPosition() {
return Settings.System.getInt(mContext.getContentResolver(), SETTING_KEY, 0);
}
@Override
public boolean setSliderPosition(int position) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY, position);
}
@Override
public int getMax() {
return MAX_VALUE;
}
@Override
public int getMin() {
return 0;
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(), AVAILABILITY_KEY, AVAILABLE);
}
@Override
public boolean isSliceable() {
return true;
}
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
import android.content.IntentFilter;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.slices.SliceBackgroundWorker;
public class FakeToggleController extends TogglePreferenceController {
public static final String AVAILABILITY_KEY = "fake_toggle_availability_key";
public static final int HIGHLIGHT_MENU_RES = R.string.menu_key_about_device;
public static final IntentFilter INTENT_FILTER = new IntentFilter(
WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
private static final String SETTING_KEY = "toggle_key";
private static final int ON = 1;
private static final int OFF = 0;
private boolean mIsAsyncUpdate = false;
public FakeToggleController(Context context, String preferenceKey) {
super(context, preferenceKey);
}
@Override
public boolean isChecked() {
return Settings.System.getInt(mContext.getContentResolver(),
SETTING_KEY, OFF) == ON;
}
@Override
public boolean setChecked(boolean isChecked) {
return Settings.System.putInt(mContext.getContentResolver(), SETTING_KEY,
isChecked ? ON : OFF);
}
@Override
public int getAvailabilityStatus() {
return Settings.Global.getInt(mContext.getContentResolver(),
AVAILABILITY_KEY, AVAILABLE);
}
@Override
public IntentFilter getIntentFilter() {
return INTENT_FILTER;
}
@Override
public boolean isSliceable() {
return true;
}
@Override
public int getSliceHighlightMenuRes() {
return HIGHLIGHT_MENU_RES;
}
@Override
public Class<? extends SliceBackgroundWorker> getBackgroundWorkerClass() {
return TestWorker.class;
}
@Override
public boolean hasAsyncUpdate() {
return mIsAsyncUpdate;
}
public void setAsyncUpdate(boolean isAsyncUpdate) {
mIsAsyncUpdate = isAsyncUpdate;
}
public static class TestWorker extends SliceBackgroundWorker<Void> {
public TestWorker(Context context, Uri uri) {
super(context, uri);
}
@Override
protected void onSlicePinned() {
}
@Override
protected void onSliceUnpinned() {
}
@Override
public void close() {
}
}
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (C) 2022 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.testutils;
import androidx.arch.core.executor.ArchTaskExecutor;
import androidx.arch.core.executor.TaskExecutor;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
/**
* A JUnit Test Rule that swaps the background executor used by the Architecture Components with a
* different one which executes each task synchronously.
*
* We can't refer it in prebuilt androidX library.
* Copied it from androidx/arch/core/executor/testing/InstantTaskExecutorRule.java
*/
public class InstantTaskExecutorRule extends TestWatcher {
@Override
protected void starting(Description description) {
super.starting(description);
ArchTaskExecutor.getInstance().setDelegate(new TaskExecutor() {
@Override
public void executeOnDiskIO(Runnable runnable) {
runnable.run();
}
@Override
public void postToMainThread(Runnable runnable) {
runnable.run();
}
@Override
public boolean isMainThread() {
return true;
}
});
}
@Override
protected void finished(Description description) {
super.finished(description);
ArchTaskExecutor.getInstance().setDelegate(null);
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2020 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.testutils;
import android.content.Context;
/**
* Test util to provide the correct resources.
*/
public final class ResourcesUtils {
/**
* Return a resource identifier for the given resource name.
* @param context Context to use.
* @param type Optional default resource type to find, if "type/" is not included in the name.
* Can be null to require an explicit type.
* @param name The name of the desired resource.
* @return The associated resource identifier. Returns 0 if no such resource was found.
* (0 is not a valid resource ID.)
*/
public static int getResourcesId(Context context, String type, String name) {
return context.getResources().getIdentifier(name, type, context.getPackageName());
}
/**
* Returns a localized string from the application's package's default string table.
* @param context Context to use.
* @param name The name of the desired resource.
* @return The string data associated with the resource, stripped of styled text information.
*/
public static String getResourcesString(Context context, String name) {
return context.getResources().getString(getResourcesId(context, "string", name));
}
/**
* Return the string value associated with a particular neame of resource,
* substituting the format arguments as defined in {@link java.util.Formatter}
* and {@link java.lang.String#format}. It will be stripped of any styled text
* information.
* @param context Context to use.
* @param name The name of the desired resource.
* @param value The format arguments that will be used for substitution.
* @return The string data associated with the resource, stripped of styled text information.
*/
public static String getResourcesString(Context context, String name, Object... value) {
return context.getResources().getString(getResourcesId(context, "string", name), value);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2020 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.testutils;
import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag
.FLAG_INCLUDE_PREF_SCREEN;
import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.android.settings.core.PreferenceXmlParserUtils;
import org.xmlpull.v1.XmlPullParserException;
import java.util.ArrayList;
import java.util.List;
/**
* Util class for parsing XML
*/
public class XmlTestUtils {
/**
* Parses a preference screen's xml, collects and returns all keys used by preferences
* on the screen.
*
* @param context of the preference screen.
* @param xmlId of the Preference Xml to be parsed.
* @return List of all keys in the preference Xml
*/
public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) {
final List<String> keys = new ArrayList<>();
try {
List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context, xmlId,
FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN);
for (Bundle bundle : metadata) {
final String key = bundle.getString(METADATA_KEY);
if (!TextUtils.isEmpty(key)) {
keys.add(key);
}
}
} catch (java.io.IOException | XmlPullParserException e) {
return null;
}
return keys;
}
}