Add tests for policy serialisation in the policy engine

but ignore for now until we add a test API to trigger the reloading of
policies from disk.

Bug: 273494642
Test: btest a.d.c.DeviceManagementCoexistenceTest
Change-Id: If510693a9fae48bea503cdbb2e5168fc6a8bbf76
diff --git a/tests/devicepolicy/src/android/devicepolicy/cts/DeviceManagementCoexistenceTest.java b/tests/devicepolicy/src/android/devicepolicy/cts/DeviceManagementCoexistenceTest.java
index 442893f..60ed99d 100644
--- a/tests/devicepolicy/src/android/devicepolicy/cts/DeviceManagementCoexistenceTest.java
+++ b/tests/devicepolicy/src/android/devicepolicy/cts/DeviceManagementCoexistenceTest.java
@@ -85,6 +85,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -878,6 +879,548 @@
         }
     }
 
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void autoTimezoneSet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().setAutoTimeZoneEnabled(
+                    sDeviceState.dpc().componentName(), true);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Boolean> policyState = getBooleanPolicyState(
+                    new NoArgsPolicyKey(AUTO_TIMEZONE_POLICY),
+                    UserHandle.ALL);
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isTrue();
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setAutoTimeZoneEnabled(
+                    sDeviceState.dpc().componentName(), false);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void permissionGrantStateSet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager()
+                    .setPermissionGrantState(
+                            sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                            GRANTABLE_PERMISSION, PERMISSION_GRANT_STATE_GRANTED);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Integer> policyState = getIntegerPolicyState(
+                    new PackagePermissionPolicyKey(
+                            PERMISSION_GRANT_POLICY,
+                            sTestApp.packageName(),
+                            GRANTABLE_PERMISSION),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isEqualTo(
+                    PERMISSION_GRANT_STATE_GRANTED);
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setPermissionGrantState(
+                    sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                    GRANTABLE_PERMISSION, PERMISSION_GRANT_STATE_DEFAULT);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void lockTaskPolicySet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskPackages(
+                            sDeviceState.dpc().componentName(), new String[]{PACKAGE_NAME});
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskFeatures(sDeviceState.dpc().componentName(), LOCK_TASK_FEATURES);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<LockTaskPolicy> policyState = getLockTaskPolicyState(
+                    new NoArgsPolicyKey(LOCK_TASK_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy().getPackages())
+                    .containsExactly(PACKAGE_NAME);
+            assertThat(policyState.getCurrentResolvedPolicy().getFlags())
+                    .isEqualTo(LOCK_TASK_FEATURES);
+        } finally {
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskPackages(sDeviceState.dpc().componentName(), new String[]{});
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void userControlDisabledPackagesSet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().setUserControlDisabledPackages(
+                    sDeviceState.dpc().componentName(),
+                    Arrays.asList(sTestApp.packageName()));
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Set<String>> policyState = getStringSetPolicyState(
+                    new NoArgsPolicyKey(USER_CONTROL_DISABLED_PACKAGES_POLICY),
+                    UserHandle.ALL);
+
+            assertThat(policyState.getCurrentResolvedPolicy()).containsExactly(
+                    sTestApp.packageName());
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setUserControlDisabledPackages(
+                    sDeviceState.dpc().componentName(),
+                    new ArrayList<>());
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void uninstallBlockedSet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().setUninstallBlocked(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), /* uninstallBlocked= */ true);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Boolean> policyState = getBooleanPolicyState(
+                    new PackagePolicyKey(
+                            PACKAGE_UNINSTALL_BLOCKED_POLICY,
+                            sTestApp.packageName()),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isTrue();
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setUninstallBlocked(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), /* uninstallBlocked= */ false);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void persistentPreferredActivitySet_serialisation_loadsPolicy() {
+        try {
+            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
+            sDeviceState.dpc().devicePolicyManager().addPersistentPreferredActivity(
+                    sDeviceState.dpc().componentName(),
+                    intentFilter,
+                    sDeviceState.dpc().componentName());
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<ComponentName> policyState = getComponentNamePolicyState(
+                    new IntentFilterPolicyKey(
+                            PERSISTENT_PREFERRED_ACTIVITY_POLICY,
+                            intentFilter),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isEqualTo(
+                    sDeviceState.dpc().componentName());
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().clearPackagePersistentPreferredActivities(
+                    sDeviceState.dpc().componentName(),
+                    sDeviceState.dpc().packageName());
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void appRestrictionsSet_serialisation_loadsPolicy() {
+        Bundle bundle = BundleUtils.createBundle(
+                "appRestrictionsSet_serialisation_loadsPolicy");
+        try {
+            sDeviceState.dpc().devicePolicyManager()
+                    .setApplicationRestrictions(
+                            sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                            bundle);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Bundle> policyState = getBundlePolicyState(
+                    new PackagePolicyKey(
+                            APPLICATION_RESTRICTIONS_POLICY,
+                            sTestApp.packageName()),
+                    sDeviceState.dpc().user().userHandle());
+
+            // app restrictions is a non-coexistable policy, so should not have a resolved policy.
+            assertThat(policyState.getCurrentResolvedPolicy()).isNull();
+            Bundle returnedBundle = policyState.getPoliciesSetByAdmins().get(
+                    new EnforcingAdmin(sDeviceState.dpc().packageName(),
+                            DpcAuthority.DPC_AUTHORITY,
+                            sDeviceState.dpc().user().userHandle()));
+            assertThat(returnedBundle).isNotNull();
+            BundleUtils.assertEqualToBundle(
+                    "appRestrictionsSet_serialisation_loadsPolicy",
+                    returnedBundle);
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setApplicationRestrictions(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), new Bundle());
+        }
+    }
+
+    @Ignore("If ResetPasswordWithTokenTest for managed profile is executed before device owner " +
+            "and primary user profile owner tests, password reset token would have been " +
+            "disabled for the primary user, disabling this test until this gets fixed.")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void resetPasswordTokenSet_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().setResetPasswordToken(
+                    sDeviceState.dpc().componentName(), TOKEN);
+
+            PolicyState<Long> policyState = getLongPolicyState(
+                    new NoArgsPolicyKey(RESET_PASSWORD_TOKEN_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            // reset password token is a non-coexistable policy, so should not have a resolved
+            // policy.
+            assertThat(policyState.getCurrentResolvedPolicy()).isNull();
+            Long token = policyState.getPoliciesSetByAdmins().get(
+                    new EnforcingAdmin(sDeviceState.dpc().packageName(),
+                            DpcAuthority.DPC_AUTHORITY,
+                            sDeviceState.dpc().user().userHandle()));
+            assertThat(token).isNotNull();
+            assertThat(token).isNotEqualTo(0);
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().clearResetPasswordToken(
+                    sDeviceState.dpc().componentName());
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void addUserRestriction_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().addUserRestriction(
+                    sDeviceState.dpc().componentName(), LOCAL_USER_RESTRICTION);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Boolean> policyState = getBooleanPolicyState(
+                    new UserRestrictionPolicyKey(
+                            getIdentifierForUserRestriction(LOCAL_USER_RESTRICTION),
+                            LOCAL_USER_RESTRICTION),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isTrue();
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().clearUserRestriction(
+                    sDeviceState.dpc().componentName(), LOCAL_USER_RESTRICTION);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void addUserRestrictionGlobally_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().addUserRestrictionGlobally(
+                    GLOBAL_USER_RESTRICTION);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Boolean> policyState = getBooleanPolicyState(
+                    new UserRestrictionPolicyKey(
+                            getIdentifierForUserRestriction(GLOBAL_USER_RESTRICTION),
+                            GLOBAL_USER_RESTRICTION),
+                    UserHandle.ALL);
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isTrue();
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().clearUserRestriction(
+                    sDeviceState.dpc().componentName(), GLOBAL_USER_RESTRICTION);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void setKeyguardDisabledFeatures_serialisation_loadsPolicy() {
+        try {
+            sDeviceState.dpc().devicePolicyManager().setKeyguardDisabledFeatures(
+                    sDeviceState.dpc().componentName(), KEYGUARD_DISABLED_FEATURE);
+
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            PolicyState<Integer> policyState = getIntegerPolicyState(
+                    new NoArgsPolicyKey(
+                            KEYGUARD_DISABLED_FEATURES_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            assertThat(policyState.getCurrentResolvedPolicy()).isEqualTo(KEYGUARD_DISABLED_FEATURE);
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setKeyguardDisabledFeatures(
+                    sDeviceState.dpc().componentName(), KEYGUARD_DISABLE_FEATURES_NONE);
+        }
+    }
+
+    @Ignore("b/277071699: add test API to trigger reloading from disk")
+    @Test
+    @EnsureHasDevicePolicyManagerRoleHolder
+    @EnsureHasDeviceOwner
+    @Postsubmit(reason = "new test")
+    public void multiplePoliciesSet_serialisation_loadsPolicies() {
+        try {
+            // Policy Setting
+            sDeviceState.dpc().devicePolicyManager().setAutoTimeZoneEnabled(
+                    sDeviceState.dpc().componentName(), true);
+
+            sDeviceState.dpc().devicePolicyManager()
+                    .setPermissionGrantState(
+                            sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                            GRANTABLE_PERMISSION, PERMISSION_GRANT_STATE_GRANTED);
+
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskPackages(
+                            sDeviceState.dpc().componentName(), new String[]{PACKAGE_NAME});
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskFeatures(sDeviceState.dpc().componentName(), LOCK_TASK_FEATURES);
+
+            sDeviceState.dpc().devicePolicyManager().setUserControlDisabledPackages(
+                    sDeviceState.dpc().componentName(),
+                    Arrays.asList(sTestApp.packageName()));
+
+            sDeviceState.dpc().devicePolicyManager().setUninstallBlocked(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), /* uninstallBlocked= */ true);
+
+            sDeviceState.dpc().devicePolicyManager().setResetPasswordToken(
+                    sDeviceState.dpc().componentName(), TOKEN);
+
+            sDeviceState.dpc().devicePolicyManager().addUserRestriction(
+                    sDeviceState.dpc().componentName(), LOCAL_USER_RESTRICTION);
+
+            sDeviceState.dpc().devicePolicyManager().addUserRestrictionGlobally(
+                    GLOBAL_USER_RESTRICTION);
+
+            sDeviceState.dpc().devicePolicyManager().setKeyguardDisabledFeatures(
+                    sDeviceState.dpc().componentName(), KEYGUARD_DISABLED_FEATURE);
+
+            IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
+            sDeviceState.dpc().devicePolicyManager().addPersistentPreferredActivity(
+                    sDeviceState.dpc().componentName(),
+                    intentFilter,
+                    sDeviceState.dpc().componentName());
+
+            Bundle bundle = BundleUtils.createBundle(
+                    "appRestrictionsSet_serialisation_loadsPolicy");
+            sDeviceState.dpc().devicePolicyManager()
+                    .setApplicationRestrictions(
+                            sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                            bundle);
+
+            // Reloading policies from disk
+            // TODO(b/277071699): Add test API to trigger reloading from disk. Currently I've tested
+            //  this locally by triggering the loading in DPM#getDevicePolicyState in my local
+            //  build.
+
+            // Getting policy states
+            PolicyState<Boolean> autoTimezonePolicy = getBooleanPolicyState(
+                    new NoArgsPolicyKey(AUTO_TIMEZONE_POLICY),
+                    UserHandle.ALL);
+
+            PolicyState<Integer> permissionGrantStatePolicy = getIntegerPolicyState(
+                    new PackagePermissionPolicyKey(
+                            PERMISSION_GRANT_POLICY,
+                            sTestApp.packageName(),
+                            GRANTABLE_PERMISSION),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<LockTaskPolicy> lockTaskPolicy = getLockTaskPolicyState(
+                    new NoArgsPolicyKey(LOCK_TASK_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<Set<String>> userControlDisabledPackagesPolicy = getStringSetPolicyState(
+                    new NoArgsPolicyKey(USER_CONTROL_DISABLED_PACKAGES_POLICY),
+                    UserHandle.ALL);
+
+            PolicyState<Boolean> packageUninstallBlockedPolicy = getBooleanPolicyState(
+                    new PackagePolicyKey(
+                            PACKAGE_UNINSTALL_BLOCKED_POLICY,
+                            sTestApp.packageName()),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<Long> resetPasswordTokenPolicy = getLongPolicyState(
+                    new NoArgsPolicyKey(RESET_PASSWORD_TOKEN_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<Boolean> userRestrictionPolicy = getBooleanPolicyState(
+                    new UserRestrictionPolicyKey(
+                            getIdentifierForUserRestriction(LOCAL_USER_RESTRICTION),
+                            LOCAL_USER_RESTRICTION),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<Boolean> globalUserRestrictionPolicy = getBooleanPolicyState(
+                    new UserRestrictionPolicyKey(
+                            getIdentifierForUserRestriction(GLOBAL_USER_RESTRICTION),
+                            GLOBAL_USER_RESTRICTION),
+                    UserHandle.ALL);
+
+            PolicyState<Integer> keyguardDisabledPolicy = getIntegerPolicyState(
+                    new NoArgsPolicyKey(
+                            KEYGUARD_DISABLED_FEATURES_POLICY),
+                    sDeviceState.dpc().user().userHandle());
+
+            PolicyState<ComponentName> persistentPreferredActivityPolicy =
+                    getComponentNamePolicyState(
+                            new IntentFilterPolicyKey(
+                                    PERSISTENT_PREFERRED_ACTIVITY_POLICY,
+                                    intentFilter),
+                            sDeviceState.dpc().user().userHandle());
+
+            PolicyState<Bundle> applicationRestrictionsPolicy = getBundlePolicyState(
+                    new PackagePolicyKey(
+                            APPLICATION_RESTRICTIONS_POLICY,
+                            sTestApp.packageName()),
+                    sDeviceState.dpc().user().userHandle());
+
+
+            // Asserting policies loaded correctly
+            assertThat(autoTimezonePolicy.getCurrentResolvedPolicy()).isTrue();
+
+            assertThat(permissionGrantStatePolicy.getCurrentResolvedPolicy()).isEqualTo(
+                    PERMISSION_GRANT_STATE_GRANTED);
+
+            assertThat(lockTaskPolicy.getCurrentResolvedPolicy().getPackages())
+                    .containsExactly(PACKAGE_NAME);
+            assertThat(lockTaskPolicy.getCurrentResolvedPolicy().getFlags())
+                    .isEqualTo(LOCK_TASK_FEATURES);
+
+            assertThat(userControlDisabledPackagesPolicy.getCurrentResolvedPolicy())
+                    .containsExactly(sTestApp.packageName());
+
+            assertThat(packageUninstallBlockedPolicy.getCurrentResolvedPolicy()).isTrue();
+
+            // reset password token is a non-coexistable policy, so should not have a resolved
+            // policy.
+            assertThat(resetPasswordTokenPolicy.getCurrentResolvedPolicy()).isNull();
+            Long token = resetPasswordTokenPolicy.getPoliciesSetByAdmins().get(
+                    new EnforcingAdmin(sDeviceState.dpc().packageName(),
+                            DpcAuthority.DPC_AUTHORITY,
+                            sDeviceState.dpc().user().userHandle()));
+            assertThat(token).isNotNull();
+            assertThat(token).isNotEqualTo(0);
+
+            assertThat(userRestrictionPolicy.getCurrentResolvedPolicy()).isTrue();
+
+            assertThat(globalUserRestrictionPolicy.getCurrentResolvedPolicy()).isTrue();
+
+            assertThat(keyguardDisabledPolicy.getCurrentResolvedPolicy()).isEqualTo(
+                    KEYGUARD_DISABLED_FEATURE);
+
+            assertThat(persistentPreferredActivityPolicy.getCurrentResolvedPolicy()).isEqualTo(
+                    sDeviceState.dpc().componentName());
+
+            // app restrictions is a non-coexistable policy, so should not have a resolved policy.
+            assertThat(applicationRestrictionsPolicy.getCurrentResolvedPolicy()).isNull();
+            Bundle returnedBundle = applicationRestrictionsPolicy.getPoliciesSetByAdmins().get(
+                    new EnforcingAdmin(sDeviceState.dpc().packageName(),
+                            DpcAuthority.DPC_AUTHORITY,
+                            sDeviceState.dpc().user().userHandle()));
+            assertThat(returnedBundle).isNotNull();
+            BundleUtils.assertEqualToBundle(
+                    "appRestrictionsSet_serialisation_loadsPolicy",
+                    returnedBundle);
+
+
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setAutoTimeZoneEnabled(
+                    sDeviceState.dpc().componentName(), false);
+
+            sDeviceState.dpc().devicePolicyManager().setPermissionGrantState(
+                    sDeviceState.dpc().componentName(), sTestApp.packageName(),
+                    GRANTABLE_PERMISSION, PERMISSION_GRANT_STATE_DEFAULT);
+
+            sDeviceState.dpc().devicePolicyManager()
+                    .setLockTaskPackages(sDeviceState.dpc().componentName(), new String[]{});
+
+            sDeviceState.dpc().devicePolicyManager().setUserControlDisabledPackages(
+                    sDeviceState.dpc().componentName(),
+                    new ArrayList<>());
+
+            sDeviceState.dpc().devicePolicyManager().setUninstallBlocked(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), /* uninstallBlocked= */ false);
+
+            sDeviceState.dpc().devicePolicyManager().clearResetPasswordToken(
+                    sDeviceState.dpc().componentName());
+
+            sDeviceState.dpc().devicePolicyManager().clearUserRestriction(
+                    sDeviceState.dpc().componentName(), LOCAL_USER_RESTRICTION);
+
+            sDeviceState.dpc().devicePolicyManager().clearUserRestriction(
+                    sDeviceState.dpc().componentName(), GLOBAL_USER_RESTRICTION);
+
+            sDeviceState.dpc().devicePolicyManager().setKeyguardDisabledFeatures(
+                    sDeviceState.dpc().componentName(), KEYGUARD_DISABLE_FEATURES_NONE);
+            sDeviceState.dpc().devicePolicyManager().clearPackagePersistentPreferredActivities(
+                    sDeviceState.dpc().componentName(),
+                    sDeviceState.dpc().packageName());
+
+            sDeviceState.dpc().devicePolicyManager().setApplicationRestrictions(
+                    sDeviceState.dpc().componentName(),
+                    sTestApp.packageName(), new Bundle());
+        }
+    }
+
     private PolicyState<Long> getLongPolicyState(PolicyKey policyKey, UserHandle user) {
         try {
             DevicePolicyState state =