Skip tests in android.admin package if device does not support FEATURE_DEVICE_ADMIN

Change-Id: I7d5ee12bb3320302c44c66e0b012da6b8c85c2d8
diff --git a/tests/tests/admin/src/android/admin/cts/DeviceAdminActivationTest.java b/tests/tests/admin/src/android/admin/cts/DeviceAdminActivationTest.java
index 3e60be1..6fcd0de 100644
--- a/tests/tests/admin/src/android/admin/cts/DeviceAdminActivationTest.java
+++ b/tests/tests/admin/src/android/admin/cts/DeviceAdminActivationTest.java
@@ -21,6 +21,7 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.deviceadmin.cts.CtsDeviceAdminBrokenReceiver;
 import android.deviceadmin.cts.CtsDeviceAdminBrokenReceiver2;
 import android.deviceadmin.cts.CtsDeviceAdminBrokenReceiver3;
@@ -31,6 +32,7 @@
 import android.deviceadmin.cts.CtsDeviceAdminActivationTestActivity.OnActivityResultListener;
 import android.os.SystemClock;
 import android.test.ActivityInstrumentationTestCase2;
+import android.util.Log;
 
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mock;
@@ -45,6 +47,8 @@
 public class DeviceAdminActivationTest
     extends ActivityInstrumentationTestCase2<CtsDeviceAdminActivationTestActivity> {
 
+    private static final String TAG = DeviceAdminActivationTest.class.getSimpleName();
+
     // IMPLEMENTATION NOTE: Because Device Admin activation requires the use of
     // Activity.startActivityForResult, this test creates an empty Activity which then invokes
     // startActivityForResult.
@@ -57,6 +61,7 @@
      */
     private static final int UI_EFFECT_TIMEOUT_MILLIS = 5000;
 
+    private boolean mDeviceAdmin;
     @Mock private OnActivityResultListener mMockOnActivityResultListener;
 
     public DeviceAdminActivationTest() {
@@ -68,6 +73,8 @@
         super.setUp();
         MockitoAnnotations.initMocks(this);
         getActivity().setOnActivityResultListener(mMockOnActivityResultListener);
+        mDeviceAdmin = getInstrumentation().getContext().getPackageManager().hasSystemFeature(
+                PackageManager.FEATURE_DEVICE_ADMIN);
     }
 
     @Override
@@ -80,6 +87,10 @@
     }
 
     public void testActivateGoodReceiverDisplaysActivationUi() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateGoodReceiverDisplaysActivationUi");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminDeactivatedReceiver.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminDeactivatedReceiver.class);
         assertWithTimeoutOnActivityResultNotInvoked();
@@ -91,6 +102,10 @@
     }
 
     public void testActivateBrokenReceiverFails() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateBrokenReceiverFails");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminBrokenReceiver.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminBrokenReceiver.class);
         assertWithTimeoutOnActivityResultInvokedWithResultCode(Activity.RESULT_CANCELED);
@@ -98,6 +113,10 @@
     }
 
     public void testActivateBrokenReceiver2Fails() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateBrokenReceiver2Fails");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminBrokenReceiver2.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminBrokenReceiver2.class);
         assertWithTimeoutOnActivityResultInvokedWithResultCode(Activity.RESULT_CANCELED);
@@ -105,6 +124,10 @@
     }
 
     public void testActivateBrokenReceiver3Fails() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateBrokenReceiver3Fails");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminBrokenReceiver3.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminBrokenReceiver3.class);
         assertWithTimeoutOnActivityResultInvokedWithResultCode(Activity.RESULT_CANCELED);
@@ -112,6 +135,10 @@
     }
 
     public void testActivateBrokenReceiver4Fails() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateBrokenReceiver4Fails");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminBrokenReceiver4.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminBrokenReceiver4.class);
         assertWithTimeoutOnActivityResultInvokedWithResultCode(Activity.RESULT_CANCELED);
@@ -119,6 +146,10 @@
     }
 
     public void testActivateBrokenReceiver5Fails() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testActivateBrokenReceiver5Fails");
+            return;
+        }
         assertDeviceAdminDeactivated(CtsDeviceAdminBrokenReceiver5.class);
         startAddDeviceAdminActivityForResult(CtsDeviceAdminBrokenReceiver5.class);
         assertWithTimeoutOnActivityResultInvokedWithResultCode(Activity.RESULT_CANCELED);
diff --git a/tests/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java b/tests/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
index b38ef4c..fe68073 100644
--- a/tests/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
+++ b/tests/tests/admin/src/android/admin/cts/DeviceAdminInfoTest.java
@@ -21,14 +21,16 @@
 import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 public class DeviceAdminInfoTest extends AndroidTestCase {
 
+    private static final String TAG = DeviceAdminInfoTest.class.getSimpleName();
+
     private PackageManager mPackageManager;
-
     private ComponentName mComponent;
-
     private ComponentName mSecondComponent;
+    private boolean mDeviceAdmin;
 
     @Override
     protected void setUp() throws Exception {
@@ -36,6 +38,8 @@
         mPackageManager = mContext.getPackageManager();
         mComponent = getReceiverComponent();
         mSecondComponent = getSecondReceiverComponent();
+        mDeviceAdmin =
+                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
     }
 
     static ComponentName getReceiverComponent() {
@@ -49,6 +53,10 @@
     }
 
     public void testDeviceAdminInfo() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testDeviceAdminInfo");
+            return;
+        }
         ResolveInfo resolveInfo = new ResolveInfo();
         resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mComponent,
                 PackageManager.GET_META_DATA);
