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; 18 19 import static com.android.bedstead.harrier.UserType.INITIAL_USER; 20 import static com.android.bedstead.harrier.annotations.AnnotationRunPrecedence.REQUIRE_RUN_ON_PRECEDENCE; 21 import static com.android.bedstead.nene.types.OptionalBoolean.ANY; 22 import static com.android.bedstead.nene.types.OptionalBoolean.FALSE; 23 24 import com.android.bedstead.harrier.UserType; 25 import com.android.bedstead.harrier.annotations.enterprise.AdditionalQueryParameters; 26 import com.android.bedstead.harrier.annotations.enterprise.EnsureHasNoDeviceOwner; 27 import com.android.bedstead.harrier.annotations.meta.EnsureHasProfileAnnotation; 28 import com.android.bedstead.nene.types.OptionalBoolean; 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 method should run on a user which has a work profile. 38 * 39 * <p>Use of this annotation implies 40 * {@code RequireFeature("android.software.managed_users", SKIP)}. 41 * 42 * <p>Your test configuration may be configured so that this test is only run on a user which has 43 * a work profile. Otherwise, you can use {@code Devicestate} to ensure that the device enters 44 * the correct state for the method. 45 */ 46 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE, ElementType.TYPE}) 47 @Retention(RetentionPolicy.RUNTIME) 48 @EnsureHasProfileAnnotation(value = "android.os.usertype.profile.MANAGED", hasProfileOwner = true) 49 @RequireFeature("android.software.managed_users") 50 @EnsureHasNoDeviceOwner // TODO: This should only apply on Android R+ 51 public @interface EnsureHasWorkProfile { 52 53 // Must be before RequireRunOn to ensure users exist 54 int ENSURE_HAS_WORK_PROFILE_WEIGHT = REQUIRE_RUN_ON_PRECEDENCE - 1; 55 56 /** Which user type the work profile should be attached to. */ forUser()57 UserType forUser() default INITIAL_USER; 58 59 /** Whether the instrumented test app should be installed in the work profile. */ installInstrumentedApp()60 OptionalBoolean installInstrumentedApp() default ANY; 61 62 String DEFAULT_KEY = "profileOwner"; 63 64 /** 65 * The key used to identify the profile owner. 66 * 67 * <p>This can be used with {@link AdditionalQueryParameters} to modify the requirements for 68 * the DPC. */ dpcKey()69 String dpcKey() default DEFAULT_KEY; 70 71 /** 72 * Requirements for the Profile Owner. 73 * 74 * <p>Defaults to the default version of RemoteDPC. 75 */ dpc()76 Query dpc() default @Query(); 77 78 /** 79 * Whether the profile owner's DPC should be returned by calls to {@code Devicestate#dpc()}. 80 * 81 * <p>Only one device policy controller per test should be marked as primary. 82 */ dpcIsPrimary()83 boolean dpcIsPrimary() default false; 84 85 /** Whether the work profile device will be in COPE mode. */ isOrganizationOwned()86 boolean isOrganizationOwned() default false; 87 88 /** 89 * If true, uses the {@code DevicePolicyManager#getParentProfileInstance(ComponentName)} 90 * instance of the dpc when calling to .dpc() 91 * 92 * <p>Only used if {@link #dpcIsPrimary()} is true. 93 */ useParentInstanceOfDpc()94 boolean useParentInstanceOfDpc() default false; 95 96 /** 97 * Should we ensure that we are switched to the parent of the profile. 98 */ switchedToParentUser()99 OptionalBoolean switchedToParentUser() default ANY; 100 101 /** 102 * Is the profile in quiet mode? 103 */ isQuietModeEnabled()104 OptionalBoolean isQuietModeEnabled() default FALSE; 105 106 /** 107 * Weight sets the order that annotations will be resolved. 108 * 109 * <p>Annotations with a lower weight will be resolved before annotations with a higher weight. 110 * 111 * <p>If there is an order requirement between annotations, ensure that the weight of the 112 * annotation which must be resolved first is lower than the one which must be resolved later. 113 * 114 * <p>Weight can be set to a {@link AnnotationRunPrecedence} constant, or to any {@link int}. 115 */ weight()116 int weight() default ENSURE_HAS_WORK_PROFILE_WEIGHT; 117 } 118