Add COPE states for tests.

Test: atest CtsDevicePolicyTestCases
Bug: 198774281
Change-Id: I2948a06a56b12726d832633637823cf8a01cd8a6
diff --git a/common/device-side/bedstead/btest/btest b/common/device-side/bedstead/btest/btest
index 93e46e4..88426bc 100755
--- a/common/device-side/bedstead/btest/btest
+++ b/common/device-side/bedstead/btest/btest
@@ -502,6 +502,11 @@
                     args.states.append(state.__class__)
                     break
 
+        # We calculate annotations before filtering so we properly exclude all
+        all_include_annotations = []
+        for state in states:
+            all_include_annotations.extend(state.include_annotations())
+
         states = [m for m in states if m.__class__ in args.states]
 
         first_state = None
@@ -521,10 +526,6 @@
         states.insert(0, states.pop(states.index(first_state)))
         needs_to_enter_state = False
 
-        all_include_annotations = []
-        for state in states:
-            all_include_annotations.extend(state.include_annotations())
-
         try:
             for i, state in enumerate(states):
                 debug(args, "[Test] Running tests for " + state.name())
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/Policy.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/Policy.java
index 45dd656..c8dde39 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/Policy.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/Policy.java
@@ -21,7 +21,10 @@
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_AFFILIATED_PROFILE_OWNER_USER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_FINANCED_DEVICE_OWNER;
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_NON_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER_USER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_USER;
@@ -59,7 +62,9 @@
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnBackgroundDeviceOwnerUser;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnDeviceOwnerUser;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnFinancedDeviceOwnerUser;
-import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfCorporateOwnedProfileOwner;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnOrganizationOwnedProfileOwner;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwner;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfProfileOwnerUsingParentInstance;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnProfileOwnerPrimaryUser;
@@ -133,13 +138,21 @@
 
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnProfileOwnerProfileWithNoDeviceOwner()))
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER | CAN_BE_DELEGATED, generateDelegateAnnotation(includeRunOnProfileOwnerProfileWithNoDeviceOwner(), /* isPrimary= */ true))
+                    .put(APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnOrganizationOwnedProfileOwner()))
+                    .put(APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER | CAN_BE_DELEGATED, generateDelegateAnnotation(includeRunOnOrganizationOwnedProfileOwner(), /* isPrimary= */ true))
+
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_PARENT, singleAnnotation(includeRunOnParentOfProfileOwnerWithNoDeviceOwner()))
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_PARENT | CAN_BE_DELEGATED, generateDelegateAnnotation(includeRunOnParentOfProfileOwnerWithNoDeviceOwner(), /* isPrimary= */ true))
+                    .put(APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_PARENT, singleAnnotation(includeRunOnParentOfOrganizationOwnedProfileOwner()))
+                    .put(APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_PARENT | CAN_BE_DELEGATED, generateDelegateAnnotation(includeRunOnParentOfOrganizationOwnedProfileOwner(), /* isPrimary= */ true))
 
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_UNAFFILIATED_OTHER_USERS, singleAnnotation(includeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerProfile()))
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE | APPLIES_TO_UNAFFILIATED_OTHER_USERS | CAN_BE_DELEGATED, generateDelegateAnnotation(includeRunOnSecondaryUserInDifferentProfileGroupToProfileOwnerProfile(), /* isPrimary= */ true))
 
-                    .put(APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnParentOfProfileOwnerUsingParentInstance()))
+                    // The model here is that APPLIED_BY_PARENT + APPLIES_TO_OWN_USER means it applies to the parent of the DPC - I'm not sure this is the best model (APPLIES_TO_PARENT would also be reasonable)
+                    .put(APPLIED_BY_PARENT_INSTANCE_OF_NON_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE
+                            | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnParentOfProfileOwnerUsingParentInstance()))
+                    .put(APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance()))
 
                     .put(APPLIED_BY_FINANCED_DEVICE_OWNER | APPLIES_TO_OWN_USER, singleAnnotation(includeRunOnFinancedDeviceOwnerUser()))
 
@@ -154,6 +167,7 @@
                     .put(APPLIED_BY_AFFILIATED_PROFILE_OWNER, singleAnnotation(includeRunOnAffiliatedProfileOwnerSecondaryUser()))
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_USER, singleAnnotation(includeRunOnProfileOwnerPrimaryUser()))
                     .put(APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO, singleAnnotation(includeRunOnProfileOwnerPrimaryUser()))
+                    .put(APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE, singleAnnotation(includeRunOnOrganizationOwnedProfileOwner()))
                     .put(APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE, singleAnnotation(includeRunOnProfileOwnerProfileWithNoDeviceOwner()))
                     .put(APPLIED_BY_FINANCED_DEVICE_OWNER, singleAnnotation(includeRunOnFinancedDeviceOwnerUser()))
                     .build();
@@ -165,8 +179,11 @@
                     | APPLIED_BY_AFFILIATED_PROFILE_OWNER_PROFILE
                     | APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_USER
                     | APPLIED_BY_AFFILIATED_PROFILE_OWNER_USER
