Merge "Do not add SelectUsbConfigPreferenceController if USB is disabled."
This commit is contained in:
committed by
Android (Google) Code Review
commit
197c0c83de
@@ -20,6 +20,7 @@ import android.content.BroadcastReceiver;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.annotation.VisibleForTesting;
|
import android.support.annotation.VisibleForTesting;
|
||||||
@@ -109,6 +110,14 @@ public class SelectUsbConfigPreferenceController extends
|
|||||||
mContext.unregisterReceiver(mUsbReceiver);
|
mContext.unregisterReceiver(mUsbReceiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isAvailable() {
|
||||||
|
final PackageManager packageManager = mContext.getPackageManager();
|
||||||
|
|
||||||
|
return packageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
|
||||||
|
|| packageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDeveloperOptionsSwitchEnabled() {
|
protected void onDeveloperOptionsSwitchEnabled() {
|
||||||
mPreference.setEnabled(true);
|
mPreference.setEnabled(true);
|
||||||
|
@@ -20,6 +20,9 @@ import static android.arch.lifecycle.Lifecycle.Event.ON_CREATE;
|
|||||||
import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
|
import static android.arch.lifecycle.Lifecycle.Event.ON_DESTROY;
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertFalse;
|
||||||
|
import static junit.framework.Assert.assertTrue;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
@@ -31,6 +34,7 @@ import static org.mockito.Mockito.verify;
|
|||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.support.v7.preference.ListPreference;
|
import android.support.v7.preference.ListPreference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
@@ -46,6 +50,7 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
@@ -62,6 +67,8 @@ public class SelectUsbConfigPreferenceControllerTest {
|
|||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
@Mock
|
@Mock
|
||||||
private UsbManager mUsbManager;
|
private UsbManager mUsbManager;
|
||||||
|
@Mock
|
||||||
|
private PackageManager mPackageManager;
|
||||||
|
|
||||||
private Context mContext;
|
private Context mContext;
|
||||||
private Lifecycle mLifecycle;
|
private Lifecycle mLifecycle;
|
||||||
@@ -86,6 +93,7 @@ public class SelectUsbConfigPreferenceControllerTest {
|
|||||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
doReturn(mUsbManager).when(mContext).getSystemService(Context.USB_SERVICE);
|
doReturn(mUsbManager).when(mContext).getSystemService(Context.USB_SERVICE);
|
||||||
|
doReturn(mPackageManager).when(mContext).getPackageManager();
|
||||||
mValues = mContext.getResources().getStringArray(R.array.usb_configuration_values);
|
mValues = mContext.getResources().getStringArray(R.array.usb_configuration_values);
|
||||||
mSummaries = mContext.getResources().getStringArray(R.array.usb_configuration_titles);
|
mSummaries = mContext.getResources().getStringArray(R.array.usb_configuration_titles);
|
||||||
mController = spy(new SelectUsbConfigPreferenceController(mContext, mLifecycle));
|
mController = spy(new SelectUsbConfigPreferenceController(mContext, mLifecycle));
|
||||||
@@ -108,6 +116,30 @@ public class SelectUsbConfigPreferenceControllerTest {
|
|||||||
verify(mController).setCurrentFunction(mValues[0], false /* usb data unlock */);
|
verify(mController).setCurrentFunction(mValues[0], false /* usb data unlock */);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onUsbAccessoryAndHostDisabled_shouldNotBeAvailable() {
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
|
||||||
|
false);
|
||||||
|
assertFalse(mController.isAvailable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onUsbHostEnabled_shouldBeAvailable() {
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(true);
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
|
||||||
|
false);
|
||||||
|
assertTrue(mController.isAvailable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onUsbAccessoryEnabled_shouldBeAvailable() {
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)).thenReturn(false);
|
||||||
|
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY)).thenReturn(
|
||||||
|
true);
|
||||||
|
assertTrue(mController.isAvailable());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onPreferenceChange_setMtp_shouldEnableMtp() {
|
public void onPreferenceChange_setMtp_shouldEnableMtp() {
|
||||||
when(mUsbManager.isFunctionEnabled(mValues[1])).thenReturn(true);
|
when(mUsbManager.isFunctionEnabled(mValues[1])).thenReturn(true);
|
||||||
|
Reference in New Issue
Block a user