Revert "Revert "Prevent monkey from changing usb settings""
This reverts commit da3f0d9e56
.
Change-Id: I6d63305e18c2b95676feb60f2526dcade951cd3f
This commit is contained in:
committed by
jeffreyhuang
parent
2575d3fd45
commit
5a280667ba
@@ -28,6 +28,7 @@ import android.support.v7.preference.PreferenceScreen;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
import com.android.settingslib.development.DeveloperOptionsPreferenceController;
|
||||||
|
|
||||||
@@ -75,6 +76,10 @@ public class ClearAdbKeysPreferenceController extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean handlePreferenceTreeClick(Preference preference) {
|
public boolean handlePreferenceTreeClick(Preference preference) {
|
||||||
|
if (Utils.isMonkeyRunning()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
|
if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
|
||||||
ClearAdbKeysWarningDialog.show(mFragment);
|
ClearAdbKeysWarningDialog.show(mFragment);
|
||||||
return true;
|
return true;
|
||||||
|
@@ -29,6 +29,7 @@ import android.support.v7.preference.PreferenceScreen;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.Utils;
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
@@ -89,6 +90,10 @@ public class SelectUsbConfigPreferenceController extends
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
|
if (Utils.isMonkeyRunning()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
writeUsbConfigurationOption(newValue.toString());
|
writeUsbConfigurationOption(newValue.toString());
|
||||||
updateUsbConfigurationValues();
|
updateUsbConfigurationValues();
|
||||||
return true;
|
return true;
|
||||||
@@ -138,5 +143,4 @@ public class SelectUsbConfigPreferenceController extends
|
|||||||
setCurrentFunction(newValue, true);
|
setCurrentFunction(newValue, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -38,6 +38,7 @@ import android.support.v7.preference.PreferenceScreen;
|
|||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
|
import com.android.settings.testutils.shadow.SettingsShadowSystemProperties;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -54,7 +55,7 @@ import org.robolectric.util.ReflectionHelpers;
|
|||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH,
|
@Config(manifest = TestConfig.MANIFEST_PATH,
|
||||||
sdk = TestConfig.SDK_VERSION,
|
sdk = TestConfig.SDK_VERSION,
|
||||||
shadows = {SettingsShadowSystemProperties.class})
|
shadows = {SettingsShadowSystemProperties.class, ShadowUtils.class})
|
||||||
public class ClearAdbKeysPreferenceControllerTest {
|
public class ClearAdbKeysPreferenceControllerTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
@@ -83,6 +84,7 @@ public class ClearAdbKeysPreferenceControllerTest {
|
|||||||
public void tearDown() {
|
public void tearDown() {
|
||||||
ShadowClearAdbKeysWarningDialog.resetDialog();
|
ShadowClearAdbKeysWarningDialog.resetDialog();
|
||||||
SettingsShadowSystemProperties.clear();
|
SettingsShadowSystemProperties.clear();
|
||||||
|
ShadowUtils.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -136,6 +138,20 @@ public class ClearAdbKeysPreferenceControllerTest {
|
|||||||
assertThat(isHandled).isFalse();
|
assertThat(isHandled).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void handlePreferenceTreeClick_monkeyUser_shouldReturnFalse() {
|
||||||
|
SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
|
||||||
|
doReturn(true).when(mController).isAdminUser();
|
||||||
|
ShadowUtils.setIsUserAMonkey(true);
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
final String preferenceKey = mController.getPreferenceKey();
|
||||||
|
when(mPreference.getKey()).thenReturn(preferenceKey);
|
||||||
|
|
||||||
|
final boolean isHandled = mController.handlePreferenceTreeClick(mPreference);
|
||||||
|
|
||||||
|
assertThat(isHandled).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onDeveloperOptionsSwitchEnabled_isAdminUser_shouldEnablePreference() {
|
public void onDeveloperOptionsSwitchEnabled_isAdminUser_shouldEnablePreference() {
|
||||||
SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
|
SystemProperties.set(RO_ADB_SECURE_PROPERTY_KEY, Boolean.toString(true));
|
||||||
|
@@ -18,12 +18,14 @@ package com.android.settings.development;
|
|||||||
|
|
||||||
import static android.arch.lifecycle.Lifecycle.Event.ON_CREATE;
|
import static android.arch.lifecycle.Lifecycle.Event.ON_CREATE;
|
||||||
import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
|
import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
|
||||||
|
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.anyBoolean;
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.Mockito.doNothing;
|
import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.doReturn;
|
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 static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
@@ -36,8 +38,10 @@ import android.support.v7.preference.PreferenceScreen;
|
|||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
|
|
||||||
|
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;
|
||||||
@@ -47,7 +51,9 @@ import org.robolectric.RuntimeEnvironment;
|
|||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH,
|
||||||
|
sdk = TestConfig.SDK_VERSION,
|
||||||
|
shadows = {ShadowUtils.class})
|
||||||
public class SelectUsbConfigPreferenceControllerTest {
|
public class SelectUsbConfigPreferenceControllerTest {
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
@@ -88,6 +94,11 @@ public class SelectUsbConfigPreferenceControllerTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void teardown() {
|
||||||
|
ShadowUtils.reset();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onPreferenceChange_setCharging_shouldEnableCharging() {
|
public void onPreferenceChange_setCharging_shouldEnableCharging() {
|
||||||
when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
|
when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
|
||||||
@@ -106,6 +117,18 @@ public class SelectUsbConfigPreferenceControllerTest {
|
|||||||
verify(mController).setCurrentFunction(mValues[1], true /* usb data unlock */);
|
verify(mController).setCurrentFunction(mValues[1], true /* usb data unlock */);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onPreferenceChange_monkeyUser_shouldReturnFalse() {
|
||||||
|
when(mUsbManager.isFunctionEnabled(mValues[1])).thenReturn(true);
|
||||||
|
ShadowUtils.setIsUserAMonkey(true);
|
||||||
|
doNothing().when(mController).setCurrentFunction(anyString(), anyBoolean());
|
||||||
|
|
||||||
|
final boolean isHandled = mController.onPreferenceChange(mPreference, mValues[1]);
|
||||||
|
|
||||||
|
assertThat(isHandled).isFalse();
|
||||||
|
verify(mController, never()).setCurrentFunction(any(), anyBoolean());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateState_chargingEnabled_shouldSetPreferenceToCharging() {
|
public void updateState_chargingEnabled_shouldSetPreferenceToCharging() {
|
||||||
when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
|
when(mUsbManager.isFunctionEnabled(mValues[0])).thenReturn(true);
|
||||||
|
Reference in New Issue
Block a user