Don't throw when noting ENABLE_MOBILE_DATA
There not much to do if it does throw, and it breaks some tests
Bug: 325375860
Test: atest BluetoothWiFiResetPreferenceControllerTest
Test: atest PhoneInterfaceManagerTest
Change-Id: I97f6b76252f661b6eeb72a595e65b27b155103ab
diff --git a/src/com/android/phone/PhoneInterfaceManager.java b/src/com/android/phone/PhoneInterfaceManager.java
index e18818c..87fe7c5 100644
--- a/src/com/android/phone/PhoneInterfaceManager.java
+++ b/src/com/android/phone/PhoneInterfaceManager.java
@@ -428,11 +428,11 @@
private final SatelliteController mSatelliteController;
private final SatelliteAccessController mSatelliteAccessController;
private final UserManager mUserManager;
- private final AppOpsManager mAppOps;
private final MainThreadHandler mMainThreadHandler;
private final SharedPreferences mTelephonySharedPreferences;
private final PhoneConfigurationManager mPhoneConfigurationManager;
private final RadioInterfaceCapabilityController mRadioInterfaceCapabilities;
+ private AppOpsManager mAppOps;
private PackageManager mPackageManager;
/** User Activity */
@@ -9463,7 +9463,7 @@
try {
if (reason == TelephonyManager.DATA_ENABLED_REASON_USER && enabled
&& null != callingPackage && opEnableMobileDataByUser()) {
- mAppOps.noteOp(AppOpsManager.OPSTR_ENABLE_MOBILE_DATA_BY_USER,
+ mAppOps.noteOpNoThrow(AppOpsManager.OPSTR_ENABLE_MOBILE_DATA_BY_USER,
callingUid, callingPackage, null, null);
}
Phone phone = getPhone(subId);
@@ -14016,6 +14016,16 @@
}
/*
+ * PhoneInterfaceManager is a singleton. Unit test calls the init() with context.
+ * But the context that is passed in is unused if the phone app is already alive.
+ * In this case PackageManager object is different in PhoneInterfaceManager and Unit test.
+ */
+ @VisibleForTesting
+ public void setAppOpsManager(AppOpsManager appOps) {
+ mAppOps = appOps;
+ }
+
+ /*
* PhoneInterfaceManager is a singleton. Unit test calls the init() with FeatureFlags.
* But the FeatureFlags that is passed in is unused if the phone app is already alive.
* In this case FeatureFlags object is different in PhoneInterfaceManager and Unit test.
diff --git a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
index 2d338f2..6227e32 100644
--- a/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
+++ b/tests/src/com/android/phone/PhoneInterfaceManagerTest.java
@@ -23,6 +23,8 @@
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
@@ -32,6 +34,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import android.app.AppOpsManager;
import android.compat.testing.PlatformCompatChangeRule;
import android.content.Context;
import android.content.SharedPreferences;
@@ -90,6 +93,9 @@
@Mock
private SubscriptionManagerService mSubscriptionManagerService;
+ @Mock
+ private AppOpsManager mAppOps;
+
@Rule public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
@Before
@@ -117,6 +123,8 @@
doReturn(false).when(mFeatureFlags).enforceTelephonyFeatureMappingForPublicApis();
mPhoneInterfaceManager.setPackageManager(mPackageManager);
doReturn(true).when(mPackageManager).hasSystemFeature(anyString());
+
+ mPhoneInterfaceManager.setAppOpsManager(mAppOps);
}
@Test
@@ -422,37 +430,18 @@
public void notifyEnableDataWithAppOps_enableByUser_doNoteOp() {
mSetFlagsRule.enableFlags(Flags.FLAG_OP_ENABLE_MOBILE_DATA_BY_USER);
String packageName = "INVALID_PACKAGE";
- String error = "";
- try {
- mPhoneInterfaceManager.setDataEnabledForReason(1,
- TelephonyManager.DATA_ENABLED_REASON_USER, true, packageName);
- } catch (SecurityException expected) {
- // The test doesn't have access to note the op, but we're just interested that it makes
- // the attempt.
- error = expected.getMessage();
- }
-
- String appop = "ENABLE_MOBILE_DATA_BY_USER";
- assertTrue("expected error to contain " + packageName + " but it didn't: " + error,
- error.contains(packageName));
- assertTrue("expected error to contain " + appop + " but it didn't: " + error,
- error.contains(appop));
+ mPhoneInterfaceManager.setDataEnabledForReason(1,
+ TelephonyManager.DATA_ENABLED_REASON_USER, true, packageName);
+ verify(mAppOps).noteOpNoThrow(eq(AppOpsManager.OPSTR_ENABLE_MOBILE_DATA_BY_USER), anyInt(),
+ eq(packageName), isNull(), isNull());
}
@Test
public void notifyEnableDataWithAppOps_enableByCarrier_doNotNoteOp() {
mSetFlagsRule.enableFlags(Flags.FLAG_OP_ENABLE_MOBILE_DATA_BY_USER);
String packageName = "INVALID_PACKAGE";
- String error = "";
- try {
- mPhoneInterfaceManager.setDataEnabledForReason(1,
- TelephonyManager.DATA_ENABLED_REASON_CARRIER, true, packageName);
- } catch (SecurityException expected) {
- // The test doesn't have access to note the op, but we're just interested that it makes
- // the attempt.
- error = expected.getMessage();
- }
- assertEquals("Expected error to be empty, was " + error, error, "");
+ verify(mAppOps, never()).noteOpNoThrow(eq(AppOpsManager.OPSTR_ENABLE_MOBILE_DATA_BY_USER),
+ anyInt(), eq(packageName), isNull(), isNull());
}
@Test