Merge "Use new UsbManager#getPorts API"

This commit is contained in:
Philip P. Moltmann
2018-12-19 19:10:06 +00:00
committed by Android (Google) Code Review
13 changed files with 211 additions and 167 deletions

View File

@@ -15,9 +15,12 @@
*/
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
@@ -43,7 +46,7 @@ public class ConnectedUsbDeviceUpdater {
UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
(connected, functions, powerRole, dataRole) -> {
if (connected) {
mUsbPreference.setSummary(getSummary(dataRole == UsbPort.DATA_ROLE_DEVICE
mUsbPreference.setSummary(getSummary(dataRole == DATA_ROLE_DEVICE
? functions : UsbManager.FUNCTION_NONE, powerRole));
mDevicePreferenceCallback.onDeviceAdded(mUsbPreference);
} else {
@@ -100,7 +103,7 @@ public class ConnectedUsbDeviceUpdater {
public static int getSummary(long functions, int power) {
switch (power) {
case UsbPort.POWER_ROLE_SINK:
case POWER_ROLE_SINK:
if (functions == UsbManager.FUNCTION_MTP) {
return R.string.usb_summary_file_transfers;
} else if (functions == UsbManager.FUNCTION_RNDIS) {
@@ -112,7 +115,7 @@ public class ConnectedUsbDeviceUpdater {
} else {
return R.string.usb_summary_charging_only;
}
case UsbPort.POWER_ROLE_SOURCE:
case POWER_ROLE_SOURCE:
if (functions == UsbManager.FUNCTION_MTP) {
return R.string.usb_summary_file_transfers_power;
} else if (functions == UsbManager.FUNCTION_RNDIS) {

View File

@@ -15,6 +15,13 @@
*/
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import static android.service.usb.UsbPortStatusProto.DATA_ROLE_HOST;
import static android.service.usb.UsbPortStatusProto.DATA_ROLE_NONE;
import static android.service.usb.UsbPortStatusProto.POWER_ROLE_SINK;
import android.annotation.Nullable;
import android.content.Context;
import android.content.pm.PackageManager;
@@ -27,6 +34,8 @@ import android.os.UserManager;
import androidx.annotation.VisibleForTesting;
import java.util.List;
/**
* Provides access to underlying system USB functionality.
*/
@@ -96,30 +105,30 @@ public class UsbBackend {
public int getPowerRole() {
updatePorts();
return mPortStatus == null ? UsbPort.POWER_ROLE_NONE : mPortStatus.getCurrentPowerRole();
return mPortStatus == null ? POWER_ROLE_NONE : mPortStatus.getCurrentPowerRole();
}
public int getDataRole() {
updatePorts();
return mPortStatus == null ? UsbPort.DATA_ROLE_NONE : mPortStatus.getCurrentDataRole();
return mPortStatus == null ? DATA_ROLE_NONE : mPortStatus.getCurrentDataRole();
}
public void setPowerRole(int role) {
int newDataRole = getDataRole();
if (!areAllRolesSupported()) {
switch (role) {
case UsbPort.POWER_ROLE_SINK:
newDataRole = UsbPort.DATA_ROLE_DEVICE;
case POWER_ROLE_SINK:
newDataRole = DATA_ROLE_DEVICE;
break;
case UsbPort.POWER_ROLE_SOURCE:
newDataRole = UsbPort.DATA_ROLE_HOST;
case POWER_ROLE_SOURCE:
newDataRole = DATA_ROLE_HOST;
break;
default:
newDataRole = UsbPort.DATA_ROLE_NONE;
newDataRole = DATA_ROLE_NONE;
}
}
if (mPort != null) {
mUsbManager.setPortRoles(mPort, role, newDataRole);
mPort.setRoles(role, newDataRole);
}
}
@@ -127,31 +136,27 @@ public class UsbBackend {
int newPowerRole = getPowerRole();
if (!areAllRolesSupported()) {
switch (role) {
case UsbPort.DATA_ROLE_DEVICE:
newPowerRole = UsbPort.POWER_ROLE_SINK;
case DATA_ROLE_DEVICE:
newPowerRole = POWER_ROLE_SINK;
break;
case UsbPort.DATA_ROLE_HOST:
newPowerRole = UsbPort.POWER_ROLE_SOURCE;
case DATA_ROLE_HOST:
newPowerRole = POWER_ROLE_SOURCE;
break;
default:
newPowerRole = UsbPort.POWER_ROLE_NONE;
newPowerRole = POWER_ROLE_NONE;
}
}
if (mPort != null) {
mUsbManager.setPortRoles(mPort, newPowerRole, role);
mPort.setRoles(newPowerRole, role);
}
}
public boolean areAllRolesSupported() {
return mPort != null && mPortStatus != null
&& mPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE)
&& mPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST)
&& mPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE)
&& mPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE)
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_HOST)
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE)
&& mPortStatus.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST);
}
public static String usbFunctionsToString(long functions) {
@@ -205,17 +210,14 @@ public class UsbBackend {
private void updatePorts() {
mPort = null;
mPortStatus = null;
UsbPort[] ports = mUsbManager.getPorts();
if (ports == null) {
return;
}
List<UsbPort> ports = mUsbManager.getPorts();
// For now look for a connected port, in the future we should identify port in the
// notification and pick based on that.
final int N = ports.length;
final int N = ports.size();
for (int i = 0; i < N; i++) {
UsbPortStatus status = mUsbManager.getPortStatus(ports[i]);
UsbPortStatus status = ports.get(i).getStatus();
if (status.isConnected()) {
mPort = ports[i];
mPort = ports.get(i);
mPortStatus = status;
break;
}

View File

@@ -49,8 +49,8 @@ public class UsbConnectionBroadcastReceiver extends BroadcastReceiver implements
mUsbBackend = backend;
mFunctions = UsbManager.FUNCTION_NONE;
mDataRole = UsbPort.DATA_ROLE_NONE;
mPowerRole = UsbPort.POWER_ROLE_NONE;
mDataRole = UsbPortStatus.DATA_ROLE_NONE;
mPowerRole = UsbPortStatus.POWER_ROLE_NONE;
}
@Override

View File

@@ -16,6 +16,10 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import android.content.Context;
import android.hardware.usb.UsbPort;
@@ -55,23 +59,23 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
mPreferenceCategory = (PreferenceCategory) screen.findPreference(getPreferenceKey());
mHostPref = makeRadioPreference(UsbBackend.dataRoleToString(UsbPort.DATA_ROLE_HOST),
mHostPref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_HOST),
R.string.usb_control_host);
mDevicePref = makeRadioPreference(UsbBackend.dataRoleToString(UsbPort.DATA_ROLE_DEVICE),
mDevicePref = makeRadioPreference(UsbBackend.dataRoleToString(DATA_ROLE_DEVICE),
R.string.usb_control_device);
}
@Override
protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
if (dataRole == UsbPort.DATA_ROLE_DEVICE) {
if (dataRole == DATA_ROLE_DEVICE) {
mDevicePref.setChecked(true);
mHostPref.setChecked(false);
mPreferenceCategory.setEnabled(true);
} else if (dataRole == UsbPort.DATA_ROLE_HOST) {
} else if (dataRole == DATA_ROLE_HOST) {
mDevicePref.setChecked(false);
mHostPref.setChecked(true);
mPreferenceCategory.setEnabled(true);
} else if (!connected || dataRole == UsbPort.DATA_ROLE_NONE){
} else if (!connected || dataRole == DATA_ROLE_NONE){
mPreferenceCategory.setEnabled(false);
if (mNextRolePref == null) {
// Disconnected with no operation pending, so clear subtexts
@@ -80,7 +84,7 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
}
}
if (mNextRolePref != null && dataRole != UsbPort.DATA_ROLE_NONE) {
if (mNextRolePref != null && dataRole != DATA_ROLE_NONE) {
if (UsbBackend.dataRoleFromString(mNextRolePref.getKey()) == dataRole) {
// Clear switching text if switch succeeded
mNextRolePref.setSummary("");

View File

@@ -16,6 +16,7 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.net.ConnectivityManager.TETHERING_USB;
import android.content.Context;
@@ -88,7 +89,7 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
@Override
protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
if (!connected || dataRole != UsbPort.DATA_ROLE_DEVICE) {
if (!connected || dataRole != DATA_ROLE_DEVICE) {
mProfilesContainer.setEnabled(false);
} else {
// Functions are only available in device mode

View File

@@ -16,8 +16,13 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import android.content.Context;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import androidx.preference.Preference;
import androidx.preference.Preference.OnPreferenceClickListener;
@@ -40,16 +45,16 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
private int mNextPowerRole;
private final Runnable mFailureCallback = () -> {
if (mNextPowerRole != UsbPort.POWER_ROLE_NONE) {
if (mNextPowerRole != POWER_ROLE_NONE) {
mSwitchPreference.setSummary(R.string.usb_switching_failed);
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
mNextPowerRole = POWER_ROLE_NONE;
}
};
public UsbDetailsPowerRoleController(Context context, UsbDetailsFragment fragment,
UsbBackend backend) {
super(context, fragment, backend);
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
mNextPowerRole = POWER_ROLE_NONE;
}
@Override
@@ -70,20 +75,21 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
} else if (connected && mUsbBackend.areAllRolesSupported()){
mFragment.getPreferenceScreen().addPreference(mPreferenceCategory);
}
if (powerRole == UsbPort.POWER_ROLE_SOURCE) {
if (powerRole == POWER_ROLE_SOURCE) {
mSwitchPreference.setChecked(true);
mPreferenceCategory.setEnabled(true);
} else if (powerRole == UsbPort.POWER_ROLE_SINK) {
} else if (powerRole == POWER_ROLE_SINK) {
mSwitchPreference.setChecked(false);
mPreferenceCategory.setEnabled(true);
} else if (!connected || powerRole == UsbPort.POWER_ROLE_NONE){
} else if (!connected || powerRole == POWER_ROLE_NONE){
mPreferenceCategory.setEnabled(false);
if (mNextPowerRole == UsbPort.POWER_ROLE_NONE) {
if (mNextPowerRole == POWER_ROLE_NONE) {
mSwitchPreference.setSummary("");
}
}
if (mNextPowerRole != UsbPort.POWER_ROLE_NONE && powerRole != UsbPort.POWER_ROLE_NONE) {
if (mNextPowerRole != POWER_ROLE_NONE
&& powerRole != POWER_ROLE_NONE) {
if (mNextPowerRole == powerRole) {
// Clear switching text if switch succeeded
mSwitchPreference.setSummary("");
@@ -91,16 +97,16 @@ public class UsbDetailsPowerRoleController extends UsbDetailsController
// Set failure text if switch failed
mSwitchPreference.setSummary(R.string.usb_switching_failed);
}
mNextPowerRole = UsbPort.POWER_ROLE_NONE;
mNextPowerRole = POWER_ROLE_NONE;
mHandler.removeCallbacks(mFailureCallback);
}
}
@Override
public boolean onPreferenceClick(Preference preference) {
int newRole = mSwitchPreference.isChecked() ? UsbPort.POWER_ROLE_SOURCE
: UsbPort.POWER_ROLE_SINK;
if (mUsbBackend.getPowerRole() != newRole && mNextPowerRole == UsbPort.POWER_ROLE_NONE
int newRole = mSwitchPreference.isChecked() ? POWER_ROLE_SOURCE
: POWER_ROLE_SINK;
if (mUsbBackend.getPowerRole() != newRole && mNextPowerRole == POWER_ROLE_NONE
&& !Utils.isMonkeyRunning()) {
mUsbBackend.setPowerRole(newRole);

View File

@@ -15,6 +15,11 @@
*/
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
@@ -22,7 +27,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import com.android.settings.R;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
@@ -78,7 +82,7 @@ public class ConnectedUsbDeviceUpdaterTest {
public void initUsbPreference_usbConnected_preferenceAdded() {
mDeviceUpdater.initUsbPreference(mContext);
mDeviceUpdater.mUsbConnectionListener.onUsbConnectionChanged(true /* connected */,
UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
UsbManager.FUNCTION_NONE, POWER_ROLE_SINK, DATA_ROLE_DEVICE);
verify(mDevicePreferenceCallback).onDeviceAdded(mDeviceUpdater.mUsbPreference);
}
@@ -87,7 +91,7 @@ public class ConnectedUsbDeviceUpdaterTest {
public void initUsbPreference_usbDisconnected_preferenceRemoved() {
mDeviceUpdater.initUsbPreference(mContext);
mDeviceUpdater.mUsbConnectionListener.onUsbConnectionChanged(false /* connected */,
UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
UsbManager.FUNCTION_NONE, POWER_ROLE_NONE, DATA_ROLE_NONE);
verify(mDevicePreferenceCallback).onDeviceRemoved(mDeviceUpdater.mUsbPreference);
}

View File

@@ -16,6 +16,11 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
@@ -40,6 +45,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
public class UsbBackendTest {
@@ -64,9 +71,9 @@ public class UsbBackendTest {
when((Object) mContext.getSystemService(UsbManager.class)).thenReturn(mUsbManager);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mConnectivityManager);
when(mUsbManager.getPorts()).thenReturn(new UsbPort[] {mUsbPort});
when(mUsbManager.getPorts()).thenReturn(Collections.singletonList(mUsbPort));
when(mUsbPortStatus.isConnected()).thenReturn(true);
when(mUsbManager.getPortStatus(mUsbPort)).thenReturn(mUsbPortStatus);
when(mUsbPort.getStatus()).thenReturn(mUsbPortStatus);
}
@Test
@@ -74,22 +81,22 @@ public class UsbBackendTest {
final UsbBackend usbBackend = new UsbBackend(mContext, mUserManager);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(POWER_ROLE_SINK);
usbBackend.setDataRole(UsbPort.DATA_ROLE_HOST);
usbBackend.setDataRole(DATA_ROLE_HOST);
verify(mUsbManager).setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST);
verify(mUsbPort).setRoles(POWER_ROLE_SINK, DATA_ROLE_HOST);
}
@Test
@@ -97,17 +104,16 @@ public class UsbBackendTest {
final UsbBackend usbBackend = new UsbBackend(mContext, mUserManager);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(POWER_ROLE_SINK);
usbBackend.setDataRole(UsbPort.DATA_ROLE_HOST);
usbBackend.setDataRole(DATA_ROLE_HOST);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
verify(mUsbPort).setRoles(POWER_ROLE_SOURCE, DATA_ROLE_HOST);
}
@Test
@@ -115,23 +121,22 @@ public class UsbBackendTest {
final UsbBackend usbBackend = new UsbBackend(mContext, mUserManager);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(UsbPort.DATA_ROLE_DEVICE);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(DATA_ROLE_DEVICE);
usbBackend.setPowerRole(UsbPort.POWER_ROLE_SOURCE);
usbBackend.setPowerRole(POWER_ROLE_SOURCE);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE);
verify(mUsbPort).setRoles(POWER_ROLE_SOURCE, DATA_ROLE_DEVICE);
}
@Test
@@ -139,17 +144,16 @@ public class UsbBackendTest {
final UsbBackend usbBackend = new UsbBackend(mContext, mUserManager);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.isRoleCombinationSupported(POWER_ROLE_SINK, DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.isRoleCombinationSupported(POWER_ROLE_SOURCE, DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(UsbPort.DATA_ROLE_DEVICE);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(DATA_ROLE_DEVICE);
usbBackend.setPowerRole(UsbPort.POWER_ROLE_SOURCE);
usbBackend.setPowerRole(POWER_ROLE_SOURCE);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
verify(mUsbPort).setRoles(POWER_ROLE_SOURCE, DATA_ROLE_HOST);
}
@Test

View File

@@ -15,6 +15,11 @@
*/
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
@@ -22,7 +27,6 @@ import static org.mockito.Mockito.verify;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import org.junit.Before;
@@ -63,7 +67,7 @@ public class UsbConnectionBroadcastReceiverTest {
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
}
@Test
@@ -75,7 +79,7 @@ public class UsbConnectionBroadcastReceiverTest {
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
}
@Test
@@ -89,21 +93,21 @@ public class UsbConnectionBroadcastReceiverTest {
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbManager.FUNCTION_MTP,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
}
@Test
public void onReceive_usbPortStatus_invokeCallback() {
final Intent intent = new Intent();
intent.setAction(UsbManager.ACTION_USB_PORT_CHANGED);
final UsbPortStatus status = new UsbPortStatus(0, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE, 0);
final UsbPortStatus status = new UsbPortStatus(0, POWER_ROLE_SINK,
DATA_ROLE_DEVICE, 0);
intent.putExtra(UsbManager.EXTRA_PORT_STATUS, status);
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SINK, DATA_ROLE_DEVICE);
}
@Test

View File

@@ -16,6 +16,12 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
@@ -24,7 +30,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.os.Handler;
import androidx.fragment.app.FragmentActivity;
@@ -92,11 +97,11 @@ public class UsbDetailsDataRoleControllerTest {
public void displayRefresh_deviceRole_shouldCheckDevice() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(DATA_ROLE_HOST);
assertThat(devicePref.isChecked()).isTrue();
assertThat(hostPref.isChecked()).isFalse();
}
@@ -105,11 +110,11 @@ public class UsbDetailsDataRoleControllerTest {
public void displayRefresh_hostRole_shouldCheckHost() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_HOST);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(DATA_ROLE_HOST);
assertThat(devicePref.isChecked()).isFalse();
assertThat(hostPref.isChecked()).isTrue();
}
@@ -118,8 +123,8 @@ public class UsbDetailsDataRoleControllerTest {
public void displayRefresh_disconnected_shouldDisable() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(false, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsDataRoleController.refresh(false, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
assertThat(mPreference.isEnabled()).isFalse();
}
@@ -127,12 +132,12 @@ public class UsbDetailsDataRoleControllerTest {
@Test
public void onClickDevice_hostEnabled_shouldSetDevice() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
verify(mUsbBackend).setDataRole(DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
}
@@ -140,50 +145,50 @@ public class UsbDetailsDataRoleControllerTest {
@Test
public void onClickDeviceTwice_hostEnabled_shouldSetDeviceOnce() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
devicePref.performClick();
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
verify(mUsbBackend).setDataRole(DATA_ROLE_DEVICE);
}
@Test
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
verify(mUsbBackend).setDataRole(DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsDataRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
mDetailsDataRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SINK, DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary()).isEqualTo("");
}
@Test
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
verify(mUsbBackend).setDataRole(DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsDataRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
mDetailsDataRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST);
POWER_ROLE_SINK, DATA_ROLE_HOST);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
@@ -191,18 +196,18 @@ public class UsbDetailsDataRoleControllerTest {
@Test
public void onClickDevice_timedOut_shouldShowFailureText() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
verify(mUsbBackend).setDataRole(DATA_ROLE_DEVICE);
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(mHandler).postDelayed(captor.capture(), anyLong());
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsDataRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
captor.getValue().run();
assertThat(devicePref.getSummary())

View File

@@ -16,6 +16,8 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.net.ConnectivityManager.TETHERING_USB;
import static com.google.common.truth.Truth.assertThat;
@@ -105,8 +107,8 @@ public class UsbDetailsFunctionsControllerTest {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
Iterator<Long> iter = UsbDetailsFunctionsController.FUNCTIONS_MAP.keySet().iterator();
@@ -120,7 +122,7 @@ public class UsbDetailsFunctionsControllerTest {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(false, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SINK, DATA_ROLE_DEVICE);
assertThat(mPreferenceCategory.isEnabled()).isFalse();
}
@@ -131,8 +133,8 @@ public class UsbDetailsFunctionsControllerTest {
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_PTP)).thenReturn(false);
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_RNDIS)).thenReturn(false);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
assertThat(prefs.size()).isEqualTo(1);
assertThat(prefs.get(0).getKey())
@@ -143,8 +145,8 @@ public class UsbDetailsFunctionsControllerTest {
public void displayRefresh_mtpEnabled_shouldCheckSwitches() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
assertThat(prefs.get(0).getKey())
@@ -156,8 +158,8 @@ public class UsbDetailsFunctionsControllerTest {
public void onClickMtp_noneEnabled_shouldEnableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_NONE);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(0).performClick();
@@ -165,8 +167,8 @@ public class UsbDetailsFunctionsControllerTest {
assertThat(prefs.get(0).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
verify(mUsbBackend).setCurrentFunctions(UsbManager.FUNCTION_MTP);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
assertThat(prefs.get(0).isChecked()).isTrue();
}
@@ -174,8 +176,8 @@ public class UsbDetailsFunctionsControllerTest {
public void onClickMtp_ptpEnabled_shouldEnableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_PTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_PTP, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_PTP);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(0).performClick();
@@ -183,8 +185,8 @@ public class UsbDetailsFunctionsControllerTest {
assertThat(prefs.get(0).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
verify(mUsbBackend).setCurrentFunctions(UsbManager.FUNCTION_MTP);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
assertThat(prefs.get(0).isChecked()).isTrue();
assertThat(prefs.get(3).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
@@ -195,8 +197,8 @@ public class UsbDetailsFunctionsControllerTest {
public void onClickNone_mtpEnabled_shouldDisableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_MTP);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(4).performClick();
@@ -204,8 +206,8 @@ public class UsbDetailsFunctionsControllerTest {
assertThat(prefs.get(4).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_NONE));
verify(mUsbBackend).setCurrentFunctions(UsbManager.FUNCTION_NONE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
assertThat(prefs.get(0).isChecked()).isFalse();
}

View File

@@ -16,6 +16,9 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -101,8 +104,8 @@ public class UsbDetailsHeaderControllerTest {
@Test
public void displayRefresh_charging_shouldSetHeader() {
mDetailsHeaderController.displayPreference(mScreen);
mDetailsHeaderController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsHeaderController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
verify(mHeaderController).setLabel(mContext.getString(R.string.usb_pref));
verify(mHeaderController).setIcon(argThat((ArgumentMatcher<Drawable>) t -> {
DrawableTestHelper.assertDrawableResId(t, R.drawable.ic_usb);

View File

@@ -16,6 +16,13 @@
package com.android.settings.connecteddevice.usb;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_DEVICE;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_HOST;
import static android.hardware.usb.UsbPortStatus.DATA_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SOURCE;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyLong;
@@ -25,7 +32,6 @@ import static org.mockito.Mockito.when;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.os.Handler;
import androidx.fragment.app.FragmentActivity;
@@ -95,8 +101,8 @@ public class UsbDetailsPowerRoleControllerTest {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
SwitchPreference pref = getPreference();
assertThat(pref.isChecked()).isFalse();
@@ -108,7 +114,7 @@ public class UsbDetailsPowerRoleControllerTest {
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
POWER_ROLE_SOURCE, DATA_ROLE_HOST);
SwitchPreference pref = getPreference();
assertThat(pref.isChecked()).isTrue();
@@ -120,7 +126,7 @@ public class UsbDetailsPowerRoleControllerTest {
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(false, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SINK, DATA_ROLE_DEVICE);
assertThat(mPreference.isEnabled()).isFalse();
assertThat((Preference) mScreen.findPreference(
@@ -132,8 +138,8 @@ public class UsbDetailsPowerRoleControllerTest {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(false);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
DATA_ROLE_DEVICE);
assertThat((Preference) mScreen.findPreference(
mDetailsPowerRoleController.getPreferenceKey())).isNull();
@@ -142,12 +148,12 @@ public class UsbDetailsPowerRoleControllerTest {
@Test
public void onClick_sink_shouldSetSource() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbBackend.getPowerRole()).thenReturn(POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbBackend).setPowerRole(POWER_ROLE_SOURCE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
}
@@ -155,7 +161,7 @@ public class UsbDetailsPowerRoleControllerTest {
@Test
public void onClickTwice_sink_shouldSetSourceOnce() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbBackend.getPowerRole()).thenReturn(POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
@@ -163,42 +169,42 @@ public class UsbDetailsPowerRoleControllerTest {
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
pref.performClick();
verify(mUsbBackend, times(1)).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbBackend, times(1)).setPowerRole(POWER_ROLE_SOURCE);
}
@Test
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbBackend.getPowerRole()).thenReturn(POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbBackend).setPowerRole(POWER_ROLE_SOURCE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsPowerRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
mDetailsPowerRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SOURCE, DATA_ROLE_DEVICE);
assertThat(pref.getSummary()).isEqualTo("");
}
@Test
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbBackend.getPowerRole()).thenReturn(POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbBackend).setPowerRole(POWER_ROLE_SOURCE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsPowerRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
mDetailsPowerRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
POWER_ROLE_SINK, DATA_ROLE_DEVICE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
@@ -206,18 +212,18 @@ public class UsbDetailsPowerRoleControllerTest {
@Test
public void onClickDevice_timedOut_shouldShowFailureText() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
when(mUsbBackend.getPowerRole()).thenReturn(POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbBackend).setPowerRole(POWER_ROLE_SOURCE);
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
verify(mHandler).postDelayed(captor.capture(), anyLong());
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
mDetailsPowerRoleController.refresh(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
POWER_ROLE_NONE, DATA_ROLE_NONE);
captor.getValue().run();
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));