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