Merge "Avoid oem-unlock preference controller crash"

This commit is contained in:
TreeHugger Robot
2019-07-09 21:00:04 +00:00
committed by Android (Google) Code Review
2 changed files with 18 additions and 14 deletions

View File

@@ -22,11 +22,13 @@ import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Build;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.service.oemlock.OemLockManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
@@ -43,6 +45,9 @@ public class OemUnlockPreferenceController extends DeveloperOptionsPreferenceCon
private static final String PREFERENCE_KEY = "oem_unlock_enable";
private static final String TAG = "OemUnlockPreferenceController";
private static final String OEM_UNLOCK_SUPPORTED_KEY = "ro.oem_unlock_supported";
private static final String UNSUPPORTED = "-9999";
private static final String SUPPORTED = "1";
private final OemLockManager mOemLockManager;
private final UserManager mUserManager;
@@ -55,8 +60,10 @@ public class OemUnlockPreferenceController extends DeveloperOptionsPreferenceCon
DevelopmentSettingsDashboardFragment fragment) {
super(context);
if (Build.IS_EMULATOR && Build.IS_ENG) {
if (!TextUtils.equals(SystemProperties.get(OEM_UNLOCK_SUPPORTED_KEY, UNSUPPORTED),
SUPPORTED)) {
mOemLockManager = null;
Log.w(TAG, "oem_unlock not supported.");
} else {
mOemLockManager = (OemLockManager) context.getSystemService(Context.OEM_LOCK_SERVICE);
}

View File

@@ -32,7 +32,7 @@ import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.SystemProperties;
import android.os.UserManager;
import android.service.oemlock.OemLockManager;
import android.telephony.TelephonyManager;
@@ -49,12 +49,14 @@ import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class OemUnlockPreferenceControllerTest {
private static final String OEM_UNLOCK_SUPPORTED_KEY = "ro.oem_unlock_supported";
private static final String UNSUPPORTED = "-9999";
private static final String SUPPORTED = "1";
@Mock
private Context mContext;
@Mock
@@ -80,6 +82,7 @@ public class OemUnlockPreferenceControllerTest {
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
SystemProperties.set(OEM_UNLOCK_SUPPORTED_KEY, SUPPORTED);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getPackageManager().hasSystemFeature(PackageManager
.FEATURE_TELEPHONY_CARRIERLOCK)).thenReturn(true);
@@ -96,20 +99,14 @@ public class OemUnlockPreferenceControllerTest {
}
@Test
@Config(qualifiers = "mcc999")
public void OemUnlockPreferenceController_shouldNotCrashInEmulatorEngBuild() {
ReflectionHelpers.setStaticField(Build.class, "IS_EMULATOR", true);
ReflectionHelpers.setStaticField(Build.class, "IS_ENG", true);
public void OemUnlockPreferenceController_oemUnlockUnsupported_shouldNotCrash() {
SystemProperties.set(OEM_UNLOCK_SUPPORTED_KEY, UNSUPPORTED);
new OemUnlockPreferenceController(mContext, mActivity, mFragment);
}
@Test
@Config(qualifiers = "mcc999")
public void OemUnlockPreferenceController_shouldNotCrashInOtherBuild() {
ReflectionHelpers.setStaticField(Build.class, "IS_EMULATOR", false);
ReflectionHelpers.setStaticField(Build.class, "IS_ENG", false);
public void OemUnlockPreferenceController_oemUnlockSupported_shouldNotCrash() {
new OemUnlockPreferenceController(mContext, mActivity, mFragment);
}