1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.bedstead.enterprise.annotations
17 
18 import com.android.bedstead.harrier.annotations.AnnotationPriorityRunPrecedence
19 import com.android.bedstead.harrier.annotations.meta.RequiresBedsteadJUnit4
20 import com.google.auto.value.AutoAnnotation
21 import kotlin.reflect.KClass
22 
23 /**
24  * Mark a test as testing the states where a policy is allowed to be applied.
25  *
26  *
27  * This will generate parameterized runs for all matching states. Tests will only be run on
28  * the same user as the DPC. If you wish to test that a policy applies across all relevant states,
29  * use [PolicyAppliesTest].
30  */
31 @Target(AnnotationTarget.FUNCTION)
32 @Retention(
33     AnnotationRetention.RUNTIME)
34 @RequiresBedsteadJUnit4
35 annotation class CanSetPolicyTest(
36     /**
37      * The policy being tested.
38      *
39      * Setting policy will run tests in all states where the caller has access to either of the policies specified
40      *
41      * If multiple policies are specified, then they will be merged so that all valid states for
42      * all specified policies are considered as valid.
43      *
44      * Same as [policyUnion].
45      *
46      * <p> Example usage
47      * Policy1: APPLIED_BY_DEVICE_OWNER | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER
48      * Policy 2: APPLIED_BY_DEVICE_OWNER | APPLIES_TO_OWN_USER
49      * policy: APPLIED_BY_DEVICE_OWNER | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER
50      *
51      * Only 1 of policy/policyUnion/policyIntersection must be set at a time
52      *
53      * This is used to calculate which states are required to be tested.
54      */
55     val policy: Array<KClass<*>> = [],
56     /**
57      * The policy being tested.
58      *
59      * Setting policyUnion will run tests in all states where the caller has access to either of the policies specified
60      *
61      * If multiple policies are specified, then they will be merged so that all valid states for
62      * all specified policies are considered as valid.
63      *
64      * Same as [policy].
65      *
66      * <p> Example usage
67      * Policy1: APPLIED_BY_DEVICE_OWNER | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER
68      * Policy 2: APPLIED_BY_DEVICE_OWNER | APPLIES_TO_OWN_USER
69      * policyUnion: APPLIED_BY_DEVICE_OWNER | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER
70      *
71      * Only 1 of policy/policyUnion/policyIntersection must be set at a time
72      *
73      * This is used to calculate which states are required to be tested.
74      */
75     val policyUnion: Array<KClass<*>> = [],
76     /**
77      * The policy being tested.
78      *
79      * Setting policyIntersection will run tests in all states where the caller has access to all of the policies specified
80      *
81      * If multiple policies are specified, then they will be filtered so that only states that
82      * apply to all specified policies are considered as valid.
83      *
84      * <p> Example usage
85      * Policy1: APPLIED_BY_DEVICE_OWNER | APPLIES_GLOBALLY, APPLIED_BY_PROFILE_OWNER | APPLIES_TO_OWN_USER
86      * Policy 2: APPLIED_BY_DEVICE_OWNER | APPLIES_TO_OWN_USER
87      * policyIntersection: APPLIED_BY_DEVICE_OWNER | APPLIES_TO_OWN_USER
88      *
89      * Only 1 of policy/policyUnion/policyIntersection must be set at a time
90      *
91      * This is used to calculate which states are required to be tested.
92      */
93     val policyIntersection: Array<KClass<*>> = [],
94     /**
95      * If true, this test will only be run in a single state.
96      *
97      *
98      * This is useful for tests of invalid inputs, where running in multiple states is unlikely
99      * to add meaningful coverage.
100      *
101      * By default, all states where the policy can be set will be included.
102      */
103     val singleTestOnly: Boolean = false,
104     /**
105      * Priority sets the order that annotations will be resolved.
106      *
107      *
108      * Annotations with a lower priority will be resolved before annotations with a higher
109      * priority.
110      *
111      *
112      * If there is an order requirement between annotations, ensure that the priority of the
113      * annotation which must be resolved first is lower than the one which must be resolved later.
114      *
115      *
116      * Priority can be set to a [AnnotationPriorityRunPrecedence] constant, or to any [Int].
117      */
118     val priority: Int = AnnotationPriorityRunPrecedence.PRECEDENCE_NOT_IMPORTANT)
119 
120 @AutoAnnotation
canSetPolicyTestnull121 fun canSetPolicyTest(
122     policy: Array<Class<*>>? = emptyArray(),
123     policyUnion: Array<Class<*>>? = emptyArray(),
124     policyIntersection: Array<Class<*>>? = emptyArray()
125 ): CanSetPolicyTest {
126     return AutoAnnotation_CanSetPolicyTestKt_canSetPolicyTest(policy,
127         policyUnion,
128         policyIntersection)
129 }