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 static com.android.bedstead.harrier.annotations.AnnotationPriorityRunPrecedence.MIDDLE;
20 import static com.android.bedstead.nene.packages.CommonPackages.FEATURE_DEVICE_ADMIN;
21 
22 import com.android.bedstead.harrier.annotations.AnnotationPriorityRunPrecedence;
23 import com.android.bedstead.harrier.annotations.FailureMode;
24 import com.android.bedstead.harrier.annotations.RequireFeature;
25 import com.android.bedstead.harrier.annotations.RequireNotInstantApp;
26 import com.android.bedstead.harrier.annotations.RequireNotWatch;
27 import com.android.bedstead.harrier.annotations.UsesAnnotationExecutor;
28 import com.android.bedstead.nene.devicepolicy.DeviceOwnerType;
29 import com.android.queryable.annotations.Query;
30 
31 import java.lang.annotation.ElementType;
32 import java.lang.annotation.Retention;
33 import java.lang.annotation.RetentionPolicy;
34 import java.lang.annotation.Target;
35 
36 /**
37  * Mark that a test requires that a device owner is available on the device.
38  *
39  * <p>Your test configuration may be configured so that this test is only run on a device which has
40  * a device owner. Otherwise, you can use {@code Devicestate} to ensure that the device enters
41  * the correct state for the method. If using {@code Devicestate}, you can use
42  * {@code Devicestate#deviceOwner()} to interact with the device owner.
43  *
44  * <p>When running on a device with a headless system user, enforcing this with {@code Devicestate}
45  * will also result in the profile owner of the current user being set to the same device policy
46  * controller.
47  *
48  * <p>If {@code Devicestate} is required to set the device owner (because there isn't one already)
49  * then all users and accounts may be removed from the device.
50  */
51 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE})
52 @Retention(RetentionPolicy.RUNTIME)
53 @RequireFeature(FEATURE_DEVICE_ADMIN)
54 // TODO(b/206441366): Add instant app support
55 @RequireNotInstantApp(reason = "Instant Apps cannot run Enterprise Tests")
56 @RequireNotWatch(reason = "b/270121483 Watches get marked as paired which means we can't change Device Owner")
57 @UsesAnnotationExecutor(UsesAnnotationExecutor.ENTERPRISE)
58 public @interface EnsureHasDeviceOwner {
59 
60     /** See {@link EnsureHasDeviceOwner#headlessDeviceOwnerType }. */
61     enum HeadlessDeviceOwnerType {
62         /**
63          * When used - the Device Owner will be set but no profile owners will be set.
64          */
65         NONE,
66 
67         /**
68          * When used - when setting the device owner on a headless system user mode device, a profile
69          * owner will also be set on the initial user. This matches the behaviour when the DPC
70          * has 'headless-system-user device-owner-mode="affiliated"' in device_admin.xml.
71          *
72          * <p>Note that when this is set - a default affiliation ID will be added to the Device
73          * Owner and to the Profile Owner set on any other users.
74          */
75         AFFILIATED,
76 
77         /**
78          * When used - the Device Owner will be set on the first secondary user (user 10).
79          * This matches the behaviour when the DPC has
80          * 'headless-system-user device-owner-mode="single_user"'
81          * in headless_single_user_device_admin.xml.
82          */
83         SINGLE_USER;
84     }
85 
86     int DO_PO_PRIORITY = MIDDLE;
87 
88     String DEFAULT_KEY = "deviceOwner";
89 
90     /**
91      * The key used to identify this DPC.
92      *
93      * <p>This can be used with {@link AdditionalQueryParameters} to modify the requirements for
94      * the DPC. */
key()95     String key() default DEFAULT_KEY;
96 
97     /** Behaviour if the device owner cannot be set. */
failureMode()98     FailureMode failureMode() default FailureMode.FAIL;
99 
100     /**
101      * Requirements for the DPC.
102      *
103      * <p>Defaults to the default version of RemoteDPC.
104      */
dpc()105     Query dpc() default @Query();
106 
107     /**
108      * Whether this DPC should be returned by calls to {@code Devicestate#dpc()}.
109      *
110      * <p>Only one policy manager per test should be marked as primary.
111      */
isPrimary()112     boolean isPrimary() default false;
113 
114     /**
115      * Affiliation ids to be set for the device owner.
116      */
affiliationIds()117     String[] affiliationIds() default {};
118 
119      /**
120      * Priority sets the order that annotations will be resolved.
121      *
122      * <p>Annotations with a lower priority will be resolved before annotations with a higher
123      * priority.
124      *
125      * <p>If there is an order requirement between annotations, ensure that the priority of the
126      * annotation which must be resolved first is lower than the one which must be resolved later.
127      *
128      * <p>Priority can be set to a {@link AnnotationPriorityRunPrecedence} constant, or to any {@link int}.
129      */
priority()130     int priority() default DO_PO_PRIORITY;
131 
132     /**
133      * The type of device owner that is managing the device which can be {@link
134      * DeviceOwnerType#DEFAULT} or {@link DeviceOwnerType#FINANCED}.
135      */
type()136     int type() default DeviceOwnerType.DEFAULT;
137 
138     /**
139      * The behaviour when running tests on a HSUM device.
140      */
headlessDeviceOwnerType()141     HeadlessDeviceOwnerType headlessDeviceOwnerType() default HeadlessDeviceOwnerType.AFFILIATED;
142 }
143