-                    | APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER
-                    | APPLIED_BY_FINANCED_DEVICE_OWNER;
+                    | APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER_USER
+                    | APPLIED_BY_FINANCED_DEVICE_OWNER
+                    | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE
+                    | APPLIED_BY_PARENT_INSTANCE_OF_NON_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE
+                    | APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
     private static final Map<Function<EnterprisePolicy, Set<Annotation>>, Set<Integer>>
             ANNOTATIONS_MAP = calculateAnnotationsMap(STATE_ANNOTATIONS);
 
@@ -244,8 +261,13 @@
     }
 
     @AutoAnnotation
-    private static IncludeRunOnParentOfCorporateOwnedProfileOwner includeRunOnParentOfCorporateOwnedProfileOwner() {
-        return new AutoAnnotation_Policy_includeRunOnParentOfCorporateOwnedProfileOwner();
+    private static IncludeRunOnOrganizationOwnedProfileOwner includeRunOnOrganizationOwnedProfileOwner() {
+        return new AutoAnnotation_Policy_includeRunOnOrganizationOwnedProfileOwner();
+    }
+
+    @AutoAnnotation
+    private static IncludeRunOnParentOfOrganizationOwnedProfileOwner includeRunOnParentOfOrganizationOwnedProfileOwner() {
+        return new AutoAnnotation_Policy_includeRunOnParentOfOrganizationOwnedProfileOwner();
     }
 
     @AutoAnnotation
@@ -270,6 +292,12 @@
     }
 
     @AutoAnnotation
+    private static IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance() {
+        return new AutoAnnotation_Policy_includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance();
+    }
+
+
+    @AutoAnnotation
     private static IncludeRunOnFinancedDeviceOwnerUser includeRunOnFinancedDeviceOwnerUser() {
         return new AutoAnnotation_Policy_includeRunOnFinancedDeviceOwnerUser();
     }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/enterprise/EnterprisePolicy.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/enterprise/EnterprisePolicy.java
index 44257a1b..1f5ea38 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/enterprise/EnterprisePolicy.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/enterprise/EnterprisePolicy.java
@@ -88,14 +88,14 @@
     int APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE = 1 << 7;
     /** A policy that can be applied by a profile owner of an affiliated profile */
     int APPLIED_BY_AFFILIATED_PROFILE_OWNER_PROFILE = 1 << 8;
-    /** A policy that can be applied by a profile owner of a cope profile */
-    int APPLIED_BY_COPE_PROFILE_OWNER = 1 << 9;
+    /** A policy that can be applied by a profile owner of an organization owned profile */
+    int APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE = 1 << 9;
 
-    /** A policy that can be applied by a profile owner of an affiliated or unaffiliated profile.
-     * This does not include cope profiles. */
+    /** A policy that can be applied by a profile owner of an affiliated or unaffiliated profile. */
     int APPLIED_BY_PROFILE_OWNER_PROFILE =
             APPLIED_BY_UNAFFILIATED_PROFILE_OWNER_PROFILE
