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 
17 package com.android.bedstead.enterprise.annotations
18 
19 import com.android.bedstead.enterprise.annotations.EnsureHasDeviceOwner.DO_PO_PRIORITY
20 import com.android.bedstead.harrier.HarrierRule
21 import com.android.bedstead.harrier.UserType
22 import com.android.bedstead.harrier.UserType.INSTRUMENTED_USER
23 import com.android.bedstead.harrier.annotations.RequireFeature
24 import com.android.bedstead.harrier.annotations.RequireNotInstantApp
25 import com.android.bedstead.harrier.annotations.UsesAnnotationExecutor
26 import com.android.bedstead.nene.packages.CommonPackages.FEATURE_DEVICE_ADMIN
27 import com.android.queryable.annotations.Query
28 import com.google.auto.value.AutoAnnotation
29 
30 /**
31  * Mark that a test requires that a profile owner is set.
32  *
33  * You can use {@code DeviceState} to ensure that the device enters
34  * the correct state for the method. If using [DeviceState], you can use
35  * [DeviceState#profileOwner()] to interact with the profile owner.
36  *
37  * @param onUser Which user type the profile owner should be installed on.
38  * @param key The key used to identify this DPC.
39  *  This can be used with [AdditionalQueryParameters] to modify the requirements for
40  *  the DPC.
41  * @param dpc Requirements for the DPC. Defaults to the default version of RemoteDPC.
42  * @param isPrimary Whether this DPC should be returned by calls to [Devicestate#dpc()].
43  *  Only one policy manager per test should be marked as primary.
44  * @param useParentInstance If true, uses the [DevicePolicyManager#getParentProfileInstance(ComponentName)]
45  *  instance of the dpc when calling to .dpc()
46  *  Only used if [isPrimary] is true.
47  * @param affiliationIds Affiliation ids to be set for the profile owner.
48  * @param priority Priority sets the order that annotations will be resolved.
49  *  Annotations with a lower priority will be resolved before annotations with a higher
50  *  priority.
51  *
52  *  If there is an order requirement between annotations, ensure that the priority of the
53  *  annotation which must be resolved first is lower than the one which must be resolved later.
54  *
55  *  Priority can be set to a [AnnotationPriorityRunPrecedence] constant, or to any [int].
56  */
57 @Target(AnnotationTarget.FUNCTION, AnnotationTarget.TYPE)
58 @Retention(AnnotationRetention.RUNTIME)
59 @RequireFeature(FEATURE_DEVICE_ADMIN)
60 // TODO(b/206441366): Add instant app support
61 @RequireNotInstantApp(reason = "Instant Apps cannot run Enterprise Tests")
62 @UsesAnnotationExecutor(UsesAnnotationExecutor.ENTERPRISE)
63 annotation class EnsureHasProfileOwner(
64     val onUser: UserType = INSTRUMENTED_USER,
65     val key: String = DEFAULT_KEY,
66     val dpc: Query = Query(),
67     val isPrimary: Boolean = false,
68     val useParentInstance: Boolean = false,
69     val affiliationIds: Array<String> = [],
70     val priority: Int = DO_PO_PRIORITY
71 )
72 
73 const val DEFAULT_KEY = "profileOwner"
74 
75 /**
76  * Return an instance of the generated class that conforms to the specification of
77  * [EnsureHasProfileOwner]. See [AutoAnnotation].
78  */
ensureHasProfileOwnernull79 fun ensureHasProfileOwner(): EnsureHasProfileOwner {
80     return ensureHasProfileOwner(query())
81 }
82 
83 @AutoAnnotation
ensureHasProfileOwnernull84 private fun ensureHasProfileOwner(dpc: Query): EnsureHasProfileOwner {
85     return AutoAnnotation_EnsureHasProfileOwnerKt_ensureHasProfileOwner(dpc)
86 }
87 
88 /**
89  * A workaround to create an [AutoAnnotation] of [EnsureHasProfileOwner]. [AutoAnnotation]
90  * cannot set default values for fields of type Annotation, hence we create an object of [Query]
91  * explicitly to pass as the default value of the [dpc] field.
92  */
querynull93 private fun query(): Query {
94     return HarrierRule::class.java.getAnnotation<Query>(Query::class.java)
95 }
96