Remove more wrappers

Bug: 76167422
Test: make RunSettingsRoboTests -j40
Change-Id: I0dc42c483ae92d508dec94e42573b55ced0e4bb0
This commit is contained in:
Fan Zhang
2018-03-30 11:33:08 -07:00
parent e645558049
commit c0c3eaa66c
19 changed files with 130 additions and 358 deletions

View File

@@ -16,6 +16,8 @@
package com.android.settings; package com.android.settings;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
@@ -27,6 +29,7 @@ import android.net.Uri;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.RecoverySystem;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.support.annotation.VisibleForTesting; import android.support.annotation.VisibleForTesting;
@@ -41,13 +44,9 @@ import android.widget.Toast;
import com.android.ims.ImsManager; import com.android.ims.ImsManager;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.telephony.PhoneConstants; import com.android.internal.telephony.PhoneConstants;
import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
import com.android.settings.wrapper.RecoverySystemWrapper;
import com.android.settings.core.InstrumentedFragment; import com.android.settings.core.InstrumentedFragment;
import com.android.settings.enterprise.ActionDisabledByAdminDialogHelper;
import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.utils.ThreadUtils;
import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/** /**
* Confirm and execute a reset of the network settings to a clean "just out of the box" * Confirm and execute a reset of the network settings to a clean "just out of the box"
@@ -65,7 +64,6 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; private int mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
@VisibleForTesting boolean mEraseEsim; @VisibleForTesting boolean mEraseEsim;
@VisibleForTesting EraseEsimAsyncTask mEraseEsimTask; @VisibleForTesting EraseEsimAsyncTask mEraseEsimTask;
@VisibleForTesting static RecoverySystemWrapper mRecoverySystem;
/** /**
* Async task used to erase all the eSIM profiles from the phone. If error happens during * Async task used to erase all the eSIM profiles from the phone. If error happens during
@@ -82,7 +80,7 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
return mRecoverySystem.wipeEuiccData(mContext, mPackageName); return RecoverySystem.wipeEuiccData(mContext, mPackageName);
} }
@Override @Override
@@ -218,7 +216,6 @@ public class ResetNetworkConfirm extends InstrumentedFragment {
SubscriptionManager.INVALID_SUBSCRIPTION_ID); SubscriptionManager.INVALID_SUBSCRIPTION_ID);
mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA); mEraseEsim = args.getBoolean(MasterClear.ERASE_ESIMS_EXTRA);
} }
mRecoverySystem = new RecoverySystemWrapper();
} }
@Override @Override

View File

@@ -40,7 +40,6 @@ import com.android.settings.R;
import com.android.settings.applications.AppInfoBase; import com.android.settings.applications.AppInfoBase;
import com.android.settings.notification.EmptyTextSettings; import com.android.settings.notification.EmptyTextSettings;
import com.android.settings.widget.AppPreference; import com.android.settings.widget.AppPreference;
import com.android.settings.wrapper.ActivityInfoWrapper;
import com.android.settings.wrapper.UserManagerWrapper; import com.android.settings.wrapper.UserManagerWrapper;
import com.android.settingslib.wrapper.PackageManagerWrapper; import com.android.settingslib.wrapper.PackageManagerWrapper;
@@ -96,25 +95,9 @@ public class PictureInPictureSettings extends EmptyTextSettings {
* @return true if the package has any activities that declare that they support * @return true if the package has any activities that declare that they support
* picture-in-picture. * picture-in-picture.
*/ */
public static boolean checkPackageHasPictureInPictureActivities(String packageName, public static boolean checkPackageHasPictureInPictureActivities(String packageName,
ActivityInfo[] activities) { ActivityInfo[] activities) {
ActivityInfoWrapper[] wrappedActivities = null;
if (activities != null) {
wrappedActivities = new ActivityInfoWrapper[activities.length];
for (int i = 0; i < activities.length; i++) {
wrappedActivities[i] = new ActivityInfoWrapper(activities[i]);
}
}
return checkPackageHasPictureInPictureActivities(packageName, wrappedActivities);
}
/**
* @return true if the package has any activities that declare that they support
* picture-in-picture.
*/
@VisibleForTesting
static boolean checkPackageHasPictureInPictureActivities(String packageName,
ActivityInfoWrapper[] activities) {
// Skip if it's in the ignored list // Skip if it's in the ignored list
if (IGNORE_PACKAGE_LIST.contains(packageName)) { if (IGNORE_PACKAGE_LIST.contains(packageName)) {
return false; return false;

View File

@@ -34,7 +34,6 @@ import android.view.WindowManagerGlobal;
import android.widget.Toast; import android.widget.Toast;
import com.android.internal.app.LocalePicker; import com.android.internal.app.LocalePicker;
import com.android.settings.wrapper.IWindowManagerWrapper;
import com.android.settingslib.development.SystemPropPoker; import com.android.settingslib.development.SystemPropPoker;
public abstract class DevelopmentTiles extends TileService { public abstract class DevelopmentTiles extends TileService {
@@ -150,14 +149,13 @@ public abstract class DevelopmentTiles extends TileService {
@VisibleForTesting @VisibleForTesting
static final int SURFACE_FLINGER_LAYER_TRACE_STATUS_CODE = 1026; static final int SURFACE_FLINGER_LAYER_TRACE_STATUS_CODE = 1026;
private IBinder mSurfaceFlinger; private IBinder mSurfaceFlinger;
private IWindowManagerWrapper mWindowManager; private IWindowManager mWindowManager;
private Toast mToast; private Toast mToast;
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
mWindowManager = new IWindowManagerWrapper(WindowManagerGlobal mWindowManager = WindowManagerGlobal.getWindowManagerService();
.getWindowManagerService());
mSurfaceFlinger = ServiceManager.getService("SurfaceFlinger"); mSurfaceFlinger = ServiceManager.getService("SurfaceFlinger");
Context context = getApplicationContext(); Context context = getApplicationContext();
CharSequence text = "Trace files written to /data/misc/wmtrace"; CharSequence text = "Trace files written to /data/misc/wmtrace";

View File

@@ -24,8 +24,6 @@ import android.text.format.DateUtils;
import android.util.KeyValueListParser; import android.util.KeyValueListParser;
import android.util.Log; import android.util.Log;
import com.android.settings.wrapper.KeyValueListParserWrapper;
import java.util.Arrays; import java.util.Arrays;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@@ -118,37 +116,32 @@ public class AnomalyDetectionPolicy {
*/ */
public final long bluetoothScanThreshold; public final long bluetoothScanThreshold;
private final KeyValueListParserWrapper mParserWrapper; private final KeyValueListParser mParser;
public AnomalyDetectionPolicy(Context context) { public AnomalyDetectionPolicy(Context context) {
this(context, new KeyValueListParserWrapper(new KeyValueListParser(','))); mParser = new KeyValueListParser(',');
}
@VisibleForTesting
AnomalyDetectionPolicy(Context context, KeyValueListParserWrapper parserWrapper) {
mParserWrapper = parserWrapper;
final String value = Settings.Global.getString(context.getContentResolver(), final String value = Settings.Global.getString(context.getContentResolver(),
Settings.Global.ANOMALY_DETECTION_CONSTANTS); Settings.Global.ANOMALY_DETECTION_CONSTANTS);
try { try {
mParserWrapper.setString(value); mParser.setString(value);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
Log.e(TAG, "Bad anomaly detection constants"); Log.e(TAG, "Bad anomaly detection constants");
} }
anomalyDetectionEnabled = anomalyDetectionEnabled =
mParserWrapper.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, false); mParser.getBoolean(KEY_ANOMALY_DETECTION_ENABLED, false);
wakeLockDetectionEnabled = wakeLockDetectionEnabled =
mParserWrapper.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED,false); mParser.getBoolean(KEY_WAKELOCK_DETECTION_ENABLED,false);
wakeupAlarmDetectionEnabled = wakeupAlarmDetectionEnabled =
mParserWrapper.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,false); mParser.getBoolean(KEY_WAKEUP_ALARM_DETECTION_ENABLED,false);
bluetoothScanDetectionEnabled = mParserWrapper.getBoolean( bluetoothScanDetectionEnabled = mParser.getBoolean(
KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false); KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false);
wakeLockThreshold = mParserWrapper.getLong(KEY_WAKELOCK_THRESHOLD, wakeLockThreshold = mParser.getLong(KEY_WAKELOCK_THRESHOLD,
DateUtils.HOUR_IN_MILLIS); DateUtils.HOUR_IN_MILLIS);
wakeupAlarmThreshold = mParserWrapper.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 10); wakeupAlarmThreshold = mParser.getLong(KEY_WAKEUP_ALARM_THRESHOLD, 10);
wakeupBlacklistedTags = parseStringSet(KEY_WAKEUP_BLACKLISTED_TAGS, null); wakeupBlacklistedTags = parseStringSet(KEY_WAKEUP_BLACKLISTED_TAGS, null);
bluetoothScanThreshold = mParserWrapper.getLong(KEY_BLUETOOTH_SCAN_THRESHOLD, bluetoothScanThreshold = mParser.getLong(KEY_BLUETOOTH_SCAN_THRESHOLD,
30 * DateUtils.MINUTE_IN_MILLIS); 30 * DateUtils.MINUTE_IN_MILLIS);
} }
@@ -170,7 +163,7 @@ public class AnomalyDetectionPolicy {
} }
private Set<String> parseStringSet(final String key, final Set<String> defaultSet) { private Set<String> parseStringSet(final String key, final Set<String> defaultSet) {
final String value = mParserWrapper.getString(key, null); final String value = mParser.getString(key, null);
if (value != null) { if (value != null) {
return Arrays.stream(value.split(":")) return Arrays.stream(value.split(":"))
.map(String::trim).map(Uri::decode).collect(Collectors.toSet()); .map(String::trim).map(Uri::decode).collect(Collectors.toSet());

View File

@@ -32,7 +32,7 @@ import android.telephony.ServiceState;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.wrapper.RestrictedLockUtilsWrapper; import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedPreference; import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.Utils; import com.android.settingslib.Utils;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
@@ -74,9 +74,8 @@ public class MobileNetworkPreferenceController extends AbstractPreferenceControl
} }
public boolean isUserRestricted() { public boolean isUserRestricted() {
final RestrictedLockUtilsWrapper wrapper = new RestrictedLockUtilsWrapper();
return mIsSecondaryUser || return mIsSecondaryUser ||
wrapper.hasBaseUserRestriction( RestrictedLockUtils.hasBaseUserRestriction(
mContext, mContext,
DISALLOW_CONFIG_MOBILE_NETWORKS, DISALLOW_CONFIG_MOBILE_NETWORKS,
myUserId()); myUserId());

View File

@@ -1,40 +0,0 @@
/*
* 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.wrapper;
import android.content.pm.ActivityInfo;
/**
* This class replicates a subset of the android.content.pm.ActivityInfo. The class
* exists so that we can use a thin wrapper around the ActivityInfo in production code and a mock in
* tests.
*/
public class ActivityInfoWrapper {
private final ActivityInfo mInfo;
public ActivityInfoWrapper(ActivityInfo info) {
mInfo = info;
}
/**
* Returns whether this activity supports picture-in-picture.
*/
public boolean supportsPictureInPicture() {
return mInfo.supportsPictureInPicture();
}
}

View File

@@ -1,55 +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.settings.wrapper;
import android.os.RemoteException;
import android.view.IWindowManager;
/**
* This class replicates a subset of the android.view.IWindowManager. The class
* exists so that we can use a thin wrapper around the IWindowManager in production code
* and a mock in tests.
*/
public class IWindowManagerWrapper {
private final IWindowManager mWindowManager;
public IWindowManagerWrapper(IWindowManager wm) {
mWindowManager = wm;
}
/**
* Returns true if window trace is enabled.
*/
public boolean isWindowTraceEnabled() throws RemoteException {
return mWindowManager.isWindowTraceEnabled();
}
/**
* Starts a window trace.
*/
public void startWindowTrace() throws RemoteException {
mWindowManager.startWindowTrace();
}
/**
* Stops a window trace.
*/
public void stopWindowTrace() throws RemoteException {
mWindowManager.stopWindowTrace();
}
}

View File

@@ -1,85 +0,0 @@
/*
* 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.wrapper;
import android.util.KeyValueListParser;
/**
* This class replicates a subset of the {@link KeyValueListParser}. The class
* exists so that we can use a thin wrapper around the PM in production code and a mock in tests.
* We cannot directly mock or shadow the {@link KeyValueListParser}, because some of the methods
* we rely on are newer than the API version supported by Robolectric.
*/
public class KeyValueListParserWrapper {
private KeyValueListParser mParser;
public KeyValueListParserWrapper(KeyValueListParser parser) {
mParser = parser;
}
/**
* Get real {@link KeyValueListParser}
*/
public KeyValueListParser getKeyValueListParser() {
return mParser;
}
/**
* Resets the parser with a new string to parse. The string is expected to be in the following
* format:
* <pre>key1=value,key2=value,key3=value</pre>
*
* where the delimiter is a comma.
*
* @param str the string to parse.
* @throws IllegalArgumentException if the string is malformed.
*/
public void setString(String str) throws IllegalArgumentException {
mParser.setString(str);
}
/**
* Get the value for key as a boolean.
* @param key The key to lookup.
* @param defaultValue The value to return if the key was not found.
* @return the boolean value associated with the key.
*/
public boolean getBoolean(String key, boolean defaultValue) {
return mParser.getBoolean(key, defaultValue);
}
/**
* Get the value for key as a string.
* @param key The key to lookup.
* @param defaultValue The value to return if the key was not found.
* @return the string value associated with the key.
*/
public String getString(String key, String defaultValue) {
return mParser.getString(key, defaultValue);
}
/**
* Get the value for key as a long.
* @param key The key to lookup.
* @param defaultValue The value to return if the key was not found, or the value was not a
* long.
* @return the long value associated with the key.
*/
public long getLong(String key, long defaultValue) {
return mParser.getLong(key, defaultValue);
}
}

View File

@@ -1,15 +0,0 @@
package com.android.settings.wrapper;
import android.content.Context;
import com.android.settingslib.RestrictedLockUtils;
/**
* Wrapper class needed to be able to test classes which use RestrictedLockUtils methods.
* Unfortunately there is no way to deal with this until robolectric is updated due to the fact
* that it is a static method and it uses new API's.
*/
public class RestrictedLockUtilsWrapper {
public boolean hasBaseUserRestriction(Context context, String userRestriction, int userId) {
return RestrictedLockUtils.hasBaseUserRestriction(context, userRestriction, userId);
}
}

View File

@@ -16,55 +16,53 @@
package com.android.settings; package com.android.settings;
import static org.mockito.Matchers.any; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.RecoverySystemWrapper; import com.android.settings.testutils.shadow.ShadowRecoverySystem;
import org.junit.Assert; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = {ShadowRecoverySystem.class})
public class ResetNetworkConfirmTest { public class ResetNetworkConfirmTest {
private Activity mActivity; private Activity mActivity;
@Mock @Mock
private ResetNetworkConfirm mResetNetworkConfirm; private ResetNetworkConfirm mResetNetworkConfirm;
@Mock
private RecoverySystemWrapper mRecoverySystem;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mResetNetworkConfirm = spy(new ResetNetworkConfirm()); mResetNetworkConfirm = spy(new ResetNetworkConfirm());
mRecoverySystem = spy(new RecoverySystemWrapper());
ResetNetworkConfirm.mRecoverySystem = mRecoverySystem;
mActivity = Robolectric.setupActivity(Activity.class); mActivity = Robolectric.setupActivity(Activity.class);
} }
@After
public void tearDown() {
ShadowRecoverySystem.reset();
}
@Test @Test
public void testResetNetworkData_resetEsim() { public void testResetNetworkData_resetEsim() {
mResetNetworkConfirm.mEraseEsim = true; mResetNetworkConfirm.mEraseEsim = true;
doReturn(true).when(mRecoverySystem).wipeEuiccData(any(Context.class), anyString());
mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */); mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */);
Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable(); Robolectric.getBackgroundThreadScheduler().advanceToLastPostedRunnable();
Assert.assertNotNull(mResetNetworkConfirm.mEraseEsimTask); assertThat(mResetNetworkConfirm.mEraseEsimTask).isNotNull();
verify(mRecoverySystem).wipeEuiccData(any(Context.class), anyString()); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount())
.isEqualTo(1);
} }
@Test @Test
@@ -73,7 +71,8 @@ public class ResetNetworkConfirmTest {
mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */); mResetNetworkConfirm.esimFactoryReset(mActivity, "" /* packageName */);
Assert.assertNull(mResetNetworkConfirm.mEraseEsimTask); assertThat(mResetNetworkConfirm.mEraseEsimTask).isNull();
verify(mRecoverySystem, never()).wipeEuiccData(any(Context.class), anyString()); assertThat(ShadowRecoverySystem.getWipeEuiccCalledCount())
.isEqualTo(0);
} }
} }

