Skip BlockedNumberProvider tests on system user.

bug: 127522355
Test: cts-tradefed run cts-dev -m CtsProviderTestCases
Change-Id: I3c4d11800150d9b883d686baeb4a3f8c8510dc99
Merged-In: I3c4d11800150d9b883d686baeb4a3f8c8510dc99
diff --git a/tests/tests/provider/AndroidTest.xml b/tests/tests/provider/AndroidTest.xml
index b4a2280..64fe88c 100644
--- a/tests/tests/provider/AndroidTest.xml
+++ b/tests/tests/provider/AndroidTest.xml
@@ -16,9 +16,6 @@
 <configuration description="Config for CTS Provider test cases">
     <option name="test-suite-tag" value="cts" />
     <option name="config-descriptor:metadata" key="component" value="framework" />
-    <target_preparer class="com.android.tradefed.targetprep.SwitchUserTargetPreparer">
-        <option name="user-type" value="system"/>
-    </target_preparer>
     <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
         <option name="cleanup-apks" value="true" />
         <option name="test-file-name" value="CtsProviderTestCases.apk" />
diff --git a/tests/tests/provider/src/android/provider/cts/BlockedNumberBackupRestoreTest.java b/tests/tests/provider/src/android/provider/cts/BlockedNumberBackupRestoreTest.java
index adf558b..d98a25a 100644
--- a/tests/tests/provider/src/android/provider/cts/BlockedNumberBackupRestoreTest.java
+++ b/tests/tests/provider/src/android/provider/cts/BlockedNumberBackupRestoreTest.java
@@ -21,6 +21,7 @@
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.pm.PackageManager;
+import android.os.UserManager;
 import android.provider.BlockedNumberContract;
 import android.telecom.Log;
 
@@ -44,6 +45,7 @@
     private String mOldTransport;
     private boolean mOldBackupEnabled;
     private boolean mHasFeature;