-                    | APPLIED_BY_AFFILIATED_PROFILE_OWNER_PROFILE;
+                    | APPLIED_BY_AFFILIATED_PROFILE_OWNER_PROFILE
+                    | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
     /**
      * A policy that can be applied by a Profile Owner for a User (not Profile) with no Device
      * Owner.
@@ -122,11 +122,11 @@
             APPLIED_BY_PROFILE_OWNER_PROFILE
             | APPLIED_BY_PROFILE_OWNER_USER;
 
-    int APPLIED_BY_PARENT_INSTANCE_OF_NON_COPE_PROFILE_OWNER_PROFILE = 1 << 13;
-    int APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE = 1 << 14;
+    int APPLIED_BY_PARENT_INSTANCE_OF_NON_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE = 1 << 13;
+    int APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE = 1 << 14;
 
     int APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER_PROFILE =
-            APPLIED_BY_PARENT_INSTANCE_OF_NON_COPE_PROFILE_OWNER_PROFILE | APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE;
+            APPLIED_BY_PARENT_INSTANCE_OF_NON_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE | APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
 
     int APPLIED_BY_PARENT_INSTANCE_OF_PROFILE_OWNER_USER = 1 << 15;
 
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedManagedProfile.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedProfileOwner.java
similarity index 92%
rename from common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedManagedProfile.java
rename to common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedProfileOwner.java
index ed076b5..8c0ed08 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedManagedProfile.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnOrganizationOwnedProfileOwner.java
@@ -30,9 +30,9 @@
 /** Parameterize a test so that it runs on an organization-owned managed profile. */
 @Target({ElementType.METHOD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
-@ParameterizedAnnotation
+@ParameterizedAnnotation(shadows = IncludeRunOnProfileOwnerProfileWithNoDeviceOwner.class)
 @RequireRunOnWorkProfile(isOrganizationOwned = true)
-public @interface IncludeRunOnOrganizationOwnedManagedProfile {
+public @interface IncludeRunOnOrganizationOwnedProfileOwner {
     /**
      * Weight sets the order that annotations will be resolved.
      *
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwner.java
similarity index 85%
rename from common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java
rename to common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwner.java
index 88b203e..447f1d1 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwner.java
@@ -17,9 +17,9 @@
 package com.android.bedstead.harrier.annotations.parameterized;
 
 import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.EARLY;
-import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.LATE;
 
 import com.android.bedstead.harrier.annotations.AnnotationRunPrecedence;
+import com.android.bedstead.harrier.annotations.EnsureHasWorkProfile;
 import com.android.bedstead.harrier.annotations.RequireRunOnPrimaryUser;
 import com.android.bedstead.harrier.annotations.meta.ParameterizedAnnotation;
 
@@ -29,14 +29,14 @@
 import java.lang.annotation.Target;
 
 /**
- * Parameterize a test so that it runs on the parent of a corporate-owned profile owner.
+ * Parameterize a test so that it runs on the parent of an organization-owned profile owner.
  */
 @Target({ElementType.METHOD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
 @ParameterizedAnnotation(shadows = IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner.class)
 @RequireRunOnPrimaryUser
-// TODO(scottjonathan): Add annotation to create corporate-owned profile
-public @interface IncludeRunOnParentOfCorporateOwnedProfileOwner {
+@EnsureHasWorkProfile(isOrganizationOwned = true, dpcIsPrimary = true)
+public @interface IncludeRunOnParentOfOrganizationOwnedProfileOwner {
     /**
      * Weight sets the order that annotations will be resolved.
      *
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance.java
similarity index 80%
copy from common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java
copy to common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance.java
index 88b203e..cd29c38 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfCorporateOwnedProfileOwner.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/annotations/parameterized/IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2021 The Android Open Source Project
+ * Copyright (C) 2022 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,9 +17,9 @@
 package com.android.bedstead.harrier.annotations.parameterized;
 
 import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.EARLY;
-import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.LATE;
 
 import com.android.bedstead.harrier.annotations.AnnotationRunPrecedence;
+import com.android.bedstead.harrier.annotations.EnsureHasWorkProfile;
 import com.android.bedstead.harrier.annotations.RequireRunOnPrimaryUser;
 import com.android.bedstead.harrier.annotations.meta.ParameterizedAnnotation;
 
@@ -29,14 +29,14 @@
 import java.lang.annotation.Target;
 
 /**
- * Parameterize a test so that it runs on the parent of a corporate-owned profile owner.
+ * Parameterize a test so that it runs on the parent of an organization-owned profile owner.
  */
 @Target({ElementType.METHOD, ElementType.TYPE})
 @Retention(RetentionPolicy.RUNTIME)
-@ParameterizedAnnotation(shadows = IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner.class)
+@ParameterizedAnnotation(shadows = IncludeRunOnParentOfProfileOwnerUsingParentInstance.class)
 @RequireRunOnPrimaryUser
-// TODO(scottjonathan): Add annotation to create corporate-owned profile
-public @interface IncludeRunOnParentOfCorporateOwnedProfileOwner {
+@EnsureHasWorkProfile(isOrganizationOwned = true, dpcIsPrimary = true, useParentInstanceOfDpc = true)
+public @interface IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance {
     /**
      * Weight sets the order that annotations will be resolved.
      *
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHidden.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHidden.java
index ad8ebbe..0723c7a 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHidden.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHidden.java
@@ -18,6 +18,7 @@
 
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PROFILE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_TO_OWN_USER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.CAN_BE_DELEGATED;
 import static com.android.bedstead.nene.devicepolicy.CommonDevicePolicy.DELEGATION_PACKAGE_ACCESS;
 
@@ -32,7 +33,7 @@
  */
 @EnterprisePolicy(
         dpc = {
-                APPLIED_BY_DEVICE_OWNER | APPLIED_BY_PROFILE_OWNER | CAN_BE_DELEGATED},
+                APPLIED_BY_DEVICE_OWNER | APPLIED_BY_PROFILE_OWNER | CAN_BE_DELEGATED | APPLIES_TO_OWN_USER},
         delegatedScopes = DELEGATION_PACKAGE_ACCESS
 )
 public class ApplicationHidden {
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHiddenSystemOnly.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHiddenSystemOnly.java
index f41a8d1..2ea3fb1 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHiddenSystemOnly.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ApplicationHiddenSystemOnly.java
@@ -16,7 +16,8 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_TO_OWN_USER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.CAN_BE_DELEGATED;
 import static com.android.bedstead.nene.devicepolicy.CommonDevicePolicy.DELEGATION_PACKAGE_ACCESS;
 
@@ -30,7 +31,7 @@
  * {@code DevicePolicyManager#isApplicationHidden(ComponentName, String)}.
  */
 @EnterprisePolicy(
-        dpc = {APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE | CAN_BE_DELEGATED},
+        dpc = {APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE | CAN_BE_DELEGATED | APPLIES_TO_OWN_USER},
         delegatedScopes = DELEGATION_PACKAGE_ACCESS
 )
 public class ApplicationHiddenSystemOnly {
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTime.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTime.java
index 96f4785..2a3e373 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTime.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTime.java
@@ -16,8 +16,8 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 
@@ -29,7 +29,8 @@
  * <p>This is used by {@code DevicePolicyManager#setAutoTimeEnabled}
  * {@code DevicePolicyManager#setAutoTimeZoneEnabled}.
  */
-@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER
+@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER
+        | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE
         | APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO | APPLIES_GLOBALLY)
 public final class AutoTime {
 }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTimeRequired.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTimeRequired.java
index 08ec587..b246676 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTimeRequired.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/AutoTimeRequired.java
@@ -17,9 +17,9 @@
 package com.android.bedstead.harrier.policies;
 
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_AFFILIATED_PROFILE_OWNER;
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_FINANCED_DEVICE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 
@@ -31,7 +31,8 @@
  * <p>This is used by {@code DevicePolicyManager#setAutoTimeRequired}
  */
 // TODO: Should this be applicable by financed DO?
-@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER
+@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER
+        | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE
         | APPLIED_BY_PROFILE_OWNER_USER_WITH_NO_DO | APPLIED_BY_FINANCED_DEVICE_OWNER
         | APPLIED_BY_AFFILIATED_PROFILE_OWNER | APPLIES_GLOBALLY)
 public final class AutoTimeRequired {
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/LostMode.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/LostMode.java
index 7ade805..6229c18 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/LostMode.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/LostMode.java
@@ -16,7 +16,7 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_TO_OWN_USER;
 
@@ -28,6 +28,6 @@
  * <p>This is used by {@code DevicePolicyManager#sendLostModeLocationUpdate}.
  */
 @EnterprisePolicy(dpc = {
-        APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER | APPLIES_TO_OWN_USER})
+        APPLIED_BY_DEVICE_OWNER | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_TO_OWN_USER})
 public final class LostMode {
 }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ScreenCaptureDisabled.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ScreenCaptureDisabled.java
index 045b625..0c00934 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ScreenCaptureDisabled.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/ScreenCaptureDisabled.java
@@ -17,7 +17,7 @@
 package com.android.bedstead.harrier.policies;
 
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_TO_OWN_USER;
@@ -31,7 +31,7 @@
  * {@code DevicePolicyManager#setScreenCaptureDisabled(ComponentName, boolean)}.
  */
 @EnterprisePolicy(dpc = {
-        APPLIED_BY_DEVICE_OWNER | APPLIED_BY_PARENT_INSTANCE_OF_COPE_PROFILE_OWNER_PROFILE
+        APPLIED_BY_DEVICE_OWNER | APPLIED_BY_PARENT_INSTANCE_OF_ORGANIZATIONAL_OWNED_PROFILE_OWNER_PROFILE
                 | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER})
 public final class ScreenCaptureDisabled {
 }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/Time.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/Time.java
index 8be58d6..04d2db0 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/Time.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/Time.java
@@ -16,8 +16,8 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 
 import com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy;
@@ -28,6 +28,7 @@
  * <p>This is used by {@code DevicePolicyManager#setTime} and
  * {@code DevicePolicyManager#setTimeZone}.
  */
-@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER | APPLIES_GLOBALLY)
+@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER
+        | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE | APPLIES_GLOBALLY)
 public final class Time {
 }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiMinimumSecurity.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiMinimumSecurity.java
index 6f15bc9..b8f67d7 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiMinimumSecurity.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiMinimumSecurity.java
@@ -16,8 +16,8 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 
 import com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy;
@@ -28,6 +28,7 @@
  * <p>Users of this policy are {@code DevicePolicyManager#setMinimumRequiredWifiSecurityLevel(int)}
  * and {@code DevicePolicyManager#getMinimumRequiredWifiSecurityLevel()}.
  */
-@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER | APPLIES_GLOBALLY)
+@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE
+        | APPLIES_GLOBALLY)
 public class WifiMinimumSecurity {
 }
diff --git a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiSsidRestriction.java b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiSsidRestriction.java
index f88bd38..1b2bb30 100644
--- a/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiSsidRestriction.java
+++ b/common/device-side/bedstead/harrier/common/src/main/java/com/android/bedstead/harrier/policies/WifiSsidRestriction.java
@@ -16,8 +16,8 @@
 
 package com.android.bedstead.harrier.policies;
 
-import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_COPE_PROFILE_OWNER;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_DEVICE_OWNER;
+import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE;
 import static com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy.APPLIES_GLOBALLY;
 
 import com.android.bedstead.harrier.annotations.enterprise.EnterprisePolicy;
@@ -28,6 +28,7 @@
  * <p>Users of this policy are {@code DevicePolicyManager#setWifiSsidPolicy(WifiSsidPolicy)}
  * and {@code DevicePolicyManager#getWifiSsidPolicy()}.
  */
-@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_COPE_PROFILE_OWNER | APPLIES_GLOBALLY)
+@EnterprisePolicy(dpc = APPLIED_BY_DEVICE_OWNER | APPLIED_BY_ORGANIZATION_OWNED_PROFILE_OWNER_PROFILE
+        | APPLIES_GLOBALLY)
 public class WifiSsidRestriction {
 }
diff --git a/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java b/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java
index de18123..540a4d1 100644
--- a/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java
+++ b/common/device-side/bedstead/harrier/src/main/java/com/android/bedstead/harrier/DeviceState.java
@@ -37,6 +37,7 @@
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
+import android.content.IntentFilter;
 import android.os.Build;
 import android.util.Log;
 
@@ -1509,6 +1510,12 @@
                         resolvedProfileType,
                         forUserReference);
         if (profile != null) {
+            // We can't remove an organization owned profile
+            ProfileOwner profileOwner = TestApis.devicePolicy().getProfileOwner(profile);
+            if (profileOwner != null && profileOwner.isOrganizationOwned()) {
+                profileOwner.setIsOrganizationOwned(false);
+            }
+
             removeAndRecordUser(profile);
         }
     }
@@ -1601,6 +1608,14 @@
      * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
      * test has run.
      */
+    public BlockingBroadcastReceiver registerBroadcastReceiver(IntentFilter intentFilter) {
+        return registerBroadcastReceiver(intentFilter, /* checker= */ null);
+    }
+
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
     public BlockingBroadcastReceiver registerBroadcastReceiver(
             String action, Function<Intent, Boolean> checker) {
         BlockingBroadcastReceiver broadcastReceiver =
@@ -1615,6 +1630,20 @@
      * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
      * test has run.
      */
+    public BlockingBroadcastReceiver registerBroadcastReceiver(
+            IntentFilter intentfilter, Function<Intent, Boolean> checker) {
+        BlockingBroadcastReceiver broadcastReceiver =
+                new BlockingBroadcastReceiver(mContext, intentfilter, checker);
+        broadcastReceiver.register();
+        mRegisteredBroadcastReceivers.add(broadcastReceiver);
+
+        return broadcastReceiver;
+    }
+
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
     public BlockingBroadcastReceiver registerBroadcastReceiverForUser(
             UserReference user, String action) {
         return registerBroadcastReceiverForUser(user, action, /* checker= */ null);
@@ -1625,6 +1654,15 @@
      * test has run.
      */
     public BlockingBroadcastReceiver registerBroadcastReceiverForUser(
+            UserReference user, IntentFilter intentFilter) {
+        return registerBroadcastReceiverForUser(user, intentFilter, /* checker= */ null);
+    }
+
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
+    public BlockingBroadcastReceiver registerBroadcastReceiverForUser(
             UserReference user, String action, Function<Intent, Boolean> checker) {
         try (PermissionContext p =
                      TestApis.permissions().withPermission(INTERACT_ACROSS_USERS_FULL)) {
@@ -1642,6 +1680,24 @@
      * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
      * test has run.
      */
+    public BlockingBroadcastReceiver registerBroadcastReceiverForUser(
+            UserReference user, IntentFilter intentFilter, Function<Intent, Boolean> checker) {
+        try (PermissionContext p =
+                     TestApis.permissions().withPermission(INTERACT_ACROSS_USERS_FULL)) {
+            BlockingBroadcastReceiver broadcastReceiver =
+                    new BlockingBroadcastReceiver(
+                            TestApis.context().androidContextAsUser(user), intentFilter, checker);
+            broadcastReceiver.register();
+            mRegisteredBroadcastReceivers.add(broadcastReceiver);
+
+            return broadcastReceiver;
+        }
+    }
+
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
     public BlockingBroadcastReceiver registerBroadcastReceiverForAllUsers(String action) {
         return registerBroadcastReceiverForAllUsers(action, /* checker= */ null);
     }
@@ -1650,6 +1706,14 @@
      * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
      * test has run.
      */
+    public BlockingBroadcastReceiver registerBroadcastReceiverForAllUsers(IntentFilter intentFilter) {
+        return registerBroadcastReceiverForAllUsers(intentFilter, /* checker= */ null);
+    }
+
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
     public BlockingBroadcastReceiver registerBroadcastReceiverForAllUsers(
             String action, Function<Intent, Boolean> checker) {
             try (PermissionContext p =
@@ -1664,6 +1728,24 @@
             }
     }
 
+    /**
+     * Create and register a {@link BlockingBroadcastReceiver} which will be unregistered after the
+     * test has run.
+     */
+    public BlockingBroadcastReceiver registerBroadcastReceiverForAllUsers(
+            IntentFilter intentFilter, Function<Intent, Boolean> checker) {
+        try (PermissionContext p =
+                     TestApis.permissions().withPermission(INTERACT_ACROSS_USERS_FULL)) {
+            BlockingBroadcastReceiver broadcastReceiver =
+                    new BlockingBroadcastReceiver(mContext, intentFilter, checker);
+            broadcastReceiver.registerForAllUsers();
+
+            mRegisteredBroadcastReceivers.add(broadcastReceiver);
+
+            return broadcastReceiver;
+        }
+    }
+
     private UserReference resolveUserTypeToUser(UserType userType) {
         switch (userType) {
             case SYSTEM_USER:
diff --git a/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java b/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java
index 6ed6734..e9df389 100644
--- a/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java
+++ b/common/device-side/bedstead/harrier/src/test/java/com/android/bedstead/harrier/DeviceStateTest.java
@@ -103,7 +103,9 @@
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnBackgroundDeviceOwnerUser;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnDeviceOwnerUser;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnFinancedDeviceOwnerUser;
-import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnOrganizationOwnedManagedProfile;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnOrganizationOwnedProfileOwner;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwner;
+import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfProfileOwnerUsingParentInstance;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnParentOfProfileOwnerWithNoDeviceOwner;
 import com.android.bedstead.harrier.annotations.parameterized.IncludeRunOnProfileOwnerProfileWithNoDeviceOwner;
@@ -476,6 +478,34 @@
     }
 
     @Test
+    @IncludeRunOnOrganizationOwnedProfileOwner
+    public void includeRunOnOrganizationOwnedProfileOwnerAnnotation_isRunningOnOrganizationOwnedManagedProfile() {
+        assertThat(TestApis.users().instrumented().type().name())
+                .isEqualTo(MANAGED_PROFILE_TYPE_NAME);
+        assertThat(TestApis.devicePolicy().getProfileOwner().isOrganizationOwned()).isTrue();
+    }
+
+    @Test
+    @IncludeRunOnParentOfOrganizationOwnedProfileOwner
+    public void includeRunOnParentOfOrganizationOwnedProfileOwner_isRunningOnParentOfOrganizationOwnedProfileOwner() {
+        UserReference dpcUser = sDeviceState.dpc().user();
+
+        assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME);
+        assertThat(TestApis.devicePolicy().getProfileOwner(dpcUser).isOrganizationOwned()).isTrue();
+        assertThat(sDeviceState.dpc().isParentInstance()).isFalse();
+    }
+
+    @Test
+    @IncludeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance
+    public void includeRunOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance_isRunningOnParentOfOrganizationOwnedProfileOwnerUsingParentInstance() {
+        UserReference dpcUser = sDeviceState.dpc().user();
+
+        assertThat(dpcUser.type().name()).isEqualTo(MANAGED_PROFILE_TYPE_NAME);
+        assertThat(TestApis.devicePolicy().getProfileOwner(dpcUser).isOrganizationOwned()).isTrue();
+        assertThat(sDeviceState.dpc().isParentInstance()).isTrue();
+    }
+
+    @Test
     @IncludeRunOnDeviceOwnerUser
     public void includeRunOnDeviceOwnerUserAnnotation_isRunningOnDeviceOwnerUser() {
         assertThat(TestApis.devicePolicy().getDeviceOwner().user())
@@ -1025,8 +1055,8 @@
     }
 
     @Test
-    @IncludeRunOnOrganizationOwnedManagedProfile
-    public void includeRunOnOrganizationOwnedManagedProfile_isOrganizationOwned() {
+    @IncludeRunOnOrganizationOwnedProfileOwner
+    public void includeRunOnOrganizationOwnedProfileOwner_isOrganizationOwned() {
         assertThat(((ProfileOwner) sDeviceState.profileOwner(
                 sDeviceState.workProfile()).devicePolicyController()).isOrganizationOwned())
                 .isTrue();
diff --git a/tests/devicepolicy/src/android/devicepolicy/cts/ApplicationHiddenTest.java b/tests/devicepolicy/src/android/devicepolicy/cts/ApplicationHiddenTest.java
index 8dd5f38..b3aeb80 100644
--- a/tests/devicepolicy/src/android/devicepolicy/cts/ApplicationHiddenTest.java
+++ b/tests/devicepolicy/src/android/devicepolicy/cts/ApplicationHiddenTest.java
@@ -18,14 +18,13 @@
 
 import static android.content.Intent.ACTION_PACKAGE_ADDED;
 import static android.content.Intent.ACTION_PACKAGE_REMOVED;
-import static android.content.pm.PackageManager.MATCH_UNINSTALLED_PACKAGES;
-
-import static com.android.bedstead.nene.permissions.CommonPermissions.QUERY_ALL_PACKAGES;
 
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.testng.Assert.assertThrows;
 
+import android.content.Intent;
+import android.content.IntentFilter;
 import android.content.pm.PackageManager;
 import android.stats.devicepolicy.EventId;
 
@@ -35,6 +34,7 @@
 import com.android.bedstead.harrier.annotations.enterprise.CanSetPolicyTest;
 import com.android.bedstead.harrier.annotations.enterprise.CannotSetPolicyTest;
 import com.android.bedstead.harrier.annotations.enterprise.PolicyAppliesTest;
+import com.android.bedstead.harrier.annotations.enterprise.PolicyDoesNotApplyTest;
 import com.android.bedstead.harrier.annotations.enterprise.RequireHasPolicyExemptApps;
 import com.android.bedstead.harrier.policies.ApplicationHidden;
 import com.android.bedstead.harrier.policies.ApplicationHiddenSystemOnly;
@@ -42,14 +42,15 @@
 import com.android.bedstead.metricsrecorder.truth.MetricQueryBuilderSubject;
 import com.android.bedstead.nene.TestApis;
 import com.android.bedstead.nene.packages.Package;
-import com.android.bedstead.nene.permissions.PermissionContext;
 import com.android.compatibility.common.util.BlockingBroadcastReceiver;
 
+import org.junit.Before;
 import org.junit.ClassRule;
 import org.junit.Rule;
 import org.junit.runner.RunWith;
 
 import java.util.Set;
+import java.util.function.Function;
 
 @RunWith(BedsteadJUnit4.class)
 public class ApplicationHiddenTest {
@@ -63,6 +64,25 @@
     private static final PackageManager sLocalPackageManager =
             TestApis.context().instrumentedContext().getPackageManager();
 
+    // TODO: All references to isApplicationHidden and setApplicationHidden which are not part of
+    //  the "act" step of the test should run through a Nene API, once those APIs are permission
+    //  accessible
+
+    private static final IntentFilter sPackageAddedIntentFilter = new IntentFilter();
+    private static final IntentFilter sPackageRemovedIntentFilter = new IntentFilter();
+    static {
+        sPackageAddedIntentFilter.addAction(ACTION_PACKAGE_ADDED);
+        sPackageRemovedIntentFilter.addAction(ACTION_PACKAGE_REMOVED);
+        sPackageAddedIntentFilter.addDataScheme("package");
+        sPackageRemovedIntentFilter.addDataScheme("package");
+    }
+
+    @Before
+    public void ensureSystemPackageInstalled() {
+        SYSTEM_PACKAGE.installExisting(TestApis.users().instrumented());
+        SYSTEM_PACKAGE.installExisting(sDeviceState.dpc().user());
+    }
+
     @CanSetPolicyTest(policy = {ApplicationHidden.class, ApplicationHiddenSystemOnly.class})
     public void isApplicationHidden_systemApp_isHidden_returnsTrue() {
         boolean originalValue = sDeviceState.dpc().devicePolicyManager().isApplicationHidden(
@@ -157,13 +177,14 @@
                 sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName());
 
         try {
+            sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
+                    sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
+                    false);
 
             try (BlockingBroadcastReceiver broadcastReceiver =
-                         sDeviceState.registerBroadcastReceiver(ACTION_PACKAGE_ADDED,
-                                 (intent) -> intent.getData() != null
-                                         && intent.getData().getSchemeSpecificPart().equals(
-                                         SYSTEM_PACKAGE.packageName()))) {
-
+                         sDeviceState.registerBroadcastReceiverForAllUsers(
+                                 sPackageRemovedIntentFilter,
+                                 isSchemeSpecificPart(SYSTEM_PACKAGE.packageName()))) {
                 boolean result = sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                         sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
                         true);
@@ -172,15 +193,26 @@
                 broadcastReceiver.awaitForBroadcastOrFail();
             }
 
-            try (PermissionContext p = TestApis.permissions().withPermission(QUERY_ALL_PACKAGES)) {
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        SYSTEM_PACKAGE.packageName(),
-                        PackageManager.PackageInfoFlags.of(0))).isNull();
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        SYSTEM_PACKAGE.packageName(),
-                        PackageManager.PackageInfoFlags.of(
-                                MATCH_UNINSTALLED_PACKAGES))).isNotNull();
-            }
+            assertThat(SYSTEM_PACKAGE.installedOnUser()).isFalse();
+            assertThat(SYSTEM_PACKAGE.exists()).isTrue();
+        } finally {
+            sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
+                    sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
+                    originalValue);
+        }
+    }
+
+    @PolicyDoesNotApplyTest(policy = {ApplicationHidden.class, ApplicationHiddenSystemOnly.class})
+    public void setApplicationHidden_systemApp_true_applicationIsNotHidden() throws Exception {
+        boolean originalValue = sDeviceState.dpc().devicePolicyManager().isApplicationHidden(
+                sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName());
+
+        try {
+            sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
+                    sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
+                    true);
+
+            assertThat(SYSTEM_PACKAGE.installedOnUser()).isTrue();
         } finally {
             sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                     sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
@@ -195,11 +227,14 @@
                 sDeviceState.dpc().componentName(), sDeviceState.testApp().packageName());
 
         try {
+            sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
+                    sDeviceState.dpc().componentName(), sDeviceState.testApp().packageName(),
+                    false);
+
             try (BlockingBroadcastReceiver broadcastReceiver =
-                         sDeviceState.registerBroadcastReceiver(ACTION_PACKAGE_ADDED,
-                                 (intent) -> intent.getData() != null
-                                         && intent.getData().getSchemeSpecificPart().equals(
-                                         sDeviceState.testApp().packageName()))) {
+                         sDeviceState.registerBroadcastReceiverForAllUsers(
+                                 sPackageRemovedIntentFilter,
+                                 isSchemeSpecificPart(sDeviceState.testApp().packageName()))) {
 
                 boolean result = sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                         sDeviceState.dpc().componentName(), sDeviceState.testApp().packageName(),
@@ -209,15 +244,8 @@
                 broadcastReceiver.awaitForBroadcastOrFail();
             }
 
-            try (PermissionContext p = TestApis.permissions().withPermission(QUERY_ALL_PACKAGES)) {
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        sDeviceState.testApp().packageName(),
-                        PackageManager.PackageInfoFlags.of(0))).isNull();
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        sDeviceState.testApp().packageName(),
-                        PackageManager.PackageInfoFlags.of(
-                                MATCH_UNINSTALLED_PACKAGES))).isNotNull();
-            }
+            assertThat(sDeviceState.testApp().testApp().pkg().installedOnUser()).isFalse();
+            assertThat(sDeviceState.testApp().testApp().pkg().exists()).isTrue();
         } finally {
             sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                     sDeviceState.dpc().componentName(), sDeviceState.testApp().packageName(),
@@ -235,10 +263,9 @@
                     true);
 
             try (BlockingBroadcastReceiver broadcastReceiver =
-                         sDeviceState.registerBroadcastReceiver(ACTION_PACKAGE_REMOVED,
-                                 (intent) -> intent.getData() != null
-                                         && intent.getData().getSchemeSpecificPart().equals(
-                                         SYSTEM_PACKAGE.packageName()))) {
+                         sDeviceState.registerBroadcastReceiverForAllUsers(
+                                 sPackageAddedIntentFilter,
+                                 isSchemeSpecificPart(SYSTEM_PACKAGE.packageName()))) {
 
                 boolean result = sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                         sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
@@ -248,11 +275,7 @@
                 broadcastReceiver.awaitForBroadcastOrFail();
             }
 
-            try (PermissionContext p = TestApis.permissions().withPermission(QUERY_ALL_PACKAGES)) {
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        SYSTEM_PACKAGE.packageName(),
-                        PackageManager.PackageInfoFlags.of(0))).isNotNull();
-            }
+            assertThat(SYSTEM_PACKAGE.installedOnUser()).isTrue();
         } finally {
             sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                     sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
@@ -271,10 +294,9 @@
                     true);
 
             try (BlockingBroadcastReceiver broadcastReceiver =
-                         sDeviceState.registerBroadcastReceiver(ACTION_PACKAGE_REMOVED,
-                                 (intent) -> intent.getData() != null
-                                         && intent.getData().getSchemeSpecificPart().equals(
-                                         sDeviceState.testApp().packageName()))) {
+                         sDeviceState.registerBroadcastReceiverForAllUsers(
+                                 sPackageAddedIntentFilter,
+                                 isSchemeSpecificPart(sDeviceState.testApp().packageName()))) {
 
                 boolean result = sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                         sDeviceState.dpc().componentName(), sDeviceState.testApp().packageName(),
@@ -284,11 +306,7 @@
                 broadcastReceiver.awaitForBroadcastOrFail();
             }
 
-            try (PermissionContext p = TestApis.permissions().withPermission(QUERY_ALL_PACKAGES)) {
-                assertThat(sLocalPackageManager.getPackageInfo(
-                        sDeviceState.testApp().packageName(),
-                        PackageManager.PackageInfoFlags.of(0))).isNotNull();
-            }
+            assertThat(sDeviceState.testApp().testApp().pkg().installedOnUser()).isTrue();
         } finally {
             sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                     sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
@@ -308,11 +326,7 @@
                         true);
 
                 assertThat(result).isFalse();
-                try (PermissionContext p = TestApis.permissions().withPermission(QUERY_ALL_PACKAGES)) {
-                    assertThat(sLocalPackageManager.getPackageInfo(
-                            packageName,
-                            PackageManager.PackageInfoFlags.of(0))).isNotNull();
-                }
+                assertThat(TestApis.packages().find(packageName).installedOnUser()).isTrue();
             } finally {
                 sDeviceState.dpc().devicePolicyManager().setApplicationHidden(
                         sDeviceState.dpc().componentName(), SYSTEM_PACKAGE.packageName(),
@@ -405,4 +419,9 @@
 
         assertThat(result).isFalse();
     }
+
+    private Function<Intent, Boolean> isSchemeSpecificPart(String part) {
+        return (intent) -> intent.getData() != null
+                && intent.getData().getSchemeSpecificPart().equals(part);
+    }
 }