View File

@@ -22,11 +22,11 @@ import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import android.content.Context; import android.content.Context;
import android.content.pm.ActivityInfo;
import com.android.internal.logging.nano.MetricsProto; import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.ActivityInfoWrapper;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -84,9 +84,9 @@ public class PictureInPictureDetailsTest {
private boolean checkPackageHasPictureInPictureActivities(String packageName, private boolean checkPackageHasPictureInPictureActivities(String packageName,
boolean... resizeableActivityState) { boolean... resizeableActivityState) {
ActivityInfoWrapper[] activities = null; ActivityInfo[] activities = null;
if (resizeableActivityState.length > 0) { if (resizeableActivityState.length > 0) {
activities = new ActivityInfoWrapper[resizeableActivityState.length]; activities = new ActivityInfo[resizeableActivityState.length];
for (int i = 0; i < activities.length; i++) { for (int i = 0; i < activities.length; i++) {
activities[i] = new MockActivityInfo(resizeableActivityState[i]); activities[i] = new MockActivityInfo(resizeableActivityState[i]);
} }
@@ -95,12 +95,11 @@ public class PictureInPictureDetailsTest {
activities); activities);
} }
private class MockActivityInfo extends ActivityInfoWrapper { private class MockActivityInfo extends ActivityInfo {
private boolean mSupportsPictureInPicture; private boolean mSupportsPictureInPicture;
private MockActivityInfo(boolean supportsPictureInPicture) { private MockActivityInfo(boolean supportsPictureInPicture) {
super(null);
mSupportsPictureInPicture = supportsPictureInPicture; mSupportsPictureInPicture = supportsPictureInPicture;
} }

View File

@@ -16,8 +16,10 @@
package com.android.settings.development.qstile; package com.android.settings.development.qstile;
import static com.android.settings.development.qstile.DevelopmentTiles.WinscopeTrace.SURFACE_FLINGER_LAYER_TRACE_CONTROL_CODE; import static com.android.settings.development.qstile.DevelopmentTiles.WinscopeTrace
import static com.android.settings.development.qstile.DevelopmentTiles.WinscopeTrace.SURFACE_FLINGER_LAYER_TRACE_STATUS_CODE; .SURFACE_FLINGER_LAYER_TRACE_CONTROL_CODE;
import static com.android.settings.development.qstile.DevelopmentTiles.WinscopeTrace
.SURFACE_FLINGER_LAYER_TRACE_STATUS_CODE;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@@ -30,11 +32,11 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
import android.os.IBinder; import android.os.IBinder;
import android.os.RemoteException; import android.os.RemoteException;
import android.view.IWindowManager;
import android.widget.Toast; import android.widget.Toast;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowParcel; import com.android.settings.testutils.shadow.ShadowParcel;
import com.android.settings.wrapper.IWindowManagerWrapper;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
@@ -49,7 +51,7 @@ import org.robolectric.util.ReflectionHelpers;
public class WinscopeTraceTest { public class WinscopeTraceTest {
@Mock @Mock
private IWindowManagerWrapper mWindowManager; private IWindowManager mWindowManager;
@Mock @Mock
private IBinder mSurfaceFlinger; private IBinder mSurfaceFlinger;
@Mock @Mock

View File

@@ -28,12 +28,12 @@ import android.text.format.DateUtils;
import android.util.KeyValueListParser; import android.util.KeyValueListParser;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.KeyValueListParserWrapper;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.util.ReflectionHelpers;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
public class AnomalyDetectionPolicyTest { public class AnomalyDetectionPolicyTest {
@@ -48,13 +48,12 @@ public class AnomalyDetectionPolicyTest {
+ ",bluetooth_scan_enabled=true" + ",bluetooth_scan_enabled=true"
+ ",bluetooth_scan_threshold=2000"; + ",bluetooth_scan_threshold=2000";
private Context mContext; private Context mContext;
private KeyValueListParserWrapper mKeyValueListParserWrapper; private KeyValueListParser mKeyValueListParser;
@Before @Before
public void setUp() { public void setUp() {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mKeyValueListParserWrapper = spy( mKeyValueListParser = spy((new KeyValueListParser(',')));
new KeyValueListParserWrapper(new KeyValueListParser(',')));
} }
@Test @Test
@@ -77,11 +76,11 @@ public class AnomalyDetectionPolicyTest {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ""); Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
// Mock it to avoid noSuchMethodError // Mock it to avoid noSuchMethodError
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(true)); doReturn(true).when(mKeyValueListParser).getBoolean(anyString(), eq(true));
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(false)); doReturn(false).when(mKeyValueListParser).getBoolean(anyString(), eq(false));
AnomalyDetectionPolicy anomalyDetectionPolicy = AnomalyDetectionPolicy anomalyDetectionPolicy = new AnomalyDetectionPolicy(mContext);
new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper); ReflectionHelpers.setField(anomalyDetectionPolicy, "mParser", mKeyValueListParser);
assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isFalse(); assertThat(anomalyDetectionPolicy.anomalyDetectionEnabled).isFalse();
assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse(); assertThat(anomalyDetectionPolicy.wakeLockDetectionEnabled).isFalse();
@@ -108,11 +107,11 @@ public class AnomalyDetectionPolicyTest {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ""); Settings.Global.ANOMALY_DETECTION_CONSTANTS, "");
// Mock it to avoid noSuchMethodError // Mock it to avoid noSuchMethodError
doReturn(true).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(true)); doReturn(true).when(mKeyValueListParser).getBoolean(anyString(), eq(true));
doReturn(false).when(mKeyValueListParserWrapper).getBoolean(anyString(), eq(false)); doReturn(false).when(mKeyValueListParser).getBoolean(anyString(), eq(false));
AnomalyDetectionPolicy policy = new AnomalyDetectionPolicy(mContext, AnomalyDetectionPolicy policy = new AnomalyDetectionPolicy(mContext);
mKeyValueListParserWrapper); ReflectionHelpers.setField(policy, "mParser", mKeyValueListParser);
assertThat(policy.isAnomalyDetectorEnabled(Anomaly.AnomalyType.WAKE_LOCK)).isFalse(); assertThat(policy.isAnomalyDetectorEnabled(Anomaly.AnomalyType.WAKE_LOCK)).isFalse();
assertThat(policy.isAnomalyDetectorEnabled(Anomaly.AnomalyType.WAKEUP_ALARM)).isFalse(); assertThat(policy.isAnomalyDetectorEnabled(Anomaly.AnomalyType.WAKEUP_ALARM)).isFalse();
@@ -123,15 +122,17 @@ public class AnomalyDetectionPolicyTest {
Settings.Global.putString(mContext.getContentResolver(), Settings.Global.putString(mContext.getContentResolver(),
Settings.Global.ANOMALY_DETECTION_CONSTANTS, ANOMALY_DETECTION_CONSTANTS_VALUE); Settings.Global.ANOMALY_DETECTION_CONSTANTS, ANOMALY_DETECTION_CONSTANTS_VALUE);
// Mock it to avoid noSuchMethodError // Mock it to avoid noSuchMethodError
doReturn(true).when(mKeyValueListParserWrapper) doReturn(true).when(mKeyValueListParser)
.getBoolean(AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, false); .getBoolean(AnomalyDetectionPolicy.KEY_ANOMALY_DETECTION_ENABLED, false);
doReturn(false).when(mKeyValueListParserWrapper) doReturn(false).when(mKeyValueListParser)
.getBoolean(AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, false); .getBoolean(AnomalyDetectionPolicy.KEY_WAKELOCK_DETECTION_ENABLED, false);
doReturn(true).when(mKeyValueListParserWrapper) doReturn(true).when(mKeyValueListParser)
.getBoolean(AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, false); .getBoolean(AnomalyDetectionPolicy.KEY_WAKEUP_ALARM_DETECTION_ENABLED, false);
doReturn(true).when(mKeyValueListParserWrapper) doReturn(true).when(mKeyValueListParser)
.getBoolean(AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false); .getBoolean(AnomalyDetectionPolicy.KEY_BLUETOOTH_SCAN_DETECTION_ENABLED, false);
return new AnomalyDetectionPolicy(mContext, mKeyValueListParserWrapper); final AnomalyDetectionPolicy policy = new AnomalyDetectionPolicy(mContext);
ReflectionHelpers.setField(policy, "mParser", mKeyValueListParser);
return policy;
} }
} }

View File

@@ -28,7 +28,7 @@ import com.android.settings.fuelgauge.anomaly.action.StopAndBackgroundCheckActio
import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector; import com.android.settings.fuelgauge.anomaly.checker.WakeLockAnomalyDetector;
import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector; import com.android.settings.fuelgauge.anomaly.checker.WakeupAlarmAnomalyDetector;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowKeyValueListParserWrapperImpl; import com.android.settings.testutils.shadow.ShadowKeyValueListParser;
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
import org.junit.Before; import org.junit.Before;
@@ -43,7 +43,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = ShadowKeyValueListParserWrapperImpl.class) @Config(shadows = ShadowKeyValueListParser.class)
public class AnomalyUtilsTest { public class AnomalyUtilsTest {
private static final String PACKAGE_NAME_WAKEUP = "com.android.app1"; private static final String PACKAGE_NAME_WAKEUP = "com.android.app1";

View File

@@ -38,7 +38,6 @@ import android.telephony.TelephonyManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowConnectivityManager; import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsWrapper;
import com.android.settings.testutils.shadow.ShadowUserManager; import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
import com.android.settingslib.RestrictedPreference; import com.android.settingslib.RestrictedPreference;
@@ -54,7 +53,6 @@ import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(shadows = { @Config(shadows = {
ShadowRestrictedLockUtilsWrapper.class,
ShadowConnectivityManager.class, ShadowConnectivityManager.class,
ShadowUserManager.class} ShadowUserManager.class}
) )

View File

@@ -0,0 +1,36 @@
/*
* 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.testutils.shadow;
import android.util.KeyValueListParser;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
/**
* Shadow for {@link KeyValueListParser} so we could implement
* {@link #getBoolean(String, boolean)} that doesn't support in the current
* robolectric
*/
@Implements(KeyValueListParser.class)
public class ShadowKeyValueListParser {
@Implementation
public boolean getBoolean(String key, boolean defaultValue) {
return defaultValue;
}
}

View File

@@ -1,20 +0,0 @@
package com.android.settings.testutils.shadow;
import com.android.settings.wrapper.KeyValueListParserWrapper;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
/**
* Shadow for {@link KeyValueListParserWrapperImpl} so we could implement
* {@link #getBoolean(String, boolean)} that doesn't support in the current
* robolectric
*/
@Implements(KeyValueListParserWrapper.class)
public class ShadowKeyValueListParserWrapperImpl {
@Implementation
public boolean getBoolean(String key, boolean defaultValue) {
return defaultValue;
}
}

View File

@@ -14,25 +14,32 @@
* limitations under the License. * limitations under the License.
*/ */
package com.android.settings.wrapper; package com.android.settings.testutils.shadow;
import android.content.Context; import android.content.Context;
import android.os.RecoverySystem; import android.os.RecoverySystem;
/** import org.robolectric.annotation.Implementation;
* This class replicates a subset of the {@link RecoverySystem}. import org.robolectric.annotation.Implements;
* The interface exists so that we can use a thin wrapper around the RecoverySystem in import org.robolectric.annotation.Resetter;
* production code and a mock in tests.
*/
public class RecoverySystemWrapper {
/** @Implements(RecoverySystem.class)
* Returns whether wipe Euicc data successfully or not. public class ShadowRecoverySystem {
*
* @param packageName the package name of the caller app. private static int sWipeEuiccCalledCount;
*/
public boolean wipeEuiccData( @Implementation
Context context, final String packageName) { public static boolean wipeEuiccData(Context context, final String packageName) {
return RecoverySystem.wipeEuiccData(context, packageName); sWipeEuiccCalledCount++;
return true;
}
@Resetter
public static void reset() {
sWipeEuiccCalledCount = 0;
}
public static int getWipeEuiccCalledCount() {
return sWipeEuiccCalledCount;
} }
} }

View File

@@ -1,25 +0,0 @@
package com.android.settings.testutils.shadow;
import android.content.Context;
import com.android.settings.wrapper.RestrictedLockUtilsWrapper;
import org.robolectric.annotation.Implements;
/**
* Shadow for the wrapper around RestrictedLockUtils. Should be removed/updated once robolectric is
* updated to allow usage of new UserManager API's. see
* {@link com.android.settingslib.RestrictedLockUtils} and
* {@link com.android.settings.network.RestrictedLockUtilsWrapper}
*/
@Implements(RestrictedLockUtilsWrapper.class)
public class ShadowRestrictedLockUtilsWrapper {
private boolean isRestricted;
public boolean hasBaseUserRestriction(Context context, String userRestriction, int userId) {
return isRestricted;
}
public void setRestricted(boolean restricted) {
isRestricted = restricted;
}
}