+    private boolean mIsSystemUser;
 
     @Override
     protected void setUp() throws Exception {
@@ -54,8 +56,9 @@
         mUiAutomation = getInstrumentation().getUiAutomation();
 
         mHasFeature = isFeatureSupported();
+        mIsSystemUser = isSystemUser(mContext);
 
-        if (mHasFeature) {
+        if (mHasFeature && mIsSystemUser) {
             ProviderTestUtils.setDefaultSmsApp(true, mContext.getPackageName(), mUiAutomation);
 
             mOldTransport = ProviderTestUtils.setBackupTransport(
@@ -68,7 +71,7 @@
 
     @Override
     protected void tearDown() throws Exception {
-        if (mHasFeature) {
+        if (mHasFeature  && mIsSystemUser) {
             wipeBackup();
             clearBlockedNumbers();
             ProviderTestUtils.setBackupEnabled(mOldBackupEnabled, mUiAutomation);
@@ -80,7 +83,7 @@
     }
 
     public void testBackupAndRestoreForSingleNumber() throws Exception {
-        if (!mHasFeature) {
+        if (!mHasFeature || !mIsSystemUser) {
             Log.i(TAG, "skipping BlockedNumberBackupRestoreTest");
             return;
         }
@@ -101,7 +104,7 @@
     }
 
     public void testBackupAndRestoreWithDeletion() throws Exception {
-        if (!mHasFeature) {
+        if (!mHasFeature || !mIsSystemUser) {
             Log.i(TAG, "skipping BlockedNumberBackupRestoreTest");
             return;
         }
@@ -175,4 +178,8 @@
         ProviderTestUtils.wipeBackup(LOCAL_BACKUP_COMPONENT, BLOCKED_NUMBERS_PROVIDER_PACKAGE,
                 mUiAutomation);
     }
+
+    private static boolean isSystemUser(Context context) {
+        return context.getSystemService(UserManager.class).isSystemUser();
+    }
 }
diff --git a/tests/tests/provider/src/android/provider/cts/BlockedNumberContractTest.java b/tests/tests/provider/src/android/provider/cts/BlockedNumberContractTest.java
index 30fdf24..756fa76 100644
--- a/tests/tests/provider/src/android/provider/cts/BlockedNumberContractTest.java
+++ b/tests/tests/provider/src/android/provider/cts/BlockedNumberContractTest.java
@@ -24,9 +24,11 @@
 import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
+import android.os.UserManager;
 import android.provider.BlockedNumberContract;
 import android.provider.BlockedNumberContract.BlockedNumbers;
 import android.telephony.TelephonyManager;
+import android.util.Log;
 
 import junit.framework.Assert;
 
@@ -42,9 +44,11 @@
 // cts-tradefed
 // run cts -m CtsProviderTestCases --test android.provider.cts.BlockedNumberContractTest
 public class BlockedNumberContractTest extends TestCaseThatRunsIfTelephonyIsEnabled {
+    private static final String TAG = "BlockedNumberContractTest";
     private ContentResolver mContentResolver;
     private Context mContext;
     private ArrayList<Uri> mAddedUris;
+    private boolean mIsSystemUser;
 
     private static final String[] BLOCKED_NUMBERS_PROJECTION = new String[]{
             BlockedNumbers.COLUMN_ORIGINAL_NUMBER,
@@ -56,6 +60,7 @@
         mContext = getInstrumentation().getContext();
         mContentResolver = mContext.getContentResolver();
         mAddedUris = new ArrayList<>();
+        mIsSystemUser = isSystemUser(mContext);
     }
 
     @Override
@@ -70,6 +75,10 @@
     }
 
     public void testProviderInteractionsAsRegularApp_fails() {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         TelephonyManager telephonyManager = mContext.getSystemService(TelephonyManager.class);
         // Don't run this test if we're carrier privileged.
         if (telephonyManager.checkCarrierPrivilegesForPackage(mContext.getPackageName())
@@ -123,6 +132,10 @@
     }
 
     public void testInsertAndBlockCheck_succeeds() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         assertTrue(BlockedNumberContract.canCurrentUserBlockNumbers(mContext));
@@ -150,6 +163,10 @@
     }
 
     public void testUnblock_succeeds() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         // Unblocking non-existent blocked number should return 0.
@@ -165,6 +182,10 @@
     }
 
     public void testInsert_failsWithInvalidInputs() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         try {
@@ -206,6 +227,10 @@
     }
 
     public void testUpdate_isUnsupported() throws  Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
         try {
             mContentResolver.update(
@@ -216,12 +241,20 @@
     }
 
     public void testIsBlocked_returnsFalseForNullAndEmpty() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
         assertFalse(BlockedNumberContract.isBlocked(mContext, null));
         assertFalse(BlockedNumberContract.isBlocked(mContext, ""));
     }
 
     public void testDelete() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         assertInsertBlockedNumberSucceeds("12345", "+112345");
@@ -264,6 +297,10 @@
     }
 
     public void testDelete_failsOnInvalidInputs() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         try {
@@ -285,6 +322,10 @@
     }
 
     public void testProviderNotifiesChangesUsingContentObserver() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         Cursor cursor = mContentResolver.query(BlockedNumbers.CONTENT_URI, null, null, null, null);
@@ -311,6 +352,10 @@
     }
 
     public void testAccessingNonExistentMethod_fails() throws Exception {
+        if (!mIsSystemUser) {
+            Log.i(TAG, "skipping BlockedNumberContractTest");
+            return;
+        }
         setDefaultSmsApp(true);
 
         try {
@@ -358,4 +403,8 @@
         ProviderTestUtils.setDefaultSmsApp(
                 setToSmsApp, mContext.getPackageName(), getInstrumentation().getUiAutomation());
     }
+
+    private static boolean isSystemUser(Context context) {
+        return context.getSystemService(UserManager.class).isSystemUser();
+    }
 }