@@ -77,6 +85,10 @@
     }
 
     public void testDeviceAdminInfo2() throws Exception {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testDeviceAdminInfo2");
+            return;
+        }
         ResolveInfo resolveInfo = new ResolveInfo();
         resolveInfo.activityInfo = mPackageManager.getReceiverInfo(mSecondComponent,
                 PackageManager.GET_META_DATA);
diff --git a/tests/tests/admin/src/android/admin/cts/DeviceAdminReceiverTest.java b/tests/tests/admin/src/android/admin/cts/DeviceAdminReceiverTest.java
index b9f842d..b804625 100644
--- a/tests/tests/admin/src/android/admin/cts/DeviceAdminReceiverTest.java
+++ b/tests/tests/admin/src/android/admin/cts/DeviceAdminReceiverTest.java
@@ -19,10 +19,13 @@
 import android.app.admin.DeviceAdminReceiver;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 public class DeviceAdminReceiverTest extends AndroidTestCase {
 
+    private static final String TAG = DeviceAdminReceiverTest.class.getSimpleName();
     private static final String DISABLE_WARNING = "Disable Warning";
 
     private static final int PASSWORD_CHANGED = 0x1;
@@ -33,14 +36,21 @@
     private static final int DEVICE_ADMIN_DISABLED = 0x20;
 
     private TestReceiver mReceiver;
+    private boolean mDeviceAdmin;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mReceiver = new TestReceiver();
+        mDeviceAdmin =
+                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
     }
 
     public void testOnReceive() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testOnReceive");
+            return;
+        }
         mReceiver.reset();
         mReceiver.onReceive(mContext, new Intent(DeviceAdminReceiver.ACTION_PASSWORD_CHANGED));
         assertTrue(mReceiver.hasFlags(PASSWORD_CHANGED));
diff --git a/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java b/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
index 62f3daa..e953305 100644
--- a/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
+++ b/tests/tests/admin/src/android/admin/cts/DevicePolicyManagerTest.java
@@ -19,7 +19,9 @@
 import android.app.admin.DevicePolicyManager;
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.test.AndroidTestCase;
+import android.util.Log;
 
 import java.util.List;
 
@@ -30,9 +32,11 @@
  */
 public class DevicePolicyManagerTest extends AndroidTestCase {
 
-    private DevicePolicyManager mDevicePolicyManager;
+    private static final String TAG = DevicePolicyManagerTest.class.getSimpleName();
 
+    private DevicePolicyManager mDevicePolicyManager;
     private ComponentName mComponent;
+    private boolean mDeviceAdmin;
 
     @Override
     protected void setUp() throws Exception {
@@ -40,6 +44,8 @@
         mDevicePolicyManager = (DevicePolicyManager)
                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
         mComponent = DeviceAdminInfoTest.getReceiverComponent();
+        mDeviceAdmin =
+                mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
         setBlankPassword();
     }
 
@@ -50,6 +56,9 @@
     }
 
     private void setBlankPassword() {
+        if (!mDeviceAdmin) {
+            return;
+        }
         // Reset the password to nothing for future tests...
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
@@ -58,6 +67,10 @@
     }
 
     public void testGetActiveAdmins() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testGetActiveAdmins");
+            return;
+        }
         List<ComponentName> activeAdmins = mDevicePolicyManager.getActiveAdmins();
         assertFalse(activeAdmins.isEmpty());
         assertTrue(activeAdmins.contains(mComponent));
@@ -65,6 +78,10 @@
     }
 
     public void testGetMaximumFailedPasswordsForWipe() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testGetMaximumFailedPasswordsForWipe");
+            return;
+        }
         mDevicePolicyManager.setMaximumFailedPasswordsForWipe(mComponent, 3);
         assertEquals(3, mDevicePolicyManager.getMaximumFailedPasswordsForWipe(mComponent));
 
@@ -73,6 +90,10 @@
     }
 
     public void testPasswordQuality_something() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testPasswordQuality_something");
+            return;
+        }
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING,
@@ -102,6 +123,10 @@
     }
 
     public void testPasswordQuality_numeric() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testPasswordQuality_numeric");
+            return;
+        }
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC,
@@ -131,6 +156,10 @@
     }
 
     public void testPasswordQuality_alphabetic() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testPasswordQuality_alphabetic");
+            return;
+        }
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC);
         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC,
@@ -160,6 +189,10 @@
     }
 
     public void testPasswordQuality_alphanumeric() {
+        if (!mDeviceAdmin) {
+            Log.w(TAG, "Skipping testPasswordQuality_alphanumeric");
+            return;
+        }
         mDevicePolicyManager.setPasswordQuality(mComponent,
                 DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
         assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC,