Merge "Add UsbDefaultFragment and tests"

This commit is contained in:
Jerry Zhang
2018-02-21 21:13:03 +00:00
committed by Android (Google) Code Review
12 changed files with 424 additions and 110 deletions

View File

@@ -8096,6 +8096,10 @@
for this device should be used for. These options are less commonly used.
Choices are usb_use_tethering, usb_use_photo_transfers, usb_use_MIDI, and usb_use_power_only.-->
<string name="usb_use_also">Also use USB for</string>
<!-- The label that leads to the Default USB configuration window. -->
<string name="usb_default_label">Default USB Configuration</string>
<!-- Description at the footer of the default USB configuration window that describes how the setting works. -->
<string name="usb_default_info">When another device is connected and your phone is unlocked, these settings will be applied. Only connect to trusted devices.</string>
<!-- Settings item title for USB preference [CHAR LIMIT=35] -->
<string name="usb_pref">USB</string>

View File

@@ -235,12 +235,11 @@
android:title="@string/tethering_hardware_offload"
android:summary="@string/tethering_hardware_offload_summary" />
<ListPreference
android:key="select_usb_configuration"
android:title="@string/select_usb_configuration_title"
android:dialogTitle="@string/select_usb_configuration_dialog_title"
android:entries="@array/usb_configuration_titles"
android:entryValues="@array/usb_configuration_values" />
<Preference
android:key="default_usb_configuration"
android:fragment="com.android.settings.connecteddevice.usb.UsbDefaultFragment"
android:icon="@drawable/ic_usb"
android:title="@string/usb_default_label"/>
<SwitchPreference
android:key="bluetooth_show_devices_without_names"

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/usb_pref"
android:key="usb_default_fragment">
</PreferenceScreen>

View File

