Require authentication for changing USB preferences.
Test: atest SettingsRoboTests Test: also tested manually Bug: 317367746 Change-Id: I17daaad7c7772664cae6d12cf2322991827f33bb
This commit is contained in:
@@ -20,3 +20,10 @@ flag {
|
|||||||
description: "Gates whether to enable LE audio private broadcast sharing via QR code"
|
description: "Gates whether to enable LE audio private broadcast sharing via QR code"
|
||||||
bug: "308368124"
|
bug: "308368124"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flag {
|
||||||
|
name: "enable_auth_challenge_for_usb_preferences"
|
||||||
|
namespace: "safety_center"
|
||||||
|
description: "Gates whether to require an auth challenge for changing USB preferences"
|
||||||
|
bug: "317367746"
|
||||||
|
}
|
||||||
|
@@ -23,6 +23,8 @@ import androidx.annotation.UiThread;
|
|||||||
import androidx.annotation.VisibleForTesting;
|
import androidx.annotation.VisibleForTesting;
|
||||||
|
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
|
import com.android.settings.wifi.dpp.WifiDppUtils;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -61,4 +63,16 @@ public abstract class UsbDetailsController extends AbstractPreferenceController
|
|||||||
*/
|
*/
|
||||||
@UiThread
|
@UiThread
|
||||||
protected abstract void refresh(boolean connected, long functions, int powerRole, int dataRole);
|
protected abstract void refresh(boolean connected, long functions, int powerRole, int dataRole);
|
||||||
|
|
||||||
|
/** Protects given action with an auth challenge. */
|
||||||
|
protected final void requireAuthAndExecute(Runnable action) {
|
||||||
|
if (Flags.enableAuthChallengeForUsbPreferences() && !mFragment.isUserAuthenticated()) {
|
||||||
|
WifiDppUtils.showLockScreen(mContext, () -> {
|
||||||
|
mFragment.setUserAuthenticated(true);
|
||||||
|
action.run();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
action.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -98,17 +98,19 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
|
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
|
||||||
int role = UsbBackend.dataRoleFromString(preference.getKey());
|
requireAuthAndExecute(() -> {
|
||||||
if (role != mUsbBackend.getDataRole() && mNextRolePref == null
|
int role = UsbBackend.dataRoleFromString(preference.getKey());
|
||||||
&& !Utils.isMonkeyRunning()) {
|
if (role != mUsbBackend.getDataRole() && mNextRolePref == null
|
||||||
mUsbBackend.setDataRole(role);
|
&& !Utils.isMonkeyRunning()) {
|
||||||
mNextRolePref = preference;
|
mUsbBackend.setDataRole(role);
|
||||||
preference.setSummary(R.string.usb_switching);
|
mNextRolePref = preference;
|
||||||
|
preference.setSummary(R.string.usb_switching);
|
||||||
|
|
||||||
mHandler.postDelayed(mFailureCallback,
|
mHandler.postDelayed(mFailureCallback,
|
||||||
mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
|
mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
|
||||||
: UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
|
: UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@@ -45,6 +45,7 @@ public class UsbDetailsFragment extends DashboardFragment {
|
|||||||
|
|
||||||
private List<UsbDetailsController> mControllers;
|
private List<UsbDetailsController> mControllers;
|
||||||
private UsbBackend mUsbBackend;
|
private UsbBackend mUsbBackend;
|
||||||
|
private boolean mUserAuthenticated = false;
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
UsbConnectionBroadcastReceiver mUsbReceiver;
|
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||||
@@ -56,6 +57,20 @@ public class UsbDetailsFragment extends DashboardFragment {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
boolean isUserAuthenticated() {
|
||||||
|
return mUserAuthenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setUserAuthenticated(boolean userAuthenticated) {
|
||||||
|
mUserAuthenticated = userAuthenticated;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
super.onStart();
|
||||||
|
mUserAuthenticated = false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
super.onViewCreated(view, savedInstanceState);
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
@@ -130,37 +130,39 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
|
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
|
||||||
final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
|
requireAuthAndExecute(() -> {
|
||||||
final long previousFunction = mUsbBackend.getCurrentFunctions();
|
final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
|
||||||
if (DEBUG) {
|
final long previousFunction = mUsbBackend.getCurrentFunctions();
|
||||||
Log.d(TAG, "onRadioButtonClicked() function : " + function + ", toString() : "
|
if (DEBUG) {
|
||||||
+ UsbManager.usbFunctionsToString(function) + ", previousFunction : "
|
Log.d(TAG, "onRadioButtonClicked() function : " + function + ", toString() : "
|
||||||
+ previousFunction + ", toString() : "
|
+ UsbManager.usbFunctionsToString(function) + ", previousFunction : "
|
||||||
+ UsbManager.usbFunctionsToString(previousFunction));
|
+ previousFunction + ", toString() : "
|
||||||
}
|
+ UsbManager.usbFunctionsToString(previousFunction));
|
||||||
if (function != previousFunction && !Utils.isMonkeyRunning()
|
|
||||||
&& !isClickEventIgnored(function, previousFunction)) {
|
|
||||||
mPreviousFunction = previousFunction;
|
|
||||||
|
|
||||||
//Update the UI in advance to make it looks smooth
|
|
||||||
final SelectorWithWidgetPreference prevPref =
|
|
||||||
(SelectorWithWidgetPreference) mProfilesContainer.findPreference(
|
|
||||||
UsbBackend.usbFunctionsToString(mPreviousFunction));
|
|
||||||
if (prevPref != null) {
|
|
||||||
prevPref.setChecked(false);
|
|
||||||
preference.setChecked(true);
|
|
||||||
}
|
}
|
||||||
|
if (function != previousFunction && !Utils.isMonkeyRunning()
|
||||||
|
&& !isClickEventIgnored(function, previousFunction)) {
|
||||||
|
mPreviousFunction = previousFunction;
|
||||||
|
|
||||||
if (function == UsbManager.FUNCTION_RNDIS || function == UsbManager.FUNCTION_NCM) {
|
//Update the UI in advance to make it looks smooth
|
||||||
// We need to have entitlement check for usb tethering, so use API in
|
final SelectorWithWidgetPreference prevPref =
|
||||||
// TetheringManager.
|
(SelectorWithWidgetPreference) mProfilesContainer.findPreference(
|
||||||
mTetheringManager.startTethering(
|
UsbBackend.usbFunctionsToString(mPreviousFunction));
|
||||||
TetheringManager.TETHERING_USB, new HandlerExecutor(mHandler),
|
if (prevPref != null) {
|
||||||
mOnStartTetheringCallback);
|
prevPref.setChecked(false);
|
||||||
} else {
|
preference.setChecked(true);
|
||||||
mUsbBackend.setCurrentFunctions(function);
|
}
|
||||||
|
|
||||||
|
if (function == UsbManager.FUNCTION_RNDIS || function == UsbManager.FUNCTION_NCM) {
|
||||||
|
// We need to have entitlement check for usb tethering, so use API in
|
||||||
|
// TetheringManager.
|
||||||
|
mTetheringManager.startTethering(
|
||||||
|
TetheringManager.TETHERING_USB, new HandlerExecutor(mHandler),
|
||||||
|
mOnStartTetheringCallback);
|
||||||
|
} else {
|
||||||
|
mUsbBackend.setCurrentFunctions(function);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isClickEventIgnored(long function, long previousFunction) {
|
private boolean isClickEventIgnored(long function, long previousFunction) {
|
||||||
|
@@ -78,13 +78,15 @@ public class UsbDetailsTranscodeMtpController extends UsbDetailsController
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onPreferenceClick(Preference preference) {
|
public boolean onPreferenceClick(Preference preference) {
|
||||||
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY,
|
requireAuthAndExecute(() -> {
|
||||||
Boolean.toString(mSwitchPreference.isChecked()));
|
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY,
|
||||||
|
Boolean.toString(mSwitchPreference.isChecked()));
|
||||||
|
|
||||||
final long previousFunctions = mUsbBackend.getCurrentFunctions();
|
final long previousFunctions = mUsbBackend.getCurrentFunctions();
|
||||||
// Toggle the MTP connection to reload file sizes for files shared via MTP clients
|
// Toggle the MTP connection to reload file sizes for files shared via MTP clients
|
||||||
mUsbBackend.setCurrentFunctions(previousFunctions & ~UsbManager.FUNCTION_MTP);
|
mUsbBackend.setCurrentFunctions(previousFunctions & ~UsbManager.FUNCTION_MTP);
|
||||||
mUsbBackend.setCurrentFunctions(previousFunctions);
|
mUsbBackend.setCurrentFunctions(previousFunctions);
|
||||||
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,113 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 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 androidx.test.core.app.ApplicationProvider.getApplicationContext;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.app.KeyguardManager;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.usb.UsbPortStatus;
|
||||||
|
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||||
|
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||||
|
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||||
|
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
import org.robolectric.Shadows;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.ShadowKeyguardManager;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
@Config(shadows = {
|
||||||
|
ShadowKeyguardManager.class
|
||||||
|
})
|
||||||
|
public class UsbDetailsControllerTest {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UsbBackend mUsbBackend;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private UsbDetailsController mUsbDetailsController;
|
||||||
|
private UsbDetailsFragment mUsbDetailsFragment;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = getApplicationContext();
|
||||||
|
mUsbDetailsFragment = new UsbDetailsFragment();
|
||||||
|
mUsbDetailsController = new UsbDetailsController(
|
||||||
|
mContext, mUsbDetailsFragment, mUsbBackend) {
|
||||||
|
@Override
|
||||||
|
protected void refresh(boolean connected, long functions, int powerRole, int dataRole) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getPreferenceKey() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isAvailable_returnsTrue() {
|
||||||
|
assertThat(mUsbDetailsController.isAvailable()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_AUTH_CHALLENGE_FOR_USB_PREFERENCES)
|
||||||
|
public void requireAuthAndExecute_whenAlreadyAuthenticated_executes() {
|
||||||
|
mUsbDetailsFragment.setUserAuthenticated(true);
|
||||||
|
Runnable action = () -> mUsbBackend.setDataRole(UsbPortStatus.DATA_ROLE_HOST);
|
||||||
|
|
||||||
|
mUsbDetailsController.requireAuthAndExecute(action);
|
||||||
|
|
||||||
|
verify(mUsbBackend).setDataRole(anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_AUTH_CHALLENGE_FOR_USB_PREFERENCES)
|
||||||
|
public void requireAuthAndExecute_authenticatesAndExecutes() {
|
||||||
|
mUsbDetailsFragment.setUserAuthenticated(false);
|
||||||
|
setAuthPassesAutomatically();
|
||||||
|
Runnable action = () -> mUsbBackend.setDataRole(UsbPortStatus.DATA_ROLE_HOST);
|
||||||
|
|
||||||
|
mUsbDetailsController.requireAuthAndExecute(action);
|
||||||
|
|
||||||
|
assertThat(mUsbDetailsFragment.isUserAuthenticated()).isTrue();
|
||||||
|
verify(mUsbBackend).setDataRole(anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAuthPassesAutomatically() {
|
||||||
|
Shadows.shadowOf(mContext.getSystemService(KeyguardManager.class))
|
||||||
|
.setIsKeyguardSecure(false);
|
||||||
|
}
|
||||||
|
}
|
@@ -25,12 +25,15 @@ import static android.hardware.usb.UsbPortStatus.POWER_ROLE_SINK;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyLong;
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.KeyguardManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
|
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
@@ -38,6 +41,7 @@ import androidx.preference.PreferenceManager;
|
|||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||||
|
|
||||||
@@ -49,6 +53,7 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@@ -63,12 +68,11 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
private PreferenceCategory mPreference;
|
private PreferenceCategory mPreference;
|
||||||
private PreferenceManager mPreferenceManager;
|
private PreferenceManager mPreferenceManager;
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
|
private UsbDetailsFragment mFragment;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UsbBackend mUsbBackend;
|
private UsbBackend mUsbBackend;
|
||||||
@Mock
|
@Mock
|
||||||
private UsbDetailsFragment mFragment;
|
|
||||||
@Mock
|
|
||||||
private FragmentActivity mActivity;
|
private FragmentActivity mActivity;
|
||||||
@Mock
|
@Mock
|
||||||
private Handler mHandler;
|
private Handler mHandler;
|
||||||
@@ -76,7 +80,7 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mFragment = spy(new UsbDetailsFragment());
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||||
mPreferenceManager = new PreferenceManager(mContext);
|
mPreferenceManager = new PreferenceManager(mContext);
|
||||||
@@ -95,12 +99,12 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
mScreen.addPreference(mPreference);
|
mScreen.addPreference(mPreference);
|
||||||
|
|
||||||
mDetailsDataRoleController.mHandler = mHandler;
|
mDetailsDataRoleController.mHandler = mHandler;
|
||||||
|
|
||||||
|
mDetailsDataRoleController.displayPreference(mScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_deviceRole_shouldCheckDevice() {
|
public void displayRefresh_deviceRole_shouldCheckDevice() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
||||||
DATA_ROLE_DEVICE);
|
DATA_ROLE_DEVICE);
|
||||||
|
|
||||||
@@ -112,8 +116,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_hostRole_shouldCheckHost() {
|
public void displayRefresh_hostRole_shouldCheckHost() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
mDetailsDataRoleController.refresh(true, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
||||||
DATA_ROLE_HOST);
|
DATA_ROLE_HOST);
|
||||||
|
|
||||||
@@ -125,8 +127,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_disconnected_shouldDisable() {
|
public void displayRefresh_disconnected_shouldDisable() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
|
|
||||||
mDetailsDataRoleController.refresh(false, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
mDetailsDataRoleController.refresh(false, UsbManager.FUNCTION_NONE, POWER_ROLE_SINK,
|
||||||
DATA_ROLE_DEVICE);
|
DATA_ROLE_DEVICE);
|
||||||
|
|
||||||
@@ -135,7 +135,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onClickDevice_hostEnabled_shouldSetDevice() {
|
public void onClickDevice_hostEnabled_shouldSetDevice() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
||||||
|
|
||||||
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
@@ -148,7 +147,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onClickDeviceTwice_hostEnabled_shouldSetDeviceOnce() {
|
public void onClickDeviceTwice_hostEnabled_shouldSetDeviceOnce() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
||||||
|
|
||||||
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
@@ -162,7 +160,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
|
public void onClickDeviceAndRefresh_success_shouldClearSubtext() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
||||||
|
|
||||||
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
@@ -180,7 +177,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
|
public void onClickDeviceAndRefresh_failed_shouldShowFailureText() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
||||||
|
|
||||||
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
@@ -199,7 +195,6 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onClickDevice_timedOut_shouldShowFailureText() {
|
public void onClickDevice_timedOut_shouldShowFailureText() {
|
||||||
mDetailsDataRoleController.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
when(mUsbBackend.getDataRole()).thenReturn(DATA_ROLE_HOST);
|
||||||
|
|
||||||
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
final SelectorWithWidgetPreference devicePref = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
@@ -218,6 +213,22 @@ public class UsbDetailsDataRoleControllerTest {
|
|||||||
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
|
.isEqualTo(mContext.getString(R.string.usb_switching_failed));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_AUTH_CHALLENGE_FOR_USB_PREFERENCES)
|
||||||
|
public void onRadioButtonClicked_userAuthenticated() {
|
||||||
|
SelectorWithWidgetPreference preference = getRadioPreference(DATA_ROLE_DEVICE);
|
||||||
|
setAuthPassesAutomatically();
|
||||||
|
|
||||||
|
mDetailsDataRoleController.onRadioButtonClicked(preference);
|
||||||
|
|
||||||
|
assertThat(mFragment.isUserAuthenticated()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAuthPassesAutomatically() {
|
||||||
|
Shadows.shadowOf(mContext.getSystemService(KeyguardManager.class))
|
||||||
|
.setIsKeyguardSecure(false);
|
||||||
|
}
|
||||||
|
|
||||||
private SelectorWithWidgetPreference getRadioPreference(int role) {
|
private SelectorWithWidgetPreference getRadioPreference(int role) {
|
||||||
return (SelectorWithWidgetPreference)
|
return (SelectorWithWidgetPreference)
|
||||||
mPreference.findPreference(UsbBackend.dataRoleToString(role));
|
mPreference.findPreference(UsbBackend.dataRoleToString(role));
|
||||||
|
@@ -30,15 +30,18 @@ import static org.mockito.Mockito.spy;
|
|||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.KeyguardManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.net.TetheringManager;
|
import android.net.TetheringManager;
|
||||||
|
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
import com.android.settingslib.core.lifecycle.Lifecycle;
|
import com.android.settingslib.core.lifecycle.Lifecycle;
|
||||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||||
@@ -51,6 +54,7 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -70,12 +74,11 @@ public class UsbDetailsFunctionsControllerTest {
|
|||||||
private PreferenceManager mPreferenceManager;
|
private PreferenceManager mPreferenceManager;
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
private SelectorWithWidgetPreference mRadioButtonPreference;
|
private SelectorWithWidgetPreference mRadioButtonPreference;
|
||||||
|
private UsbDetailsFragment mFragment;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UsbBackend mUsbBackend;
|
private UsbBackend mUsbBackend;
|
||||||
@Mock
|
@Mock
|
||||||
private UsbDetailsFragment mFragment;
|
|
||||||
@Mock
|
|
||||||
private FragmentActivity mActivity;
|
private FragmentActivity mActivity;
|
||||||
@Mock
|
@Mock
|
||||||
private TetheringManager mTetheringManager;
|
private TetheringManager mTetheringManager;
|
||||||
@@ -83,7 +86,7 @@ public class UsbDetailsFunctionsControllerTest {
|
|||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mFragment = spy(new UsbDetailsFragment());
|
||||||
mContext = spy(RuntimeEnvironment.application);
|
mContext = spy(RuntimeEnvironment.application);
|
||||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||||
mPreferenceManager = new PreferenceManager(mContext);
|
mPreferenceManager = new PreferenceManager(mContext);
|
||||||
@@ -334,6 +337,23 @@ public class UsbDetailsFunctionsControllerTest {
|
|||||||
eq(mDetailsFunctionsController.mOnStartTetheringCallback));
|
eq(mDetailsFunctionsController.mOnStartTetheringCallback));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_AUTH_CHALLENGE_FOR_USB_PREFERENCES)
|
||||||
|
public void onRadioButtonClicked_userAuthenticated() {
|
||||||
|
mRadioButtonPreference.setKey(UsbBackend.usbFunctionsToString(UsbManager.FUNCTION_PTP));
|
||||||
|
doReturn(UsbManager.FUNCTION_MTP).when(mUsbBackend).getCurrentFunctions();
|
||||||
|
setAuthPassesAutomatically();
|
||||||
|
|
||||||
|
mDetailsFunctionsController.onRadioButtonClicked(mRadioButtonPreference);
|
||||||
|
|
||||||
|
assertThat(mFragment.isUserAuthenticated()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAuthPassesAutomatically() {
|
||||||
|
Shadows.shadowOf(mContext.getSystemService(KeyguardManager.class))
|
||||||
|
.setIsKeyguardSecure(false);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onTetheringFailed_resetPreviousFunctions() {
|
public void onTetheringFailed_resetPreviousFunctions() {
|
||||||
mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_PTP;
|
mDetailsFunctionsController.mPreviousFunction = UsbManager.FUNCTION_PTP;
|
||||||
|
@@ -21,11 +21,14 @@ import static android.hardware.usb.UsbPortStatus.POWER_ROLE_NONE;
|
|||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.app.KeyguardManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.hardware.usb.UsbManager;
|
import android.hardware.usb.UsbManager;
|
||||||
import android.os.SystemProperties;
|
import android.os.SystemProperties;
|
||||||
|
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||||
|
|
||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
import androidx.preference.PreferenceCategory;
|
import androidx.preference.PreferenceCategory;
|
||||||
@@ -33,6 +36,7 @@ import androidx.preference.PreferenceManager;
|
|||||||
import androidx.preference.PreferenceScreen;
|
import androidx.preference.PreferenceScreen;
|
||||||
import androidx.preference.SwitchPreference;
|
import androidx.preference.SwitchPreference;
|
||||||
|
|
||||||
|
import com.android.settings.flags.Flags;
|
||||||
import com.android.settings.testutils.shadow.ShadowUtils;
|
import com.android.settings.testutils.shadow.ShadowUtils;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -43,6 +47,7 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.Shadows;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
@@ -57,18 +62,18 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
private PreferenceManager mPreferenceManager;
|
private PreferenceManager mPreferenceManager;
|
||||||
private PreferenceScreen mScreen;
|
private PreferenceScreen mScreen;
|
||||||
private UsbDetailsTranscodeMtpController mUnderTest;
|
private UsbDetailsTranscodeMtpController mUnderTest;
|
||||||
|
private UsbDetailsFragment mFragment;
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UsbBackend mUsbBackend;
|
private UsbBackend mUsbBackend;
|
||||||
@Mock
|
@Mock
|
||||||
private UsbDetailsFragment mFragment;
|
|
||||||
@Mock
|
|
||||||
private FragmentActivity mActivity;
|
private FragmentActivity mActivity;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mFragment = spy(new UsbDetailsFragment());
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mPreferenceManager = new PreferenceManager(mContext);
|
mPreferenceManager = new PreferenceManager(mContext);
|
||||||
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
|
mScreen = mPreferenceManager.createPreferenceScreen(mContext);
|
||||||
@@ -84,11 +89,12 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
mPreference = new PreferenceCategory(mContext);
|
mPreference = new PreferenceCategory(mContext);
|
||||||
mPreference.setKey(mUnderTest.getPreferenceKey());
|
mPreference.setKey(mUnderTest.getPreferenceKey());
|
||||||
mScreen.addPreference(mPreference);
|
mScreen.addPreference(mPreference);
|
||||||
|
|
||||||
|
mUnderTest.displayPreference(mScreen);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_noUsbConnection_shouldDisablePrefCategory() {
|
public void displayRefresh_noUsbConnection_shouldDisablePrefCategory() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
mUnderTest.refresh(false /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
mUnderTest.refresh(false /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
||||||
@@ -99,7 +105,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_noDataTransfer_shouldDisablePrefCategory() {
|
public void displayRefresh_noDataTransfer_shouldDisablePrefCategory() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_NONE, POWER_ROLE_NONE,
|
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_NONE, POWER_ROLE_NONE,
|
||||||
@@ -110,7 +115,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_noDataRole_shouldDisablePrefCategory() throws InterruptedException {
|
public void displayRefresh_noDataRole_shouldDisablePrefCategory() throws InterruptedException {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
||||||
@@ -122,7 +126,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
@Ignore("b/313362757")
|
@Ignore("b/313362757")
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_fileTransfer_withAbsentProp_shouldCheck() {
|
public void displayRefresh_fileTransfer_withAbsentProp_shouldCheck() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
mUnderTest.refresh(true /* connected */, UsbManager.FUNCTION_MTP, POWER_ROLE_NONE,
|
||||||
@@ -134,7 +137,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
@Ignore("b/313362757")
|
@Ignore("b/313362757")
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_fileTransfer_withUnsetProp_shouldUncheck() {
|
public void displayRefresh_fileTransfer_withUnsetProp_shouldUncheck() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(false));
|
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(false));
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
@@ -147,7 +149,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
@Ignore("b/313362757")
|
@Ignore("b/313362757")
|
||||||
@Test
|
@Test
|
||||||
public void displayRefresh_fileTransfer_withSetProp_shouldCheck() {
|
public void displayRefresh_fileTransfer_withSetProp_shouldCheck() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(true));
|
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY, Boolean.toString(true));
|
||||||
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
when(mUsbBackend.areAllRolesSupported()).thenReturn(true);
|
||||||
|
|
||||||
@@ -160,7 +161,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
@Ignore("b/313362757")
|
@Ignore("b/313362757")
|
||||||
@Test
|
@Test
|
||||||
public void click_checked_shouldSetSystemProperty() {
|
public void click_checked_shouldSetSystemProperty() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
getSwitchPreference().performClick();
|
getSwitchPreference().performClick();
|
||||||
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, false)).isTrue();
|
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, false)).isTrue();
|
||||||
}
|
}
|
||||||
@@ -168,7 +168,6 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
@Ignore("b/313362757")
|
@Ignore("b/313362757")
|
||||||
@Test
|
@Test
|
||||||
public void click_unChecked_shouldUnsetSystemProperty() {
|
public void click_unChecked_shouldUnsetSystemProperty() {
|
||||||
mUnderTest.displayPreference(mScreen);
|
|
||||||
getSwitchPreference().performClick();
|
getSwitchPreference().performClick();
|
||||||
getSwitchPreference().performClick();
|
getSwitchPreference().performClick();
|
||||||
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, true)).isFalse();
|
assertThat(SystemProperties.getBoolean(TRANSCODE_MTP_SYS_PROP_KEY, true)).isFalse();
|
||||||
@@ -181,6 +180,21 @@ public class UsbDetailsTranscodeMtpControllerTest {
|
|||||||
assertThat(mUnderTest.isAvailable()).isFalse();
|
assertThat(mUnderTest.isAvailable()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_AUTH_CHALLENGE_FOR_USB_PREFERENCES)
|
||||||
|
public void onClick_userAuthenticated() {
|
||||||
|
setAuthPassesAutomatically();
|
||||||
|
|
||||||
|
mUnderTest.onPreferenceClick(null);
|
||||||
|
|
||||||
|
assertThat(mFragment.isUserAuthenticated()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setAuthPassesAutomatically() {
|
||||||
|
Shadows.shadowOf(mContext.getSystemService(KeyguardManager.class))
|
||||||
|
.setIsKeyguardSecure(false);
|
||||||
|
}
|
||||||
|
|
||||||
private SwitchPreference getSwitchPreference() {
|
private SwitchPreference getSwitchPreference() {
|
||||||
return (SwitchPreference) mPreference.getPreference(0);
|
return (SwitchPreference) mPreference.getPreference(0);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user