Merge "Update USB settings screen" into pi-dev

This commit is contained in:
Jerry Zhang
2018-03-16 22:40:38 +00:00
committed by Android (Google) Code Review
23 changed files with 1510 additions and 747 deletions

View File

@@ -16,10 +16,13 @@
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.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import com.android.settings.R;
import com.android.settings.connecteddevice.DevicePreferenceCallback;
@@ -60,7 +63,7 @@ public class ConnectedUsbDeviceUpdaterTest {
}
@Test
public void testInitUsbPreference_preferenceInit() {
public void initUsbPreference_preferenceInit() {
mDeviceUpdater.initUsbPreference(mContext);
assertThat(mDeviceUpdater.mUsbPreference.getTitle()).isEqualTo("USB");
@@ -70,20 +73,20 @@ public class ConnectedUsbDeviceUpdaterTest {
}
@Test
public void testInitUsbPreference_usbConnected_preferenceAdded() {
public void initUsbPreference_usbConnected_preferenceAdded() {
mDeviceUpdater.initUsbPreference(mContext);
mDeviceUpdater.mUsbConnectionListener
.onUsbConnectionChanged(true /* connected */, UsbBackend.MODE_DATA_NONE);
mDeviceUpdater.mUsbConnectionListener.onUsbConnectionChanged(true /* connected */,
UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
verify(mDevicePreferenceCallback).onDeviceAdded(mDeviceUpdater.mUsbPreference);
}
@Test
public void testInitUsbPreference_usbDisconnected_preferenceRemoved() {
public void initUsbPreference_usbDisconnected_preferenceRemoved() {
mDeviceUpdater.initUsbPreference(mContext);
mDeviceUpdater.mUsbConnectionListener
.onUsbConnectionChanged(false /* connected */, UsbBackend.MODE_DATA_NONE);
mDeviceUpdater.mUsbConnectionListener.onUsbConnectionChanged(false /* connected */,
UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
verify(mDevicePreferenceCallback).onDeviceRemoved(mDeviceUpdater.mUsbPreference);
}
}
}

View File

@@ -16,15 +16,22 @@
package com.android.settings.connecteddevice.usb;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.hardware.usb.UsbPortStatus;
import android.net.ConnectivityManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.wrapper.UsbManagerWrapper;
import com.android.settings.wrapper.UserManagerWrapper;
import org.junit.Before;
@@ -43,7 +50,13 @@ public class UsbBackendTest {
@Mock
private UserManagerWrapper mUserManagerWrapper;
@Mock
private UsbManagerWrapper mUsbManagerWrapper;
@Mock
private ConnectivityManager mConnectivityManager;
@Mock
private UsbPort mUsbPort;
@Mock
private UsbPortStatus mUsbPortStatus;
@Before
public void setUp() {
@@ -53,11 +66,124 @@ 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(mUsbPortStatus.isConnected()).thenReturn(true);
when(mUsbManager.getPortStatus(mUsbPort)).thenReturn(mUsbPortStatus);
}
@Test
public void constructor_noUsbPort_shouldNotCrash() {
UsbBackend usbBackend = new UsbBackend(mContext, mUserManagerWrapper, null);
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
// Should not crash
}
@Test
public void setDataRole_allRolesSupported_shouldSetDataRole() {
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
usbBackend.setDataRole(UsbPort.DATA_ROLE_HOST);
verify(mUsbManager).setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST);
}
@Test
public void setDataRole_notAllRolesSupported_shouldSetDataAndPowerRole() {
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
usbBackend.setDataRole(UsbPort.DATA_ROLE_HOST);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
}
@Test
public void setPowerRole_allRolesSupported_shouldSetPowerRole() {
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(UsbPort.DATA_ROLE_DEVICE);
usbBackend.setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE);
}
@Test
public void setPowerRole_notAllRolesSupported_shouldSetDataAndPowerRole() {
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE))
.thenReturn(true);
when(mUsbPortStatus
.isRoleCombinationSupported(UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST))
.thenReturn(true);
when(mUsbPortStatus.getCurrentDataRole()).thenReturn(UsbPort.DATA_ROLE_DEVICE);
usbBackend.setPowerRole(UsbPort.POWER_ROLE_SOURCE);
verify(mUsbManager)
.setPortRoles(mUsbPort, UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
}
@Test
public void areFunctionsSupported_fileTransferDisallowed_shouldReturnFalse() {
when(mUserManagerWrapper.isUsbFileTransferRestricted()).thenReturn(true);
when(mUserManagerWrapper.isUsbFileTransferRestrictedBySystem()).thenReturn(true);
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
assertThat(usbBackend.areFunctionsSupported(UsbManager.FUNCTION_MTP)).isFalse();
}
@Test
public void areFunctionsSupported_fileTransferAllowed_shouldReturnTrue() {
when(mUserManagerWrapper.isUsbFileTransferRestricted()).thenReturn(false);
when(mUserManagerWrapper.isUsbFileTransferRestrictedBySystem()).thenReturn(false);
final UsbBackend usbBackend =
new UsbBackend(mContext, mUserManagerWrapper, mUsbManagerWrapper);
assertThat(usbBackend.areFunctionsSupported(UsbManager.FUNCTION_MTP)).isTrue();
}
}

View File

@@ -16,11 +16,14 @@
package com.android.settings.connecteddevice.usb;
import static com.google.common.truth.Truth.assertThat;
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 com.android.settings.testutils.SettingsRobolectricTestRunner;
@@ -53,29 +56,31 @@ public class UsbConnectionBroadcastReceiverTest {
}
@Test
public void testOnReceive_usbConnected_invokeCallback() {
public void onReceive_usbConnected_invokeCallback() {
final Intent intent = new Intent();
intent.setAction(UsbManager.ACTION_USB_STATE);
intent.putExtra(UsbManager.USB_CONNECTED, true);
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbBackend.MODE_DATA_NONE);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
}
@Test
public void testOnReceive_usbDisconnected_invokeCallback() {
public void onReceive_usbDisconnected_invokeCallback() {
final Intent intent = new Intent();
intent.setAction(UsbManager.ACTION_USB_STATE);
intent.putExtra(UsbManager.USB_CONNECTED, false);
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(false /* connected */, UsbBackend.MODE_DATA_NONE);
verify(mListener).onUsbConnectionChanged(false /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
}
@Test
public void testOnReceive_usbConnectedMtpEnabled_invokeCallback() {
public void onReceive_usbConnectedMtpEnabled_invokeCallback() {
final Intent intent = new Intent();
intent.setAction(UsbManager.ACTION_USB_STATE);
intent.putExtra(UsbManager.USB_CONNECTED, true);
@@ -84,11 +89,26 @@ public class UsbConnectionBroadcastReceiverTest {
mReceiver.onReceive(mContext, intent);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbBackend.MODE_DATA_MTP);
verify(mListener).onUsbConnectionChanged(true /* connected */, UsbManager.FUNCTION_MTP,
UsbPort.POWER_ROLE_NONE, UsbPort.DATA_ROLE_NONE);
}
@Test
public void testRegister_invokeMethodTwice_registerOnce() {
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);
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);
}
@Test
public void register_invokeMethodTwice_registerOnce() {
mReceiver.register();
mReceiver.register();
@@ -96,7 +116,7 @@ public class UsbConnectionBroadcastReceiverTest {
}
@Test
public void testUnregister_invokeMethodTwice_unregisterOnce() {
public void unregister_invokeMethodTwice_unregisterOnce() {
mReceiver.register();
mReceiver.unregister();
mReceiver.unregister();
@@ -113,4 +133,4 @@ public class UsbConnectionBroadcastReceiverTest {
}
return count;
}
}
}

View File

@@ -18,17 +18,20 @@ 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.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import android.hardware.usb.UsbManager;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils;
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)
public class UsbDefaultFragmentTest {
@@ -46,62 +49,75 @@ public class UsbDefaultFragmentTest {
}
@Test
public void testGetDefaultKey_isNone_shouldReturnNone() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_NONE);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_NONE);
public void getDefaultKey_isNone_shouldReturnNone() {
when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_NONE);
assertThat(mFragment.getDefaultKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_NONE));
}
@Test
public void testGetDefaultKey_isMtp_shouldReturnMtp() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_MTP);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
public void getDefaultKey_isMtp_shouldReturnMtp() {
when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_MTP);
assertThat(mFragment.getDefaultKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
}
@Test
public void testGetDefaultKey_isPtp_shouldReturnPtp() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_PTP);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_PTP);
public void getDefaultKey_isPtp_shouldReturnPtp() {
when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_PTP);
assertThat(mFragment.getDefaultKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
}
@Test
public void testGetDefaultKey_isRndis_shouldReturnRndis() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_TETHER);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_RNDIS);
public void getDefaultKey_isRndis_shouldReturnRndis() {
when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_RNDIS);
assertThat(mFragment.getDefaultKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_RNDIS));
}
@Test
public void testGetDefaultKey_isMidi_shouldReturnMidi() {
when(mUsbBackend.getDefaultUsbMode()).thenReturn(UsbBackend.MODE_DATA_MIDI);
assertThat(mFragment.getDefaultKey()).isEqualTo(UsbManager.USB_FUNCTION_MIDI);
public void getDefaultKey_isMidi_shouldReturnMidi() {
when(mUsbBackend.getDefaultUsbFunctions()).thenReturn(UsbManager.FUNCTION_MIDI);
assertThat(mFragment.getDefaultKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MIDI));
}
@Test
public void testSetDefaultKey_isNone_shouldSetNone() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_NONE);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_NONE);
public void setDefaultKey_isNone_shouldSetNone() {
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_NONE));
verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_NONE);
}
@Test
public void testSetDefaultKey_isMtp_shouldSetMtp() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_MTP);
public void setDefaultKey_isMtp_shouldSetMtp() {
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_MTP);
}
@Test
public void testSetDefaultKey_isPtp_shouldSetPtp() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_PTP);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_PTP);
public void setDefaultKey_isPtp_shouldSetPtp() {
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_PTP);
}
@Test
public void testSetDefaultKey_isRndis_shouldSetRndis() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_RNDIS);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_TETHER);
public void setDefaultKey_isRndis_shouldSetRndis() {
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_RNDIS));
verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_RNDIS);
}
@Test
public void testSetDefaultKey_isMidi_shouldSetMidi() {
mFragment.setDefaultKey(UsbManager.USB_FUNCTION_MIDI);
verify(mUsbBackend).setDefaultUsbMode(UsbBackend.MODE_DATA_MIDI);
public void setDefaultKey_isMidi_shouldSetMidi() {
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MIDI));
verify(mUsbBackend).setDefaultUsbFunctions(UsbManager.FUNCTION_MIDI);
}
@Test
@Config(shadows = ShadowUtils.class)
public void setDefaultKey_isMonkey_shouldDoNothing() {
ShadowUtils.setIsUserAMonkey(true);
mFragment.setDefaultKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
verifyZeroInteractions(mUsbBackend);
}
}