@@ -21,10 +21,12 @@ import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.net.ConnectivityManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.support.annotation.VisibleForTesting;
import com.android.settings.wrapper.UsbManagerWrapper;
import com.android.settings.wrapper.UserManagerWrapper;
public class UsbBackend {
public static final int MODE_POWER_MASK = 0x01;
@@ -47,31 +49,31 @@ public class UsbBackend {
private UsbManager mUsbManager;
@VisibleForTesting
UsbManagerPassThrough mUsbManagerPassThrough;
UsbManagerWrapper mUsbManagerWrapper;
private UsbPort mPort;
private UsbPortStatus mPortStatus;
private Context mContext;
public UsbBackend(Context context) {
this(context, new UserRestrictionUtil(context), null);
this(context, new UserManagerWrapper(UserManager.get(context)), null);
}
@VisibleForTesting
public UsbBackend(Context context, UserRestrictionUtil userRestrictionUtil,
UsbManagerPassThrough usbManagerPassThrough) {
public UsbBackend(Context context, UserManagerWrapper userManagerWrapper,
UsbManagerWrapper usbManagerWrapper) {
mContext = context;
mUsbManager = context.getSystemService(UsbManager.class);
mUsbManagerPassThrough = usbManagerPassThrough;
if (mUsbManagerPassThrough == null) {
mUsbManagerPassThrough = new UsbManagerPassThrough(mUsbManager);
mUsbManagerWrapper = usbManagerWrapper;
if (mUsbManagerWrapper == null) {
mUsbManagerWrapper = new UsbManagerWrapper(mUsbManager);
}
mFileTransferRestricted = userRestrictionUtil.isUsbFileTransferRestricted();
mFileTransferRestrictedBySystem = userRestrictionUtil.isUsbFileTransferRestrictedBySystem();
mTetheringRestricted = userRestrictionUtil.isUsbTetheringRestricted();
mTetheringRestrictedBySystem = userRestrictionUtil.isUsbTetheringRestrictedBySystem();
mFileTransferRestricted = userManagerWrapper.isUsbFileTransferRestricted();
mFileTransferRestrictedBySystem = userManagerWrapper.isUsbFileTransferRestrictedBySystem();
mTetheringRestricted = userManagerWrapper.isUsbTetheringRestricted();
mTetheringRestrictedBySystem = userManagerWrapper.isUsbTetheringRestrictedBySystem();
mMidiSupported = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_MIDI);
ConnectivityManager cm =
@@ -106,37 +108,15 @@ public class UsbBackend {
}
public int getUsbDataMode() {
long functions = mUsbManagerPassThrough.getCurrentFunctions();
if (functions == UsbManager.FUNCTION_MTP) {
return MODE_DATA_MTP;
} else if (functions == UsbManager.FUNCTION_PTP) {
return MODE_DATA_PTP;
} else if (functions == UsbManager.FUNCTION_MIDI) {
return MODE_DATA_MIDI;
} else if (functions == UsbManager.FUNCTION_RNDIS) {
return MODE_DATA_TETHER;
}
return MODE_DATA_NONE;
return usbFunctionToMode(mUsbManagerWrapper.getCurrentFunctions());
}
private void setUsbFunction(int mode) {
switch (mode) {
case MODE_DATA_MTP:
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_MTP);
break;
case MODE_DATA_PTP:
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_PTP);
break;
case MODE_DATA_MIDI:
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_MIDI);
break;
case MODE_DATA_TETHER:
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_RNDIS);
break;
default:
mUsbManager.setCurrentFunctions(UsbManager.FUNCTION_NONE);
break;
public void setDefaultUsbMode(int mode) {
mUsbManager.setScreenUnlockedFunctions(modeToUsbFunction(mode & MODE_DATA_MASK));
}
public int getDefaultUsbMode() {
return usbFunctionToMode(mUsbManager.getScreenUnlockedFunctions());
}
public void setMode(int mode) {
@@ -153,11 +133,6 @@ public class UsbBackend {
setUsbFunction(mode & MODE_DATA_MASK);
}
private int modeToPower(int mode) {
return (mode & MODE_POWER_MASK) == MODE_POWER_SOURCE
? UsbPort.POWER_ROLE_SOURCE : UsbPort.POWER_ROLE_SINK;
}
public boolean isModeDisallowed(int mode) {
if (mFileTransferRestricted && ((mode & MODE_DATA_MASK) == MODE_DATA_MTP
|| (mode & MODE_DATA_MASK) == MODE_DATA_PTP)) {
@@ -201,47 +176,40 @@ public class UsbBackend {
return (mode & MODE_POWER_MASK) != MODE_POWER_SOURCE;
}
// Wrapper class to enable testing with UserManager APIs
public static class UserRestrictionUtil {
private UserManager mUserManager;
public UserRestrictionUtil(Context context) {
mUserManager = UserManager.get(context);
private static int usbFunctionToMode(long functions) {
if (functions == UsbManager.FUNCTION_MTP) {
return MODE_DATA_MTP;
} else if (functions == UsbManager.FUNCTION_PTP) {
return MODE_DATA_PTP;
} else if (functions == UsbManager.FUNCTION_MIDI) {
return MODE_DATA_MIDI;
} else if (functions == UsbManager.FUNCTION_RNDIS) {
return MODE_DATA_TETHER;
}
return MODE_DATA_NONE;
}
public boolean isUsbFileTransferRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
}
public boolean isUsbTetheringRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING);
}
public boolean isUsbFileTransferRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.of(UserHandle.myUserId()));
}
public boolean isUsbTetheringRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
private static long modeToUsbFunction(int mode) {
switch (mode) {
case MODE_DATA_MTP:
return UsbManager.FUNCTION_MTP;
case MODE_DATA_PTP:
return UsbManager.FUNCTION_PTP;
case MODE_DATA_MIDI:
return UsbManager.FUNCTION_MIDI;
case MODE_DATA_TETHER:
return UsbManager.FUNCTION_RNDIS;
default:
return UsbManager.FUNCTION_NONE;
}
}
// Temporary pass-through to allow roboelectric to use getCurrentFunctions()
public static class UsbManagerPassThrough {
private UsbManager mUsbManager;
public UsbManagerPassThrough(UsbManager manager) {
mUsbManager = manager;
private static int modeToPower(int mode) {
return (mode & MODE_POWER_MASK) == MODE_POWER_SOURCE
? UsbPort.POWER_ROLE_SOURCE : UsbPort.POWER_ROLE_SINK;
}
public long getCurrentFunctions() {
return mUsbManager.getCurrentFunctions();
}
public long usbFunctionsFromString(String str) {
return UsbManager.usbFunctionsFromString(str);
}
private void setUsbFunction(int mode) {
mUsbManager.setCurrentFunctions(modeToUsbFunction(mode));
}
}

View File

@@ -0,0 +1,159 @@
/*
* 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.connecteddevice.usb;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.nano.MetricsProto;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.widget.RadioButtonPickerFragment;
import com.android.settingslib.widget.FooterPreference;
import com.android.settingslib.widget.FooterPreferenceMixin;
import com.google.android.collect.Lists;
import java.util.List;
/**
* Provides options for selecting the default USB mode.
*/
public class UsbDefaultFragment extends RadioButtonPickerFragment {
@VisibleForTesting
UsbBackend mUsbBackend;
private static final String[] FUNCTIONS_LIST = {
UsbManager.USB_FUNCTION_NONE,
UsbManager.USB_FUNCTION_MTP,
UsbManager.USB_FUNCTION_RNDIS,
UsbManager.USB_FUNCTION_MIDI,
UsbManager.USB_FUNCTION_PTP
};
@Override
public void onAttach(Context context) {
super.onAttach(context);
mUsbBackend = new UsbBackend(context);
}
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
super.onCreatePreferences(savedInstanceState, rootKey);
FooterPreferenceMixin footer = new FooterPreferenceMixin(this, this.getLifecycle());
FooterPreference pref = footer.createFooterPreference();
pref.setTitle(R.string.usb_default_info);
}
@Override
public int getMetricsCategory() {
return MetricsProto.MetricsEvent.USB_DEFAULT;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.usb_default_fragment;
}
@Override
protected List<? extends CandidateInfo> getCandidates() {
List<CandidateInfo> ret = Lists.newArrayList();
for (final String option : FUNCTIONS_LIST) {
int newMode = 0;
final String title;
final Context context = getContext();
if (option.equals(UsbManager.USB_FUNCTION_MTP)) {
newMode = UsbBackend.MODE_DATA_MTP;
title = context.getString(R.string.usb_use_file_transfers);
} else if (option.equals(UsbManager.USB_FUNCTION_PTP)) {
newMode = UsbBackend.MODE_DATA_PTP;
title = context.getString(R.string.usb_use_photo_transfers);
} else if (option.equals(UsbManager.USB_FUNCTION_MIDI)) {
newMode = UsbBackend.MODE_DATA_MIDI;
title = context.getString(R.string.usb_use_MIDI);
} else if (option.equals(UsbManager.USB_FUNCTION_RNDIS)) {
newMode = UsbBackend.MODE_DATA_TETHER;
title = context.getString(R.string.usb_use_tethering);
} else if (option.equals(UsbManager.USB_FUNCTION_NONE)) {
newMode = UsbBackend.MODE_DATA_NONE;
title = context.getString(R.string.usb_use_charging_only);
} else {
title = "";
}
// Only show supported and allowed options
if (mUsbBackend.isModeSupported(newMode)
&& !mUsbBackend.isModeDisallowedBySystem(newMode)
&& !mUsbBackend.isModeDisallowed(newMode)) {
ret.add(new CandidateInfo(true /* enabled */) {
@Override
public CharSequence loadLabel() {
return title;
}
@Override
public Drawable loadIcon() {
return null;
}
@Override
public String getKey() {
return option;
}
});
}
}
return ret;
}
@Override
protected String getDefaultKey() {
switch (mUsbBackend.getDefaultUsbMode()) {
case UsbBackend.MODE_DATA_MTP:
return UsbManager.USB_FUNCTION_MTP;
case UsbBackend.MODE_DATA_PTP:
return UsbManager.USB_FUNCTION_PTP;
case UsbBackend.MODE_DATA_TETHER:
return UsbManager.USB_FUNCTION_RNDIS;
case UsbBackend.MODE_DATA_MIDI:
return UsbManager.USB_FUNCTION_MIDI;
default:
return UsbManager.USB_FUNCTION_NONE;
}
}
@Override
protected boolean setDefaultKey(String key) {
int thisMode = UsbBackend.MODE_DATA_NONE;
if (key.equals(UsbManager.USB_FUNCTION_MTP)) {
thisMode = UsbBackend.MODE_DATA_MTP;
} else if (key.equals(UsbManager.USB_FUNCTION_PTP)) {
thisMode = UsbBackend.MODE_DATA_PTP;
} else if (key.equals(UsbManager.USB_FUNCTION_RNDIS)) {
thisMode = UsbBackend.MODE_DATA_TETHER;
} else if (key.equals(UsbManager.USB_FUNCTION_MIDI)) {
thisMode = UsbBackend.MODE_DATA_MIDI;
}
if (!Utils.isMonkeyRunning()) {
mUsbBackend.setDefaultUsbMode(thisMode);
}
return true;
}
}

View File

@@ -401,7 +401,6 @@ public class DevelopmentSettingsDashboardFragment extends RestrictedDashboardFra
controllers.add(new WifiConnectedMacRandomizationPreferenceController(context));
controllers.add(new MobileDataAlwaysOnPreferenceController(context));
controllers.add(new TetheringHardwareAccelPreferenceController(context));
controllers.add(new SelectUsbConfigPreferenceController(context, lifecycle));
controllers.add(new BluetoothDeviceNoNamePreferenceController(context));
controllers.add(new BluetoothAbsoluteVolumePreferenceController(context));
controllers.add(new BluetoothInbandRingingPreferenceController(context));

View File

@@ -30,8 +30,8 @@ import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.wrapper.UsbManagerWrapper;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.connecteddevice.usb.UsbBackend;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnCreate;
@@ -49,7 +49,7 @@ public class SelectUsbConfigPreferenceController extends
private final String[] mListSummaries;
private final UsbManager mUsbManager;
@VisibleForTesting
UsbBackend.UsbManagerPassThrough mUsbManagerPassThrough;
UsbManagerWrapper mUsbManagerWrapper;
private BroadcastReceiver mUsbReceiver;
private ListPreference mPreference;
@@ -59,7 +59,7 @@ public class SelectUsbConfigPreferenceController extends
mListValues = context.getResources().getStringArray(R.array.usb_configuration_values);
mListSummaries = context.getResources().getStringArray(R.array.usb_configuration_titles);
mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
mUsbManagerPassThrough = new UsbBackend.UsbManagerPassThrough(mUsbManager);
mUsbManagerWrapper = new UsbManagerWrapper(mUsbManager);
mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
@@ -98,7 +98,7 @@ public class SelectUsbConfigPreferenceController extends
return false;
}
writeUsbConfigurationOption(mUsbManagerPassThrough
writeUsbConfigurationOption(mUsbManagerWrapper
.usbFunctionsFromString(newValue.toString()));
updateUsbConfigurationValues();
return true;
@@ -138,10 +138,10 @@ public class SelectUsbConfigPreferenceController extends
}
private void updateUsbConfigurationValues() {
long functions = mUsbManagerPassThrough.getCurrentFunctions();
long functions = mUsbManagerWrapper.getCurrentFunctions();
int index = 0;
for (int i = 0; i < mListValues.length; i++) {
if (functions == mUsbManagerPassThrough.usbFunctionsFromString(mListValues[i])) {
if (functions == mUsbManagerWrapper.usbFunctionsFromString(mListValues[i])) {
index = i;
break;
}

View File

@@ -0,0 +1,34 @@
/*
* 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.hardware.usb.UsbManager;
public class UsbManagerWrapper {
private UsbManager mUsbManager;
public UsbManagerWrapper(UsbManager manager) {
mUsbManager = manager;
}
public long getCurrentFunctions() {
return mUsbManager.getCurrentFunctions();
}
public long usbFunctionsFromString(String str) {
return UsbManager.usbFunctionsFromString(str);
}
}

View File

@@ -17,6 +17,7 @@
package com.android.settings.wrapper;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import java.util.List;
@@ -45,4 +46,22 @@ public class UserManagerWrapper {
public List<UserInfo> getProfiles(int userHandle) {
return mUserManager.getProfiles(userHandle);
}
public boolean isUsbFileTransferRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
}
public boolean isUsbTetheringRestricted() {
return mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING);
}
public boolean isUsbFileTransferRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_USB_FILE_TRANSFER, UserHandle.of(UserHandle.myUserId()));
}
public boolean isUsbTetheringRestrictedBySystem() {
return mUserManager.hasBaseUserRestriction(
UserManager.DISALLOW_CONFIG_TETHERING, UserHandle.of(UserHandle.myUserId()));
}
}

View File

@@ -27,9 +27,9 @@ import android.content.pm.PackageManager;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import com.android.settings.connecteddevice.usb.UsbBackend;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.wrapper.UserManagerWrapper;
import org.junit.Before;
import org.junit.Test;
@@ -47,7 +47,7 @@ public class UsbBackendTest {
@Mock
private UsbManager mUsbManager;
@Mock
private UsbBackend.UserRestrictionUtil mUserRestrictionUtil;
private UserManagerWrapper mUserManagerWrapper;
@Mock
private ConnectivityManager mConnectivityManager;
@@ -63,7 +63,7 @@ public class UsbBackendTest {
@Test
public void constructor_noUsbPort_shouldNotCrash() {
UsbBackend usbBackend = new UsbBackend(mContext, mUserRestrictionUtil, null);
UsbBackend usbBackend = new UsbBackend(mContext, mUserManagerWrapper, null);
// Should not crash
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.connecteddevice.usb;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.hardware.usb.UsbManager;
import com.android.settings.TestConfig;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class UsbDefaultFragmentTest {
@Mock
private UsbBackend mUsbBackend;
private UsbDefaultFragment mFragment;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mFragment = new UsbDefaultFragment();
mFragment.mUsbBackend = mUsbBackend;
}
@Test
public void testGetDefaultKey_isNone_shouldReturnNone() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_NONE);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_NONE);
}
@Test
public void testGetDefaultKey_isMtp_shouldReturnMtp() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_MTP);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
}
@Test
public void testGetDefaultKey_isPtp_shouldReturnPtp() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_PTP);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_PTP);
}
@Test
public void testGetDefaultKey_isRndis_shouldReturnRndis() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_TETHER);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_RNDIS);
}
@Test
public void testGetDefaultKey_isMidi_shouldReturnMidi() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_MIDI);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_MIDI);
}
@Test
public void testSetDefaultKey_isNone_shouldSetNone() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_NONE);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_NONE);
}
@Test
public void testSetDefaultKey_isMtp_shouldSetMtp() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_MTP);
}
@Test
public void testSetDefaultKey_isPtp_shouldSetPtp() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_PTP);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_PTP);
}
@Test
public void testSetDefaultKey_isRndis_shouldSetRndis() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_RNDIS);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_TETHER);
}
@Test
public void testSetDefaultKey_isMidi_shouldSetMidi() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_MIDI);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_MIDI);
}
}

View File

@@ -44,7 +44,7 @@ import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.TestConfig;
import com.android.settings.connecteddevice.usb.UsbBackend;
import com.android.settings.wrapper.UsbManagerWrapper;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
@@ -73,7 +73,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Mock
private PackageManager mPackageManager;
@Mock
private UsbBackend.UsbManagerPassThrough mUsbManagerPassThrough;
private UsbManagerWrapper mUsbManagerWrapper;
private Context mContext;
private LifecycleOwner mLifecycleOwner;
@@ -106,12 +106,12 @@ public class SelectUsbConfigPreferenceControllerTest {
mController = spy(new SelectUsbConfigPreferenceController(mContext, mLifecycle));
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
mController.mUsbManagerPassThrough = mUsbManagerPassThrough;
mController.mUsbManagerWrapper = mUsbManagerWrapper;
when(mUsbManagerPassThrough.usbFunctionsFromString("mtp")).thenReturn(UsbManagerExtras.MTP);
when(mUsbManagerPassThrough.usbFunctionsFromString("rndis"))
when(mUsbManagerWrapper.usbFunctionsFromString("mtp")).thenReturn(UsbManagerExtras.MTP);
when(mUsbManagerWrapper.usbFunctionsFromString("rndis"))
.thenReturn(UsbManagerExtras.RNDIS);
when(mUsbManagerPassThrough.usbFunctionsFromString("none"))
when(mUsbManagerWrapper.usbFunctionsFromString("none"))
.thenReturn(UsbManagerExtras.NONE);
}
@@ -123,7 +123,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void onPreferenceChange_setCharging_shouldEnableCharging() {
when(mUsbManagerPassThrough.getCurrentFunctions()).thenReturn(
when(mUsbManagerWrapper.getCurrentFunctions()).thenReturn(
UsbManagerExtras.usbFunctionsFromString(mValues[0]));
doNothing().when(mController).setCurrentFunctions(anyLong());
mController.onPreferenceChange(mPreference, mValues[0]);
@@ -158,7 +158,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void onPreferenceChange_setMtp_shouldEnableMtp() {
when(mUsbManagerPassThrough.getCurrentFunctions())
when(mUsbManagerWrapper.getCurrentFunctions())
.thenReturn(UsbManagerExtras.usbFunctionsFromString(mValues[1]));
doNothing().when(mController).setCurrentFunctions(anyLong());
mController.onPreferenceChange(mPreference, mValues[1]);
@@ -169,7 +169,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void onPreferenceChange_monkeyUser_shouldReturnFalse() {
when(mUsbManagerPassThrough.getCurrentFunctions())
when(mUsbManagerWrapper.getCurrentFunctions())
.thenReturn(UsbManagerExtras.usbFunctionsFromString(mValues[1]));
ShadowUtils.setIsUserAMonkey(true);
doNothing().when(mController).setCurrentFunctions(anyLong());
@@ -182,7 +182,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void updateState_chargingEnabled_shouldSetPreferenceToCharging() {
when(mUsbManagerPassThrough.getCurrentFunctions())
when(mUsbManagerWrapper.getCurrentFunctions())
.thenReturn(UsbManagerExtras.usbFunctionsFromString(mValues[0]));
mController.updateState(mPreference);
@@ -193,7 +193,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void updateState_RndisEnabled_shouldEnableRndis() {
when(mUsbManagerPassThrough.getCurrentFunctions())
when(mUsbManagerWrapper.getCurrentFunctions())
.thenReturn(UsbManagerExtras.usbFunctionsFromString(mValues[3]));
mController.updateState(mPreference);
@@ -204,7 +204,7 @@ public class SelectUsbConfigPreferenceControllerTest {
@Test
public void updateState_noValueSet_shouldEnableChargingAsDefault() {
when(mUsbManagerPassThrough.getCurrentFunctions()).thenReturn(UsbManagerExtras.NONE);
when(mUsbManagerWrapper.getCurrentFunctions()).thenReturn(UsbManagerExtras.NONE);
mController.updateState(mPreference);
verify(mPreference).setValue(mValues[0]);