Merge "Tests for metadata check of target administrator when transferring ownership."
diff --git a/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/AndroidManifest.xml b/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/AndroidManifest.xml
index 2a64ca2..138275f 100644
--- a/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/AndroidManifest.xml
@@ -30,6 +30,18 @@
             android:permission="android.permission.BIND_DEVICE_ADMIN">
             <meta-data android:name="android.app.device_admin"
                        android:resource="@xml/device_admin"/>
+            <meta-data
+                android:name="android.app.support_transfer_ownership"
+                android:value="true"/>
+            <intent-filter>
+                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
+            </intent-filter>
+        </receiver>
+        <receiver
+            android:name="com.android.cts.transferowner.DeviceAndProfileOwnerTransferIncomingTest$BasicAdminReceiverNoMetadata"
+            android:permission="android.permission.BIND_DEVICE_ADMIN">
+            <meta-data android:name="android.app.device_admin"
+                       android:resource="@xml/device_admin"/>
             <intent-filter>
                 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
             </intent-filter>
diff --git a/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferIncomingTest.java b/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferIncomingTest.java
index 868ae3b..6a62620 100644
--- a/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferIncomingTest.java
+++ b/hostsidetests/devicepolicy/app/TransferOwnerIncomingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferIncomingTest.java
@@ -40,6 +40,10 @@
         }
     }
 
+    public static class BasicAdminReceiverNoMetadata extends DeviceAdminReceiver {
+        public BasicAdminReceiverNoMetadata() {}
+    }
+
     private final static String SHARED_PREFERENCE_NAME = "shared-preference-name";
     private final static String KEY_TRANSFER_COMPLETED_CALLED = "key-transfer-completed-called";
 
diff --git a/hostsidetests/devicepolicy/app/TransferOwnerOutgoingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferOutgoingTest.java b/hostsidetests/devicepolicy/app/TransferOwnerOutgoingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferOutgoingTest.java
index 328a7ae..cc9b201 100644
--- a/hostsidetests/devicepolicy/app/TransferOwnerOutgoingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferOutgoingTest.java
+++ b/hostsidetests/devicepolicy/app/TransferOwnerOutgoingApp/src/com/android/cts/transferowner/DeviceAndProfileOwnerTransferOutgoingTest.java
@@ -43,9 +43,14 @@
             "com.android.cts.transferownerincoming";
     private static final String TRANSFER_OWNER_INCOMING_TEST_RECEIVER_CLASS =
             "com.android.cts.transferowner.DeviceAndProfileOwnerTransferIncomingTest$BasicAdminReceiver";
+    private static final String TRANSFER_OWNER_INCOMING_TEST_RECEIVER_NO_METADATA_CLASS =
+            "com.android.cts.transferowner.DeviceAndProfileOwnerTransferIncomingTest$BasicAdminReceiverNoMetadata";
     static final ComponentName INCOMING_COMPONENT_NAME =
             new ComponentName(
                     TRANSFER_OWNER_INCOMING_PKG, TRANSFER_OWNER_INCOMING_TEST_RECEIVER_CLASS);
+    static final ComponentName INCOMING_NO_METADATA_COMPONENT_NAME =
+            new ComponentName(TRANSFER_OWNER_INCOMING_PKG,
+                    TRANSFER_OWNER_INCOMING_TEST_RECEIVER_NO_METADATA_CLASS);
     private static final ComponentName INVALID_TARGET_COMPONENT =
             new ComponentName("com.android.cts.intent.receiver", ".BroadcastIntentReceiver");
 
@@ -118,4 +123,15 @@
         PersistableBundle b = new PersistableBundle();
         transferOwnership(mOutgoingComponentName, INCOMING_COMPONENT_NAME, b);
     }
+
+    @Test
+    public void testTransferNoMetadata() throws Throwable {
+        PersistableBundle b = new PersistableBundle();
+        assertThrows(
+                IllegalArgumentException.class,
+                () -> {
+                    transferOwnership(mOutgoingComponentName,
+                            INCOMING_NO_METADATA_COMPONENT_NAME, b);
+                });
+    }
 }
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerHostSideTransferTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerHostSideTransferTest.java
index 2456eb1..7367e76 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerHostSideTransferTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerHostSideTransferTest.java
@@ -88,6 +88,15 @@
         mIncomingTestClassName = incomingTestClassName;
     }
 
+    public void testTransferNoMetadata() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        runDeviceTestsAsUser(TRANSFER_OWNER_OUTGOING_PKG,
+                mOutgoingTestClassName,
+                "testTransferNoMetadata", mUserId);
+    }
+
     /* TODO: Add tests for:
     * 1. startServiceForOwner
     * 2. passwordOwner