View File

@@ -0,0 +1,215 @@
/*
* 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.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.os.Handler;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.widget.RadioButtonPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
@RunWith(SettingsRobolectricTestRunner.class)
public class UsbDetailsDataRoleControllerTest {
private UsbDetailsDataRoleController mDetailsDataRoleController;
private Context mContext;
private Lifecycle mLifecycle;
private PreferenceCategory mPreference;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
@Mock
private UsbBackend mUsbBackend;
@Mock
private UsbDetailsFragment mFragment;
@Mock
private Activity mActivity;
@Mock
private Handler mHandler;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycle = new Lifecycle(() -> mLifecycle);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getApplicationContext()).thenReturn(mContext);
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
mDetailsDataRoleController = new UsbDetailsDataRoleController(mContext, mFragment,
mUsbBackend);
mPreference = new PreferenceCategory(mContext);
mPreference.setKey(mDetailsDataRoleController.getPreferenceKey());
mScreen.addPreference(mPreference);
mDetailsDataRoleController.mHandler = mHandler;
}
@Test
public void displayRefresh_deviceRole_shouldCheckDevice() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(UsbPort.DATA_ROLE_HOST);
assertThat(devicePref.isChecked()).isTrue();
assertThat(hostPref.isChecked()).isFalse();
}
@Test
public void displayRefresh_hostRole_shouldCheckHost() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
final RadioButtonPreference hostPref = getRadioPreference(UsbPort.DATA_ROLE_HOST);
assertThat(devicePref.isChecked()).isFalse();
assertThat(hostPref.isChecked()).isTrue();
}
@Test
public void displayRefresh_disconnected_shouldDisable() {
mDetailsDataRoleController.displayPreference(mScreen);
mDetailsDataRoleController.refresh(false, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void onClickDevice_hostEnabled_shouldSetDevice() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
}
@Test
public void onClickDeviceTwice_hostEnabled_shouldSetDeviceOnce() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
devicePref.performClick();
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.DATA_ROLE_DEVICE);
}
@Test
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.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);
mDetailsDataRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
assertThat(devicePref.getSummary()).isEqualTo("");
}
@Test
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.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);
mDetailsDataRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_HOST);
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
@Test
public void onClickDevice_timedOut_shouldShowFailureText() {
mDetailsDataRoleController.displayPreference(mScreen);
when(mUsbBackend.getDataRole()).thenReturn(UsbPort.DATA_ROLE_HOST);
final RadioButtonPreference devicePref = getRadioPreference(UsbPort.DATA_ROLE_DEVICE);
devicePref.performClick();
verify(mUsbBackend).setDataRole(UsbPort.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);
captor.getValue().run();
assertThat(devicePref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
private RadioButtonPreference getRadioPreference(int role) {
return (RadioButtonPreference)
mPreference.findPreference(UsbBackend.dataRoleToString(role));
}
}

View File

@@ -0,0 +1,218 @@
/*
* 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.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settings.widget.RadioButtonPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class UsbDetailsFunctionsControllerTest {
private UsbDetailsFunctionsController mDetailsFunctionsController;
private Context mContext;
private Lifecycle mLifecycle;
private PreferenceCategory mPreference;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
@Mock
private UsbBackend mUsbBackend;
@Mock
private UsbDetailsFragment mFragment;
@Mock
private Activity mActivity;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycle = new Lifecycle(() -> mLifecycle);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getApplicationContext()).thenReturn(mContext);
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
mDetailsFunctionsController = new UsbDetailsFunctionsController(mContext, mFragment,
mUsbBackend);
mPreference = new PreferenceCategory(mContext);
mPreference.setKey(mDetailsFunctionsController.getPreferenceKey());
mScreen.addPreference(mPreference);
}
@Test
public void displayRefresh_allAllowed_shouldCreatePrefs() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
Iterator<Long> iter = UsbDetailsFunctionsController.FUNCTIONS_MAP.keySet().iterator();
for (RadioButtonPreference pref : prefs) {
assertThat(pref.getKey()).isEqualTo(UsbBackend.usbFunctionsToString(iter.next()));
}
}
@Test
public void displayRefresh_disconnected_shouldDisable() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(false, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
assertThat(mPreference.isEnabled()).isFalse();
}
@Test
public void displayRefresh_onlyMidiAllowed_shouldCreateOnlyMidiPref() {
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_MIDI)).thenReturn(true);
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_MTP)).thenReturn(false);
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_PTP)).thenReturn(false);
when(mUsbBackend.areFunctionsSupported(UsbManager.FUNCTION_RNDIS)).thenReturn(false);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
assertThat(prefs.size()).isEqualTo(1);
assertThat(prefs.get(0).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MIDI));
}
@Test
public void displayRefresh_mtpEnabled_shouldCheckSwitches() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
List<RadioButtonPreference> prefs = getRadioPreferences();
assertThat(prefs.get(0).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_MTP));
assertThat(prefs.get(0).isChecked()).isTrue();
}
@Test
public void onClickMtp_noneEnabled_shouldEnableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_NONE);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(0).performClick();
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);
assertThat(prefs.get(0).isChecked()).isTrue();
}
@Test
public void onClickMtp_ptpEnabled_shouldEnableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_PTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_PTP);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(0).performClick();
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);
assertThat(prefs.get(0).isChecked()).isTrue();
assertThat(prefs.get(3).getKey())
.isEqualTo(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
assertThat(prefs.get(3).isChecked()).isFalse();
}
@Test
public void onClickNone_mtpEnabled_shouldDisableMtp() {
when(mUsbBackend.areFunctionsSupported(anyLong())).thenReturn(true);
mDetailsFunctionsController.displayPreference(mScreen);
mDetailsFunctionsController.refresh(true, UsbManager.FUNCTION_MTP, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
when(mUsbBackend.getCurrentFunctions()).thenReturn(UsbManager.FUNCTION_MTP);
List<RadioButtonPreference> prefs = getRadioPreferences();
prefs.get(4).performClick();
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);
assertThat(prefs.get(0).isChecked()).isFalse();
}
@Test
@Config(shadows = ShadowUtils.class)
public void isAvailable_isMonkey_shouldReturnFalse() {
ShadowUtils.setIsUserAMonkey(true);
assertThat(mDetailsFunctionsController.isAvailable()).isFalse();
}
private List<RadioButtonPreference> getRadioPreferences() {
ArrayList<RadioButtonPreference> result = new ArrayList<>();
for (int i = 0; i < mPreference.getPreferenceCount(); i++) {
result.add((RadioButtonPreference) mPreference.getPreference(i));
}
return result;
}
}

View File

@@ -23,6 +23,8 @@ import android.app.Activity;
import android.arch.lifecycle.LifecycleOwner;
import android.content.Context;
import android.support.v14.preference.PreferenceFragment;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
@@ -58,7 +60,7 @@ public class UsbDetailsHeaderControllerTest {
@Mock
private UsbBackend mUsbBackend;
@Mock
private PreferenceFragment mFragment;
private UsbDetailsFragment mFragment;
@Mock
private Activity mActivity;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -95,22 +97,10 @@ public class UsbDetailsHeaderControllerTest {
@Test
public void displayRefresh_charging_shouldSetHeader() {
mDetailsHeaderController.displayPreference(mScreen);
mDetailsHeaderController.refresh(UsbBackend.MODE_DATA_NONE);
mDetailsHeaderController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
verify(mHeaderController).setLabel(mContext.getString(R.string.usb_pref));
verify(mHeaderController).setIcon(mContext.getDrawable(R.drawable.ic_usb));
verify(mHeaderController)
.setSummary(mContext.getString(R.string.usb_summary_charging_only));
verify(mHeaderController).done(mActivity, true);
}
@Test
public void displayRefresh_mtp_shouldSetHeader() {
mDetailsHeaderController.displayPreference(mScreen);
mDetailsHeaderController.refresh(UsbBackend.MODE_DATA_MTP);
verify(mHeaderController).setLabel(mContext.getString(R.string.usb_pref));
verify(mHeaderController).setIcon(mContext.getDrawable(R.drawable.ic_usb));
verify(mHeaderController)
.setSummary(mContext.getString(R.string.usb_summary_file_transfers));
verify(mHeaderController).done(mActivity, true);
}
}

View File

@@ -0,0 +1,229 @@
/*
* 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.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.os.Handler;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settingslib.core.lifecycle.Lifecycle;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(SettingsRobolectricTestRunner.class)
public class UsbDetailsPowerRoleControllerTest {
private UsbDetailsPowerRoleController mDetailsPowerRoleController;
private Context mContext;
private Lifecycle mLifecycle;
private PreferenceCategory mPreference;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
@Mock
private UsbBackend mUsbBackend;
@Mock
private UsbDetailsFragment mFragment;
@Mock
private Activity mActivity;
@Mock
private Handler mHandler;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycle = new Lifecycle(() -> mLifecycle);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getApplicationContext()).thenReturn(mContext);
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
mDetailsPowerRoleController = new UsbDetailsPowerRoleController(mContext, mFragment,
mUsbBackend);
mPreference = new PreferenceCategory(mContext);
mPreference.setKey(mDetailsPowerRoleController.getPreferenceKey());
mScreen.addPreference(mPreference);
mDetailsPowerRoleController.mHandler = mHandler;
}
@Test
public void displayRefresh_sink_shouldUncheck() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
SwitchPreference pref = getPreference();
assertThat(pref.isChecked()).isFalse();
}
@Test
public void displayRefresh_source_shouldCheck() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_HOST);
SwitchPreference pref = getPreference();
assertThat(pref.isChecked()).isTrue();
}
@Test
public void displayRefresh_disconnected_shouldDisable() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
mDetailsPowerRoleController.refresh(false, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
assertThat(mPreference.isEnabled()).isFalse();
assertThat(mScreen.findPreference(mDetailsPowerRoleController.getPreferenceKey()))
.isEqualTo(mPreference);
}
@Test
public void displayRefresh_notSupported_shouldRemove() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.areAllRolesSupported()).thenReturn(false);
mDetailsPowerRoleController.refresh(true, UsbManager.FUNCTION_NONE, UsbPort.POWER_ROLE_SINK,
UsbPort.DATA_ROLE_DEVICE);
assertThat(mScreen.findPreference(mDetailsPowerRoleController.getPreferenceKey())).isNull();
}
@Test
public void onClick_sink_shouldSetSource() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
}
@Test
public void onClickTwice_sink_shouldSetSourceOnce() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching));
pref.performClick();
verify(mUsbBackend, times(1)).setPowerRole(UsbPort.POWER_ROLE_SOURCE);
}
@Test
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.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);
mDetailsPowerRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SOURCE, UsbPort.DATA_ROLE_DEVICE);
assertThat(pref.getSummary()).isEqualTo("");
}
@Test
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.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);
mDetailsPowerRoleController.refresh(true /* connected */, UsbManager.FUNCTION_NONE,
UsbPort.POWER_ROLE_SINK, UsbPort.DATA_ROLE_DEVICE);
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
@Test
public void onClickDevice_timedOut_shouldShowFailureText() {
mDetailsPowerRoleController.displayPreference(mScreen);
when(mUsbBackend.getPowerRole()).thenReturn(UsbPort.POWER_ROLE_SINK);
SwitchPreference pref = getPreference();
pref.performClick();
verify(mUsbBackend).setPowerRole(UsbPort.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);
captor.getValue().run();
assertThat(pref.getSummary())
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
}
private SwitchPreference getPreference() {
return (SwitchPreference) mPreference.getPreference(0);
}
}

View File

@@ -1,238 +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.connecteddevice.usb;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbManager;
import android.support.v14.preference.PreferenceFragment;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.testutils.shadow.ShadowUtils;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.google.android.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class)
public class UsbDetailsProfilesControllerTest {
private UsbDetailsProfilesController mDetailsProfilesController;
private Context mContext;
private Lifecycle mLifecycle;
private PreferenceCategory mPreference;
private PreferenceManager mPreferenceManager;
private PreferenceScreen mScreen;
private List<String> mOptions;
@Mock
private UsbBackend mUsbBackend;
@Mock
private PreferenceFragment mFragment;
@Mock
private Activity mActivity;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mLifecycle = new Lifecycle(() -> mLifecycle);
mPreferenceManager = new PreferenceManager(mContext);
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
when(mFragment.getActivity()).thenReturn(mActivity);
when(mActivity.getApplicationContext()).thenReturn(mContext);
when(mFragment.getContext()).thenReturn(mContext);
when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
mOptions = Lists.newArrayList(UsbManager.USB_FUNCTION_MTP, UsbManager.USB_FUNCTION_PTP,
UsbManager.USB_FUNCTION_MIDI, UsbDetailsProfilesController.KEY_POWER);
mDetailsProfilesController = new UsbDetailsProfilesController(mContext, mFragment,
mUsbBackend, mOptions, "usb_options");
mPreference = new PreferenceCategory(mContext);
mPreference.setKey(mDetailsProfilesController.getPreferenceKey());
mScreen.addPreference(mPreference);
}
@Test
public void testDisplayRefresh_allAllowed_shouldCreateSwitches() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_NONE);
List<SwitchPreference> switches = getProfileSwitches();
for (int i = 0; i < switches.size(); i++) {
assertThat(switches.get(i).getKey()).isEqualTo(mOptions.get(i));
}
}
@Test
public void testDisplayRefresh_onlyMidiAllowed_shouldCreateOnlyMidiSwitch() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(UsbBackend.MODE_DATA_MIDI)).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(UsbBackend.MODE_DATA_MTP)).thenReturn(true);
when(mUsbBackend.isModeDisallowedBySystem(UsbBackend.MODE_DATA_PTP)).thenReturn(true);
when(mUsbBackend.isModeDisallowedBySystem(UsbBackend.MODE_POWER_SOURCE)).thenReturn(true);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_NONE);
List<SwitchPreference> switches = getProfileSwitches();
assertThat(switches.size()).isEqualTo(1);
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MIDI);
}
@Test
public void testDisplayRefresh_mtpEnabled_shouldCheckSwitches() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_MTP);
List<SwitchPreference> switches = getProfileSwitches();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
assertThat(switches.get(0).isChecked()).isTrue();
}
@Test
public void testDisplayRefresh_mtpSupplyPowerEnabled_shouldCheckSwitches() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_MTP | UsbBackend.MODE_POWER_SOURCE);
List<SwitchPreference> switches = getProfileSwitches();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
assertThat(switches.get(0).isChecked()).isTrue();
assertThat(switches.get(3).getKey()).isEqualTo(UsbDetailsProfilesController.KEY_POWER);
assertThat(switches.get(3).isChecked()).isTrue();
}
@Test
public void testOnClickMtp_noneEnabled_shouldEnableMtp() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_NONE);
List<SwitchPreference> switches = getProfileSwitches();
switches.get(0).performClick();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setMode(UsbBackend.MODE_DATA_MTP);
assertThat(switches.get(0).isChecked()).isTrue();
}
@Test
public void testOnClickMtp_supplyingPowerEnabled_shouldEnableBoth() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_POWER_SOURCE);
when(mUsbBackend.getCurrentMode()).thenReturn(UsbBackend.MODE_POWER_SOURCE);
List<SwitchPreference> switches = getProfileSwitches();
switches.get(0).performClick();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setMode(UsbBackend.MODE_DATA_MTP | UsbBackend.MODE_POWER_SOURCE);
assertThat(switches.get(0).isChecked()).isTrue();
assertThat(switches.get(3).getKey()).isEqualTo(UsbDetailsProfilesController.KEY_POWER);
assertThat(switches.get(3).isChecked()).isTrue();
}
@Test
public void testOnClickMtp_ptpEnabled_shouldEnableMtpOnly() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_PTP);
when(mUsbBackend.getCurrentMode()).thenReturn(UsbBackend.MODE_DATA_PTP);
List<SwitchPreference> switches = getProfileSwitches();
switches.get(0).performClick();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setMode(UsbBackend.MODE_DATA_MTP);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_MTP);
assertThat(switches.get(0).isChecked()).isTrue();
assertThat(switches.get(1).getKey()).isEqualTo(UsbManager.USB_FUNCTION_PTP);
assertThat(switches.get(1).isChecked()).isFalse();
}
@Test
public void testOnClickMtp_mtpEnabled_shouldDisableMtp() {
when(mUsbBackend.isModeSupported(anyInt())).thenReturn(true);
when(mUsbBackend.isModeDisallowed(anyInt())).thenReturn(false);
when(mUsbBackend.isModeDisallowedBySystem(anyInt())).thenReturn(false);
mDetailsProfilesController.displayPreference(mScreen);
mDetailsProfilesController.refresh(UsbBackend.MODE_DATA_MTP);
when(mUsbBackend.getCurrentMode()).thenReturn(UsbBackend.MODE_DATA_MTP);
List<SwitchPreference> switches = getProfileSwitches();
switches.get(0).performClick();
assertThat(switches.get(0).getKey()).isEqualTo(UsbManager.USB_FUNCTION_MTP);
verify(mUsbBackend).setMode(UsbBackend.MODE_DATA_NONE);
assertThat(switches.get(0).isChecked()).isFalse();
}
@Test
@Config(shadows = ShadowUtils.class)
public void testIsAvailable_isMonkey_shouldReturnFalse() {
ShadowUtils.setIsUserAMonkey(true);
assertThat(mDetailsProfilesController.isAvailable()).isFalse();
}
private List<SwitchPreference> getProfileSwitches() {
List<SwitchPreference> result = new ArrayList<>();
for (int i = 0; i < mPreference.getPreferenceCount(); i++) {
result.add((SwitchPreference) mPreference.getPreference(i));
}
return result;
}
}