Fix MixedProfileOwnerTest#testApplicationRestrictions

MixedDeviceOwnerTest#testApplicationRestrictions is also fixed

This CTS failure is caused by API behavior change in ag/863785

Also, fix CTS corresponding to setApplicationRestrictionsManagingPackage API change in b/27532565

BUG: 27450018, 27532565

Change-Id: Idb20bccf5dfc2c81ca28633ac4184a605d402d8b
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
index 923fc44..9ecc339 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ApplicationRestrictionsTest.java
@@ -20,6 +20,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.Bundle;
 import android.os.Parcelable;
 import android.os.UserManager;
@@ -183,13 +184,13 @@
         }
     }
 
-    public void testSetApplicationRestrictionsManagingPackage() {
+    public void testSetApplicationRestrictionsManagingPackage() throws NameNotFoundException {
         final String previousValue = mDevicePolicyManager.getApplicationRestrictionsManagingPackage(
                 ADMIN_RECEIVER_COMPONENT);
         try {
             mDevicePolicyManager.setApplicationRestrictionsManagingPackage(
-                    ADMIN_RECEIVER_COMPONENT, OTHER_PACKAGE);
-            assertEquals(OTHER_PACKAGE,
+                    ADMIN_RECEIVER_COMPONENT, APP_RESTRICTIONS_TARGET_PKG);
+            assertEquals(APP_RESTRICTIONS_TARGET_PKG,
                     mDevicePolicyManager.getApplicationRestrictionsManagingPackage(
                             ADMIN_RECEIVER_COMPONENT));
             mDevicePolicyManager.setApplicationRestrictionsManagingPackage(
@@ -205,6 +206,22 @@
         }
     }
 
+    public void testSetApplicationRestrictionsManagingPackageForNotInstalledPackage()
+            throws NameNotFoundException {
+        try {
+            mDevicePolicyManager.setApplicationRestrictionsManagingPackage(ADMIN_RECEIVER_COMPONENT,
+                    OTHER_PACKAGE);
+            fail("Not throwing exception for not installed package name");
+        } catch (NameNotFoundException expected) {
+            MoreAsserts.assertContainsRegex(OTHER_PACKAGE, expected.getMessage());
+        } finally {
+            mDevicePolicyManager.setApplicationRestrictionsManagingPackage(ADMIN_RECEIVER_COMPONENT,
+                    null);
+            assertNull(mDevicePolicyManager.getApplicationRestrictionsManagingPackage(
+                    ADMIN_RECEIVER_COMPONENT));
+        }
+    }
+
     // Should be consistent with assertBundle0
     private static Bundle createBundle0() {
         Bundle result = new Bundle();
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/SetPolicyActivity.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/SetPolicyActivity.java
index 890daa3..aaa017b 100644
--- a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/SetPolicyActivity.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/SetPolicyActivity.java
@@ -19,6 +19,7 @@
 import android.app.admin.DevicePolicyManager;
 import android.content.Context;
 import android.content.Intent;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.os.Bundle;
 import android.os.Process;
 import android.util.Log;
@@ -94,8 +95,12 @@
                     + " for user " + Process.myUserHandle());
         } else if (COMMAND_SET_APP_RESTRICTIONS_MANAGER.equals(command)) {
             String packageName = intent.getStringExtra(EXTRA_PACKAGE_NAME);
-            dpm.setApplicationRestrictionsManagingPackage(
-                    BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, packageName);
+            try {
+                dpm.setApplicationRestrictionsManagingPackage(
+                        BaseDeviceAdminTest.ADMIN_RECEIVER_COMPONENT, packageName);
+            } catch (NameNotFoundException e) {
+                throw new IllegalArgumentException(e);
+            }
             Log.i(TAG, "Setting the application restrictions managing package to " + packageName);
         } else {
             Log.e(TAG, "Invalid command: " + command);