Refactor isCurrentProcessUserHasRestriction()
CarUserManagerHelper is in the process of getting deprecated and
removed. The isCurrentProcessUserHasRestriction() method is only a
wrapper method for UserManager API calls.
This CL updates Settings to call UserManager directly.
Bug: 137136907
Test: atest CarSettingsRoboTests
Test: atest CarUserManagerHelperTest
Change-Id: I52cc903169dc91354043ddd9b580743d3eb5acdf
diff --git a/tests/robotests/src/com/android/car/settings/applications/ApplicationDetailsFragmentTest.java b/tests/robotests/src/com/android/car/settings/applications/ApplicationDetailsFragmentTest.java
index 8c9ac2a..7da1425 100644
--- a/tests/robotests/src/com/android/car/settings/applications/ApplicationDetailsFragmentTest.java
+++ b/tests/robotests/src/com/android/car/settings/applications/ApplicationDetailsFragmentTest.java
@@ -157,8 +157,8 @@
getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME));
mActivity.launchFragment(mFragment);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_APPS_CONTROL)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_APPS_CONTROL, true);
mController.start();
assertThat(findForceStopButton(mActivity).isEnabled()).isFalse();
@@ -436,8 +436,8 @@
getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME));
mActivity.launchFragment(mFragment);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_APPS_CONTROL)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_APPS_CONTROL, true);
mController.start();
assertThat(findUninstallButton(mActivity).isEnabled()).isFalse();
@@ -448,8 +448,8 @@
getShadowPackageManager().addPackage(createPackageInfoWithApplicationInfo(PACKAGE_NAME));
mActivity.launchFragment(mFragment);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_UNINSTALL_APPS)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), UserManager.DISALLOW_UNINSTALL_APPS, true);
mController.start();
assertThat(findUninstallButton(mActivity).isEnabled()).isFalse();
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothBondedDevicesPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothBondedDevicesPreferenceControllerTest.java
index bb92029..0d74bb0 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothBondedDevicesPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothBondedDevicesPreferenceControllerTest.java
@@ -30,8 +30,9 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.car.drivingstate.CarUxRestrictions;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceGroup;
@@ -40,7 +41,6 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -56,19 +56,17 @@
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
/** Unit test for {@link BluetoothBondedDevicesPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothBondedDevicesPreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private CachedBluetoothDevice mBondedCachedDevice;
@Mock
private BluetoothDevice mBondedDevice;
@@ -80,14 +78,16 @@
private PreferenceControllerTestHelper<BluetoothBondedDevicesPreferenceController>
mControllerHelper;
private BluetoothBondedDevicesPreferenceController mController;
+ private Context mContext;
+ private UserHandle mMyUserHandle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
- Context context = RuntimeEnvironment.application;
+ mContext = RuntimeEnvironment.application;
+ mMyUserHandle = UserHandle.of(UserHandle.myUserId());
- mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, /* onInitCallback= */
+ mLocalBluetoothManager = LocalBluetoothManager.getInstance(mContext, /* onInitCallback= */
null);
mSaveRealCachedDeviceManager = mLocalBluetoothManager.getCachedDeviceManager();
ReflectionHelpers.setField(mLocalBluetoothManager, "mCachedDeviceManager",
@@ -104,20 +104,19 @@
Arrays.asList(mBondedCachedDevice, unbondedCachedDevice));
// Make sure controller is available.
- Shadows.shadowOf(context.getPackageManager()).setSystemFeature(
+ Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
BluetoothAdapter.getDefaultAdapter().enable();
getShadowBluetoothAdapter().setState(BluetoothAdapter.STATE_ON);
- mPreferenceGroup = new LogicalPreferenceGroup(context);
- mControllerHelper = new PreferenceControllerTestHelper<>(context,
+ mPreferenceGroup = new LogicalPreferenceGroup(mContext);
+ mControllerHelper = new PreferenceControllerTestHelper<>(mContext,
BluetoothBondedDevicesPreferenceController.class, mPreferenceGroup);
mController = mControllerHelper.getController();
}
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
ReflectionHelpers.setField(mLocalBluetoothManager, "mCachedDeviceManager",
mSaveRealCachedDeviceManager);
@@ -172,8 +171,7 @@
@Test
public void devicePreferenceButtonClicked_noUserRestrictions_launchesDetailsFragment() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(false);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_CONFIG_BLUETOOTH, false);
mControllerHelper.markState(Lifecycle.State.STARTED);
BluetoothDevicePreference devicePreference =
(BluetoothDevicePreference) mPreferenceGroup.getPreference(0);
@@ -187,8 +185,7 @@
@Test
public void devicePreferenceButton_disallowConfigBluetooth_actionStaysHidden() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_CONFIG_BLUETOOTH, true);
mControllerHelper.markState(Lifecycle.State.STARTED);
BluetoothDevicePreference devicePreference =
(BluetoothDevicePreference) mPreferenceGroup.getPreference(0);
@@ -229,4 +226,8 @@
private ShadowBluetoothAdapter getShadowBluetoothAdapter() {
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothDevicePreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothDevicePreferenceControllerTest.java
index 05f44ff..66e4f57 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothDevicePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothDevicePreferenceControllerTest.java
@@ -25,13 +25,13 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
import static org.testng.Assert.assertThrows;
import android.bluetooth.BluetoothAdapter;
import android.car.drivingstate.CarUxRestrictions;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
@@ -40,7 +40,6 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import org.junit.After;
@@ -55,16 +54,14 @@
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link BluetoothDevicePreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothDevicePreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private CachedBluetoothDevice mDevice;
private Context mContext;
private PreferenceControllerTestHelper<TestBluetoothDevicePreferenceController>
@@ -73,7 +70,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
// Make sure controller is available.
@@ -90,7 +86,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
}
@@ -104,8 +99,8 @@
@Test
public void getAvailabilityStatus_disallowConfigBluetooth_disabledForUser() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_CONFIG_BLUETOOTH, true);
assertThat(mControllerHelper.getController().getAvailabilityStatus()).isEqualTo(
DISABLED_FOR_USER);
@@ -149,6 +144,10 @@
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
+
/** Concrete impl of {@link BluetoothDevicePreferenceController} for testing. */
private static class TestBluetoothDevicePreferenceController extends
BluetoothDevicePreferenceController<Preference> {
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothEntryPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothEntryPreferenceControllerTest.java
index 3987c74..ec00913 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothEntryPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothEntryPreferenceControllerTest.java
@@ -25,71 +25,63 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.when;
-
-import android.car.userlib.CarUserManagerHelper;
+import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import com.android.car.settings.common.PreferenceControllerTestHelper;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
-import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link BluetoothEntryPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class})
public class BluetoothEntryPreferenceControllerTest {
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
+ private Context mContext;
private BluetoothEntryPreferenceController mController;
+ private UserHandle mMyUserHandle;
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
+ mContext = RuntimeEnvironment.application;
+ mMyUserHandle = UserHandle.of(UserHandle.myUserId());
mController = new PreferenceControllerTestHelper<>(RuntimeEnvironment.application,
BluetoothEntryPreferenceController.class).getController();
}
- @After
- public void tearDown() {
- ShadowCarUserManagerHelper.reset();
- }
-
@Test
public void getAvailabilityStatus_bluetoothAvailable_available() {
- Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
+ Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(any())).thenReturn(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_bluetoothAvailable_disallowBluetooth_disabledForUser() {
- Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
+ Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@Test
public void getAvailabilityStatus_bluetoothNotAvailable_unsupportedOnDevice() {
- Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
+ Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothNamePreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothNamePreferenceControllerTest.java
index 22814a7..f9167f8 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothNamePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothNamePreferenceControllerTest.java
@@ -24,12 +24,12 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
import android.content.Intent;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
@@ -37,31 +37,27 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link BluetoothNamePreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothNamePreferenceControllerTest {
private static final String NAME = "name";
private static final String NAME_UPDATED = "name updated";
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
private Preference mPreference;
private PreferenceControllerTestHelper<BluetoothNamePreferenceController> mControllerHelper;
private BluetoothNamePreferenceController mController;
@@ -69,7 +65,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
Context context = RuntimeEnvironment.application;
// Make sure controller is available.
@@ -87,7 +82,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
}
@@ -109,8 +103,8 @@
@Test
public void refreshUi_userHasConfigRestriction_setsNotSelectable() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_CONFIG_BLUETOOTH, true);
mController.refreshUi();
@@ -165,4 +159,8 @@
private ShadowBluetoothAdapter getShadowBluetoothAdapter() {
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(RuntimeEnvironment.application));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothPreferenceControllerTest.java
index 498aa7b..c4e2d80 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothPreferenceControllerTest.java
@@ -27,12 +27,12 @@
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothAdapter;
import android.car.drivingstate.CarUxRestrictions;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
@@ -41,7 +41,6 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -56,17 +55,15 @@
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
import org.robolectric.util.ReflectionHelpers;
/** Unit test for {@link BluetoothPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothPreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private BluetoothEventManager mEventManager;
private BluetoothEventManager mSaveRealEventManager;
private LocalBluetoothManager mLocalBluetoothManager;
@@ -76,7 +73,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
Context context = RuntimeEnvironment.application;
mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, /* onInitCallback= */
null);
@@ -89,7 +85,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
ReflectionHelpers.setField(mLocalBluetoothManager, "mEventManager", mSaveRealEventManager);
}
@@ -106,8 +101,8 @@
public void getAvailabilityStatus_disallowBluetoothUserRestriction_disabledForUser() {
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -160,6 +155,10 @@
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(RuntimeEnvironment.application));
+ }
+
/** Concrete impl of {@link BluetoothPreferenceController} for testing. */
private static class TestBluetoothPreferenceController extends
BluetoothPreferenceController<Preference> {
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothScanningDevicesGroupPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothScanningDevicesGroupPreferenceControllerTest.java
index 94d75e0..0d01aa2 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothScanningDevicesGroupPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothScanningDevicesGroupPreferenceControllerTest.java
@@ -27,9 +27,10 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.car.drivingstate.CarUxRestrictions;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
import android.content.Intent;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceGroup;
@@ -56,19 +57,17 @@
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
import org.robolectric.util.ReflectionHelpers;
import java.util.Collections;
/** Unit test for {@link BluetoothScanningDevicesGroupPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothScanningDevicesGroupPreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private CachedBluetoothDevice mCachedDevice;
@Mock
private BluetoothDevice mDevice;
@@ -85,7 +84,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
mLocalBluetoothManager = LocalBluetoothManager.getInstance(mContext, /* onInitCallback= */
@@ -122,8 +120,8 @@
@Test
public void disallowConfigBluetooth_doesNotStartScanning() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_CONFIG_BLUETOOTH, true);
mControllerHelper.markState(Lifecycle.State.STARTED);
@@ -316,6 +314,10 @@
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
+
private static final class TestBluetoothScanningDevicesGroupPreferenceController extends
BluetoothScanningDevicesGroupPreferenceController {
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothSettingsFragmentTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothSettingsFragmentTest.java
index bde1384..acb0d52 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothSettingsFragmentTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothSettingsFragmentTest.java
@@ -24,41 +24,36 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.when;
-
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
import android.content.Intent;
+import android.os.UserHandle;
+import android.os.UserManager;
import android.widget.Switch;
import com.android.car.settings.R;
import com.android.car.settings.testutils.FragmentController;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link BluetoothSettingsFragment}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothSettingsFragmentTest {
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
private Context mContext;
private LocalBluetoothManager mLocalBluetoothManager;
private FragmentController<BluetoothSettingsFragment> mFragmentController;
@@ -67,7 +62,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
mLocalBluetoothManager = LocalBluetoothManager.getInstance(mContext, /* onInitCallback= */
@@ -78,7 +72,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
}
@@ -166,8 +159,8 @@
@Test
public void stateChanged_on_userRestricted_setsSwitchDisabled() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_BLUETOOTH, true);
mFragmentController.setup();
sendStateChangedIntent(STATE_ON);
@@ -213,8 +206,8 @@
@Test
public void stateChanged_off_userRestricted_setsSwitchDisabled() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_BLUETOOTH, true);
mFragmentController.setup();
sendStateChangedIntent(STATE_OFF);
@@ -245,4 +238,8 @@
private ShadowBluetoothAdapter getShadowBluetoothAdapter() {
return Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothUnbondedDevicesPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothUnbondedDevicesPreferenceControllerTest.java
index df2b136..dc70acf 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothUnbondedDevicesPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/BluetoothUnbondedDevicesPreferenceControllerTest.java
@@ -29,8 +29,9 @@
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.PreferenceGroup;
@@ -39,7 +40,6 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -55,19 +55,17 @@
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
import org.robolectric.util.ReflectionHelpers;
import java.util.Arrays;
/** Unit test for {@link BluetoothUnbondedDevicesPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class BluetoothUnbondedDevicesPreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private CachedBluetoothDevice mUnbondedCachedDevice;
@Mock
private BluetoothDevice mUnbondedDevice;
@@ -82,7 +80,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
Context context = RuntimeEnvironment.application;
mLocalBluetoothManager = LocalBluetoothManager.getInstance(context, /* onInitCallback= */
@@ -114,7 +111,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
ReflectionHelpers.setField(mLocalBluetoothManager, "mCachedDeviceManager",
mSaveRealCachedDeviceManager);
@@ -179,8 +175,8 @@
@Test
public void getAvailabilityStatus_disallowConfigBluetooth_disabledForUser() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_CONFIG_BLUETOOTH, true);
assertThat(mControllerHelper.getController().getAvailabilityStatus()).isEqualTo(
DISABLED_FOR_USER);
@@ -189,4 +185,8 @@
private ShadowBluetoothAdapter getShadowBluetoothAdapter() {
return (ShadowBluetoothAdapter) Shadow.extract(BluetoothAdapter.getDefaultAdapter());
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(RuntimeEnvironment.application));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/bluetooth/PairNewDevicePreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/bluetooth/PairNewDevicePreferenceControllerTest.java
index de2d63b..a3d5e4a 100644
--- a/tests/robotests/src/com/android/car/settings/bluetooth/PairNewDevicePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/bluetooth/PairNewDevicePreferenceControllerTest.java
@@ -26,13 +26,13 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.when;
import static org.testng.Assert.assertThrows;
import android.bluetooth.BluetoothAdapter;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
import android.content.Intent;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
@@ -41,7 +41,6 @@
import com.android.car.settings.common.PreferenceControllerTestHelper;
import com.android.car.settings.testutils.ShadowBluetoothAdapter;
import com.android.car.settings.testutils.ShadowBluetoothPan;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.settingslib.bluetooth.BluetoothEventManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -55,17 +54,16 @@
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
import org.robolectric.util.ReflectionHelpers;
/** Unit test for {@link PairNewDevicePreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowBluetoothAdapter.class,
- ShadowBluetoothPan.class})
+@Config(shadows = {ShadowBluetoothAdapter.class, ShadowBluetoothPan.class})
public class PairNewDevicePreferenceControllerTest {
@Mock
- private CarUserManagerHelper mCarUserManagerHelper;
- @Mock
private BluetoothEventManager mEventManager;
private BluetoothEventManager mSaveRealEventManager;
private LocalBluetoothManager mLocalBluetoothManager;
@@ -77,7 +75,6 @@
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
mLocalBluetoothManager = LocalBluetoothManager.getInstance(mContext, /* onInitCallback= */
null);
@@ -98,7 +95,6 @@
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowBluetoothAdapter.reset();
ReflectionHelpers.setField(mLocalBluetoothManager, "mEventManager", mSaveRealEventManager);
}
@@ -122,8 +118,8 @@
public void getAvailabilityStatus_disallowBluetoothUserRestriction_disabledForUser() {
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -132,8 +128,8 @@
public void getAvailabilityStatus_disallowConfigBluetoothUserRestriction_disabledForUser() {
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_CONFIG_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -192,4 +188,8 @@
assertThat(mPreference.getOnPreferenceClickListener().onPreferenceClick(
mPreference)).isFalse();
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(RuntimeEnvironment.application));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java
index 76f85d9..207bb6c 100644
--- a/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/network/MobileNetworkEntryPreferenceControllerTest.java
@@ -16,6 +16,8 @@
package com.android.car.settings.network;
+import static android.os.UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS;
+
import static com.android.car.settings.common.PreferenceController.AVAILABLE;
import static com.android.car.settings.common.PreferenceController.DISABLED_FOR_USER;
import static com.android.car.settings.common.PreferenceController.UNSUPPORTED_ON_DEVICE;
@@ -28,15 +30,13 @@
import static org.mockito.Mockito.when;
import static org.robolectric.shadow.api.Shadow.extract;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
-import android.content.pm.UserInfo;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
+import android.os.UserHandle;
import android.os.UserManager;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
-import android.telephony.TelephonyManager;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Lifecycle;
@@ -44,7 +44,6 @@
import com.android.car.settings.R;
import com.android.car.settings.common.PreferenceControllerTestHelper;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import com.android.car.settings.testutils.ShadowConnectivityManager;
import com.android.car.settings.testutils.ShadowSubscriptionManager;
import com.android.car.settings.testutils.ShadowTelephonyManager;
@@ -63,35 +62,31 @@
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowNetwork;
+import org.robolectric.shadows.ShadowUserManager;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class, ShadowConnectivityManager.class,
- ShadowTelephonyManager.class, ShadowSubscriptionManager.class})
+@Config(shadows = {ShadowConnectivityManager.class, ShadowTelephonyManager.class,
+ ShadowSubscriptionManager.class})
public class MobileNetworkEntryPreferenceControllerTest {
private static final String TEST_NETWORK_NAME = "test network name";
- private static final UserInfo TEST_ADMIN_USER = new UserInfo(10, "test_name",
- UserInfo.FLAG_ADMIN);
- private static final UserInfo TEST_NON_ADMIN_USER = new UserInfo(10, "test_name",
- /* flags= */ 0);
private Context mContext;
private Preference mPreference;
private PreferenceControllerTestHelper<MobileNetworkEntryPreferenceController>
mControllerHelper;
private MobileNetworkEntryPreferenceController mController;
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
+ private UserHandle mMyUserHandle;
@Mock
private NetworkCapabilities mNetworkCapabilities;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
+ mMyUserHandle = UserHandle.of(UserHandle.myUserId());
mPreference = new Preference(mContext);
mControllerHelper = new PreferenceControllerTestHelper<>(mContext,
MobileNetworkEntryPreferenceController.class, mPreference);
@@ -103,14 +98,13 @@
ShadowNetwork.newInstance(ConnectivityManager.TYPE_MOBILE), mNetworkCapabilities);
when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
true);
- when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+ getShadowUserManager().setIsAdminUser(true);
+ getShadowUserManager().setUserRestriction(
+ mMyUserHandle, DISALLOW_CONFIG_MOBILE_NETWORKS, false);
}
@After
public void tearDown() {
- ShadowCarUserManagerHelper.reset();
ShadowConnectivityManager.reset();
ShadowTelephonyManager.reset();
}
@@ -127,7 +121,7 @@
public void getAvailabilityStatus_notAdmin_disabledForUser() {
when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
true);
- when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_NON_ADMIN_USER);
+ getShadowUserManager().setIsAdminUser(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -136,9 +130,9 @@
public void getAvailabilityStatus_hasRestriction_disabledForUser() {
when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
true);
- when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(true);
+ getShadowUserManager().setIsAdminUser(true);
+ getShadowUserManager().setUserRestriction(
+ mMyUserHandle, DISALLOW_CONFIG_MOBILE_NETWORKS, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -147,9 +141,9 @@
public void getAvailabilityStatus_hasMobileNetwork_isAdmin_noRestriction_available() {
when(mNetworkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)).thenReturn(
true);
- when(mCarUserManagerHelper.getCurrentProcessUserInfo()).thenReturn(TEST_ADMIN_USER);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)).thenReturn(false);
+ getShadowUserManager().setIsAdminUser(true);
+ getShadowUserManager().setUserRestriction(
+ mMyUserHandle, DISALLOW_CONFIG_MOBILE_NETWORKS, false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@@ -263,10 +257,6 @@
any(MobileNetworkListFragment.class));
}
- private ShadowTelephonyManager getShadowTelephonyManager() {
- return (ShadowTelephonyManager) extract(mContext.getSystemService(TelephonyManager.class));
- }
-
private ShadowConnectivityManager getShadowConnectivityManager() {
return (ShadowConnectivityManager) extract(
mContext.getSystemService(ConnectivityManager.class));
@@ -286,4 +276,8 @@
/* accessRules= */ null, /* cardString= */ "");
return subInfo;
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/security/AddTrustedDevicePreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/security/AddTrustedDevicePreferenceControllerTest.java
index 04a910f..940f402 100644
--- a/tests/robotests/src/com/android/car/settings/security/AddTrustedDevicePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/security/AddTrustedDevicePreferenceControllerTest.java
@@ -26,12 +26,11 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.when;
-
import android.app.admin.DevicePolicyManager;
import android.bluetooth.BluetoothAdapter;
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
+import android.os.UserManager;
import androidx.lifecycle.Lifecycle;
import androidx.preference.Preference;
@@ -46,40 +45,39 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/**
* Unit tests for {@link AddTrustedDevicePreferenceController}.
*/
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowLockPatternUtils.class, ShadowBluetoothAdapter.class,
- ShadowCarUserManagerHelper.class})
+@Config(shadows = {ShadowLockPatternUtils.class, ShadowBluetoothAdapter.class})
public class AddTrustedDevicePreferenceControllerTest {
private Context mContext;
private PreferenceControllerTestHelper<AddTrustedDevicePreferenceController>
mPreferenceControllerHelper;
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
private Preference mPreference;
private AddTrustedDevicePreferenceController mController;
+ private UserHandle mMyUserHandle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
+ mMyUserHandle = UserHandle.of(UserHandle.myUserId());
mPreference = new Preference(mContext);
mPreferenceControllerHelper = new PreferenceControllerTestHelper<>(mContext,
AddTrustedDevicePreferenceController.class, mPreference);
mController = mPreferenceControllerHelper.getController();
- Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
+ Shadows.shadowOf(mContext.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
mPreferenceControllerHelper.sendLifecycleEvent(Lifecycle.Event.ON_START);
}
@@ -94,8 +92,7 @@
@Test
public void refreshUi_hasPassword_preferenceEnabled() {
ShadowLockPatternUtils.setPasswordQuality(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(false);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_BLUETOOTH, false);
mController.refreshUi();
@@ -105,8 +102,7 @@
@Test
public void refreshUi_noPassword_preferenceDisabled() {
ShadowLockPatternUtils.setPasswordQuality(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(false);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_BLUETOOTH, false);
mController.refreshUi();
@@ -116,8 +112,7 @@
@Test
public void refreshUi_bluetoothAdapterEnabled_setsEmptySummary() {
ShadowLockPatternUtils.setPasswordQuality(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(false);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_BLUETOOTH, false);
BluetoothAdapter.getDefaultAdapter().enable();
mController.refreshUi();
@@ -158,8 +153,7 @@
public void getAvailabilityStatus_disallowBluetoothUserRestriction_disabledForUser() {
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -168,8 +162,7 @@
public void getAvailabilityStatus_disallowConfigBluetoothUserRestriction_disabledForUser() {
Shadows.shadowOf(RuntimeEnvironment.application.getPackageManager()).setSystemFeature(
FEATURE_BLUETOOTH, /* supported= */ true);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_CONFIG_BLUETOOTH)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_CONFIG_BLUETOOTH, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@@ -182,4 +175,8 @@
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/security/CredentialsResetPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/security/CredentialsResetPreferenceControllerTest.java
index e263973..361c07b 100644
--- a/tests/robotests/src/com/android/car/settings/security/CredentialsResetPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/security/CredentialsResetPreferenceControllerTest.java
@@ -16,71 +16,65 @@
package com.android.car.settings.security;
+import static android.os.UserManager.DISALLOW_CONFIG_CREDENTIALS;
+
import static com.android.car.settings.common.PreferenceController.AVAILABLE;
import static com.android.car.settings.common.PreferenceController.DISABLED_FOR_USER;
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.when;
-
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
+import android.os.UserHandle;
import android.os.UserManager;
import androidx.preference.Preference;
import com.android.car.settings.common.PreferenceControllerTestHelper;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
-import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link CredentialsResetPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class})
public class CredentialsResetPreferenceControllerTest {
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
-
private PreferenceControllerTestHelper<CredentialsResetPreferenceController> mControllerHelper;
+ private UserHandle mMyUserHandle;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
+ mMyUserHandle = UserHandle.of(UserHandle.myUserId());
Context context = RuntimeEnvironment.application;
mControllerHelper = new PreferenceControllerTestHelper<>(context,
CredentialsResetPreferenceController.class, new Preference(context));
}
- @After
- public void tearDown() {
- ShadowCarUserManagerHelper.reset();
- }
-
@Test
public void getAvailabilityStatus_noRestrictions_returnsAvailable() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_CONFIG_CREDENTIALS)).thenReturn(false);
+ getShadowUserManager().setUserRestriction(
+ mMyUserHandle, DISALLOW_CONFIG_CREDENTIALS, false);
assertThat(mControllerHelper.getController().getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_userRestricted_returnsDisabledForUser() {
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- UserManager.DISALLOW_CONFIG_CREDENTIALS)).thenReturn(true);
+ getShadowUserManager().setUserRestriction(mMyUserHandle, DISALLOW_CONFIG_CREDENTIALS, true);
+
assertThat(mControllerHelper.getController().getAvailabilityStatus()).isEqualTo(
DISABLED_FOR_USER);
}
+
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(RuntimeEnvironment.application));
+ }
}
diff --git a/tests/robotests/src/com/android/car/settings/system/ResetNetworkEntryPreferenceControllerTest.java b/tests/robotests/src/com/android/car/settings/system/ResetNetworkEntryPreferenceControllerTest.java
index c78cc47..e52b3d5 100644
--- a/tests/robotests/src/com/android/car/settings/system/ResetNetworkEntryPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/car/settings/system/ResetNetworkEntryPreferenceControllerTest.java
@@ -23,43 +23,33 @@
import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.Mockito.when;
-
-import android.car.userlib.CarUserManagerHelper;
import android.content.Context;
-import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.preference.Preference;
import com.android.car.settings.common.PreferenceControllerTestHelper;
-import com.android.car.settings.testutils.ShadowCarUserManagerHelper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
-import org.robolectric.Shadows;
-import org.robolectric.annotation.Config;
+import org.robolectric.shadow.api.Shadow;
+import org.robolectric.shadows.ShadowUserManager;
/** Unit test for {@link ResetNetworkEntryPreferenceController}. */
@RunWith(RobolectricTestRunner.class)
-@Config(shadows = {ShadowCarUserManagerHelper.class})
public class ResetNetworkEntryPreferenceControllerTest {
- @Mock
- private CarUserManagerHelper mCarUserManagerHelper;
private ResetNetworkEntryPreferenceController mController;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowCarUserManagerHelper.setMockInstance(mCarUserManagerHelper);
mContext = RuntimeEnvironment.application;
mController = new PreferenceControllerTestHelper<>(
@@ -70,29 +60,28 @@
@Test
public void getAvailabilityStatus_nonAdminUser_disabledForUser() {
- setCurrentUserWithFlags(/* flags= */ 0);
+ getShadowUserManager().setIsAdminUser(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
@Test
public void getAvailabilityStatus_adminUser_available() {
- setCurrentUserWithFlags(UserInfo.FLAG_ADMIN);
+ getShadowUserManager().setIsAdminUser(true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
}
@Test
public void getAvailabilityStatus_adminUser_restricted_disabledForUser() {
- setCurrentUserWithFlags(UserInfo.FLAG_ADMIN);
- when(mCarUserManagerHelper.isCurrentProcessUserHasRestriction(
- DISALLOW_NETWORK_RESET)).thenReturn(true);
+ getShadowUserManager().setIsAdminUser(true);
+ getShadowUserManager().setUserRestriction(
+ UserHandle.of(UserHandle.myUserId()), DISALLOW_NETWORK_RESET, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
}
- private void setCurrentUserWithFlags(int flags) {
- Shadows.shadowOf(UserManager.get(mContext))
- .addUser(UserHandle.myUserId(), "test user", flags);
+ private ShadowUserManager getShadowUserManager() {
+ return Shadow.extract(UserManager.get(mContext));
}
}
diff --git a/tests/robotests/src/com/android/car/settings/testutils/ShadowCarUserManagerHelper.java b/tests/robotests/src/com/android/car/settings/testutils/ShadowCarUserManagerHelper.java
index dd3cb71..50ececb 100644
--- a/tests/robotests/src/com/android/car/settings/testutils/ShadowCarUserManagerHelper.java
+++ b/tests/robotests/src/com/android/car/settings/testutils/ShadowCarUserManagerHelper.java
@@ -114,11 +114,6 @@
}
@Implementation
- protected boolean isCurrentProcessUserHasRestriction(String restriction) {
- return sMockInstance.isCurrentProcessUserHasRestriction(restriction);
- }
-
- @Implementation
protected boolean removeUser(UserInfo userInfo, String guestUserName) {
return sMockInstance.removeUser(userInfo, guestUserName);
}